tf.function     View source on GitHub    Compiles a function into a callable TensorFlow graph.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.function
tf.function(
    func=None, input_signature=None, autograph=True, experimental_implements=None,
    experimental_autograph_options=None, experimental_relax_shapes=False,
    experimental_compile=None, experimental_follow_type_hints=None
)
 tf.function constructs a callable that executes a TensorFlow graph (tf.Graph) created by trace-compiling the TensorFlow operations in func, effectively executing func as a TensorFlow graph. Example usage:
@tf.function
def f(x, y):
  return x ** 2 + y
x = tf.constant([2, 3])
y = tf.constant([3, -2])
f(x, y)
<tf.Tensor: ... numpy=array([7, 7], ...)>
 Features func may use data-dependent control flow, including if, for, while break, continue and return statements:
@tf.function
def f(x):
  if tf.reduce_sum(x) > 0:
    return x * x
  else:
    return -x // 2
f(tf.constant(-2))
<tf.Tensor: ... numpy=1>
 func's closure may include tf.Tensor and tf.Variable objects:
@tf.function
def f():
  return x ** 2 + y
x = tf.constant([-2, -3])
y = tf.Variable([3, -2])
f()
<tf.Tensor: ... numpy=array([7, 7], ...)>
 func may also use ops with side effects, such as tf.print, tf.Variable and others:
v = tf.Variable(1)
@tf.function
def f(x):
  for i in tf.range(x):
    v.assign_add(i)
f(3)
v
<tf.Variable ... numpy=4>
 Key Point: Any Python side-effects (appending to a list, printing with print, etc) will only happen once, when func is traced. To have side-effects executed into your tf.function they need to be written as TF ops:
l = []
@tf.function
def f(x):
  for i in x:
    l.append(i + 1)    # Caution! Will only happen once when tracing
f(tf.constant([1, 2, 3]))
l
[<tf.Tensor ...>]
 Instead, use TensorFlow collections like tf.TensorArray:
@tf.function
def f(x):
  ta = tf.TensorArray(dtype=tf.int32, size=0, dynamic_size=True)
  for i in range(len(x)):
    ta = ta.write(i, x[i] + 1)
  return ta.stack()
f(tf.constant([1, 2, 3]))
<tf.Tensor: ..., numpy=array([2, 3, 4], ...)>
 tf.function is polymorphic Internally, tf.function can build more than one graph, to support arguments with different data types or shapes, since TensorFlow can build more efficient graphs that are specialized on shapes and dtypes. tf.function also treats any pure Python value as opaque objects, and builds a separate graph for each set of Python arguments that it encounters. To obtain an individual graph, use the get_concrete_function method of the callable created by tf.function. It can be called with the same arguments as func and returns a special tf.Graph object:
@tf.function
def f(x):
  return x + 1
isinstance(f.get_concrete_function(1).graph, tf.Graph)
True
 Caution: Passing python scalars or lists as arguments to tf.function will always build a new graph. To avoid this, pass numeric arguments as Tensors whenever possible:
@tf.function
def f(x):
  return tf.abs(x)
f1 = f.get_concrete_function(1)
f2 = f.get_concrete_function(2)  # Slow - builds new graph
f1 is f2
False
f1 = f.get_concrete_function(tf.constant(1))
f2 = f.get_concrete_function(tf.constant(2))  # Fast - reuses f1
f1 is f2
True
 Python numerical arguments should only be used when they take few distinct values, such as hyperparameters like the number of layers in a neural network. Input signatures For Tensor arguments, tf.function instantiates a separate graph for every unique set of input shapes and datatypes. The example below creates two separate graphs, each specialized to a different shape:
@tf.function
def f(x):
  return x + 1
vector = tf.constant([1.0, 1.0])
matrix = tf.constant([[3.0]])
f.get_concrete_function(vector) is f.get_concrete_function(matrix)
False
 An "input signature" can be optionally provided to tf.function to control the graphs traced. The input signature specifies the shape and type of each Tensor argument to the function using a tf.TensorSpec object. More general shapes can be used. This is useful to avoid creating multiple graphs when Tensors have dynamic shapes. It also restricts the shape and datatype of Tensors that can be used:
@tf.function(
    input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)])
def f(x):
  return x + 1
vector = tf.constant([1.0, 1.0])
matrix = tf.constant([[3.0]])
f.get_concrete_function(vector) is f.get_concrete_function(matrix)
True
 Variables may only be created once tf.function only allows creating new tf.Variable objects when it is called for the first time:
class MyModule(tf.Module):
  def __init__(self):
    self.v = None

  @tf.function
  def __call__(self, x):
    if self.v is None:
      self.v = tf.Variable(tf.ones_like(x))
    return self.v * x
 In general, it is recommended to create stateful objects like tf.Variable outside of tf.function and passing them as arguments. Using type annotations to improve performance 'experimental_follow_type_hints` can be used along with type annotations to improve performance by reducing the number of expensive graph retracings. For example, an argument annotated with tf.Tensor is converted to Tensor even when the input is a non-Tensor value.
@tf.function(experimental_follow_type_hints=True)
def f_with_hints(x: tf.Tensor):
  print('Tracing')
  return x
@tf.function(experimental_follow_type_hints=False)
def f_no_hints(x: tf.Tensor):
  print('Tracing')
  return x
f_no_hints(1)
Tracing
<tf.Tensor: shape=(), dtype=int32, numpy=1>
f_no_hints(2)
Tracing
<tf.Tensor: shape=(), dtype=int32, numpy=2>
f_with_hints(1)
Tracing
<tf.Tensor: shape=(), dtype=int32, numpy=1>
f_with_hints(2)
<tf.Tensor: shape=(), dtype=int32, numpy=2>

 Args
  func   the function to be compiled. If func is None, tf.function returns a decorator that can be invoked with a single argument - func. In other words, tf.function(input_signature=...)(func) is equivalent to tf.function(func, input_signature=...). The former can be used as decorator.
  input_signature   A possibly nested sequence of tf.TensorSpec objects specifying the shapes and dtypes of the Tensors that will be supplied to this function. If None, a separate function is instantiated for each inferred input signature. If input_signature is specified, every input to func must be a Tensor, and func cannot accept **kwargs.
  autograph   Whether autograph should be applied on func before tracing a graph. Data-dependent control flow requires autograph=True. For more information, see the tf.function and AutoGraph guide.
  experimental_implements   If provided, contains a name of a "known" function this implements. For example "mycompany.my_recurrent_cell". This is stored as an attribute in inference function, which can then be detected when processing serialized function. See standardizing composite ops for details. For an example of utilizing this attribute see this example The code above automatically detects and substitutes function that implements "embedded_matmul" and allows TFLite to substitute its own implementations. For instance, a tensorflow user can use this attribute to mark that their function also implements embedded_matmul (perhaps more efficiently!) by specifying it using this parameter: @tf.function(experimental_implements="embedded_matmul") This can either be specified as just the string name of the function or a NameAttrList corresponding to a list of key-value attributes associated with the function name. The name of the function will be in the 'name' field of the NameAttrList.
  experimental_autograph_options   Optional tuple of tf.autograph.experimental.Feature values.
  experimental_relax_shapes   When True, tf.function may generate fewer, graphs that are less specialized on input shapes.
  experimental_compile   If True, the function is always compiled by XLA. XLA may be more efficient in some cases (e.g. TPU, XLA_GPU, dense tensor computations).
  experimental_follow_type_hints   When True, the function may use type annotations from func to optimize the tracing performance. For example, arguments annotated with tf.Tensor will automatically be converted to a Tensor.

 Returns   If func is not None, returns a callable that will execute the compiled function (and return zero or more tf.Tensor objects). If func is None, returns a decorator that, when invoked with a single func argument, returns a callable equivalent to the case above.

 Raises   ValueError when attempting to use experimental_compile, but XLA support is not enabled.


tf.gather       View source on GitHub    Gather slices from params axis axis according to indices.
tf.gather(
    params, indices, validate_indices=None, axis=None, batch_dims=0, name=None
)
 Gather slices from params axis axis according to indices. indices must be an integer tensor of any dimension (usually 0-D or 1-D). For 0-D (scalar) indices:  $$\begin{align*} output[p_0, ..., p_{axis-1}, && &&& p_{axis + 1}, ..., p_{N-1}] = \\ params[p_0, ..., p_{axis-1}, && indices, &&& p_{axis + 1}, ..., p_{N-1}] \end{align*}$$  Where N = ndims(params). For 1-D (vector) indices with batch_dims=0:  $$\begin{align*} output[p_0, ..., p_{axis-1}, && &i, &&p_{axis + 1}, ..., p_{N-1}] =\\ params[p_0, ..., p_{axis-1}, && indices[&i], &&p_{axis + 1}, ..., p_{N-1}] \end{align*}$$  In the general case, produces an output tensor where:  $$\begin{align*} output[p_0, &..., p_{axis-1}, & &i_{B}, ..., i_{M-1}, & p_{axis + 1}, &..., p_{N-1}] = \\ params[p_0, &..., p_{axis-1}, & indices[p_0, ..., p_{B-1}, &i_{B}, ..., i_{M-1}], & p_{axis + 1}, &..., p_{N-1}] \end{align*}$$  Where N = ndims(params), M = ndims(indices), and B = batch_dims. Note that params.shape[:batch_dims] must be identical to indices.shape[:batch_dims]. The shape of the output tensor is:  output.shape = params.shape[:axis] + indices.shape[batch_dims:] + params.shape[axis + 1:].  Note that on CPU, if an out of bound index is found, an error is returned. On GPU, if an out of bound index is found, a 0 is stored in the corresponding output value. See also tf.gather_nd.

 Args
  params   The Tensor from which to gather values. Must be at least rank axis + 1.
  indices   The index Tensor. Must be one of the following types: int32, int64. Must be in range [0, params.shape[axis]).
  validate_indices   Deprecated, does nothing.
  axis   A Tensor. Must be one of the following types: int32, int64. The axis in params to gather indices from. Must be greater than or equal to batch_dims. Defaults to the first non-batch dimension. Supports negative indexes.
  batch_dims   An integer. The number of batch dimensions. Must be less than or equal to rank(indices).
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as params.


