tf.AggregationMethod     View source on GitHub    A class listing aggregation methods used to combine gradients.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.AggregationMethod  Computing partial derivatives can require aggregating gradient contributions. This class lists the various methods that can be used to combine gradients in the graph. The following aggregation methods are part of the stable API for aggregating gradients:
ADD_N: All of the gradient terms are summed as part of one operation using the "AddN" op (see tf.add_n). This method has the property that all gradients must be ready and buffered separately in memory before any aggregation is performed.
DEFAULT: The system-chosen default aggregation method.  The following aggregation methods are experimental and may not be supported in future releases:
EXPERIMENTAL_TREE: Gradient terms are summed in pairs using using the "AddN" op. This method of summing gradients may reduce performance, but it can improve memory utilization because the gradients can be released earlier.

 Class Variables
  ADD_N   0
  DEFAULT   0
  EXPERIMENTAL_ACCUMULATE_N   2
  EXPERIMENTAL_TREE   1


tf.argsort     View source on GitHub    Returns the indices of a tensor that give its sorted order along an axis.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.argsort
tf.argsort(
    values, axis=-1, direction='ASCENDING', stable=False, name=None
)
 For a 1D tensor, tf.gather(values, tf.argsort(values)) is equivalent to tf.sort(values). For higher dimensions, the output has the same shape as values, but along the given axis, values represent the index of the sorted element in that slice of the tensor at the given position. Usage: import tensorflow as tf
a = [1, 10, 26.9, 2.8, 166.32, 62.3]
b = tf.argsort(a,axis=-1,direction='ASCENDING',stable=False,name=None)
c = tf.keras.backend.eval(b)
# Here, c = [0 3 1 2 5 4]

 Args
  values   1-D or higher numeric Tensor.
  axis   The axis along which to sort. The default is -1, which sorts the last axis.
  direction   The direction in which to sort the values ('ASCENDING' or 'DESCENDING').
  stable   If True, equal elements in the original tensor will not be re-ordered in the returned order. Unstable sort is not yet implemented, but will eventually be the default for performance reasons. If you require a stable order, pass stable=True for forwards compatibility.
  name   Optional name for the operation.

 Returns   An int32 Tensor with the same shape as values. The indices that would sort each slice of the given values along the given axis.

 Raises
  ValueError   If axis is not a constant scalar, or the direction is invalid.


Module: tf.audio Public API for tf.audio namespace. Functions decode_wav(...): Decode a 16-bit PCM WAV file to a float tensor. encode_wav(...): Encode audio data using the WAV file format.


tf.audio.decode_wav Decode a 16-bit PCM WAV file to a float tensor.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.audio.decode_wav
tf.audio.decode_wav(
    contents, desired_channels=-1, desired_samples=-1, name=None
)
 The -32768 to 32767 signed 16-bit values will be scaled to -1.0 to 1.0 in float. When desired_channels is set, if the input contains fewer channels than this then the last channel will be duplicated to give the requested number, else if the input has more channels than requested then the additional channels will be ignored. If desired_samples is set, then the audio will be cropped or padded with zeroes to the requested length. The first output contains a Tensor with the content of the audio samples. The lowest dimension will be the number of channels, and the second will be the number of samples. For example, a ten-sample-long stereo WAV file should give an output shape of [10, 2].

 Args
  contents   A Tensor of type string. The WAV-encoded audio, usually from a file.
  desired_channels   An optional int. Defaults to -1. Number of sample channels wanted.
  desired_samples   An optional int. Defaults to -1. Length of audio requested.
  name   A name for the operation (optional).

 Returns   A tuple of Tensor objects (audio, sample_rate).     audio   A Tensor of type float32.
  sample_rate   A Tensor of type int32.


tf.audio.encode_wav Encode audio data using the WAV file format.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.audio.encode_wav
tf.audio.encode_wav(
    audio, sample_rate, name=None
)
 This operation will generate a string suitable to be saved out to create a .wav audio file. It will be encoded in the 16-bit PCM format. It takes in float values in the range -1.0f to 1.0f, and any outside that value will be clamped to that range. audio is a 2-D float Tensor of shape [length, channels]. sample_rate is a scalar Tensor holding the rate to use (e.g. 44100).

 Args
  audio   A Tensor of type float32. 2-D with shape [length, channels].
  sample_rate   A Tensor of type int32. Scalar containing the sample frequency.
  name   A name for the operation (optional).

 Returns   A Tensor of type string.


Module: tf.autodiff Public API for tf.autodiff namespace. Classes class ForwardAccumulator: Computes Jacobian-vector products ("JVP"s) using forward-mode autodiff. class GradientTape: Record operations for automatic differentiation.


tf.autodiff.ForwardAccumulator Computes Jacobian-vector products ("JVP"s) using forward-mode autodiff.
tf.autodiff.ForwardAccumulator(
    primals, tangents
)
 Compare to tf.GradientTape which computes vector-Jacobian products ("VJP"s) using reverse-mode autodiff (backprop). Reverse mode is more attractive when computing gradients of a scalar-valued function with respect to many inputs (e.g. a neural network with many parameters and a scalar loss). Forward mode works best on functions with many outputs and few inputs. Since it does not hold on to intermediate activations, it is much more memory efficient than backprop where it is applicable. Consider a simple linear regression:
x = tf.constant([[2.0, 3.0], [1.0, 4.0]])
dense = tf.keras.layers.Dense(1)
dense.build([None, 2])
with tf.autodiff.ForwardAccumulator(
   primals=dense.kernel,
   tangents=tf.constant([[1.], [0.]])) as acc:
  loss = tf.reduce_sum((dense(x) - tf.constant([1., -1.])) ** 2.)
acc.jvp(loss)
<tf.Tensor: shape=(), dtype=float32, numpy=...>
 The example has two variables containing parameters, dense.kernel (2 parameters) and dense.bias (1 parameter). Considering the training data x as a constant, this means the Jacobian matrix for the function mapping from parameters to loss has one row and three columns. With forwardprop, we specify a length-three vector in advance which multiplies the Jacobian. The primals constructor argument is the parameter (a tf.Tensor or tf.Variable) we're specifying a vector for, and the tangents argument is the "vector" in Jacobian-vector product. If our goal is to compute the entire Jacobian matrix, forwardprop computes one column at a time while backprop computes one row at a time. Since the Jacobian in the linear regression example has only one row, backprop requires fewer invocations:
x = tf.constant([[2.0, 3.0], [1.0, 4.0]])
dense = tf.keras.layers.Dense(1)
dense.build([None, 2])
loss_fn = lambda: tf.reduce_sum((dense(x) - tf.constant([1., -1.])) ** 2.)
kernel_fprop = []
with tf.autodiff.ForwardAccumulator(
    dense.kernel, tf.constant([[1.], [0.]])) as acc:
  kernel_fprop.append(acc.jvp(loss_fn()))
with tf.autodiff.ForwardAccumulator(
    dense.kernel, tf.constant([[0.], [1.]])) as acc:
  kernel_fprop.append(acc.jvp(loss_fn()))
with tf.autodiff.ForwardAccumulator(dense.bias, tf.constant([1.])) as acc:
  bias_fprop = acc.jvp(loss_fn())
with tf.GradientTape() as tape:
  loss = loss_fn()
kernel_grad, bias_grad = tape.gradient(loss, (dense.kernel, dense.bias))
np.testing.assert_allclose(
    kernel_grad, tf.stack(kernel_fprop)[:, tf.newaxis])
