Function Differences with tf.clip_by_value
tf.clip_by_value
tf.clip_by_value(
t, clip_value_min, clip_value_max, name=None
)
For more information, see tf.clip_by_value.
mindspore.Tensor.clip
mindspore.Tensor.clip(xmin, xmax, dtype=None)
For more information, see mindspore.Tensor.clip.
Usage
The main functions are the same. tf.clip_by_value
throws a type error when t
is int32
and clip_value_min
or clip_value_max
is of type float32
, and mindspore.Tensor.clip
does not have this restriction.
Code Example
import mindspore as ms
x = ms.Tensor([1, 2, 3, -4, 0, 3, 2, 0]).astype(ms.int32)
print(x.clip(0, 2))
# [1 2 2 0 0 2 2 0]
print(x.clip(0., 2.))
# [1 2 2 0 0 2 2 0]
print(x.clip(Tensor([1, 1, 1, 1, 1, 1, 1, 1]), 2))
# [1 2 2 1 1 2 2 1]
import tensorflow as tf
tf.enable_eager_execution()
A = tf.constant([1, 2, 3, -4, 0, 3, 2, 0])
B = tf.clip_by_value(A, clip_value_min=0, clip_value_max=2)
print(B.numpy())
# [1 2 2 0 0 2 2 0]
C = tf.clip_by_value(A, clip_value_min=0., clip_value_max=2.)
# throws `TypeError`
D = tf.clip_by_value(A, [1, 1, 1, 1, 1, 1, 1, 1], 2)
print(D.numpy())
# [1 2 2 1 1 2 2 1]