tf.gather_nd       View source on GitHub    Gather slices from params into a Tensor with shape specified by indices.
tf.gather_nd(
    params, indices, batch_dims=0, name=None
)
 indices is an K-dimensional integer tensor, best thought of as a (K-1)-dimensional tensor of indices into params, where each element defines a slice of params: output[\\(i_0, ..., i_{K-2}\\)] = params[indices[\\(i_0, ..., i_{K-2}\\)]]
 Whereas in tf.gather indices defines slices into the first dimension of params, in tf.gather_nd, indices defines slices into the first N dimensions of params, where N = indices.shape[-1]. The last dimension of indices can be at most the rank of params: indices.shape[-1] <= params.rank
 The last dimension of indices corresponds to elements (if indices.shape[-1] == params.rank) or slices (if indices.shape[-1] < params.rank) along dimension indices.shape[-1] of params. The output tensor has shape indices.shape[:-1] + params.shape[indices.shape[-1]:]
 Additionally both 'params' and 'indices' can have M leading batch dimensions that exactly match. In this case 'batch_dims' must be M. Note that on CPU, if an out of bound index is found, an error is returned. On GPU, if an out of bound index is found, a 0 is stored in the corresponding output value. Some examples below. Simple indexing into a matrix: indices = [[0, 0], [1, 1]]
params = [['a', 'b'], ['c', 'd']]
output = ['a', 'd']
 Slice indexing into a matrix: indices = [[1], [0]]