np.testing.assert_allclose(bias_grad, bias_fprop[tf.newaxis])
 Implicit in the tape.gradient call is a length-one vector which left-multiplies the Jacobian, a vector-Jacobian product. ForwardAccumulator maintains JVPs corresponding primal tensors it is watching, derived from the original primals specified in the constructor. As soon as a primal tensor is deleted, ForwardAccumulator deletes the corresponding JVP. acc.jvp(x) retrieves acc's JVP corresponding to the primal tensor x. It does not perform any computation. acc.jvp calls can be repeated as long as acc is accessible, whether the context manager is active or not. New JVPs are only computed while the context manager is active. Note that ForwardAccumulators are always applied in the order their context managers were entered, so inner accumulators will not see JVP computation from outer accumulators. Take higher-order JVPs from outer accumulators:
primal = tf.constant(1.1)
with tf.autodiff.ForwardAccumulator(primal, tf.constant(1.)) as outer:
  with tf.autodiff.ForwardAccumulator(primal, tf.constant(1.)) as inner:
    primal_out = primal ** tf.constant(3.5)
inner_jvp = inner.jvp(primal_out)
inner_jvp  # 3.5 * 1.1 ** 2.5
<tf.Tensor: shape=(), dtype=float32, numpy=4.4417057>
outer.jvp(inner_jvp)  # 3.5 * 2.5 * 1.1 ** 1.5
<tf.Tensor: shape=(), dtype=float32, numpy=10.094786>
 Reversing the collection in the last line to instead retrieve inner.jvp(outer.jvp(primal_out)) will not work. Strict nesting also applies to combinations of ForwardAccumulator and tf.GradientTape. More deeply nested GradientTape objects will ignore the products of outer ForwardAccumulator objects. This allows (for example) memory-efficient forward-over-backward computation of Hessian-vector products, where the inner GradientTape would otherwise hold on to all intermediate JVPs:
v = tf.Variable([1., 2.])
with tf.autodiff.ForwardAccumulator(
    v,
    # The "vector" in Hessian-vector product.
    tf.constant([1., 0.])) as acc:
  with tf.GradientTape() as tape:
    y = tf.reduce_sum(v ** 3.)
  backward = tape.gradient(y, v)
backward  # gradient from backprop
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([ 3., 12.], dtype=float32)>
acc.jvp(backward)  # forward-over-backward Hessian-vector product
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([6., 0.], dtype=float32)>

 Args
  primals   A tensor or nested structure of tensors to watch.
  tangents   A tensor or nested structure of tensors, with the same nesting structure as primals, with each element being a vector with the same size as the corresponding primal element.

 Raises
  ValueError   If the same tensor or variable is specified multiple times in primals.    Methods jvp View source
jvp(
    primals, unconnected_gradients=tf.UnconnectedGradients.NONE
)
 Fetches the Jacobian-vector product computed for primals. Note that this method performs no computation, and simply looks up a JVP that was already computed (unlike backprop using a tf.GradientTape, where the computation happens on the call to tape.gradient).

 Args
  primals   A watched Tensor or structure of Tensors to fetch the JVPs for.
  unconnected_gradients   A value which can either hold 'none' or 'zero' and alters the value which will be returned if no JVP was computed for primals. The possible values and effects are detailed in 'tf.UnconnectedGradients' and it defaults to 'none'.

 Returns   Tensors with the same shapes and dtypes as primals, or None if no JVP is available.
 __enter__ View source
__enter__()
 __exit__ View source
__exit__(
    typ, value, traceback
)


Module: tf.autograph Conversion of plain Python into TensorFlow graph code.
Note: In TensorFlow 2.0, AutoGraph is automatically applied when using tf.function. This module contains lower-level APIs for advanced use.
 For more information, see the AutoGraph guide. By equivalent graph code we mean code that generates a TensorFlow graph when run. The generated graph has the same effects as the original code when executed (for example with tf.function or tf.compat.v1.Session.run). In other words, using AutoGraph can be thought of as running Python in TensorFlow. Modules experimental module: Public API for tf.autograph.experimental namespace. Functions set_verbosity(...): Sets the AutoGraph verbosity level. to_code(...): Returns the source code generated by AutoGraph, as a string. to_graph(...): Converts a Python entity into a TensorFlow graph. trace(...): Traces argument information at compilation time.


Module: tf.autograph.experimental Public API for tf.autograph.experimental namespace. Classes class Feature: This enumeration represents optional conversion options. Functions do_not_convert(...): Decorator that suppresses the conversion of a function. set_loop_options(...): Specifies additional arguments to be passed to the enclosing while_loop.


tf.autograph.experimental.do_not_convert     View source on GitHub    Decorator that suppresses the conversion of a function.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.autograph.experimental.do_not_convert
tf.autograph.experimental.do_not_convert(
    func=None
)

 Args
  func   function to decorate.

 Returns   If func is not None, returns a Callable which is equivalent to func, but is not converted by AutoGraph. If func is None, returns a decorator that, when invoked with a single func argument, returns a Callable equivalent to the above case.


tf.autograph.experimental.Feature     View source on GitHub    This enumeration represents optional conversion options.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.autograph.experimental.Feature  These conversion options are experimental. They are subject to change without notice and offer no guarantees. Example Usage optionals= tf.autograph.experimental.Feature.EQUALITY_OPERATORS
@tf.function(experimental_autograph_options=optionals)
def f(i):
  if i == 0:  # EQUALITY_OPERATORS allows the use of == here.
    tf.print('i is zero')

 Attributes
  ALL   Enable all features.
  AUTO_CONTROL_DEPS   Insert of control dependencies in the generated code.
  ASSERT_STATEMENTS   Convert Tensor-dependent assert statements to tf.Assert.
  BUILTIN_FUNCTIONS   Convert builtin functions applied to Tensors to their TF counterparts.
  EQUALITY_OPERATORS   Whether to convert the comparison operators, like equality. This is soon to be deprecated as support is being added to the Tensor class.
  LISTS   Convert list idioms, like initializers, slices, append, etc.
  NAME_SCOPES   Insert name scopes that name ops according to context, like the function they were defined in.

 Class Variables
  ALL   tf.autograph.experimental.Feature
  ASSERT_STATEMENTS   tf.autograph.experimental.Feature
  AUTO_CONTROL_DEPS   tf.autograph.experimental.Feature
  BUILTIN_FUNCTIONS   tf.autograph.experimental.Feature
  EQUALITY_OPERATORS   tf.autograph.experimental.Feature
  LISTS   tf.autograph.experimental.Feature
  NAME_SCOPES   tf.autograph.experimental.Feature


tf.autograph.experimental.set_loop_options Specifies additional arguments to be passed to the enclosing while_loop.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.autograph.experimental.set_loop_options
tf.autograph.experimental.set_loop_options(
    parallel_iterations=UNSPECIFIED, swap_memory=UNSPECIFIED,
    maximum_iterations=UNSPECIFIED, shape_invariants=UNSPECIFIED
)
 The parameters apply to and only to the immediately enclosing loop. It only has effect if the loop is staged as a TF while_loop; otherwise the parameters have no effect. Usage:
@tf.function(autograph=True)
def f():
  n = 0
  for i in tf.range(10):
    tf.autograph.experimental.set_loop_options(maximum_iterations=3)
    n += 1
  return n

