参考自tf2.1
官方文档:
https://www.tensorflow.org/api_docs/python/tf/clip_by_value
Clips tensor values to a specified min and max.
"裁剪"张量使得张量的每个元素在给定的最小值和最大值之间。
tf.clip_by_value(
t, clip_value_min, clip_value_max, name=None
)
Given a tensor t
, this operation returns a tensor of the same type and shape as t
with its values clipped to clip_value_min
andclip_value_max
.
Any values less thanclip_value_min
are set to clip_value_min
.
Any values greater than clip_value_max
are set to clip_value_max
.
给定一个张量 t ,此函数将返回与t具有相同类型和形状的张量,但是返回张量的所有元素将被裁剪到clip_value_min和clip_value_max之间。
返回张量的元素若小于clip_value_min,则被赋值为clip_value_min。
返回张量的元素若大于clip_value_max,则被赋值为clip_value_max。
Args:
-
t: A Tensor or IndexedSlices.
-
t : 一个张量或者索引切片
-
clip_value_min: A 0-D (scalar) Tensor, or a Tensor with the same shape as t. The minimum value to clip by.
-
一个数字张量(可以看做一个数字),或者与张量t同形的张量。小于此值将被裁剪为此值。
-
clip_value_max: A 0-D (scalar) Tensor, or a Tensor with the same shape as t. The maximum value to clip by.
-
一个数字张量(可以看做一个数字),或者与张量t同形的张量。大于此值将被裁剪为此值。
-
name: A name for the operation (optional).
-
(可选)该操作的名字
Returns:
- A clipped Tensor or IndexedSlices.
- 被裁剪过的张量或者索引切片。
Raises:
-
ValueError: If the clip tensors would trigger array broadcasting that would make the returned tensor larger than the input. (注:笔者没想明白这个输出比输入大是个什么意思,是形状大?维度大?还是什么别的意思)
-
ValueError:例如: 裁剪张量触发率数组广播机制,则输出张量将比输入张量大,则会raise ValueError
-
TypeError: If dtype of the input is int32 and dtype of the clip_value_min or clip_value_max is float32
-
TypeError: 例如: 输入的类型的是int32,而clip_value_min 或
clip_value_max 是 float32,则会raise TypeError