params = [['a', 'b'], ['c', 'd']]
output = [['c', 'd'], ['a', 'b']]
 Indexing into a 3-tensor: indices = [[1]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [[['a1', 'b1'], ['c1', 'd1']]]

indices = [[0, 1], [1, 0]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [['c0', 'd0'], ['a1', 'b1']]

indices = [[0, 0, 1], [1, 0, 1]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = ['b0', 'b1']
 The examples below are for the case when only indices have leading extra dimensions. If both 'params' and 'indices' have leading batch dimensions, use the 'batch_dims' parameter to run gather_nd in batch mode. Batched indexing into a matrix: indices = [[[0, 0]], [[0, 1]]]
params = [['a', 'b'], ['c', 'd']]
output = [['a'], ['b']]
 Batched slice indexing into a matrix: indices = [[[1]], [[0]]]
params = [['a', 'b'], ['c', 'd']]
output = [[['c', 'd']], [['a', 'b']]]
 Batched indexing into a 3-tensor: indices = [[[1]], [[0]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [[[['a1', 'b1'], ['c1', 'd1']]],
          [[['a0', 'b0'], ['c0', 'd0']]]]

indices = [[[0, 1], [1, 0]], [[0, 0], [1, 1]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [[['c0', 'd0'], ['a1', 'b1']],
          [['a0', 'b0'], ['c1', 'd1']]]

indices = [[[0, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 1, 0]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [['b0', 'b1'], ['d0', 'c1']]
 Examples with batched 'params' and 'indices': batch_dims = 1
indices = [[1], [0]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [['c0', 'd0'], ['a1', 'b1']]

batch_dims = 1
indices = [[[1]], [[0]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [[['c0', 'd0']], [['a1', 'b1']]]

batch_dims = 1
indices = [[[1, 0]], [[0, 1]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
          [['a1', 'b1'], ['c1', 'd1']]]
output = [['c0'], ['b1']]
 See also tf.gather.

 Args
  params   A Tensor. The tensor from which to gather values.
  indices   A Tensor. Must be one of the following types: int32, int64. Index tensor.
  name   A name for the operation (optional).
  batch_dims   An integer or a scalar 'Tensor'. The number of batch dimensions.

 Returns   A Tensor. Has the same type as params.


tf.get_logger     View source on GitHub    Return TF logger instance.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.get_logger
tf.get_logger()


tf.get_static_value     View source on GitHub    Returns the constant value of the given tensor, if efficiently calculable.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.get_static_value
tf.get_static_value(
    tensor, partial=False
)
 This function attempts to partially evaluate the given tensor, and returns its value as a numpy ndarray if this succeeds. Compatibility(V1): If constant_value(tensor) returns a non-None result, it will no longer be possible to feed a different value for tensor. This allows the result of this function to influence the graph that is constructed, and permits static shape optimizations.

 Args
  tensor   The Tensor to be evaluated.
  partial   If True, the returned numpy array is allowed to have partially evaluated values. Values that can't be evaluated will be None.

 Returns   A numpy ndarray containing the constant value of the given tensor, or None if it cannot be calculated.

 Raises
  TypeError   if tensor is not an ops.Tensor.


tf.gradients     View source on GitHub    Constructs symbolic derivatives of sum of ys w.r.t. x in xs.
tf.gradients(
    ys, xs, grad_ys=None, name='gradients', gate_gradients=False,
    aggregation_method=None, stop_gradients=None,
    unconnected_gradients=tf.UnconnectedGradients.NONE
)
 tf.gradients is only valid in a graph context. In particular, it is valid in the context of a tf.function wrapper, where code is executing as a graph. ys and xs are each a Tensor or a list of tensors. grad_ys is a list of Tensor, holding the gradients received by the ys. The list must be the same length as ys. gradients() adds ops to the graph to output the derivatives of ys with respect to xs. It returns a list of Tensor of length len(xs) where each tensor is the sum(dy/dx) for y in ys and for x in xs. grad_ys is a list of tensors of the same length as ys that holds the initial gradients for each y in ys. When grad_ys is None, we fill in a tensor of '1's of the shape of y for each y in ys. A user can provide their own initial grad_ys to compute the derivatives using a different initial gradient for each y (e.g., if one wanted to weight the gradient differently for each value in each y). stop_gradients is a Tensor or a list of tensors to be considered constant with respect to all xs. These tensors will not be backpropagated through, as though they had been explicitly disconnected using stop_gradient. Among other things, this allows computation of partial derivatives as opposed to total derivatives. For example:
@tf.function
def example():
  a = tf.constant(0.)
  b = 2 * a
  return tf.gradients(a + b, [a, b], stop_gradients=[a, b])
example()
[<tf.Tensor: shape=(), dtype=float32, numpy=1.0>,
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>]
 Here the partial derivatives g evaluate to [1.0, 1.0], compared to the total derivatives tf.gradients(a + b, [a, b]), which take into account the influence of a on b and evaluate to [3.0, 1.0]. Note that the above is equivalent to:
@tf.function
def example():
  a = tf.stop_gradient(tf.constant(0.))
  b = tf.stop_gradient(2 * a)
  return tf.gradients(a + b, [a, b])
example()
[<tf.Tensor: shape=(), dtype=float32, numpy=1.0>,
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>]
 stop_gradients provides a way of stopping gradient after the graph has already been constructed, as compared to tf.stop_gradient which is used during graph construction. When the two approaches are combined, backpropagation stops at both tf.stop_gradient nodes and nodes in stop_gradients, whichever is encountered first. All integer tensors are considered constant with respect to all xs, as if they were included in stop_gradients. unconnected_gradients determines the value returned for each x in xs if it is unconnected in the graph to ys. By default this is None to safeguard against errors. Mathematically these gradients are zero which can be requested using the 'zero' option. tf.UnconnectedGradients provides the following options and behaviors:
@tf.function
def example(use_zero):
  a = tf.ones([1, 2])
  b = tf.ones([3, 1])
  if use_zero:
    return tf.gradients([b], [a], unconnected_gradients='zero')
  else:
    return tf.gradients([b], [a], unconnected_gradients='none')
example(False)
[None]
example(True)
[<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[0., 0.]], ...)>]
 Let us take one practical example which comes during the back propogation phase. This function is used to evaluate the derivatives of the cost function with respect to Weights Ws and Biases bs. Below sample implementation provides the exaplantion of what it is actually used for :
@tf.function
def example():
  Ws = tf.constant(0.)
  bs = 2 * Ws
  cost = Ws + bs  # This is just an example. Please ignore the formulas.
  g = tf.gradients(cost, [Ws, bs])
  dCost_dW, dCost_db = g
  return dCost_dW, dCost_db
example()
(<tf.Tensor: shape=(), dtype=float32, numpy=3.0>,
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>)

 Args
  ys   A Tensor or list of tensors to be differentiated.
  xs   A Tensor or list of tensors to be used for differentiation.
  grad_ys   Optional. A Tensor or list of tensors the same size as ys and holding the gradients computed for each y in ys.
  name   Optional name to use for grouping all the gradient ops together. defaults to 'gradients'.
  gate_gradients   If True, add a tuple around the gradients returned for an operations. This avoids some race conditions.
  aggregation_method   Specifies the method used to combine gradient terms. Accepted values are constants defined in the class AggregationMethod.
  stop_gradients   Optional. A Tensor or list of tensors not to differentiate through.
  unconnected_gradients   Optional. Specifies the gradient value returned when the given input tensors are unconnected. Accepted values are constants defined in the class tf.UnconnectedGradients and the default value is none.

 Returns   A list of Tensor of length len(xs) where each tensor is the sum(dy/dx) for y in ys and for x in xs.

 Raises
  LookupError   if one of the operations between x and y does not have a registered gradient function.
  ValueError   if the arguments are invalid.
  RuntimeError   if called in Eager mode.


tf.GradientTape     View source on GitHub    Record operations for automatic differentiation.  View aliases  Main aliases
tf.autodiff.GradientTape Compat aliases for migration See Migration guide for more details. tf.compat.v1.GradientTape
tf.GradientTape(
    persistent=False, watch_accessed_variables=True
)
 Operations are recorded if they are executed within this context manager and at least one of their inputs is being "watched". Trainable variables (created by tf.Variable or tf.compat.v1.get_variable, where trainable=True is default in both cases) are automatically watched. Tensors can be manually watched by invoking the watch method on this context manager. For example, consider the function y = x * x. The gradient at x = 3.0 can be computed as:
x = tf.constant(3.0)
with tf.GradientTape() as g:
  g.watch(x)
  y = x * x
dy_dx = g.gradient(y, x)
print(dy_dx)
tf.Tensor(6.0, shape=(), dtype=float32)
 GradientTapes can be nested to compute higher-order derivatives. For example,
x = tf.constant(5.0)
with tf.GradientTape() as g:
  g.watch(x)
  with tf.GradientTape() as gg:
    gg.watch(x)
    y = x * x
  dy_dx = gg.gradient(y, x)  # dy_dx = 2 * x
d2y_dx2 = g.gradient(dy_dx, x)  # d2y_dx2 = 2
print(dy_dx)
tf.Tensor(10.0, shape=(), dtype=float32)
print(d2y_dx2)
tf.Tensor(2.0, shape=(), dtype=float32)
 By default, the resources held by a GradientTape are released as soon as GradientTape.gradient() method is called. To compute multiple gradients over the same computation, create a persistent gradient tape. This allows multiple calls to the gradient() method as resources are released when the tape object is garbage collected. For example:
x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as g:
  g.watch(x)
  y = x * x
  z = y * y
dz_dx = g.gradient(z, x)  # (4*x^3 at x = 3)
print(dz_dx)
tf.Tensor(108.0, shape=(), dtype=float32)
dy_dx = g.gradient(y, x)
print(dy_dx)
tf.Tensor(6.0, shape=(), dtype=float32)
 By default GradientTape will automatically watch any trainable variables that are accessed inside the context. If you want fine grained control over which variables are watched you can disable automatic tracking by passing watch_accessed_variables=False to the tape constructor:
x = tf.Variable(2.0)
w = tf.Variable(5.0)
with tf.GradientTape(
    watch_accessed_variables=False, persistent=True) as tape:
  tape.watch(x)
  y = x ** 2  # Gradients will be available for `x`.
  z = w ** 3  # No gradients will be available as `w` isn't being watched.
dy_dx = tape.gradient(y, x)
print(dy_dx)
tf.Tensor(4.0, shape=(), dtype=float32)
# No gradients will be available as `w` isn't being watched.
dz_dy = tape.gradient(z, w)
print(dz_dy)
None
 Note that when using models you should ensure that your variables exist when using watch_accessed_variables=False. Otherwise it's quite easy to make your first iteration not have any gradients: a = tf.keras.layers.Dense(32)
b = tf.keras.layers.Dense(32)

with tf.GradientTape(watch_accessed_variables=False) as tape:
  tape.watch(a.variables)  # Since `a.build` has not been called at this point
                           # `a.variables` will return an empty list and the
                           # tape will not be watching anything.
  result = b(a(inputs))
  tape.gradient(result, a.variables)  # The result of this computation will be
                                      # a list of `None`s since a's variables
                                      # are not being watched.
 Note that only tensors with real or complex dtypes are differentiable.

 Args
  persistent   Boolean controlling whether a persistent gradient tape is created. False by default, which means at most one call can be made to the gradient() method on this object.
  watch_accessed_variables   Boolean controlling whether the tape will automatically watch any (trainable) variables accessed while the tape is active. Defaults to True meaning gradients can be requested from any result computed in the tape derived from reading a trainable Variable. If False users must explicitly watch any Variables they want to request gradients from.    Methods batch_jacobian View source
batch_jacobian(
    target, source, unconnected_gradients=tf.UnconnectedGradients.NONE,
    parallel_iterations=None, experimental_use_pfor=True
)
 Computes and stacks per-example jacobians. See wikipedia article for the definition of a Jacobian. This function is essentially an efficient implementation of the following: tf.stack([self.jacobian(y[i], x[i]) for i in range(x.shape[0])]). Note that compared to GradientTape.jacobian which computes gradient of each output value w.r.t each input value, this function is useful when target[i,...] is independent of source[j,...] for j != i. This assumption allows more efficient computation as compared to GradientTape.jacobian. The output, as well as intermediate activations, are lower dimensional and avoid a bunch of redundant zeros which would result in the jacobian computation given the independence assumption.
Note: Unless you set persistent=True a GradientTape can only be used to compute one set of gradients (or jacobians).
 Example usage: with tf.GradientTape() as g:
  x = tf.constant([[1., 2.], [3., 4.]], dtype=tf.float32)
  g.watch(x)
  y = x * x
batch_jacobian = g.batch_jacobian(y, x)
# batch_jacobian is [[[2,  0], [0,  4]], [[6,  0], [0,  8]]]

 Args
  target   A tensor with rank 2 or higher and with shape [b, y1, ..., y_n]. target[i,...] should only depend on source[i,...].
  source   A tensor with rank 2 or higher and with shape [b, x1, ..., x_m].
  unconnected_gradients   a value which can either hold 'none' or 'zero' and alters the value which will be returned if the target and sources are unconnected. The possible values and effects are detailed in 'UnconnectedGradients' and it defaults to 'none'.
  parallel_iterations   A knob to control how many iterations are dispatched in parallel. This knob can be used to control the total memory usage.
  experimental_use_pfor   If true, uses pfor for computing the Jacobian. Else uses a tf.while_loop.

 Returns   A tensor t with shape [b, y_1, ..., y_n, x1, ..., x_m] where t[i, ...] is the jacobian of target[i, ...] w.r.t. source[i, ...], i.e. stacked per-example jacobians.

 Raises
  RuntimeError   If called on a used, non-persistent tape.
  RuntimeError   If called on a non-persistent tape with eager execution enabled and without enabling experimental_use_pfor.
  ValueError   If vectorization of jacobian computation fails or if first dimension of target and source do not match.    gradient View source
gradient(
    target, sources, output_gradients=None,
    unconnected_gradients=tf.UnconnectedGradients.NONE
)
 Computes the gradient using operations recorded in context of this tape.
Note: Unless you set persistent=True a GradientTape can only be used to compute one set of gradients (or jacobians).

 Args
  target   a list or nested structure of Tensors or Variables to be differentiated.
  sources   a list or nested structure of Tensors or Variables. target will be differentiated against elements in sources.
  output_gradients   a list of gradients, one for each element of target. Defaults to None.
  unconnected_gradients   a value which can either hold 'none' or 'zero' and alters the value which will be returned if the target and sources are unconnected. The possible values and effects are detailed in 'UnconnectedGradients' and it defaults to 'none'.

 Returns   a list or nested structure of Tensors (or IndexedSlices, or None), one for each element in sources. Returned structure is the same as the structure of sources.

 Raises
  RuntimeError   If called on a used, non-persistent tape.
  RuntimeError   If called inside the context of the tape.
  ValueError   If the target is a variable or if unconnected gradients is called with an unknown value.    jacobian View source
jacobian(
    target, sources, unconnected_gradients=tf.UnconnectedGradients.NONE,
    parallel_iterations=None, experimental_use_pfor=True
)
 Computes the jacobian using operations recorded in context of this tape.
Note: Unless you set persistent=True a GradientTape can only be used to compute one set of gradients (or jacobians).
 Seewikipedia article for the definition of a Jacobian. Example usage: with tf.GradientTape() as g:
  x  = tf.constant([1.0, 2.0])
  g.watch(x)
  y = x * x
jacobian = g.jacobian(y, x)
# jacobian value is [[2., 0.], [0., 4.]]

 Args
  target   Tensor to be differentiated.
  sources   a list or nested structure of Tensors or Variables. target will be differentiated against elements in sources.
  unconnected_gradients   a value which can either hold 'none' or 'zero' and alters the value which will be returned if the target and sources are unconnected. The possible values and effects are detailed in 'UnconnectedGradients' and it defaults to 'none'.
  parallel_iterations   A knob to control how many iterations are dispatched in parallel. This knob can be used to control the total memory usage.
  experimental_use_pfor   If true, vectorizes the jacobian computation. Else falls back to a sequential while_loop. Vectorization can sometimes fail or lead to excessive memory usage. This option can be used to disable vectorization in such cases.

 Returns   A list or nested structure of Tensors (or None), one for each element in sources. Returned structure is the same as the structure of sources. Note if any gradient is sparse (IndexedSlices), jacobian function currently makes it dense and returns a Tensor instead. This may change in the future.

 Raises
  RuntimeError   If called on a used, non-persistent tape.
  RuntimeError   If called on a non-persistent tape with eager execution enabled and without enabling experimental_use_pfor.
  ValueError   If vectorization of jacobian computation fails.    reset View source
reset()
 Clears all information stored in this tape. Equivalent to exiting and reentering the tape context manager with a new tape. For example, the two following code blocks are equivalent: with tf.GradientTape() as t:
  loss = loss_fn()
with tf.GradientTape() as t:
  loss += other_loss_fn()
t.gradient(loss, ...)  # Only differentiates other_loss_fn, not loss_fn

# The following is equivalent to the above
with tf.GradientTape() as t:
  loss = loss_fn()
  t.reset()
  loss += other_loss_fn()
t.gradient(loss, ...)  # Only differentiates other_loss_fn, not loss_fn
 This is useful if you don't want to exit the context manager for the tape, or can't because the desired reset point is inside a control flow construct: with tf.GradientTape() as t:
  loss = ...
  if loss > k:
    t.reset()
 stop_recording View source
@tf_contextlib.contextmanager
stop_recording()
 Temporarily stops recording operations on this tape. Operations executed while this context manager is active will not be recorded on the tape. This is useful for reducing the memory used by tracing all computations. For example:
x = tf.constant(4.0)
with tf.GradientTape() as tape:
  with tape.stop_recording():
    y = x ** 2
dy_dx = tape.gradient(y, x)
print(dy_dx)
None

 Yields   None

 Raises
  RuntimeError   if the tape is not currently recording.    watch View source
watch(
    tensor
)
 Ensures that tensor is being traced by this tape.

 Args
  tensor   a Tensor or list of Tensors.

 Raises
  ValueError   if it encounters something that is not a tensor.    watched_variables View source
watched_variables()
 Returns variables watched by this tape in order of construction. __enter__ View source
__enter__()
 Enters a context inside which operations are recorded on this tape. __exit__ View source
__exit__(
    typ, value, traceback
)
 Exits the recording context, no further operations are traced.


tf.grad_pass_through     View source on GitHub    Creates a grad-pass-through op with the forward behavior provided in f.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.grad_pass_through
tf.grad_pass_through(
    f
)
 Use this function to wrap any op, maintaining its behavior in the forward pass, but replacing the original op in the backward graph with an identity. For example: x = tf.Variable(1.0, name="x")
z = tf.Variable(3.0, name="z")

with tf.GradientTape() as tape:
  # y will evaluate to 9.0
  y = tf.grad_pass_through(x.assign)(z**2)
# grads will evaluate to 6.0
grads = tape.gradient(y, z)
 Another example is a 'differentiable' moving average approximation, where gradients are allowed to flow into the last value fed to the moving average, but the moving average is still used for the forward pass: x = ... # Some scalar value
# A moving average object, we don't need to know how this is implemented
moving_average = MovingAverage()
with backprop.GradientTape() as tape:
  # mavg_x will evaluate to the current running average value
  mavg_x = tf.grad_pass_through(moving_average)(x)
grads = tape.gradient(mavg_x, x) # grads will evaluate to 1.0

 Args
  f   function f(*x) that returns a Tensor or nested structure of Tensor outputs.

 Returns   A function h(x) which returns the same values as f(x) and whose gradients are the same as those of an identity function.


tf.Graph     View source on GitHub    A TensorFlow computation, represented as a dataflow graph.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.Graph
tf.Graph()
 Graphs are used by tf.functions to represent the function's computations. Each graph contains a set of tf.Operation objects, which represent units of computation; and tf.Tensor objects, which represent the units of data that flow between operations. Using graphs directly (deprecated) A tf.Graph can be constructed and used directly without a tf.function, as was required in TensorFlow 1, but this is deprecated and it is recommended to use a tf.function instead. If a graph is directly used, other deprecated TensorFlow 1 classes are also required to execute the graph, such as a tf.compat.v1.Session. A default graph can be registered with the tf.Graph.as_default context manager. Then, operations will be added to the graph instead of being executed eagerly. For example: g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  assert c.graph is g
 tf.compat.v1.get_default_graph() can be used to obtain the default graph. Important note: This class is not thread-safe for graph construction. All operations should be created from a single thread, or external synchronization must be provided. Unless otherwise specified, all methods are not thread-safe. A Graph instance supports an arbitrary number of "collections" that are identified by name. For convenience when building a large graph, collections can store groups of related objects: for example, the tf.Variable uses a collection (named tf.GraphKeys.GLOBAL_VARIABLES) for all variables that are created during the construction of a graph. The caller may define additional collections by specifying a new name.

 Attributes
  building_function   Returns True iff this graph represents a function.
  collections   Returns the names of the collections known to this graph.
  finalized   True if this graph has been finalized.
  graph_def_versions   The GraphDef version information of this graph. For details on the meaning of each version, see GraphDef.

  seed   The graph-level random seed of this graph.
  version   Returns a version number that increases as ops are added to the graph. Note that this is unrelated to the tf.Graph.graph_def_versions.
   Methods add_to_collection View source
add_to_collection(
    name, value
)
 Stores value in the collection with the given name. Note that collections are not sets, so it is possible to add a value to a collection several times.

 Args
  name   The key for the collection. The GraphKeys class contains many standard names for collections.
  value   The value to add to the collection.    add_to_collections View source
add_to_collections(
    names, value
)
 Stores value in the collections given by names. Note that collections are not sets, so it is possible to add a value to a collection several times. This function makes sure that duplicates in names are ignored, but it will not check for pre-existing membership of value in any of the collections in names. names can be any iterable, but if names is a string, it is treated as a single collection name.

 Args
  names   The keys for the collections to add to. The GraphKeys class contains many standard names for collections.
  value   The value to add to the collections.    as_default View source
as_default()
 Returns a context manager that makes this Graph the default graph. This method should be used if you want to create multiple graphs in the same process. For convenience, a global default graph is provided, and all ops will be added to this graph if you do not create a new graph explicitly. Use this method with the with keyword to specify that ops created within the scope of a block should be added to this graph. In this case, once the scope of the with is exited, the previous default graph is set again as default. There is a stack, so it's ok to have multiple nested levels of as_default calls. The default graph is a property of the current thread. If you create a new thread, and wish to use the default graph in that thread, you must explicitly add a with g.as_default(): in that thread's function. The following code examples are equivalent: # 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

# 2. Constructing and making default:
with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  assert c.graph is g
 If eager execution is enabled ops created under this context manager will be added to the graph instead of executed eagerly.

 Returns   A context manager for using this graph as the default graph.
 as_graph_def View source
as_graph_def(
    from_version=None, add_shapes=False
)
 Returns a serialized GraphDef representation of this graph. The serialized GraphDef can be imported into another Graph (using tf.import_graph_def) or used with the C++ Session API. This method is thread-safe.

 Args
  from_version   Optional. If this is set, returns a GraphDef containing only the nodes that were added to this graph since its version property had the given value.
  add_shapes   If true, adds an "_output_shapes" list attr to each node with the inferred shapes of each of its outputs.

 Returns   A GraphDef protocol buffer.

 Raises
  ValueError   If the graph_def would be too large.    as_graph_element View source
as_graph_element(
    obj, allow_tensor=True, allow_operation=True
)
 Returns the object referred to by obj, as an Operation or Tensor. This function validates that obj represents an element of this graph, and gives an informative error message if it is not. This function is the canonical way to get/validate an object of one of the allowed types from an external argument reference in the Session API. This method may be called concurrently from multiple threads.

 Args
  obj   A Tensor, an Operation, or the name of a tensor or operation. Can also be any object with an _as_graph_element() method that returns a value of one of these types. Note: _as_graph_element will be called inside the graph's lock and so may not modify the graph.
  allow_tensor   If true, obj may refer to a Tensor.
  allow_operation   If true, obj may refer to an Operation.

 Returns   The Tensor or Operation in the Graph corresponding to obj.

 Raises
  TypeError   If obj is not a type we support attempting to convert to types.
  ValueError   If obj is of an appropriate type but invalid. For example, an invalid string.
  KeyError   If obj is not an object in the graph.    clear_collection View source
clear_collection(
    name
)
 Clears all values in a collection.

 Args
  name   The key for the collection. The GraphKeys class contains many standard names for collections.    colocate_with View source
@tf_contextlib.contextmanager
colocate_with(
    op, ignore_existing=False
)
 Returns a context manager that specifies an op to colocate with.
Note: this function is not for public use, only for internal libraries.
 For example: a = tf.Variable([1.0])
with g.colocate_with(a):
  b = tf.constant(1.0)
  c = tf.add(a, b)
 b and c will always be colocated with a, no matter where a is eventually placed.
Note: Using a colocation scope resets any existing device constraints.
 If op is None then ignore_existing must be True and the new scope resets all colocation and device constraints.

 Args
  op   The op to colocate all created ops with, or None.
  ignore_existing   If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack. If op is None, this value must be True.

 Raises
  ValueError   if op is None but ignore_existing is False.

 Yields   A context manager that specifies the op with which to colocate newly created ops.
 container View source
@tf_contextlib.contextmanager
container(
    container_name
)
 Returns a context manager that specifies the resource container to use. Stateful operations, such as variables and queues, can maintain their states on devices so that they can be shared by multiple processes. A resource container is a string name under which these stateful operations are tracked. These resources can be released or cleared with tf.Session.reset(). For example: with g.container('experiment0'):
  # All stateful Operations constructed in this context will be placed
  # in resource container "experiment0".
  v1 = tf.Variable([1.0])
  v2 = tf.Variable([2.0])
  with g.container("experiment1"):
    # All stateful Operations constructed in this context will be
    # placed in resource container "experiment1".
    v3 = tf.Variable([3.0])
    q1 = tf.queue.FIFOQueue(10, tf.float32)
  # All stateful Operations constructed in this context will be
  # be created in the "experiment0".
  v4 = tf.Variable([4.0])
  q1 = tf.queue.FIFOQueue(20, tf.float32)
  with g.container(""):
    # All stateful Operations constructed in this context will be
    # be placed in the default resource container.
    v5 = tf.Variable([5.0])
    q3 = tf.queue.FIFOQueue(30, tf.float32)

# Resets container "experiment0", after which the state of v1, v2, v4, q1
# will become undefined (such as uninitialized).
tf.Session.reset(target, ["experiment0"])

 Args
  container_name   container name string.

 Returns   A context manager for defining resource containers for stateful ops, yields the container name.
 control_dependencies View source
control_dependencies(
    control_inputs
)
 Returns a context manager that specifies control dependencies. Use with the with keyword to specify that all operations constructed within the context should have control dependencies on control_inputs. For example: with g.control_dependencies([a, b, c]):
  # `d` and `e` will only run after `a`, `b`, and `c` have executed.
  d = ...
  e = ...
 Multiple calls to control_dependencies() can be nested, and in that case a new Operation will have control dependencies on the union of control_inputs from all active contexts. with g.control_dependencies([a, b]):
  # Ops constructed here run after `a` and `b`.
  with g.control_dependencies([c, d]):
    # Ops constructed here run after `a`, `b`, `c`, and `d`.
 You can pass None to clear the control dependencies: with g.control_dependencies([a, b]):
  # Ops constructed here run after `a` and `b`.
  with g.control_dependencies(None):
    # Ops constructed here run normally, not waiting for either `a` or `b`.
    with g.control_dependencies([c, d]):
      # Ops constructed here run after `c` and `d`, also not waiting
      # for either `a` or `b`.

Note: The control dependencies context applies only to ops that are constructed within the context. Merely using an op or tensor in the context does not add a control dependency. The following example illustrates this point:

# WRONG
def my_func(pred, tensor):
  t = tf.matmul(tensor, tensor)
  with tf.control_dependencies([pred]):
    # The matmul op is created outside the context, so no control
    # dependency will be added.
    return t

# RIGHT
def my_func(pred, tensor):
  with tf.control_dependencies([pred]):
    # The matmul op is created in the context, so a control dependency
    # will be added.
    return tf.matmul(tensor, tensor)
 Also note that though execution of ops created under this scope will trigger execution of the dependencies, the ops created under this scope might still be pruned from a normal tensorflow graph. For example, in the following snippet of code the dependencies are never executed: loss = model.loss()
with tf.control_dependencies(dependencies):
  loss = loss + tf.constant(1)  # note: dependencies ignored in the
                                # backward pass
return tf.gradients(loss, model.variables)
 This is because evaluating the gradient graph does not require evaluating the constant(1) op created in the forward pass.

 Args
  control_inputs   A list of Operation or Tensor objects which must be executed or computed before running the operations defined in the context. Can also be None to clear the control dependencies.

 Returns   A context manager that specifies control dependencies for all operations constructed within the context.

 Raises
  TypeError   If control_inputs is not a list of Operation or Tensor objects.    create_op View source
create_op(
    op_type, inputs, dtypes=None, input_types=None, name=None, attrs=None,
    op_def=None, compute_shapes=True, compute_device=True
)
 Creates an Operation in this graph. (deprecated arguments) Warning: SOME ARGUMENTS ARE DEPRECATED: (compute_shapes). They will be removed in a future version. Instructions for updating: Shapes are always computed; don't use the compute_shapes as it has no effect. This is a low-level interface for creating an Operation. Most programs will not call this method directly, and instead use the Python op constructors, such as tf.constant(), which add ops to the default graph.

 Args
  op_type   The Operation type to create. This corresponds to the OpDef.name field for the proto that defines the operation.
  inputs   A list of Tensor objects that will be inputs to the Operation.
  dtypes   (Optional) A list of DType objects that will be the types of the tensors that the operation produces.
  input_types   (Optional.) A list of DTypes that will be the types of the tensors that the operation consumes. By default, uses the base DType of each input in inputs. Operations that expect reference-typed inputs must specify input_types explicitly.
  name   (Optional.) A string name for the operation. If not specified, a name is generated based on op_type.
  attrs   (Optional.) A dictionary where the key is the attribute name (a string) and the value is the respective attr attribute of the NodeDef proto that will represent the operation (an AttrValue proto).
  op_def   (Optional.) The OpDef proto that describes the op_type that the operation will have.
  compute_shapes   (Optional.) Deprecated. Has no effect (shapes are always computed).
  compute_device   (Optional.) If True, device functions will be executed to compute the device property of the Operation.

 Raises
  TypeError   if any of the inputs is not a Tensor.
  ValueError   if colocation conflicts with existing device assignment.

 Returns   An Operation object.
 device View source
@tf_contextlib.contextmanager
device(
    device_name_or_function
)
 Returns a context manager that specifies the default device to use. The device_name_or_function argument may either be a device name string, a device function, or None:  If it is a device name string, all operations constructed in this context will be assigned to the device with that name, unless overridden by a nested device() context. If it is a function, it will be treated as a function from Operation objects to device name strings, and invoked each time a new Operation is created. The Operation will be assigned to the device with the returned name. If it is None, all device() invocations from the enclosing context will be ignored.  For information about the valid syntax of device name strings, see the documentation in DeviceNameUtils. For example: with g.device('/device:GPU:0'):
  # All operations constructed in this context will be placed
  # on GPU 0.
  with g.device(None):
    # All operations constructed in this context will have no
    # assigned device.

# Defines a function from `Operation` to device string.
def matmul_on_gpu(n):
  if n.type == "MatMul":
    return "/device:GPU:0"
  else:
    return "/cpu:0"

with g.device(matmul_on_gpu):
  # All operations of type "MatMul" constructed in this context
  # will be placed on GPU 0; all other operations will be placed
  # on CPU 0.

Note: The device scope may be overridden by op wrappers or other library code. For example, a variable assignment op v.assign() must be colocated with the tf.Variable v, and incompatible device scopes will be ignored.

 Args
  device_name_or_function   The device name or function to use in the context.

 Yields   A context manager that specifies the default device to use for newly created ops.

 Raises
  RuntimeError   If device scopes are not properly nested.    finalize View source
finalize()
 Finalizes this graph, making it read-only. After calling g.finalize(), no new operations can be added to g. This method is used to ensure that no operations are added to a graph when it is shared between multiple threads, for example when using a tf.compat.v1.train.QueueRunner. get_all_collection_keys View source
get_all_collection_keys()
 Returns a list of collections used in this graph. get_collection View source
get_collection(
    name, scope=None
)
 Returns a list of values in the collection with the given name. This is different from get_collection_ref() which always returns the actual collection list if it exists in that it returns a new list each time it is called.

 Args
  name   The key for the collection. For example, the GraphKeys class contains many standard names for collections.
  scope   (Optional.) A string. If supplied, the resulting list is filtered to include only items whose name attribute matches scope using re.match. Items without a name attribute are never returned if a scope is supplied. The choice of re.match means that a scope without special tokens filters by prefix.

 Returns   The list of values in the collection with the given name, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.
 get_collection_ref View source
get_collection_ref(
    name
)
 Returns a list of values in the collection with the given name. If the collection exists, this returns the list itself, which can be modified in place to change the collection. If the collection does not exist, it is created as an empty list and the list is returned. This is different from get_collection() which always returns a copy of the collection list if it exists and never creates an empty collection.

 Args
  name   The key for the collection. For example, the GraphKeys class contains many standard names for collections.

 Returns   The list of values in the collection with the given name, or an empty list if no value has been added to that collection.
 get_name_scope View source
get_name_scope()
 Returns the current name scope. For example: with tf.name_scope('scope1'):
  with tf.name_scope('scope2'):
    print(tf.compat.v1.get_default_graph().get_name_scope())
 would print the string scope1/scope2.

 Returns   A string representing the current name scope.
 get_operation_by_name View source
get_operation_by_name(
    name
)
 Returns the Operation with the given name. This method may be called concurrently from multiple threads.

 Args
  name   The name of the Operation to return.

 Returns   The Operation with the given name.

 Raises
  TypeError   If name is not a string.
  KeyError   If name does not correspond to an operation in this graph.    get_operations View source
get_operations()
 Return the list of operations in the graph. You can modify the operations in place, but modifications to the list such as inserts/delete have no effect on the list of operations known to the graph. This method may be called concurrently from multiple threads.

 Returns   A list of Operations.
 get_tensor_by_name View source
get_tensor_by_name(
    name
)
 Returns the Tensor with the given name. This method may be called concurrently from multiple threads.

 Args
  name   The name of the Tensor to return.

 Returns   The Tensor with the given name.

 Raises
  TypeError   If name is not a string.
  KeyError   If name does not correspond to a tensor in this graph.    gradient_override_map View source
@tf_contextlib.contextmanager
gradient_override_map(
    op_type_map
)
 EXPERIMENTAL: A context manager for overriding gradient functions. This context manager can be used to override the gradient function that will be used for ops within the scope of the context. For example: @tf.RegisterGradient("CustomSquare")
def _custom_square_grad(op, grad):
  # ...

with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  s_1 = tf.square(c)  # Uses the default gradient for tf.square.
  with g.gradient_override_map({"Square": "CustomSquare"}):
    s_2 = tf.square(s_2)  # Uses _custom_square_grad to compute the
                          # gradient of s_2.

 Args
  op_type_map   A dictionary mapping op type strings to alternative op type strings.

 Returns   A context manager that sets the alternative op type to be used for one or more ops created in that context.

 Raises
  TypeError   If op_type_map is not a dictionary mapping strings to strings.    is_feedable View source
is_feedable(
    tensor
)
 Returns True if and only if tensor is feedable. is_fetchable View source
is_fetchable(
    tensor_or_op
)
 Returns True if and only if tensor_or_op is fetchable. name_scope View source
@tf_contextlib.contextmanager
name_scope(
    name
)
 Returns a context manager that creates hierarchical names for operations. A graph maintains a stack of name scopes. A with name_scope(...): statement pushes a new name onto the stack for the lifetime of the context. The name argument will be interpreted as follows:  A string (not ending with '/') will create a new name scope, in which name is appended to the prefix of all operations created in the context. If name has been used before, it will be made unique by calling self.unique_name(name). A scope previously captured from a with g.name_scope(...) as scope: statement will be treated as an "absolute" name scope, which makes it possible to re-enter existing scopes. A value of None or the empty string will reset the current name scope to the top-level (empty) name scope.  For example: with tf.Graph().as_default() as g:
  c = tf.constant(5.0, name="c")
  assert c.op.name == "c"
  c_1 = tf.constant(6.0, name="c")
  assert c_1.op.name == "c_1"

  # Creates a scope called "nested"
  with g.name_scope("nested") as scope:
    nested_c = tf.constant(10.0, name="c")
    assert nested_c.op.name == "nested/c"

    # Creates a nested scope called "inner".
    with g.name_scope("inner"):
      nested_inner_c = tf.constant(20.0, name="c")
      assert nested_inner_c.op.name == "nested/inner/c"

    # Create a nested scope called "inner_1".
    with g.name_scope("inner"):
      nested_inner_1_c = tf.constant(30.0, name="c")
      assert nested_inner_1_c.op.name == "nested/inner_1/c"

      # Treats `scope` as an absolute name scope, and
      # switches to the "nested/" scope.
      with g.name_scope(scope):
        nested_d = tf.constant(40.0, name="d")
        assert nested_d.op.name == "nested/d"

        with g.name_scope(""):
          e = tf.constant(50.0, name="e")
          assert e.op.name == "e"
 The name of the scope itself can be captured by with g.name_scope(...) as scope:, which stores the name of the scope in the variable scope. This value can be used to name an operation that represents the overall result of executing the ops in a scope. For example: inputs = tf.constant(...)
with g.name_scope('my_layer') as scope:
  weights = tf.Variable(..., name="weights")
  biases = tf.Variable(..., name="biases")
  affine = tf.matmul(inputs, weights) + biases
  output = tf.nn.relu(affine, name=scope)

Note: This constructor validates the given name. Valid scope names match one of the following regular expressions:

[A-Za-z0-9.][A-Za-z0-9_.\-/]* (for scopes at the root)
[A-Za-z0-9_.\-/]* (for other scopes)

 Args
  name   A name for the scope.

 Returns   A context manager that installs name as a new name scope.

 Raises
  ValueError   If name is not a valid scope name, according to the rules above.    prevent_feeding View source
prevent_feeding(
    tensor
)
 Marks the given tensor as unfeedable in this graph. prevent_fetching View source
prevent_fetching(
    op
)
 Marks the given op as unfetchable in this graph. switch_to_thread_local View source
switch_to_thread_local()
 Make device, colocation and dependencies stacks thread-local. Device, colocation and dependencies stacks are not thread-local be default. If multiple threads access them, then the state is shared. This means that one thread may affect the behavior of another thread. After this method is called, the stacks become thread-local. If multiple threads access them, then the state is not shared. Each thread uses its own value; a thread doesn't affect other threads by mutating such a stack. The initial value for every thread's stack is set to the current value of the stack when switch_to_thread_local() was first called. unique_name View source
unique_name(
    name, mark_as_used=True
)
 Return a unique operation name for name.
Note: You rarely need to call unique_name() directly. Most of the time you just need to create with g.name_scope() blocks to generate structured names.
 unique_name is used to generate structured names, separated by "/", to help identify operations when debugging a graph. Operation names are displayed in error messages reported by the TensorFlow runtime, and in various visualization tools such as TensorBoard. If mark_as_used is set to True, which is the default, a new unique name is created and marked as in use. If it's set to False, the unique name is returned without actually being marked as used. This is useful when the caller simply wants to know what the name to be created will be.

 Args
  name   The name for an operation.
  mark_as_used   Whether to mark this name as being used.

 Returns   A string to be passed to create_op() that will be used to name the operation being created.


Module: tf.graph_util Helpers to manipulate a tensor graph in python. Functions import_graph_def(...): Imports the graph from graph_def into the current default Graph. (deprecated arguments)


tf.graph_util.import_graph_def     View source on GitHub    Imports the graph from graph_def into the current default Graph. (deprecated arguments)  View aliases  Main aliases
tf.import_graph_def Compat aliases for migration See Migration guide for more details. tf.compat.v1.graph_util.import_graph_def, tf.compat.v1.import_graph_def
tf.graph_util.import_graph_def(
    graph_def, input_map=None, return_elements=None, name=None, op_dict=None,
    producer_op_list=None
)
 Warning: SOME ARGUMENTS ARE DEPRECATED: (op_dict). They will be removed in a future version. Instructions for updating: Please file an issue at https://github.com/tensorflow/tensorflow/issues if you depend on this feature. This function provides a way to import a serialized TensorFlow GraphDef protocol buffer, and extract individual objects in the GraphDef as tf.Tensor and tf.Operation objects. Once extracted, these objects are placed into the current default Graph. See tf.Graph.as_graph_def for a way to create a GraphDef proto.

 Args
  graph_def   A GraphDef proto containing operations to be imported into the default graph.
  input_map   A dictionary mapping input names (as strings) in graph_def to Tensor objects. The values of the named input tensors in the imported graph will be re-mapped to the respective Tensor values.
  return_elements   A list of strings containing operation names in graph_def that will be returned as Operation objects; and/or tensor names in graph_def that will be returned as Tensor objects.
  name   (Optional.) A prefix that will be prepended to the names in graph_def. Note that this does not apply to imported function names. Defaults to "import".
  op_dict   (Optional.) Deprecated, do not use.
  producer_op_list   (Optional.) An OpList proto with the (possibly stripped) list of OpDefs used by the producer of the graph. If provided, unrecognized attrs for ops in graph_def that have their default value according to producer_op_list will be removed. This will allow some more GraphDefs produced by later binaries to be accepted by earlier binaries.

 Returns   A list of Operation and/or Tensor objects from the imported graph, corresponding to the names in return_elements, and None if returns_elements is None.

 Raises
  TypeError   If graph_def is not a GraphDef proto, input_map is not a dictionary mapping strings to Tensor objects, or return_elements is not a list of strings.
  ValueError   If input_map, or return_elements contains names that do not appear in graph_def, or graph_def is not well-formed (e.g. it refers to an unknown tensor).


tf.group     View source on GitHub    Create an op that groups multiple operations.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.group
tf.group(
    *inputs, **kwargs
)
 When this op finishes, all ops in inputs have finished. This op has no output.
Note: In TensorFlow 2 with eager and/or Autograph, you should not require this method, as code executes in your expected order. Only use tf.group when working with v1-style code or in a graph context such as inside Dataset.map.
 When operating in a v1-style graph context, ops are not executed in the same order as specified in the code; TensorFlow will attempt to execute ops in parallel or in an order convienient to the result it is computing. tf.group allows you to request that one or more results finish before execution continues. tf.group creates a single op (of type NoOp), and then adds appropriate control dependencies. Thus, c = tf.group(a, b) will compute the same graph as this: with tf.control_dependencies([a, b]):
    c = tf.no_op()
 See also tf.tuple and tf.control_dependencies.

 Args
  *inputs   Zero or more tensors to group.
  name   A name for this operation (optional).

 Returns   An Operation that executes all its inputs.

 Raises
  ValueError   If an unknown keyword argument is provided.


tf.guarantee_const Gives a guarantee to the TF runtime that the input tensor is a constant.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.guarantee_const
tf.guarantee_const(
    input, name=None
)
 The runtime is then free to make optimizations based on this. Only accepts value typed tensors as inputs and rejects resource variable handles as input. Returns the input tensor without modification.

 Args
  input   A Tensor.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as input.


tf.hessians     View source on GitHub    Constructs the Hessian of sum of ys with respect to x in xs.
tf.hessians(
    ys, xs, gate_gradients=False, aggregation_method=None, name='hessians'
)
 hessians() adds ops to the graph to output the Hessian matrix of ys with respect to xs. It returns a list of Tensor of length len(xs) where each tensor is the Hessian of sum(ys). The Hessian is a matrix of second-order partial derivatives of a scalar tensor (see https://en.wikipedia.org/wiki/Hessian_matrix for more details).

 Args
  ys   A Tensor or list of tensors to be differentiated.
  xs   A Tensor or list of tensors to be used for differentiation.
  gate_gradients   See gradients() documentation for details.
  aggregation_method   See gradients() documentation for details.
  name   Optional name to use for grouping all the gradient ops together. defaults to 'hessians'.

 Returns   A list of Hessian matrices of sum(ys) for each x in xs.

 Raises
  LookupError   if one of the operations between xs and ys does not have a registered gradient function.


tf.histogram_fixed_width     View source on GitHub    Return histogram of values.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.histogram_fixed_width
tf.histogram_fixed_width(
    values, value_range, nbins=100, dtype=tf.dtypes.int32, name=None
)
 Given the tensor values, this operation returns a rank 1 histogram counting the number of entries in values that fell into every bin. The bins are equal width and determined by the arguments value_range and nbins.

 Args
  values   Numeric Tensor.
  value_range   Shape [2] Tensor of same dtype as values. values <= value_range[0] will be mapped to hist[0], values >= value_range[1] will be mapped to hist[-1].
  nbins   Scalar int32 Tensor. Number of histogram bins.
  dtype   dtype for returned histogram.
  name   A name for this operation (defaults to 'histogram_fixed_width').

 Returns   A 1-D Tensor holding histogram of values.

 Raises
  TypeError   If any unsupported dtype is provided.
  tf.errors.InvalidArgumentError   If value_range does not satisfy value_range[0] < value_range[1].    Examples:
# Bins will be:  (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf)

nbins = 5
value_range = [0.0, 5.0]
new_values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15]
hist = tf.histogram_fixed_width(new_values, value_range, nbins=5)
hist.numpy()
array([2, 1, 1, 0, 2], dtype=int32)


tf.histogram_fixed_width_bins     View source on GitHub    Bins the given values for use in a histogram.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.histogram_fixed_width_bins
tf.histogram_fixed_width_bins(
    values, value_range, nbins=100, dtype=tf.dtypes.int32, name=None
)
 Given the tensor values, this operation returns a rank 1 Tensor representing the indices of a histogram into which each element of values would be binned. The bins are equal width and determined by the arguments value_range and nbins.

 Args
  values   Numeric Tensor.
  value_range   Shape [2] Tensor of same dtype as values. values <= value_range[0] will be mapped to hist[0], values >= value_range[1] will be mapped to hist[-1].
  nbins   Scalar int32 Tensor. Number of histogram bins.
  dtype   dtype for returned histogram.
  name   A name for this operation (defaults to 'histogram_fixed_width').

 Returns   A Tensor holding the indices of the binned values whose shape matches values.

 Raises
  TypeError   If any unsupported dtype is provided.
  tf.errors.InvalidArgumentError   If value_range does not satisfy value_range[0] < value_range[1].    Examples:
# Bins will be:  (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf)

nbins = 5
value_range = [0.0, 5.0]
new_values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15]
indices = tf.histogram_fixed_width_bins(new_values, value_range, nbins=5)
indices.numpy()
array([0, 0, 1, 2, 4, 4], dtype=int32)


tf.identity     View source on GitHub    Return a Tensor with the same shape and contents as input.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.identity
tf.identity(
    input, name=None
)
 The return value is not the same Tensor as the original, but contains the same values. This operation is fast when used on the same device. For example:
a = tf.constant([0.78])
a_identity = tf.identity(a)
a.numpy()
array([0.78], dtype=float32)
a_identity.numpy()
array([0.78], dtype=float32)
 Calling tf.identity on a variable will make a Tensor that represents the value of that variable at the time it is called. This is equivalent to calling <variable>.read_value().
a = tf.Variable(5)
a_identity = tf.identity(a)
a.assign_add(1)
<tf.Variable ... shape=() dtype=int32, numpy=6>
a.numpy()
6
a_identity.numpy()
5

 Args
  input   A Tensor.
  name   A name for the operation (optional).

 Returns   A Tensor. Has the same type as input.


tf.identity_n Returns a list of tensors with the same shapes and contents as the input  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.identity_n
tf.identity_n(
    input, name=None
)
 tensors. This op can be used to override the gradient for complicated functions. For example, suppose y = f(x) and we wish to apply a custom function g for backprop such that dx = g(dy). In Python, with tf.get_default_graph().gradient_override_map(
    {'IdentityN': 'OverrideGradientWithG'}):
  y, _ = identity_n([f(x), x])

@tf.RegisterGradient('OverrideGradientWithG')
def ApplyG(op, dy, _):
  return [None, g(dy)]  # Do not backprop to f(x).

 Args
  input   A list of Tensor objects.
  name   A name for the operation (optional).

 Returns   A list of Tensor objects. Has the same type as input.


Module: tf.image Image ops. The tf.image module contains various functions for image processing and decoding-encoding Ops. Many of the encoding/decoding functions are also available in the core tf.io module. Image processing Resizing The resizing Ops accept input images as tensors of several types. They always output resized images as float32 tensors. The convenience function tf.image.resize supports both 4-D and 3-D tensors as input and output. 4-D tensors are for batches of images, 3-D tensors for individual images. Resized images will be distorted if their original aspect ratio is not the same as size. To avoid distortions see tf.image.resize_with_pad.  tf.image.resize tf.image.resize_with_pad tf.image.resize_with_crop_or_pad  The Class tf.image.ResizeMethod provides various resize methods like bilinear, nearest_neighbor. Converting Between Colorspaces Image ops work either on individual images or on batches of images, depending on the shape of their input Tensor. If 3-D, the shape is [height, width, channels], and the Tensor represents one image. If 4-D, the shape is [batch_size, height, width, channels], and the Tensor represents batch_size images. Currently, channels can usefully be 1, 2, 3, or 4. Single-channel images are grayscale, images with 3 channels are encoded as either RGB or HSV. Images with 2 or 4 channels include an alpha channel, which has to be stripped from the image before passing the image to most image processing functions (and can be re-attached later). Internally, images are either stored in as one float32 per channel per pixel (implicitly, values are assumed to lie in [0,1)) or one uint8 per channel per pixel (values are assumed to lie in [0,255]). TensorFlow can convert between images in RGB or HSV or YIQ.
tf.image.rgb_to_grayscale, tf.image.grayscale_to_rgb

tf.image.rgb_to_hsv, tf.image.hsv_to_rgb

tf.image.rgb_to_yiq, tf.image.yiq_to_rgb

tf.image.rgb_to_yuv, tf.image.yuv_to_rgb
 tf.image.image_gradients tf.image.convert_image_dtype  Image Adjustments TensorFlow provides functions to adjust images in various ways: brightness, contrast, hue, and saturation. Each adjustment can be done with predefined parameters or with random parameters picked from predefined intervals. Random adjustments are often useful to expand a training set and reduce overfitting. If several adjustments are chained it is advisable to minimize the number of redundant conversions by first converting the images to the most natural data type and representation.  tf.image.adjust_brightness tf.image.adjust_contrast tf.image.adjust_gamma tf.image.adjust_hue tf.image.adjust_jpeg_quality tf.image.adjust_saturation tf.image.random_brightness tf.image.random_contrast tf.image.random_hue tf.image.random_saturation tf.image.per_image_standardization  Working with Bounding Boxes  tf.image.draw_bounding_boxes tf.image.combined_non_max_suppression tf.image.generate_bounding_box_proposals tf.image.non_max_suppression tf.image.non_max_suppression_overlaps tf.image.non_max_suppression_padded tf.image.non_max_suppression_with_scores tf.image.pad_to_bounding_box tf.image.sample_distorted_bounding_box  Cropping  tf.image.central_crop tf.image.crop_and_resize tf.image.crop_to_bounding_box tf.io.decode_and_crop_jpeg tf.image.extract_glimpse tf.image.random_crop tf.image.resize_with_crop_or_pad  Flipping, Rotating and Transposing  tf.image.flip_left_right tf.image.flip_up_down tf.image.random_flip_left_right tf.image.random_flip_up_down tf.image.rot90 tf.image.transpose  Image decoding and encoding TensorFlow provides Ops to decode and encode JPEG and PNG formats. Encoded images are represented by scalar string Tensors, decoded images by 3-D uint8 tensors of shape [height, width, channels]. (PNG also supports uint16.)
Note: decode_gif returns a 4-D array [num_frames, height, width, 3]
 The encode and decode Ops apply to one image at a time. Their input and output are all of variable size. If you need fixed size images, pass the output of the decode Ops to one of the cropping and resizing Ops.  tf.io.decode_bmp tf.io.decode_gif tf.io.decode_image tf.io.decode_jpeg tf.io.decode_and_crop_jpeg tf.io.decode_png tf.io.encode_jpeg tf.io.encode_png  Classes class ResizeMethod: See tf.image.resize for details. Functions adjust_brightness(...): Adjust the brightness of RGB or Grayscale images. adjust_contrast(...): Adjust contrast of RGB or grayscale images. adjust_gamma(...): Performs Gamma Correction. adjust_hue(...): Adjust hue of RGB images. adjust_jpeg_quality(...): Adjust jpeg encoding quality of an image. adjust_saturation(...): Adjust saturation of RGB images. central_crop(...): Crop the central region of the image(s). combined_non_max_suppression(...): Greedily selects a subset of bounding boxes in descending order of score. convert_image_dtype(...): Convert image to dtype, scaling its values if needed. crop_and_resize(...): Extracts crops from the input image tensor and resizes them. crop_to_bounding_box(...): Crops an image to a specified bounding box. decode_and_crop_jpeg(...): Decode and Crop a JPEG-encoded image to a uint8 tensor. decode_bmp(...): Decode the first frame of a BMP-encoded image to a uint8 tensor. decode_gif(...): Decode the frame(s) of a GIF-encoded image to a uint8 tensor. decode_image(...): Function for decode_bmp, decode_gif, decode_jpeg, and decode_png. decode_jpeg(...): Decode a JPEG-encoded image to a uint8 tensor. decode_png(...): Decode a PNG-encoded image to a uint8 or uint16 tensor. draw_bounding_boxes(...): Draw bounding boxes on a batch of images. encode_jpeg(...): JPEG-encode an image. encode_png(...): PNG-encode an image. extract_glimpse(...): Extracts a glimpse from the input tensor. extract_jpeg_shape(...): Extract the shape information of a JPEG-encoded image. extract_patches(...): Extract patches from images. flip_left_right(...): Flip an image horizontally (left to right). flip_up_down(...): Flip an image vertically (upside down). generate_bounding_box_proposals(...): Generate bounding box proposals from encoded bounding boxes. grayscale_to_rgb(...): Converts one or more images from Grayscale to RGB. hsv_to_rgb(...): Convert one or more images from HSV to RGB. image_gradients(...): Returns image gradients (dy, dx) for each color channel. is_jpeg(...): Convenience function to check if the 'contents' encodes a JPEG image. non_max_suppression(...): Greedily selects a subset of bounding boxes in descending order of score. non_max_suppression_overlaps(...): Greedily selects a subset of bounding boxes in descending order of score. non_max_suppression_padded(...): Greedily selects a subset of bounding boxes in descending order of score. non_max_suppression_with_scores(...): Greedily selects a subset of bounding boxes in descending order of score. pad_to_bounding_box(...): Pad image with zeros to the specified height and width. per_image_standardization(...): Linearly scales each image in image to have mean 0 and variance 1. psnr(...): Returns the Peak Signal-to-Noise Ratio between a and b. random_brightness(...): Adjust the brightness of images by a random factor. random_contrast(...): Adjust the contrast of an image or images by a random factor. random_crop(...): Randomly crops a tensor to a given size. random_flip_left_right(...): Randomly flip an image horizontally (left to right). random_flip_up_down(...): Randomly flips an image vertically (upside down). random_hue(...): Adjust the hue of RGB images by a random factor. random_jpeg_quality(...): Randomly changes jpeg encoding quality for inducing jpeg noise. random_saturation(...): Adjust the saturation of RGB images by a random factor. resize(...): Resize images to size using the specified method. resize_with_crop_or_pad(...): Crops and/or pads an image to a target width and height. resize_with_pad(...): Resizes and pads an image to a target width and height. rgb_to_grayscale(...): Converts one or more images from RGB to Grayscale. rgb_to_hsv(...): Converts one or more images from RGB to HSV. rgb_to_yiq(...): Converts one or more images from RGB to YIQ. rgb_to_yuv(...): Converts one or more images from RGB to YUV. rot90(...): Rotate image(s) counter-clockwise by 90 degrees. sample_distorted_bounding_box(...): Generate a single randomly distorted bounding box for an image. sobel_edges(...): Returns a tensor holding Sobel edge maps. ssim(...): Computes SSIM index between img1 and img2. ssim_multiscale(...): Computes the MS-SSIM between img1 and img2. stateless_random_brightness(...): Adjust the brightness of images by a random factor deterministically. stateless_random_contrast(...): Adjust the contrast of images by a random factor deterministically. stateless_random_crop(...): Randomly crops a tensor to a given size in a deterministic manner. stateless_random_flip_left_right(...): Randomly flip an image horizontally (left to right) deterministically. stateless_random_flip_up_down(...): Randomly flip an image vertically (upside down) deterministically. stateless_random_hue(...): Adjust the hue of RGB images by a random factor deterministically. stateless_random_jpeg_quality(...): Deterministically radomize jpeg encoding quality for inducing jpeg noise. stateless_random_saturation(...): Adjust the saturation of RGB images by a random factor deterministically. stateless_sample_distorted_bounding_box(...): Generate a randomly distorted bounding box for an image deterministically. total_variation(...): Calculate and return the total variation for one or more images. transpose(...): Transpose image(s) by swapping the height and width dimension. yiq_to_rgb(...): Converts one or more images from YIQ to RGB. yuv_to_rgb(...): Converts one or more images from YUV to RGB.


tf.image.adjust_brightness     View source on GitHub    Adjust the brightness of RGB or Grayscale images.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.adjust_brightness
tf.image.adjust_brightness(
    image, delta
)
 This is a convenience method that converts RGB images to float representation, adjusts their brightness, and then converts them back to the original data type. If several adjustments are chained, it is advisable to minimize the number of redundant conversions. The value delta is added to all components of the tensor image. image is converted to float and scaled appropriately if it is in fixed-point representation, and delta is converted to the same data type. For regular images, delta should be in the range (-1,1), as it is added to the image in floating point representation, where pixel values are in the [0,1) range. Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.adjust_brightness(x, delta=0.1)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[ 1.1,  2.1,  3.1],
        [ 4.1,  5.1,  6.1]],
       [[ 7.1,  8.1,  9.1],
        [10.1, 11.1, 12.1]]], dtype=float32)>

 Args
  image   RGB image or images to adjust.
  delta   A scalar. Amount to add to the pixel values.

 Returns   A brightness-adjusted tensor of the same shape and type as image.


tf.image.adjust_contrast     View source on GitHub    Adjust contrast of RGB or grayscale images.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.adjust_contrast
tf.image.adjust_contrast(
    images, contrast_factor
)
 This is a convenience method that converts RGB images to float representation, adjusts their contrast, and then converts them back to the original data type. If several adjustments are chained, it is advisable to minimize the number of redundant conversions. images is a tensor of at least 3 dimensions. The last 3 dimensions are interpreted as [height, width, channels]. The other dimensions only represent a collection of images, such as [batch, height, width, channels]. Contrast is adjusted independently for each channel of each image. For each channel, this Op computes the mean of the image pixels in the channel and then adjusts each component x of each pixel to (x - mean) * contrast_factor + mean. Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.adjust_contrast(x, 2)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[-3.5, -2.5, -1.5],
        [ 2.5,  3.5,  4.5]],
       [[ 8.5,  9.5, 10.5],
        [14.5, 15.5, 16.5]]], dtype=float32)>

 Args
  images   Images to adjust. At least 3-D.
  contrast_factor   A float multiplier for adjusting contrast.

 Returns   The contrast-adjusted image or images.


tf.image.adjust_gamma     View source on GitHub    Performs Gamma Correction.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.adjust_gamma
tf.image.adjust_gamma(
    image, gamma=1, gain=1
)
 on the input image. Also known as Power Law Transform. This function converts the input images at first to float representation, then transforms them pixelwise according to the equation Out = gain * In**gamma, and then converts the back to the original data type. Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.adjust_gamma(x, 0.2)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[1.       , 1.1486983, 1.2457309],
        [1.319508 , 1.3797297, 1.4309691]],
       [[1.4757731, 1.5157166, 1.5518456],
        [1.5848932, 1.6153942, 1.6437519]]], dtype=float32)>

 Args
  image   RGB image or images to adjust.
  gamma   A scalar or tensor. Non-negative real number.
  gain   A scalar or tensor. The constant multiplier.

 Returns   A Tensor. A Gamma-adjusted tensor of the same shape and type as image.

 Raises
  ValueError   If gamma is negative.    Notes: For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. For gamma less than 1, the histogram will shift towards right and the output image will be brighter than the input image. References: Wikipedia


tf.image.adjust_hue     View source on GitHub    Adjust hue of RGB images.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.adjust_hue
tf.image.adjust_hue(
    image, delta, name=None
)
 This is a convenience method that converts an RGB image to float representation, converts it to HSV, adds an offset to the hue channel, converts back to RGB and then back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions. image is an RGB image. The image hue is adjusted by converting the image(s) to HSV and rotating the hue channel (H) by delta. The image is then converted back to RGB. delta must be in the interval [-1, 1]. Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.adjust_hue(x, 0.2)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[ 2.3999996,  1.       ,  3.       ],
        [ 5.3999996,  4.       ,  6.       ]],
      [[ 8.4      ,  7.       ,  9.       ],
        [11.4      , 10.       , 12.       ]]], dtype=float32)>

 Args
  image   RGB image or images. The size of the last dimension must be 3.
  delta   float. How much to add to the hue channel.
  name   A name for this operation (optional).

 Returns   Adjusted image(s), same shape and DType as image.
 Usage Example:
image = [[[1, 2, 3], [4, 5, 6]],
         [[7, 8, 9], [10, 11, 12]],
         [[13, 14, 15], [16, 17, 18]]]
image = tf.constant(image)
tf.image.adjust_hue(image, 0.2)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 2,  1,  3],
      [ 5,  4,  6]],
     [[ 8,  7,  9],
      [11, 10, 12]],
     [[14, 13, 15],
      [17, 16, 18]]], dtype=int32)>


tf.image.adjust_jpeg_quality     View source on GitHub    Adjust jpeg encoding quality of an image.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.adjust_jpeg_quality
tf.image.adjust_jpeg_quality(
    image, jpeg_quality, name=None
)
 This is a convenience method that converts an image to uint8 representation, encodes it to jpeg with jpeg_quality, decodes it, and then converts back to the original data type. jpeg_quality must be in the interval [0, 100]. Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.adjust_jpeg_quality(x, 75)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
        [1., 1., 1.]],
       [[1., 1., 1.],
        [1., 1., 1.]]], dtype=float32)>

 Args
  image   3D image. The size of the last dimension must be None, 1 or 3.
  jpeg_quality   Python int or Tensor of type int32. jpeg encoding quality.
  name   A name for this operation (optional).

 Returns   Adjusted image, same shape and DType as image.

 Raises
  InvalidArgumentError   quality must be in [0,100]
  InvalidArgumentError   image must have 1 or 3 channels


