diff --git a/mindspore/nn/layer/math.py b/mindspore/nn/layer/math.py index e5f942110d..723ec6b6ee 100644 --- a/mindspore/nn/layer/math.py +++ b/mindspore/nn/layer/math.py @@ -945,7 +945,7 @@ class Moments(Cell): self.squeeze = P.Squeeze(self.axis) def construct(self, x): - tensor_dtype = x.dtype + tensor_dtype = F.dtype(x) _check_input_dtype("input x", tensor_dtype, [mstype.float16, mstype.float32], self.cls_name) if tensor_dtype == mstype.float16: x = self.cast(x, mstype.float32) diff --git a/mindspore/ops/composite/clip_ops.py b/mindspore/ops/composite/clip_ops.py index 7836e23599..14fa798456 100644 --- a/mindspore/ops/composite/clip_ops.py +++ b/mindspore/ops/composite/clip_ops.py @@ -25,6 +25,12 @@ from mindspore._checkparam import Validator as validator from mindspore.ops.primitive import constexpr +@constexpr +def _check_shape(input_shape, out_shape): + if input_shape != out_shape: + raise ValueError("Cannot broadcast the shape of x to the shape of clip_value_min or clip_value_max.") + + def clip_by_value(x, clip_value_min, clip_value_max): """ Clips tensor values to a specified min and max. @@ -63,6 +69,7 @@ def clip_by_value(x, clip_value_min, clip_value_max): max_op = P.Maximum() x_min = min_op(x, clip_value_max) x_max = max_op(x_min, clip_value_min) + _check_shape(F.shape(x), F.shape(x_max)) return x_max