@tf.function(autograph=True)
def f():
  v = tf.constant((0,))
  for i in tf.range(3):
    tf.autograph.experimental.set_loop_options(
        shape_invariants=[(v, tf.TensorShape([None]))]
    )
    v = tf.concat((v, [i]), 0)
  return v
 Also see tf.while_loop.

 Args
  parallel_iterations   The maximum number of iterations allowed to run in parallel at any given time. Note that this does not guarantee parallel execution.
  swap_memory   Whether to store intermediate values needed for gradients on the CPU instead of GPU.
  maximum_iterations   Allows limiting the total number of iterations executed by the loop.
  shape_invariants   Allows controlling the argument with the same name passed to tf.while_loop. Unlike tf.while_loop, this is a list of (tensor, shape) pairs.


tf.autograph.set_verbosity     View source on GitHub    Sets the AutoGraph verbosity level.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.autograph.set_verbosity
tf.autograph.set_verbosity(
    level, alsologtostdout=False
)
 Debug logging in AutoGraph More verbose logging is useful to enable when filing bug reports or doing more in-depth debugging. There are two means to control the logging verbosity:  The set_verbosity function The AUTOGRAPH_VERBOSITY environment variable  set_verbosity takes precedence over the environment variable. For example: import os
import tensorflow as tf

os.environ['AUTOGRAPH_VERBOSITY'] = 5
# Verbosity is now 5

tf.autograph.set_verbosity(0)
# Verbosity is now 0

os.environ['AUTOGRAPH_VERBOSITY'] = 1
# No effect, because set_verbosity was already called.
 Logs entries are output to absl's default output, with INFO level. Logs can be mirrored to stdout by using the alsologtostdout argument. Mirroring is enabled by default when Python runs in interactive mode.

 Args
  level   int, the verbosity level; larger values specify increased verbosity; 0 means no logging. When reporting bugs, it is recommended to set this value to a larger number, like 10.
  alsologtostdout   bool, whether to also output log messages to sys.stdout.


tf.autograph.to_code     View source on GitHub    Returns the source code generated by AutoGraph, as a string.
tf.autograph.to_code(
    entity, recursive=True, experimental_optional_features=None
)
 Example usage:
def f(x):
  if x < 0:
    x = -x
  return x
tf.autograph.to_code(f)
"...def tf__f(x):..."
 Also see: tf.autograph.to_graph.
Note: If a function has been decorated with tf.function, pass its underlying Python function, rather than the callable that `tf.function creates:

@tf.function
def f(x):
  if x < 0:
    x = -x
  return x
tf.autograph.to_code(f.python_function)
"...def tf__f(x):..."

 Args
  entity   Python callable or class to convert.
  recursive   Whether to recursively convert any functions that the converted function may call.
  experimental_optional_features   None, a tuple of, or a single tf.autograph.experimental.Feature value.

 Returns   The converted code as string.


tf.autograph.to_graph     View source on GitHub    Converts a Python entity into a TensorFlow graph.
tf.autograph.to_graph(
    entity, recursive=True, experimental_optional_features=None
)
 Also see: tf.autograph.to_code, tf.function. Unlike tf.function, to_graph is a low-level transpiler that converts Python code to TensorFlow graph code. It does not implement any caching, variable management or create any actual ops, and is best used where greater control over the generated TensorFlow graph is desired. Another difference from tf.function is that to_graph will not wrap the graph into a TensorFlow function or a Python callable. Internally, tf.function uses to_graph. Example usage:
def f(x):
  if x > 0:
    y = x * x
  else:
    y = -x
  return y

converted_f = to_graph(f)
x = tf.constant(2)
converted_f(x)  # converted_foo is like a TensorFlow Op.
<tf.Tensor: shape=(), dtype=int32, numpy=4>
 Supported Python entities include:  functions classes object methods  Functions are converted into new functions with converted code. Classes are converted by generating a new class whose methods use converted code. Methods are converted into unbound function that have an additional first argument called self. For a tutorial, see the tf.function and AutoGraph guide. For more detailed information, see the AutoGraph reference documentation.

 Args
  entity   Python callable or class to convert.
  recursive   Whether to recursively convert any functions that the converted function may call.
  experimental_optional_features   None, a tuple of, or a single tf.autograph.experimental.Feature value.

 Returns   Same as entity, the converted Python function or class.

 Raises
  ValueError   If the entity could not be converted.


tf.autograph.trace     View source on GitHub    Traces argument information at compilation time.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.autograph.trace
tf.autograph.trace(
    *args
)
 trace is useful when debugging, and it always executes during the tracing phase, that is, when the TF graph is constructed. Example usage import tensorflow as tf

for i in tf.range(10):
  tf.autograph.trace(i)
# Output: <Tensor ...>

 Args
  *args   Arguments to print to sys.stdout.


tf.batch_to_space     View source on GitHub    BatchToSpace for N-D tensors of type T.
tf.batch_to_space(
    input, block_shape, crops, name=None
)
 This operation reshapes the "batch" dimension 0 into M + 1 dimensions of shape block_shape + [batch], interleaves these blocks back into the grid defined by the spatial dimensions [1, ..., M], to obtain a result with the same rank as the input. The spatial dimensions of this intermediate result are then optionally cropped according to crops to produce the output. This is the reverse of SpaceToBatch (see tf.space_to_batch).

 Args
  input   A N-D Tensor with shape input_shape = [batch] + spatial_shape + remaining_shape, where spatial_shape has M dimensions.
  block_shape   A 1-D Tensor with shape [M]. Must be one of the following types: int32, int64. All values must be >= 1. For backwards compatibility with TF 1.0, this parameter may be an int, in which case it is converted to numpy.array([block_shape, block_shape], dtype=numpy.int64).
  crops   A 2-D Tensor with shape [M, 2]. Must be one of the following types: int32, int64. All values must be >= 0. crops[i] = [crop_start, crop_end] specifies the amount to crop from input dimension i + 1, which corresponds to spatial dimension i. It is required that crop_start[i] + crop_end[i] <= block_shape[i] * input_shape[i + 1]. This operation is equivalent to the following steps:  Reshape input to reshaped of shape: [block_shape[0], ..., block_shape[M-1], batch / prod(block_shape), input_shape[1], ..., input_shape[N-1]] Permute dimensions of reshaped to produce permuted of shape [batch / prod(block_shape), input_shape[1], block_shape[0], ..., input_shape[M], block_shape[M-1], input_shape[M+1], ..., input_shape[N-1]] Reshape permuted to produce reshaped_permuted of shape [batch / prod(block_shape), input_shape[1] * block_shape[0], ..., input_shape[M] * block_shape[M-1], input_shape[M+1], ..., input_shape[N-1]] Crop the start and end of dimensions [1, ..., M] of reshaped_permuted according to crops to produce the output of shape: [batch / prod(block_shape), input_shape[1] * block_shape[0] - crops[0,0] - crops[0,1], ..., input_shape[M] * block_shape[M-1] - crops[M-1,0] - crops[M-1,1], input_shape[M+1], ..., input_shape[N-1]]

  name   A name for the operation (optional).    Examples: (1) For the following input of shape [4, 1, 1, 1], block_shape = [2, 2], and crops = [[0, 0], [0, 0]]: [[[[1]]],
 [[[2]]],
 [[[3]]],
 [[[4]]]]
 The output tensor has shape [1, 2, 2, 1] and value: x = [[[[1], [2]],
    [[3], [4]]]]
 (2) For the following input of shape [4, 1, 1, 3], block_shape = [2, 2], and crops = [[0, 0], [0, 0]]: [[[1,  2,   3]],
 [[4,  5,   6]],
 [[7,  8,   9]],
 [[10, 11, 12]]]
 The output tensor has shape [1, 2, 2, 3] and value:    x = [[[[1, 2, 3], [4,  5,  6 ]],
         [[7, 8, 9], [10, 11, 12]]]]
   ```