tf.image.adjust_saturation     View source on GitHub    Adjust saturation of RGB images.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.adjust_saturation
tf.image.adjust_saturation(
    image, saturation_factor, name=None
)
 This is a convenience method that converts RGB images to float representation, converts them to HSV, adds an offset to the saturation channel, converts back to RGB and then back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions. image is an RGB image or images. The image saturation is adjusted by converting the images to HSV and multiplying the saturation (S) channel by saturation_factor and clipping. The images are then converted back to RGB. Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.adjust_saturation(x, 0.5)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[ 2. ,  2.5,  3. ],
        [ 5. ,  5.5,  6. ]],
       [[ 8. ,  8.5,  9. ],
        [11. , 11.5, 12. ]]], dtype=float32)>

 Args
  image   RGB image or images. The size of the last dimension must be 3.
  saturation_factor   float. Factor to multiply the saturation by.
  name   A name for this operation (optional).

 Returns   Adjusted image(s), same shape and DType as image.

 Raises
  InvalidArgumentError   input must have 3 channels


tf.image.central_crop     View source on GitHub    Crop the central region of the image(s).  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.central_crop
tf.image.central_crop(
    image, central_fraction
)
 Remove the outer parts of an image but retain the central region of the image along each dimension. If we specify central_fraction = 0.5, this function returns the region marked with "X" in the below diagram.  --------
