MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / clip_by_average_norm

Function clip_by_average_norm

tensorflow/python/ops/clip_ops.py:334–372  ·  view source on GitHub ↗

Clips tensor values to a maximum average L2-norm. Given a tensor `t`, and a maximum clip value `clip_norm`, this operation normalizes `t` so that its average L2-norm is less than or equal to `clip_norm`. Specifically, if the average L2-norm is already less than or equal to `clip_norm`, then

(t, clip_norm, name=None)

Source from the content-addressed store, hash-verified

332 "instead.")
333@tf_export(v1=["clip_by_average_norm"])
334def clip_by_average_norm(t, clip_norm, name=None):
335 """Clips tensor values to a maximum average L2-norm.
336
337 Given a tensor `t`, and a maximum clip value `clip_norm`, this operation
338 normalizes `t` so that its average L2-norm is less than or equal to
339 `clip_norm`. Specifically, if the average L2-norm is already less than or
340 equal to `clip_norm`, then `t` is not modified. If the average L2-norm is
341 greater than `clip_norm`, then this operation returns a tensor of the same
342 type and shape as `t` with its values set to:
343
344 `t * clip_norm / l2norm_avg(t)`
345
346 In this case, the average L2-norm of the output tensor is `clip_norm`.
347
348 This operation is typically used to clip gradients before applying them with
349 an optimizer.
350
351 Args:
352 t: A `Tensor`.
353 clip_norm: A 0-D (scalar) `Tensor` > 0. A maximum clipping value.
354 name: A name for the operation (optional).
355
356 Returns:
357 A clipped `Tensor`.
358 """
359 with ops.name_scope(name, "clip_by_average_norm", [t, clip_norm]) as name:
360 t = ops.convert_to_tensor(t, name="t")
361
362 # Calculate L2-norm per element, clip elements by ratio of clip_norm to
363 # L2-norm per element
364 n_element = math_ops.cast(array_ops.size(t), dtypes.float32)
365 l2norm_inv = math_ops.rsqrt(
366 math_ops.reduce_sum(t * t, math_ops.range(array_ops.rank(t))))
367 tclip = array_ops.identity(
368 t * clip_norm * math_ops.minimum(
369 l2norm_inv * n_element, constant_op.constant(1.0) / clip_norm),
370 name=name)
371
372 return tclip

Callers

nothing calls this directly

Calls 9

reduce_sumMethod · 0.80
minimumMethod · 0.80
name_scopeMethod · 0.45
castMethod · 0.45
sizeMethod · 0.45
rangeMethod · 0.45
rankMethod · 0.45
identityMethod · 0.45
constantMethod · 0.45

Tested by

no test coverage detected