(3) For the following
   input of shape `[4, 2, 2, 1]`,
   `block_shape = [2, 2]`, and `crops = [[0, 0], [0, 0]]`:

   ```python
   x = [[[[1], [3]], [[ 9], [11]]],
        [[[2], [4]], [[10], [12]]],
        [[[5], [7]], [[13], [15]]],
        [[[6], [8]], [[14], [16]]]]
   ```

  The output tensor has shape `[1, 4, 4, 1]` and value:

  ```python
   x = [[[1],  [2],  [ 3], [ 4]],
        [[5],  [6],  [ 7], [ 8]],
        [[9],  [10], [11], [12]],
        [[13], [14], [15], [16]]]
   ```

 (4) For the following input of shape
    `[8, 1, 3, 1]`,
    `block_shape = [2, 2]`, and `crops = [[0, 0], [2, 0]]`:

    ```python
    x = [[[[0], [ 1], [ 3]]],
         [[[0], [ 9], [11]]],
         [[[0], [ 2], [ 4]]],
         [[[0], [10], [12]]],
         [[[0], [ 5], [ 7]]],
         [[[0], [13], [15]]],
         [[[0], [ 6], [ 8]]],
         [[[0], [14], [16]]]]
    ```

    The output tensor has shape `[2, 2, 4, 1]` and value:

    ```python
    x = [[[[ 1], [ 2], [ 3], [ 4]],
          [[ 5], [ 6], [ 7], [ 8]]],
         [[[ 9], [10], [11], [12]],
          [[13], [14], [15], [16]]]]
    ```

<colgroup><col width="214px"><col></colgroup>

Returns

A `Tensor`. Has the same type as `input`.


tf.bitcast Bitcasts a tensor from one type to another without copying data.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitcast
tf.bitcast(
    input, type, name=None
)
 Given a tensor input, this operation returns a tensor that has the same buffer data as input with datatype type. If the input datatype T is larger than the output datatype type then the shape changes from [...] to [..., sizeof(T)/sizeof(type)]. If T is smaller than type, the operator requires that the rightmost dimension be equal to sizeof(type)/sizeof(T). The shape then goes from [..., sizeof(type)/sizeof(T)] to [...]. tf.bitcast() and tf.cast() work differently when real dtype is casted as a complex dtype (e.g. tf.complex64 or tf.complex128) as tf.cast() make imaginary part 0 while tf.bitcast() gives module error. For example, Example 1:
a = [1., 2., 3.]
equality_bitcast = tf.bitcast(a, tf.complex128)
Traceback (most recent call last):

InvalidArgumentError: Cannot bitcast from 1 to 18 [Op:Bitcast]
equality_cast = tf.cast(a, tf.complex128)
print(equality_cast)
tf.Tensor([1.+0.j 2.+0.j 3.+0.j], shape=(3,), dtype=complex128)
 Example 2:
tf.bitcast(tf.constant(0xffffffff, dtype=tf.uint32), tf.uint8)
<tf.Tensor: shape=(4,), dtype=uint8, numpy=array([255, 255, 255, 255], dtype=uint8)>
 Example 3:
x = [1., 2., 3.]
y = [0., 2., 3.]
equality= tf.equal(x,y)
equality_cast = tf.cast(equality,tf.float32)
equality_bitcast = tf.bitcast(equality_cast,tf.uint8)
print(equality)
tf.Tensor([False True True], shape=(3,), dtype=bool)
print(equality_cast)
tf.Tensor([0. 1. 1.], shape=(3,), dtype=float32)
print(equality_bitcast)
tf.Tensor(
    [[  0   0   0   0]
     [  0   0 128  63]
     [  0   0 128  63]], shape=(3, 4), dtype=uint8)

Note: Bitcast is implemented as a low-level cast, so machines with different endian orderings will give different results.

 Args
  input   A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int64, int32, uint8, uint16, uint32, uint64, int8, int16, complex64, complex128, qint8, quint8, qint16, quint16, qint32.
  type   A tf.DType from: tf.bfloat16, tf.half, tf.float32, tf.float64, tf.int64, tf.int32, tf.uint8, tf.uint16, tf.uint32, tf.uint64, tf.int8, tf.int16, tf.complex64, tf.complex128, tf.qint8, tf.quint8, tf.qint16, tf.quint16, tf.qint32.
  name   A name for the operation (optional).

 Returns   A Tensor of type type.


Module: tf.bitwise Operations for manipulating the binary representations of integers. Functions bitwise_and(...): Elementwise computes the bitwise AND of x and y. bitwise_or(...): Elementwise computes the bitwise OR of x and y. bitwise_xor(...): Elementwise computes the bitwise XOR of x and y. invert(...): Invert (flip) each bit of supported types; for example, type uint8 value 01010101 becomes 10101010. left_shift(...): Elementwise computes the bitwise left-shift of x and y. right_shift(...): Elementwise computes the bitwise right-shift of x and y.


tf.bitwise.bitwise_and Elementwise computes the bitwise AND of x and y.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitwise.bitwise_and
tf.bitwise.bitwise_and(
    x, y, name=None
)
 The result will have those bits set, that are set in both x and y. The computation is performed on the underlying representations of x and y. For example: import tensorflow as tf
from tensorflow.python.ops import bitwise_ops
dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64,
              tf.uint8, tf.uint16, tf.uint32, tf.uint64]

for dtype in dtype_list:
  lhs = tf.constant([0, 5, 3, 14], dtype=dtype)
  rhs = tf.constant([5, 0, 7, 11], dtype=dtype)
  exp = tf.constant([0, 0, 3, 10], dtype=tf.float32)

  res = bitwise_ops.bitwise_and(lhs, rhs)
  tf.assert_equal(tf.cast(res, tf.float32), exp) # TRUE

 Args
  x   A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  y   A Tensor. Must have the same type as x.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as x.


tf.bitwise.bitwise_or Elementwise computes the bitwise OR of x and y.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitwise.bitwise_or
tf.bitwise.bitwise_or(
    x, y, name=None
)
 The result will have those bits set, that are set in x, y or both. The computation is performed on the underlying representations of x and y. For example: import tensorflow as tf
from tensorflow.python.ops import bitwise_ops
dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64,
              tf.uint8, tf.uint16, tf.uint32, tf.uint64]

for dtype in dtype_list:
  lhs = tf.constant([0, 5, 3, 14], dtype=dtype)
  rhs = tf.constant([5, 0, 7, 11], dtype=dtype)
  exp = tf.constant([5, 5, 7, 15], dtype=tf.float32)

  res = bitwise_ops.bitwise_or(lhs, rhs)
  tf.assert_equal(tf.cast(res,  tf.float32), exp)  # TRUE

 Args
  x   A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  y   A Tensor. Must have the same type as x.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as x.