|        |
|  XXXX  |
|  XXXX  |
|        |   where "X" is the central 50% of the image.
 --------
 This function works on either a single image (image is a 3-D Tensor), or a batch of images (image is a 4-D Tensor). Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0],
      [7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]],
    [[13.0, 14.0, 15.0],
      [16.0, 17.0, 18.0],
      [19.0, 20.0, 21.0],
      [22.0, 23.0, 24.0]],
    [[25.0, 26.0, 27.0],
      [28.0, 29.0, 30.0],
      [31.0, 32.0, 33.0],
      [34.0, 35.0, 36.0]],
    [[37.0, 38.0, 39.0],
      [40.0, 41.0, 42.0],
      [43.0, 44.0, 45.0],
      [46.0, 47.0, 48.0]]]
tf.image.central_crop(x, 0.5)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[16., 17., 18.],
        [19., 20., 21.]],
       [[28., 29., 30.],
        [31., 32., 33.]]], dtype=float32)>

 Args
  image   Either a 3-D float Tensor of shape [height, width, depth], or a 4-D Tensor of shape [batch_size, height, width, depth].
  central_fraction   float (0, 1], fraction of size to crop

 Raises
  ValueError   if central_crop_fraction is not within (0, 1].

 Returns   3-D / 4-D float Tensor, as per the input.


