Clips tensor values to a maximum L2-norm.
Aliases:
tf.compat.v1.clip_by_norm
tf.compat.v2.clip_by_norm
tf.clip_by_norm(
t,
clip_norm,
axes=None,
name=None
)
Used in the guide:
E
ag
er
e
xe
cu
ti
on
Given at
ensort
, and a maximum clip valueclip_norm
,t
his operat
ion normalizest
sot
hat
it
s L2-norm is lesst
han or equalt
oclip_norm
, alongt
he dimensions given inaxes
. Specifically, int
he default
case where all dimensions are used for calculat
ion, ift
he L2-norm oft
is already lesst
han or equalt
oclip_norm
,t
hent
is not
modified. Ift
he L2-norm is great
ert
hanclip_norm
,t
hent
his operat
ion ret
urns at
ensor oft
he samet
ype and shape ast
wit
h it
s values set
t
o: t * clip_norm / l2norm(t) In this case, the L2-norm of the output tensor isclip_norm
. As anot
her example, ift
is a mat
rix andaxes == [1]
,t
hen each row oft
he out
put
will have L2-norm lesst
han or equalt
oclip_norm
. Ifaxes == [0]
inst
ead, each column oft
he out
put
will be clipped. This operation is typically used to clip gradients before applying them with an optimizer.
Args:
t
: ATensor
orIndexedSlices
.clip_norm
: A 0-D (scalar)Tensor
> 0. A maximum clipping value.axes
: A 1-D (vect
or)Tensor
oft
ype int
32 cont
ainingt
he dimensionst
o use for comput
ingt
he L2-norm. IfNone
(t
he default
), uses all dimensions.name
: Aname
fort
he operat
ion (opt
ional).
Returns:
A clipped Tensor
or IndexedSlices
.
Raises:
ValueError
: If the clip_norm tensor is not a 0-D scalar tensor.TypeError
: If dtype of the input is not a floating point or complex type.