tf.bitwise.bitwise_xor Elementwise computes the bitwise XOR of x and y.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitwise.bitwise_xor
tf.bitwise.bitwise_xor(
    x, y, name=None
)
 The result will have those bits set, that are different in x and y. The computation is performed on the underlying representations of x and y. For example: import tensorflow as tf
from tensorflow.python.ops import bitwise_ops
dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64,
              tf.uint8, tf.uint16, tf.uint32, tf.uint64]

for dtype in dtype_list:
  lhs = tf.constant([0, 5, 3, 14], dtype=dtype)
  rhs = tf.constant([5, 0, 7, 11], dtype=dtype)
  exp = tf.constant([5, 5, 4, 5],  dtype=tf.float32)

  res = bitwise_ops.bitwise_xor(lhs, rhs)
  tf.assert_equal(tf.cast(res, tf.float32), exp) # TRUE

 Args
  x   A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  y   A Tensor. Must have the same type as x.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as x.


tf.bitwise.invert Invert (flip) each bit of supported types; for example, type uint8 value 01010101 becomes 10101010.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitwise.invert
tf.bitwise.invert(
    x, name=None
)
 Flip each bit of supported types. For example, type int8 (decimal 2) binary 00000010 becomes (decimal -3) binary 11111101. This operation is performed on each element of the tensor argument x. Example: import tensorflow as tf
from tensorflow.python.ops import bitwise_ops

# flip 2 (00000010) to -3 (11111101)
tf.assert_equal(-3, bitwise_ops.invert(2))

dtype_list = [dtypes.int8, dtypes.int16, dtypes.int32, dtypes.int64,
              dtypes.uint8, dtypes.uint16, dtypes.uint32, dtypes.uint64]

inputs = [0, 5, 3, 14]
for dtype in dtype_list:
  # Because of issues with negative numbers, let's test this indirectly.
  # 1. invert(a) and a = 0
  # 2. invert(a) or a = invert(0)
  input_tensor = tf.constant([0, 5, 3, 14], dtype=dtype)
  not_a_and_a, not_a_or_a, not_0 = [bitwise_ops.bitwise_and(
                                      input_tensor, bitwise_ops.invert(input_tensor)),
                                    bitwise_ops.bitwise_or(
                                      input_tensor, bitwise_ops.invert(input_tensor)),
                                    bitwise_ops.invert(
                                      tf.constant(0, dtype=dtype))]

  expected = tf.constant([0, 0, 0, 0], dtype=tf.float32)
  tf.assert_equal(tf.cast(not_a_and_a, tf.float32), expected)

  expected = tf.cast([not_0] * 4, tf.float32)
  tf.assert_equal(tf.cast(not_a_or_a, tf.float32), expected)

  # For unsigned dtypes let's also check the result directly.
  if dtype.is_unsigned:
    inverted = bitwise_ops.invert(input_tensor)
    expected = tf.constant([dtype.max - x for x in inputs], dtype=tf.float32)
    tf.assert_equal(tf.cast(inverted, tf.float32), tf.cast(expected, tf.float32))

 Args
  x   A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as x.


tf.bitwise.left_shift Elementwise computes the bitwise left-shift of x and y.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitwise.left_shift
tf.bitwise.left_shift(
    x, y, name=None
)
 If y is negative, or greater than or equal to the width of x in bits the result is implementation defined. Example: import tensorflow as tf
from tensorflow.python.ops import bitwise_ops
import numpy as np
dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64]

for dtype in dtype_list:
  lhs = tf.constant([-1, -5, -3, -14], dtype=dtype)
  rhs = tf.constant([5, 0, 7, 11], dtype=dtype)

  left_shift_result = bitwise_ops.left_shift(lhs, rhs)

  print(left_shift_result)

# This will print:
# tf.Tensor([ -32   -5 -128    0], shape=(4,), dtype=int8)
# tf.Tensor([   -32     -5   -384 -28672], shape=(4,), dtype=int16)
# tf.Tensor([   -32     -5   -384 -28672], shape=(4,), dtype=int32)
# tf.Tensor([   -32     -5   -384 -28672], shape=(4,), dtype=int64)

lhs = np.array([-2, 64, 101, 32], dtype=np.int8)
rhs = np.array([-1, -5, -3, -14], dtype=np.int8)
bitwise_ops.left_shift(lhs, rhs)
# <tf.Tensor: shape=(4,), dtype=int8, numpy=array([ -2,  64, 101,  32], dtype=int8)>

 Args
  x   A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  y   A Tensor. Must have the same type as x.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as x.


tf.bitwise.right_shift Elementwise computes the bitwise right-shift of x and y.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.bitwise.right_shift
tf.bitwise.right_shift(
    x, y, name=None
)
 Performs a logical shift for unsigned integer types, and an arithmetic shift for signed integer types. If y is negative, or greater than or equal to than the width of x in bits the result is implementation defined. Example: import tensorflow as tf
from tensorflow.python.ops import bitwise_ops
import numpy as np
dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64]

for dtype in dtype_list:
  lhs = tf.constant([-1, -5, -3, -14], dtype=dtype)
  rhs = tf.constant([5, 0, 7, 11], dtype=dtype)

  right_shift_result = bitwise_ops.right_shift(lhs, rhs)

  print(right_shift_result)

# This will print:
# tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int8)
# tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int16)
# tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int32)
# tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int64)

lhs = np.array([-2, 64, 101, 32], dtype=np.int8)
rhs = np.array([-1, -5, -3, -14], dtype=np.int8)
bitwise_ops.right_shift(lhs, rhs)
# <tf.Tensor: shape=(4,), dtype=int8, numpy=array([ -2,  64, 101,  32], dtype=int8)>

 Args
  x   A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  y   A Tensor. Must have the same type as x.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as x.


tf.boolean_mask     View source on GitHub    Apply boolean mask to tensor.
tf.boolean_mask(
    tensor, mask, axis=None, name='boolean_mask'
)
 Numpy equivalent is tensor[mask]. In general, 0 < dim(mask) = K <= dim(tensor), and mask's shape must match the first K dimensions of tensor's shape. We then have: boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd] where (i1,...,iK) is the ith True entry of mask (row-major order). The axis could be used with mask to indicate the axis to mask from. In that case, axis + dim(mask) <= dim(tensor) and mask's shape must match the first axis + dim(mask) dimensions of tensor's shape. See also: tf.ragged.boolean_mask, which can be applied to both dense and ragged tensors, and can be used if you need to preserve the masked dimensions of tensor (rather than flattening them, as tf.boolean_mask does). Examples:
tensor = [0, 1, 2, 3]  # 1-D example
mask = np.array([True, False, True, False])
tf.boolean_mask(tensor, mask)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 2], dtype=int32)>

tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example
mask = np.array([True, False, True])
tf.boolean_mask(tensor, mask)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [5, 6]], dtype=int32)>

 Args
  tensor   N-D Tensor.
  mask   K-D boolean Tensor, K <= N and K must be known statically.
  axis   A 0-D int Tensor representing the axis in tensor to mask from. By default, axis is 0 which will mask from the first dimension. Otherwise K + axis <= N.
  name   A name for this operation (optional).

 Returns   (N-K+1)-dimensional tensor populated by entries in tensor corresponding to True values in mask.

 Raises
  ValueError   If shapes do not conform.    Examples: # 2-D example
tensor = [[1, 2], [3, 4], [5, 6]]
mask = np.array([True, False, True])
boolean_mask(tensor, mask)  # [[1, 2], [5, 6]]