tf.image.combined_non_max_suppression     View source on GitHub    Greedily selects a subset of bounding boxes in descending order of score.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.combined_non_max_suppression
tf.image.combined_non_max_suppression(
    boxes, scores, max_output_size_per_class, max_total_size, iou_threshold=0.5,
    score_threshold=float('-inf'), pad_per_class=False, clip_boxes=True,
    name=None
)
 This operation performs non_max_suppression on the inputs per batch, across all classes. Prunes away boxes that have high intersection-over-union (IOU) overlap with previously selected boxes. Bounding boxes are supplied as [y1, x1, y2, x2], where (y1, x1) and (y2, x2) are the coordinates of any diagonal pair of box corners and the coordinates can be provided as normalized (i.e., lying in the interval [0, 1]) or absolute. Note that this algorithm is agnostic to where the origin is in the coordinate system. Also note that this algorithm is invariant to orthogonal transformations and translations of the coordinate system; thus translating or reflections of the coordinate system result in the same boxes being selected by the algorithm. The output of this operation is the final boxes, scores and classes tensor returned after performing non_max_suppression.

 Args
  boxes   A 4-D float Tensor of shape [batch_size, num_boxes, q, 4]. If q is 1 then same boxes are used for all classes otherwise, if q is equal to number of classes, class-specific boxes are used.
  scores   A 3-D float Tensor of shape [batch_size, num_boxes, num_classes] representing a single score corresponding to each box (each row of boxes).
  max_output_size_per_class   A scalar integer Tensor representing the maximum number of boxes to be selected by non-max suppression per class
  max_total_size   A scalar representing the maximum number of boxes retained over all classes.
  iou_threshold   A float representing the threshold for deciding whether boxes overlap too much with respect to IOU.
  score_threshold   A float representing the threshold for deciding when to remove boxes based on score.
  pad_per_class   If false, the output nmsed boxes, scores and classes are padded/clipped to max_total_size. If true, the output nmsed boxes, scores and classes are padded to be of length max_size_per_class*num_classes, unless it exceeds max_total_size in which case it is clipped to max_total_size. Defaults to false.
  clip_boxes   If true, the coordinates of output nmsed boxes will be clipped to [0, 1]. If false, output the box coordinates as it is. Defaults to true.
  name   A name for the operation (optional).

 Returns
  'nmsed_boxes'   A [batch_size, max_detections, 4] float32 tensor containing the non-max suppressed boxes.
  'nmsed_scores'   A [batch_size, max_detections] float32 tensor containing the scores for the boxes.
  'nmsed_classes'   A [batch_size, max_detections] float32 tensor containing the class for boxes.
  'valid_detections'   A [batch_size] int32 tensor indicating the number of valid detections per batch item. Only the top valid_detections[i] entries in nms_boxes[i], nms_scores[i] and nms_class[i] are valid. The rest of the entries are zero paddings.