tf.broadcast_dynamic_shape     View source on GitHub    Computes the shape of a broadcast given symbolic shapes.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.broadcast_dynamic_shape
tf.broadcast_dynamic_shape(
    shape_x, shape_y
)
 When shape_x and shape_y are Tensors representing shapes (i.e. the result of calling tf.shape on another Tensor) this computes a Tensor which is the shape of the result of a broadcasting op applied in tensors of shapes shape_x and shape_y. This is useful when validating the result of a broadcasting operation when the tensors do not have statically known shapes. Example:
shape_x = (1, 2, 3)
shape_y = (5, 1, 3)
tf.broadcast_dynamic_shape(shape_x, shape_y)
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([5, 2, 3], ...>

 Args
  shape_x   A rank 1 integer Tensor, representing the shape of x.
  shape_y   A rank 1 integer Tensor, representing the shape of y.

 Returns   A rank 1 integer Tensor representing the broadcasted shape.

 Raises
  InvalidArgumentError   If the two shapes are incompatible for broadcasting.


tf.broadcast_static_shape     View source on GitHub    Computes the shape of a broadcast given known shapes.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.broadcast_static_shape
tf.broadcast_static_shape(
    shape_x, shape_y
)
 When shape_x and shape_y are fully known TensorShapes this computes a TensorShape which is the shape of the result of a broadcasting op applied in tensors of shapes shape_x and shape_y. For example, if shape_x is TensorShape([1, 2, 3]) and shape_y is TensorShape([5, 1, 3]), the result is a TensorShape whose value is TensorShape([5, 2, 3]). This is useful when validating the result of a broadcasting operation when the tensors have statically known shapes. Example:
shape_x = tf.TensorShape([1, 2, 3])
shape_y = tf.TensorShape([5, 1 ,3])
tf.broadcast_static_shape(shape_x, shape_y)
TensorShape([5, 2, 3])

 Args
  shape_x   A TensorShape
  shape_y   A TensorShape

 Returns   A TensorShape representing the broadcasted shape.

 Raises
  ValueError   If the two shapes can not be broadcasted.


tf.broadcast_to Broadcast an array for a compatible shape.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.broadcast_to
tf.broadcast_to(
    input, shape, name=None
)
 Broadcasting is the process of making arrays to have compatible shapes for arithmetic operations. Two shapes are compatible if for each dimension pair they are either equal or one of them is one. When trying to broadcast a Tensor to a shape, it starts with the trailing dimensions, and works its way forward. For example,
x = tf.constant([1, 2, 3])
y = tf.broadcast_to(x, [3, 3])
print(y)
tf.Tensor(
    [[1 2 3]
     [1 2 3]
     [1 2 3]], shape=(3, 3), dtype=int32)
 In the above example, the input Tensor with the shape of [1, 3] is broadcasted to output Tensor with shape of [3, 3]. When doing broadcasted operations such as multiplying a tensor by a scalar, broadcasting (usually) confers some time or space benefit, as the broadcasted tensor is never materialized. However, broadcast_to does not carry with it any such benefits. The newly-created tensor takes the full memory of the broadcasted shape. (In a graph context, broadcast_to might be fused to subsequent operation and then be optimized away, however.)

 Args
  input   A Tensor. A Tensor to broadcast.
  shape   A Tensor. Must be one of the following types: int32, int64. An 1-D int Tensor. The shape of the desired output.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as input.


tf.case     View source on GitHub    Create a case operation.
tf.case(
    pred_fn_pairs, default=None, exclusive=False, strict=False,
    name='case'
)
 See also tf.switch_case. The pred_fn_pairs parameter is a list of pairs of size N. Each pair contains a boolean scalar tensor and a python callable that creates the tensors to be returned if the boolean evaluates to True. default is a callable generating a list of tensors. All the callables in pred_fn_pairs as well as default (if provided) should return the same number and types of tensors. If exclusive==True, all predicates are evaluated, and an exception is thrown if more than one of the predicates evaluates to True. If exclusive==False, execution stops at the first predicate which evaluates to True, and the tensors generated by the corresponding function are returned immediately. If none of the predicates evaluate to True, this operation returns the tensors generated by default. tf.case supports nested structures as implemented in tf.contrib.framework.nest. All of the callables must return the same (possibly nested) value structure of lists, tuples, and/or named tuples. Singleton lists and tuples form the only exceptions to this: when returned by a callable, they are implicitly unpacked to single values. This behavior is disabled by passing strict=True. Example 1: Pseudocode: if (x < y) return 17;
else return 23;
 Expressions: f1 = lambda: tf.constant(17)
f2 = lambda: tf.constant(23)
r = tf.case([(tf.less(x, y), f1)], default=f2)
 Example 2: Pseudocode: if (x < y && x > z) raise OpError("Only one predicate may evaluate to True");
if (x < y) return 17;
else if (x > z) return 23;
else return -1;
 Expressions: def f1(): return tf.constant(17)
def f2(): return tf.constant(23)
def f3(): return tf.constant(-1)
r = tf.case([(tf.less(x, y), f1), (tf.greater(x, z), f2)],
         default=f3, exclusive=True)

 Args
  pred_fn_pairs   List of pairs of a boolean scalar tensor and a callable which returns a list of tensors.
  default   Optional callable that returns a list of tensors.
  exclusive   True iff at most one predicate is allowed to evaluate to True.
  strict   A boolean that enables/disables 'strict' mode; see above.
  name   A name for this operation (optional).

 Returns   The tensors returned by the first pair whose predicate evaluated to True, or those returned by default if none does.

 Raises
  TypeError   If pred_fn_pairs is not a list/tuple.
  TypeError   If pred_fn_pairs is a list but does not contain 2-tuples.
  TypeError   If fns[i] is not callable for any i, or default is not callable.    V2 Compatibility pred_fn_pairs could be a dictionary in v1. However, tf.Tensor and tf.Variable are no longer hashable in v2, so cannot be used as a key for a dictionary. Please use a list or a tuple instead.


tf.cast     View source on GitHub    Casts a tensor to a new type.  View aliases  Main aliases
tf.dtypes.cast Compat aliases for migration See Migration guide for more details. tf.compat.v1.cast, tf.compat.v1.dtypes.cast
tf.cast(
    x, dtype, name=None
)
 The operation casts x (in case of Tensor) or x.values (in case of SparseTensor or IndexedSlices) to dtype. For example:
x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf.dtypes.cast(x, tf.int32)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
 The operation supports data types (for x and dtype) of uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64, complex64, complex128, bfloat16. In case of casting from complex types (complex64, complex128) to real types, only the real part of x is returned. In case of casting from real types to complex types (complex64, complex128), the imaginary part of the returned value is set to 0. The handling of complex types here matches the behavior of numpy. Note casting nan and inf values to integral types has undefined behavior.

 Args
  x   A Tensor or SparseTensor or IndexedSlices of numeric type. It could be uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64, complex64, complex128, bfloat16.
  dtype   The destination type. The list of supported dtypes is the same as x.
  name   A name for the operation (optional).

 Returns   A Tensor or SparseTensor or IndexedSlices with same shape as x and same type as dtype.

 Raises
  TypeError   If x cannot be cast to the dtype.


tf.clip_by_global_norm     View source on GitHub    Clips values of multiple tensors by the ratio of the sum of their norms.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.clip_by_global_norm
tf.clip_by_global_norm(
    t_list, clip_norm, use_norm=None, name=None
)
 Given a tuple or list of tensors t_list, and a clipping ratio clip_norm, this operation returns a list of clipped tensors list_clipped and the global norm (global_norm) of all tensors in t_list. Optionally, if you've already computed the global norm for t_list, you can specify the global norm with use_norm. To perform the clipping, the values t_list[i] are set to: t_list[i] * clip_norm / max(global_norm, clip_norm)
 where: global_norm = sqrt(sum([l2norm(t)**2 for t in t_list]))
 If clip_norm > global_norm then the entries in t_list remain as they are, otherwise they're all shrunk by the global ratio. If global_norm == infinity then the entries in t_list are all set to NaN to signal that an error occurred. Any of the entries of t_list that are of type None are ignored. This is the correct way to perform gradient clipping (Pascanu et al., 2012). However, it is slower than clip_by_norm() because all the parameters must be ready before the clipping operation can be performed.

 Args
  t_list   A tuple or list of mixed Tensors, IndexedSlices, or None.
  clip_norm   A 0-D (scalar) Tensor > 0. The clipping ratio.
  use_norm   A 0-D (scalar) Tensor of type float (optional). The global norm to use. If not provided, global_norm() is used to compute the norm.
  name   A name for the operation (optional).

 Returns
  list_clipped   A list of Tensors of the same type as list_t.
  global_norm   A 0-D (scalar) Tensor representing the global norm.

 Raises
  TypeError   If t_list is not a sequence.    References: On the difficulty of training Recurrent Neural Networks: Pascanu et al., 2012 (pdf)


tf.clip_by_norm     View source on GitHub    Clips tensor values to a maximum L2-norm.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.clip_by_norm
tf.clip_by_norm(
    t, clip_norm, axes=None, name=None
)
 Given a tensor t, and a maximum clip value clip_norm, this operation normalizes t so that its L2-norm is less than or equal to clip_norm, along the dimensions given in axes. Specifically, in the default case where all dimensions are used for calculation, if the L2-norm of t is already less than or equal to clip_norm, then t is not modified. If the L2-norm is greater than clip_norm, then this operation returns a tensor of the same type and shape as t with its values set to: t * clip_norm / l2norm(t) In this case, the L2-norm of the output tensor is clip_norm. As another example, if t is a matrix and axes == [1], then each row of the output will have L2-norm less than or equal to clip_norm. If axes == [0] instead, each column of the output will be clipped. Code example:
some_nums = tf.constant([[1, 2, 3, 4, 5]], dtype=tf.float32)
tf.clip_by_norm(some_nums, 2.0).numpy()
array([[0.26967996, 0.5393599 , 0.80903983, 1.0787199 , 1.3483998 ]],
      dtype=float32)
 This operation is typically used to clip gradients before applying them with an optimizer. Most gradient data is a collection of different shaped tensors for different parts of the model. Thus, this is a common usage: # Get your gradients after training
loss_value, grads = grad(model, features, labels)

# Apply some clipping
grads = [tf.clip_by_norm(g, norm)
             for g in grads]

# Continue on with training
optimizer.apply_gradients(grads)

 Args
  t   A Tensor or IndexedSlices. This must be a floating point type.
  clip_norm   A 0-D (scalar) Tensor > 0. A maximum clipping value, also floating point
  axes   A 1-D (vector) Tensor of type int32 containing the dimensions to use for computing the L2-norm. If None (the default), uses all dimensions.
  name   A name for the operation (optional).

 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.


tf.clip_by_value     View source on GitHub    Clips tensor values to a specified min and max.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.clip_by_value
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 and clip_value_max. Any values less than clip_value_min are set to clip_value_min. Any values greater than clip_value_max are set to clip_value_max.
Note: clip_value_min needs to be smaller or equal to clip_value_max for correct results.
 For example: Basic usage passes a scalar as the min and max value.
t = tf.constant([[-10., -1., 0.], [0., 2., 10.]])
t2 = tf.clip_by_value(t, clip_value_min=-1, clip_value_max=1)
t2.numpy()
array([[-1., -1.,  0.],
       [ 0.,  1.,  1.]], dtype=float32)
 The min and max can be the same size as t, or broadcastable to that size.
t = tf.constant([[-1, 0., 10.], [-1, 0, 10]])
clip_min = [[2],[1]]
t3 = tf.clip_by_value(t, clip_value_min=clip_min, clip_value_max=100)
t3.numpy()
array([[ 2.,  2., 10.],
       [ 1.,  1., 10.]], dtype=float32)
 Broadcasting fails, intentionally, if you would expand the dimensions of t
t = tf.constant([[-1, 0., 10.], [-1, 0, 10]])
clip_min = [[[2, 1]]] # Has a third axis
t4 = tf.clip_by_value(t, clip_value_min=clip_min, clip_value_max=100)
Traceback (most recent call last):

InvalidArgumentError: Incompatible shapes: [2,3] vs. [1,1,2]
 It throws a TypeError if you try to clip an int to a float value (tf.cast the input to float first).
t = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)
t5 = tf.clip_by_value(t, clip_value_min=-3.1, clip_value_max=3.1)
Traceback (most recent call last):

TypeError: Cannot convert ...

 Args
  t   A Tensor or IndexedSlices.
  clip_value_min   The minimum value to clip to. A scalar Tensor or one that is broadcastable to the shape of t.
  clip_value_max   The maximum value to clip to. A scalar Tensor or one that is broadcastable to the shape of t.
  name   A name for the operation (optional).

 Returns   A clipped Tensor or IndexedSlices.

 Raises   tf.errors.InvalidArgumentError: If the clip tensors would trigger array broadcasting that would make the returned tensor larger than the input.     TypeError   If dtype of the input is int32 and dtype of the clip_value_min or clip_value_max is float32


Module: tf.compat Compatibility functions. The tf.compat module contains two sets of compatibility functions. Tensorflow 1.x and 2.x APIs The compat.v1 and compat.v2 submodules provide a complete copy of both the v1 and v2 APIs for backwards and forwards compatibility across TensorFlow versions 1.x and 2.x. See the migration guide for details. Utilities for writing compatible code Aside from the compat.v1 and compat.v2 submodules, tf.compat also contains a set of helper functions for writing code that works in both:  TensorFlow 1.x and 2.x Python 2 and 3  Type collections The compatibility module also provides the following aliases for common sets of python types:  bytes_or_text_types complex_types integral_types real_types  Modules v1 module: Bring in all of the public TensorFlow interface into this module. Functions as_bytes(...): Converts bytearray, bytes, or unicode python input types to bytes. as_str(...) as_str_any(...): Converts input to str type. as_text(...): Converts any string-like python input types to unicode. dimension_at_index(...): Compatibility utility required to allow for both V1 and V2 behavior in TF. dimension_value(...): Compatibility utility required to allow for both V1 and V2 behavior in TF. forward_compatibility_horizon(...): Context manager for testing forward compatibility of generated graphs. forward_compatible(...): Return true if the forward compatibility window has expired. path_to_str(...): Converts input which is a PathLike object to str type.

 Other Members
  bytes_or_text_types

  complex_types

  integral_types

  real_types