tf.image.convert_image_dtype     View source on GitHub    Convert image to dtype, scaling its values if needed.  View aliases  Compat aliases for migration
See Migration guide for more details. tf.compat.v1.image.convert_image_dtype
tf.image.convert_image_dtype(
    image, dtype, saturate=False, name=None
)
 Images that are represented using floating point values are expected to have values in the range [0,1). Image data stored in integer data types are expected to have values in the range [0,MAX], where MAX is the largest positive representable number for the data type. This op converts between data types, scaling the values appropriately before casting. Note that converting from floating point inputs to integer types may lead to over/underflow problems. Set saturate to True to avoid such problem in problematic conversions. If enabled, saturation will clip the output into the allowed range before performing a potentially dangerous cast (and only before performing such a cast, i.e., when casting from a floating point to an integer type, and when casting from a signed to an unsigned type; saturate has no effect on casts between floats, or on casts that increase the type's range). Usage Example:
x = [[[1.0, 2.0, 3.0],
      [4.0, 5.0, 6.0]],
    [[7.0, 8.0, 9.0],
      [10.0, 11.0, 12.0]]]
tf.image.convert_image_dtype(x, dtype=tf.float16, saturate=False)
<tf.Tensor: shape=(2, 2, 3), dtype=float16, numpy=
array([[[ 1.,  2.,  3.],
        [ 4.,  5.,  6.]],
       [[ 7.,  8.,  9.],
        [10., 11., 12.]]], dtype=float16)>

 Args
  image   An image.
  dtype   A DType to convert image to.
  saturate   If True, clip the input before casting (if necessary).
  name   A name for this operation (optional).

 Returns   image, converted to dtype.

 Raises
  AttributeError   Raises an attribute error when dtype is neither float nor integer


tf.image.crop_and_resize     View source on GitHub    Extracts crops from the input image tensor and resizes them.
tf.image.crop_and_resize(
    image, boxes, box_indices, crop_size, method='bilinear',
    extrapolation_value=0, name=None
)
 Extracts crops from the input image tensor and resizes them using bilinear sampling or nearest neighbor sampling (possibly with aspect ratio change) to a common output size specified by crop_size. This is more general than the crop_to_bounding_box op which extracts a fixed size slice from the input image and does not allow resizing or aspect ratio change. Returns a tensor with crops from the input image at positions defined at the bounding box locations in boxes. The cropped boxes are all resized (with bilinear or nearest neighbor interpolation) to a fixed size = [crop_height, crop_width]. The result is a 4-D tensor [num_boxes, crop_height, crop_width, depth]. The resizing is corner aligned. In particular, if boxes = [[0, 0, 1, 1]], the method will give identical results to using tf.compat.v1.image.resize_bilinear() or tf.compat.v1.image.resize_nearest_neighbor()(depends on the method argument) with align_corners=True.

 Args
  image   A 4-D tensor of shape [batch, image_height, image_width, depth]. Both image_height and image_width need to be positive.
  boxes   A 2-D tensor of shape [num_boxes, 4]. The i-th row of the tensor specifies the coordinates of a box in the box_ind[i] image and is specified in normalized coordinates [y1, x1, y2, x2]. A normalized coordinate value of y is mapped to the image coordinate at y * (image_height - 1), so as the [0, 1] interval of normalized image height is mapped to [0, image_height - 1] in image height coordinates. We do allow y1 > y2, in which case the sampled crop is an up-down flipped version of the original image. The width dimension is treated similarly. Normalized coordinates outside the [0, 1] range are allowed, in which case we use extrapolation_value to extrapolate the input image values.
  box_indices   A 1-D tensor of shape [num_boxes] with int32 values in [0, batch). The value of box_ind[i] specifies the image that the i-th box refers to.
  crop_size   A 1-D tensor of 2 elements, size = [crop_height, crop_width]. All cropped image patches are resized to this size. The aspect ratio of the image content is not preserved. Both crop_height and crop_width need to be positive.
  method   An optional string specifying the sampling method for resizing. It can be either "bilinear" or "nearest" and default to "bilinear". Currently two sampling methods are supported: Bilinear and Nearest Neighbor.
  extrapolation_value   An optional float. Defaults to 0. Value used for extrapolation, when applicable.
  name   A name for the operation (optional).

 Returns   A 4-D tensor of shape [num_boxes, crop_height, crop_width, depth].
 Example: import tensorflow as tf
BATCH_SIZE = 1
NUM_BOXES = 5
IMAGE_HEIGHT = 256
IMAGE_WIDTH = 256
CHANNELS = 3
CROP_SIZE = (24, 24)

image = tf.random.normal(shape=(BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH,
CHANNELS) )
boxes = tf.random.uniform(shape=(NUM_BOXES, 4))
box_indices = tf.random.uniform(shape=(NUM_BOXES,), minval=0,
maxval=BATCH_SIZE, dtype=tf.int32)
output = tf.image.crop_and_resize(image, boxes, box_indices, CROP_SIZE)
output.shape  #=> (5, 24, 24, 3)