tf.compat.as_bytes     View source on GitHub    Converts bytearray, bytes, or unicode python input types to bytes.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.as_bytes
tf.compat.as_bytes(
    bytes_or_text, encoding='utf-8'
)
 Uses utf-8 encoding for text by default.

 Args
  bytes_or_text   A bytearray, bytes, str, or unicode object.
  encoding   A string indicating the charset for encoding unicode.

 Returns   A bytes object.

 Raises
  TypeError   If bytes_or_text is not a binary or unicode string.


tf.compat.as_str  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.as_str
tf.compat.as_str(
    bytes_or_text, encoding='utf-8'
)


tf.compat.as_str_any     View source on GitHub    Converts input to str type.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.as_str_any
tf.compat.as_str_any(
    value
)
 Uses str(value), except for bytes typed inputs, which are converted using as_str.

 Args
  value   A object that can be converted to str.

 Returns   A str object.


tf.compat.as_text     View source on GitHub    Converts any string-like python input types to unicode.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.as_text
tf.compat.as_text(
    bytes_or_text, encoding='utf-8'
)
 Returns the input as a unicode string. Uses utf-8 encoding for text by default.

 Args
  bytes_or_text   A bytes, str, or unicode object.
  encoding   A string indicating the charset for decoding unicode.

 Returns   A unicode (Python 2) or str (Python 3) object.

 Raises
  TypeError   If bytes_or_text is not a binary or unicode string.


tf.compat.dimension_at_index     View source on GitHub    Compatibility utility required to allow for both V1 and V2 behavior in TF.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.dimension_at_index, tf.compat.v1.dimension_at_index
tf.compat.dimension_at_index(
    shape, index
)
 Until the release of TF 2.0, we need the legacy behavior of TensorShape to coexist with the new behavior. This utility is a bridge between the two. If you want to retrieve the Dimension instance corresponding to a certain index in a TensorShape instance, use this utility, like this: # If you had this in your V1 code:
dim = tensor_shape[i]

# Use `dimension_at_index` as direct replacement compatible with both V1 & V2:
dim = dimension_at_index(tensor_shape, i)

# Another possibility would be this, but WARNING: it only works if the
# tensor_shape instance has a defined rank.
dim = tensor_shape.dims[i]  # `dims` may be None if the rank is undefined!

# In native V2 code, we recommend instead being more explicit:
if tensor_shape.rank is None:
  dim = Dimension(None)
else:
  dim = tensor_shape.dims[i]

# Being more explicit will save you from the following trap (present in V1):
# you might do in-place modifications to `dim` and expect them to be reflected
# in `tensor_shape[i]`, but they would not be (as the Dimension object was
# instantiated on the fly.

 Arguments
  shape   A TensorShape instance.
  index   An integer index.

 Returns   A dimension object.


tf.compat.dimension_value     View source on GitHub    Compatibility utility required to allow for both V1 and V2 behavior in TF.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.dimension_value, tf.compat.v1.dimension_value
tf.compat.dimension_value(
    dimension
)
 Until the release of TF 2.0, we need the legacy behavior of TensorShape to coexist with the new behavior. This utility is a bridge between the two. When accessing the value of a TensorShape dimension, use this utility, like this: # If you had this in your V1 code:
value = tensor_shape[i].value

# Use `dimension_value` as direct replacement compatible with both V1 & V2:
value = dimension_value(tensor_shape[i])

# This would be the V2 equivalent:
value = tensor_shape[i]  # Warning: this will return the dim value in V2!

 Arguments
  dimension   Either a Dimension instance, an integer, or None.

 Returns   A plain value, i.e. an integer or None.


tf.compat.forward_compatibility_horizon     View source on GitHub    Context manager for testing forward compatibility of generated graphs.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.forward_compatibility_horizon
@tf_contextlib.contextmanager
tf.compat.forward_compatibility_horizon(
    year, month, day
)
 See Version compatibility. To ensure forward compatibility of generated graphs (see forward_compatible) with older binaries, new features can be gated with: if compat.forward_compatible(year=2018, month=08, date=01):
  generate_graph_with_new_features()
else:
  generate_graph_so_older_binaries_can_consume_it()
 However, when adding new features, one may want to unittest it before the forward compatibility window expires. This context manager enables such tests. For example: from tensorflow.python.compat import compat

def testMyNewFeature(self):
  with compat.forward_compatibility_horizon(2018, 08, 02):
     # Test that generate_graph_with_new_features() has an effect

 Args
  year   A year (e.g., 2018). Must be an int.
  month   A month (1 <= month <= 12) in year. Must be an int.
  day   A day (1 <= day <= 31, or 30, or 29, or 28) in month. Must be an int.

 Yields   Nothing.


tf.compat.forward_compatible     View source on GitHub    Return true if the forward compatibility window has expired.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.forward_compatible
tf.compat.forward_compatible(
    year, month, day
)
 See Version compatibility. Forward-compatibility refers to scenarios where the producer of a TensorFlow model (a GraphDef or SavedModel) is compiled against a version of the TensorFlow library newer than what the consumer was compiled against. The "producer" is typically a Python program that constructs and trains a model while the "consumer" is typically another program that loads and serves the model. TensorFlow has been supporting a 3 week forward-compatibility window for programs compiled from source at HEAD. For example, consider the case where a new operation MyNewAwesomeAdd is created with the intent of replacing the implementation of an existing Python wrapper - tf.add. The Python wrapper implementation should change from something like: def add(inputs, name=None):
  return gen_math_ops.add(inputs, name)
 to: from tensorflow.python.compat import compat

def add(inputs, name=None):
  if compat.forward_compatible(year, month, day):
    # Can use the awesome new implementation.
    return gen_math_ops.my_new_awesome_add(inputs, name)
  # To maintain forward compatibility, use the old implementation.
  return gen_math_ops.add(inputs, name)
 Where year, month, and day specify the date beyond which binaries that consume a model are expected to have been updated to include the new operations. This date is typically at least 3 weeks beyond the date the code that adds the new operation is committed.

 Args
  year   A year (e.g., 2018). Must be an int.
  month   A month (1 <= month <= 12) in year. Must be an int.
  day   A day (1 <= day <= 31, or 30, or 29, or 28) in month. Must be an int.

 Returns   True if the caller can expect that serialized TensorFlow graphs produced can be consumed by programs that are compiled with the TensorFlow library source code after (year, month, day).


tf.compat.path_to_str       View source on GitHub    Converts input which is a PathLike object to str type.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.compat.path_to_str
tf.compat.path_to_str(
    path
)
 Converts from any python constant representation of a PathLike object to a string. If the input is not a PathLike object, simply returns the input.

 Args
  path   An object that can be converted to path representation.

 Returns   A str object.
 Usage: In case a simplified str version of the path is needed from an os.PathLike object Examples: $ tf.compat.path_to_str('C:\XYZ\tensorflow\./.././tensorflow')
'C:\XYZ\tensorflow\./.././tensorflow' # Windows OS
$ tf.compat.path_to_str(Path('C:\XYZ\tensorflow\./.././tensorflow'))
'C:\XYZ\tensorflow\..\tensorflow' # Windows OS
$ tf.compat.path_to_str(Path('./corpus'))
'corpus' # Linux OS
$ tf.compat.path_to_str('./.././Corpus')
'./.././Corpus' # Linux OS
$ tf.compat.path_to_str(Path('./.././Corpus'))
'../Corpus' # Linux OS
$ tf.compat.path_to_str(Path('./..////../'))
'../..' # Linux OS