tensorflow:: ops:: Where
#include <array_ops.h>
Reshapes a quantized tensor as per the Reshape op.
Summary
Args: * scope: A Scope object * shape: Defines the shape of the output tensor. * input_min: The minimum value of the input. * input_max: The maximum value of the input.
Returns: * `Output` output * `Output` output_min: This value is copied from input_min. * `Output` output_max: This value is copied from input_max. */ class QuantizedReshape { public: QuantizedReshape(const tensorflow::Scope& scope, tensorflow::Input tensor, tensorflow::Input shape, tensorflow::Input input_min, tensorflow::Input input_max);
Operation operation; tensorflow::Output output; tensorflow::Output output_min; tensorflow::Output output_max; };
/** Returns the rank of a tensor.
This operation returns an integer representing the rank of `input`.
For example:
't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
shape of tensor 't' is [2, 2, 3]
rank(t) ==> 3
**Note**: The rank of a tensor is not the same as the rank of a matrix. The rank of a tensor is the number of indices required to uniquely select each element of the tensor. Rank is also known as "order", "degree", or "ndims."
Args: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class Rank { public: Rank(const ::tensorflow::Scope& scope, ::tensorflow::Input input); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Reshapes a tensor.
Given `tensor`, this operation returns a tensor that has the same values as `tensor` with shape `shape`.
If one component of 1-D tensor `shape` is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, a `shape` of `[-1]` flattens into 1-D. At most one component of `shape` may be unknown.
The `shape` must be 1-D and the operation returns a tensor with shape `shape` filled with the values of `tensor`. In this case, the number of elements implied by `shape` must be the same as the number of elements in `tensor`.
It is an error if `shape` is not 1-D.
For example:
tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
tensor 't' is [[[1, 1], [2, 2]],
[[3, 3], [4, 4]]]
tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2], [3, 3, 4, 4]]
tensor 't' is [[[1, 1, 1],
[2, 2, 2]],
[[3, 3, 3],
[4, 4, 4]],
[[5, 5, 5],
[6, 6, 6]]]
tensor 't' has shape [3, 2, 3]
pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
-1 can also be used to infer the shape
-1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]]
-1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]]
-1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]
tensor 't' is [7]
shape []
reshapes to a scalar
reshape(t, []) ==> 7
Args: * scope: A Scope object * shape: Defines the shape of the output tensor.
Returns: * `Output`: The output tensor. */ class Reshape { public: Reshape(const tensorflow::Scope& scope, tensorflow::Input tensor, tensorflow::Input shape); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Assign `value` to the sliced l-value reference of `ref`.
The values of `value` are assigned to the positions in the variable `ref` that are selected by the slice parameters. The slice parameters `begin, `end`, `strides`, etc. work exactly as in `StridedSlice`.
NOTE this op currently does not support broadcasting and so `value`'s shape must be exactly the shape produced by the slice of `ref`.
Args: * scope: A Scope object
Returns: * the created `Operation` */ class ResourceStridedSliceAssign { public: /// Optional attribute setters for ResourceStridedSliceAssign struct Attrs { /// Defaults to 0 TF_MUST_USE_RESULT Attrs BeginMask(int64 x) { Attrs ret = *this; ret.begin_mask_ = x; return ret; }
/// Defaults to 0 TF_MUST_USE_RESULT Attrs EndMask(int64 x) { Attrs ret = *this; ret.end_mask_ = x; return ret; }
/// Defaults to 0 TF_MUST_USE_RESULT Attrs EllipsisMask(int64 x) { Attrs ret = *this; ret.ellipsis_mask_ = x; return ret; }
/// Defaults to 0 TF_MUST_USE_RESULT Attrs NewAxisMask(int64 x) { Attrs ret = *this; ret.new_axis_mask_ = x; return ret; }
/// Defaults to 0 TF_MUST_USE_RESULT Attrs ShrinkAxisMask(int64 x) { Attrs ret = *this; ret.shrink_axis_mask_ = x; return ret; }
int64 begin_mask_ = 0; int64 end_mask_ = 0; int64 ellipsis_mask_ = 0; int64 new_axis_mask_ = 0; int64 shrink_axis_mask_ = 0; }; ResourceStridedSliceAssign(const tensorflow::Scope& scope, tensorflow::Input ref, tensorflow::Input begin, tensorflow::Input end, tensorflow::Input strides, tensorflow::Input value); ResourceStridedSliceAssign(const tensorflow::Scope& scope, tensorflow::Input ref, tensorflow::Input begin, tensorflow::Input end, tensorflow::Input strides, tensorflow::Input value, const ResourceStridedSliceAssign::Attrs& attrs); operator ::tensorflow::Operation() const { return operation; }
static Attrs BeginMask(int64 x) { return Attrs().BeginMask(x); } static Attrs EndMask(int64 x) { return Attrs().EndMask(x); } static Attrs EllipsisMask(int64 x) { return Attrs().EllipsisMask(x); } static Attrs NewAxisMask(int64 x) { return Attrs().NewAxisMask(x); } static Attrs ShrinkAxisMask(int64 x) { return Attrs().ShrinkAxisMask(x); }
Operation operation; };
/** Reverses variable length slices.
This op first slices `input` along the dimension `batch_dim`, and for each slice `i`, reverses the first `seq_lengths[i]` elements along the dimension `seq_dim`.
The elements of `seq_lengths` must obey `seq_lengths[i] <= input.dims[seq_dim]`, and `seq_lengths` must be a vector of length `input.dims[batch_dim]`.
The output slice `i` along dimension `batch_dim` is then given by input slice `i`, with the first `seq_lengths[i]` slices along dimension `seq_dim` reversed.
For example:
Given this:
batch_dim = 0 seq_dim = 1 input.dims = (4, 8, ...) seq_lengths = [7, 2, 3, 5]
then slices of input are reversed on seq_dim, but only up to seq_lengths:
output[0, 0:7, :, ...] = input[0, 7:0:-1, :, ...] output[1, 0:2, :, ...] = input[1, 2:0:-1, :, ...] output[2, 0:3, :, ...] = input[2, 3:0:-1, :, ...] output[3, 0:5, :, ...] = input[3, 5:0:-1, :, ...]
while entries past seq_lens are copied through:
output[0, 7:, :, ...] = input[0, 7:, :, ...] output[1, 2:, :, ...] = input[1, 2:, :, ...] output[2, 3:, :, ...] = input[2, 3:, :, ...] output[3, 2:, :, ...] = input[3, 2:, :, ...]
In contrast, if:
Given this:
batch_dim = 2 seq_dim = 0 input.dims = (8, ?, 4, ...) seq_lengths = [7, 2, 3, 5]
then slices of input are reversed on seq_dim, but only up to seq_lengths:
output[0:7, :, 0, :, ...] = input[7:0:-1, :, 0, :, ...] output[0:2, :, 1, :, ...] = input[2:0:-1, :, 1, :, ...] output[0:3, :, 2, :, ...] = input[3:0:-1, :, 2, :, ...] output[0:5, :, 3, :, ...] = input[5:0:-1, :, 3, :, ...]
while entries past seq_lens are copied through:
output[7:, :, 0, :, ...] = input[7:, :, 0, :, ...] output[2:, :, 1, :, ...] = input[2:, :, 1, :, ...] output[3:, :, 2, :, ...] = input[3:, :, 2, :, ...] output[2:, :, 3, :, ...] = input[2:, :, 3, :, ...]
Args: * scope: A Scope object * input: The input to reverse. * seq_lengths: 1-D with length `input.dims(batch_dim)` and `max(seq_lengths) <= input.dims(seq_dim)` * seq_dim: The dimension which is partially reversed.
Optional attributes (see `Attrs`): * batch_dim: The dimension along which reversal is performed.
Returns: * `Output`: The partially reversed input. It has the same shape as `input`. */ class ReverseSequence { public: /// Optional attribute setters for ReverseSequence struct Attrs { /** The dimension along which reversal is performed.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs BatchDim(int64 x) { Attrs ret = *this; ret.batch_dim_ = x; return ret; }
int64 batch_dim_ = 0; }; ReverseSequence(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input seq_lengths, int64 seq_dim); ReverseSequence(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input seq_lengths, int64 seq_dim, const ReverseSequence::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs BatchDim(int64 x) { return Attrs().BatchDim(x); }
Operation operation; tensorflow::Output output; };
/** Reverses specific dimensions of a tensor.
Given a `tensor`, and a `int32` tensor `axis` representing the set of dimensions of `tensor` to reverse. This operation reverses each dimension `i` for which there exists `j` s.t. `axis[j] == i`.
`tensor` can have up to 8 dimensions. The number of dimensions specified in `axis` may be 0 or more entries. If an index is specified more than once, a InvalidArgument error is raised.
For example:
tensor 't' is [[[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]]
tensor 't' shape is [1, 2, 3, 4]
'dims' is [3] or 'dims' is [-1]
reverse(t, dims) ==> [[[[ 3, 2, 1, 0], [ 7, 6, 5, 4], [ 11, 10, 9, 8]], [[15, 14, 13, 12], [19, 18, 17, 16], [23, 22, 21, 20]]]]
'dims' is '[1]' (or 'dims' is '[-3]')
reverse(t, dims) ==> [[[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23] [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]]
'dims' is '[2]' (or 'dims' is '[-2]')
reverse(t, dims) ==> [[[[8, 9, 10, 11], [4, 5, 6, 7], [0, 1, 2, 3]] [[20, 21, 22, 23], [16, 17, 18, 19], [12, 13, 14, 15]]]]
Args: * scope: A Scope object * tensor: Up to 8-D. * axis: 1-D. The indices of the dimensions to reverse. Must be in the range `[-rank(tensor), rank(tensor))`.
Returns: * `Output`: The same shape as `tensor`. */ class Reverse { public: Reverse(const tensorflow::Scope& scope, tensorflow::Input tensor, tensorflow::Input axis); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Scatters `updates` into a tensor of shape `shape` according to `indices`.
Scatter sparse `updates` according to individual values at the specified
`indices`. This op returns an output tensor with the `shape` you specify. This
op is the inverse of the tf.gather_nd
operator which extracts values or slices
from a given tensor.
This operation is similar totf.tensor_scatter_nd_add
, except that the tensor is zero-initialized. Callingtf.scatter_nd(indices, updates, shape)
is identical to calling `tf.tensor_scatter_nd_add(tf.zeros(shape, updates.dtype), indices, updates)`
If `indices` contains duplicates, the associated `updates` are accumulated (summed) into the output tensor.
**WARNING**: For floating-point data types, the output may be nondeterministic.
This is because the order in which the updates are applied is nondeterministic
and when floating-point numbers are added in different orders the resulting
numerical approximation error can be slightly different. However, the output
will be deterministic if op determinism is enabled via
tf.config.experimental.enable_op_determinism
.
`indices` is an integer tensor containing indices into the output tensor. The last dimension of `indices` can be at most the rank of `shape`:
indices.shape[-1] <= shape.rank
The last dimension of `indices` corresponds to indices of elements (if `indices.shape[-1] = shape.rank`) or slices (if `indices.shape[-1] < shape.rank`) along dimension `indices.shape[-1]` of `shape`.
`updates` is a tensor with shape:
indices.shape[:-1] + shape[indices.shape[-1]:]
The simplest form of the scatter op is to insert individual elements in a tensor by index. Consider an example where you want to insert 4 scattered elements in a rank-1 tensor with 8 elements.
In Python, this scatter operation would look like this:
The resulting tensor would look like this:
[0, 11, 0, 10, 9, 0, 0, 12]
You can also insert entire slices of a higher rank tensor all at once. For example, you can insert two slices in the first dimension of a rank-3 tensor with two matrices of new values.
In Python, this scatter operation would look like this:
The resulting tensor would look like this:
[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]]]
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, the index is ignored.
Args: * scope: A Scope object * indices: Tensor of indices. * updates: Values to scatter into the output tensor. * shape: 1-D. The shape of the output tensor.
Returns: * `Output`: A new tensor with the given shape and updates applied according to the indices. */ class ScatterNd { public: ScatterNd(const tensorflow::Scope& scope, tensorflow::Input indices, tensorflow::Input updates, tensorflow::Input shape); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Applies sparse addition to `input` using individual values or slices
from `updates` according to indices `indices`. The updates are non-aliasing: `input` is only modified in-place if no other operations will use it. Otherwise, a copy of `input` is made. This operation has a gradient with respect to both `input` and `updates`.
`input` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.
`indices` must be integer tensor, containing indices into `input`. It must be shape \([d_0, ..., d_{Q-2}, K]\) where `0 < K <= P`.
The innermost dimension of `indices` (with length `K`) corresponds to indices into elements (if `K = P`) or `(P-K)`-dimensional slices (if `K < P`) along the `K`th dimension of `input`.
`updates` is `Tensor` of rank `Q-1+P-K` with shape:
$$[d_0, ..., d_{Q-2}, input.shape[K], ..., input.shape[P-1]].$$
For example, say we want to add 4 scattered elements to a rank-1 tensor to 8 elements. In Python, that addition would look like this:
input = tf.constant([1, 2, 3, 4, 5, 6, 7, 8]) indices = tf.constant([[4], [3], [1], [7]]) updates = tf.constant([9, 10, 11, 12]) output = tf.scatter_nd_non_aliasing_add(input, indices, updates) with tf.Session() as sess: print(sess.run(output))
The resulting value `output` would look like this:
[1, 13, 3, 14, 14, 6, 7, 20]
See tf.scatter_nd
for more details about how to make updates to slices.
Args: * scope: A Scope object * input: A Tensor. * indices: A Tensor. Must be one of the following types: `int32`, `int64`. A tensor of indices into `input`. * updates: A Tensor. Must have the same type as ref. A tensor of updated values to add to `input`.
Returns: * `Output`: A `Tensor` with the same shape as `input`, containing values of `input` updated with `updates`. */ class ScatterNdNonAliasingAdd { public: ScatterNdNonAliasingAdd(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input indices, tensorflow::Input updates); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Returns the shape of a tensor.
This operation returns a 1-D integer tensor representing the shape of `input`.
For example:
't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
shape(t) ==> [2, 2, 3]
Args: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class Shape { public: /// Optional attribute setters for Shape struct Attrs { /// Defaults to DT_INT32 TF_MUST_USE_RESULT Attrs OutType(DataType x) { Attrs ret = *this; ret.out_type_ = x; return ret; }
DataType out_type_ = DT_INT32; }; Shape(const ::tensorflow::Scope& scope, ::tensorflow::Input input); Shape(const tensorflow::Scope& scope, tensorflow::Input input, const Shape::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs OutType(DataType x) { return Attrs().OutType(x); }
Operation operation; tensorflow::Output output; };
/** Returns shape of tensors.
This operation returns N 1-D integer tensors representing shape of `input[i]s`.
Args: * scope: A Scope object
Returns: * `OutputList`: The output tensor. */ class ShapeN { public: /// Optional attribute setters for ShapeN struct Attrs { /// Defaults to DT_INT32 TF_MUST_USE_RESULT Attrs OutType(DataType x) { Attrs ret = *this; ret.out_type_ = x; return ret; }
DataType out_type_ = DT_INT32; }; ShapeN(const ::tensorflow::Scope& scope, ::tensorflow::InputList input); ShapeN(const tensorflow::Scope& scope, tensorflow::InputList input, const ShapeN::Attrs& attrs); tensorflow::Output operator[](size_t index) const { return output[index]; }
static Attrs OutType(DataType x) { return Attrs().OutType(x); }
Operation operation; ::tensorflow::OutputList output; };
/** Returns the size of a tensor.
This operation returns an integer representing the number of elements in `input`.
For example:
't' is [[[1, 1,, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]
size(t) ==> 12
Args: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class Size { public: /// Optional attribute setters for Size struct Attrs { /// Defaults to DT_INT32 TF_MUST_USE_RESULT Attrs OutType(DataType x) { Attrs ret = *this; ret.out_type_ = x; return ret; }
DataType out_type_ = DT_INT32; }; Size(const ::tensorflow::Scope& scope, ::tensorflow::Input input); Size(const tensorflow::Scope& scope, tensorflow::Input input, const Size::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs OutType(DataType x) { return Attrs().OutType(x); }
Operation operation; tensorflow::Output output; };
/** Return a slice from 'input'.
The output tensor is a tensor with dimensions described by 'size' whose values are extracted from 'input' starting at the offsets in 'begin'.
*Requirements*: 0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n)
Args: * scope: A Scope object * begin: begin[i] specifies the offset into the 'i'th dimension of 'input' to slice from. * size: size[i] specifies the number of elements of the 'i'th dimension of 'input' to slice. If size[i] is -1, all remaining elements in dimension i are included in the slice (i.e. this is equivalent to setting size[i] = input.dim_size(i) - begin[i]).
Returns: * `Output`: The output tensor. */ class Slice { public: Slice(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input begin, tensorflow::Input size); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Returns a copy of the input tensor.
Args: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class Snapshot { public: Snapshot(const ::tensorflow::Scope& scope, ::tensorflow::Input input); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** SpaceToBatch for 4-D tensors of type T.
This is a legacy version of the more general SpaceToBatchND.
Zero-pads and then rearranges (permutes) blocks of spatial data into batch. More specifically, this op outputs a copy of the input tensor where values from the `height` and `width` dimensions are moved to the `batch` dimension. After the zero-padding, both `height` and `width` of the input must be divisible by the block size.
The attr `block_size` must be greater than one. It indicates the block size.
* Non-overlapping blocks of size `block_size x block size` in the height and width dimensions are rearranged into the batch dimension at each location. * The batch of the output tensor is `batch * block_size * block_size`. * Both height_pad and width_pad must be divisible by block_size.
The shape of the output will be:
[batch*block_size*block_size, height_pad/block_size, width_pad/block_size, depth]
Some examples:
(1) For the following input of shape `[1, 2, 2, 1]` and block_size of 2:
The output tensor has shape `[4, 1, 1, 1]` and value:
(2) For the following input of shape `[1, 2, 2, 3]` and block_size of 2:
The output tensor has shape `[4, 1, 1, 3]` and value:
(3) For the following input of shape `[1, 4, 4, 1]` and block_size of 2:
The output tensor has shape `[4, 2, 2, 1]` and value:
(4) For the following input of shape `[2, 2, 4, 1]` and block_size of 2:
The output tensor has shape `[8, 1, 2, 1]` and value:
Among others, this operation is useful for reducing atrous convolution into regular convolution.
Args: * scope: A Scope object * input: 4-D with shape `[batch, height, width, depth]`. * paddings: 2-D tensor of non-negative integers with shape `[2, 2]`. It specifies the padding of the input with zeros across the spatial dimensions as follows:
paddings = [[pad_top, pad_bottom], [pad_left, pad_right]]
The effective spatial dimensions of the zero-padded input tensor will be:
height_pad = pad_top + height + pad_bottom width_pad = pad_left + width + pad_right
Returns: * `Output`: The output tensor. */ class SpaceToBatch { public: SpaceToBatch(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input paddings, int64 block_size); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** SpaceToBatch for N-D tensors of type T.
This operation divides "spatial" dimensions `[1, ..., M]` of the input into a grid of blocks of shape `block_shape`, and interleaves these blocks with the "batch" dimension (0) such that in the output, the spatial dimensions `[1, ..., M]` correspond to the position within the grid, and the batch dimension combines both the position within a spatial block and the original batch position. Prior to division into blocks, the spatial dimensions of the input are optionally zero padded according to `paddings`. See below for a precise description.
This operation is equivalent to the following steps:
1. Zero-pad the start and end of dimensions `[1, ..., M]` of the input according to `paddings` to produce `padded` of shape `padded_shape`.
2. Reshape `padded` to `reshaped_padded` of shape:
[batch] + [padded_shape[1] / block_shape[0], block_shape[0], ..., padded_shape[M] / block_shape[M-1], block_shape[M-1]] + remaining_shape
3. Permute dimensions of `reshaped_padded` to produce `permuted_reshaped_padded` of shape:
block_shape + [batch] + [padded_shape[1] / block_shape[0], ..., padded_shape[M] / block_shape[M-1]] + remaining_shape
4. Reshape `permuted_reshaped_padded` to flatten `block_shape` into the batch dimension, producing an output tensor of shape:
[batch * prod(block_shape)] + [padded_shape[1] / block_shape[0], ..., padded_shape[M] / block_shape[M-1]] + remaining_shape
Some examples:
(1) For the following input of shape `[1, 2, 2, 1]`, `block_shape = [2, 2]`, and `paddings = [[0, 0], [0, 0]]`:
The output tensor has shape `[4, 1, 1, 1]` and value:
(2) For the following input of shape `[1, 2, 2, 3]`, `block_shape = [2, 2]`, and `paddings = [[0, 0], [0, 0]]`:
The output tensor has shape `[4, 1, 1, 3]` and value:
(3) For the following input of shape `[1, 4, 4, 1]`, `block_shape = [2, 2]`, and `paddings = [[0, 0], [0, 0]]`:
The output tensor has shape `[4, 2, 2, 1]` and value:
(4) For the following input of shape `[2, 2, 4, 1]`, block_shape = `[2, 2]`, and paddings = `[[0, 0], [2, 0]]`:
The output tensor has shape `[8, 1, 3, 1]` and value:
Among others, this operation is useful for reducing atrous convolution into regular convolution.
Args: * scope: A Scope object * input: N-D with shape `input_shape = [batch] + spatial_shape + remaining_shape`, where spatial_shape has `M` dimensions. * block_shape: 1-D with shape `[M]`, all values must be >= 1. * paddings: 2-D with shape `[M, 2]`, all values must be >= 0. `paddings[i] = [pad_start, pad_end]` specifies the padding for input dimension `i + 1`, which corresponds to spatial dimension `i`. It is required that `block_shape[i]` divides `input_shape[i + 1] + pad_start + pad_end`.
Returns: * `Output`: The output tensor. */ class SpaceToBatchND { public: SpaceToBatchND(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input block_shape, tensorflow::Input paddings); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** SpaceToDepth for tensors of type T.
Rearranges blocks of spatial data, into depth. More specifically, this op outputs a copy of the input tensor where values from the `height` and `width` dimensions are moved to the `depth` dimension. The attr `block_size` indicates the input block size.
* Non-overlapping blocks of size `block_size x block size` are rearranged into depth at each location. * The depth of the output tensor is `block_size * block_size * input_depth`. * The Y, X coordinates within each block of the input become the high order component of the output channel index. * The input tensor's height and width must be divisible by block_size.
The `data_format` attr specifies the layout of the input and output tensors with the following options: "NHWC": `[ batch, height, width, channels ]` "NCHW": `[ batch, channels, height, width ]` "NCHW_VECT_C": `qint8 [ batch, channels / 4, height, width, 4 ]`
It is useful to consider the operation as transforming a 6-D Tensor. e.g. for data_format = NHWC, Each element in the input tensor can be specified via 6 coordinates, ordered by decreasing memory layout significance as: n,oY,bY,oX,bX,iC (where n=batch index, oX, oY means X or Y coordinates within the output image, bX, bY means coordinates within the input block, iC means input channels). The output would be a transpose to the following layout: n,oY,oX,bY,bX,iC
This operation is useful for resizing the activations between convolutions (but keeping all data), e.g. instead of pooling. It is also useful for training purely convolutional models.
For example, given an input of shape `[1, 2, 2, 1]`, data_format = "NHWC" and
block_size = 2:
This operation will output a tensor of shape `[1, 1, 1, 4]`:
Here, the input has a batch of 1 and each batch element has shape `[2, 2, 1]`, the corresponding output will have a single element (i.e. width and height are both 1) and will have a depth of 4 channels (1 * block_size * block_size). The output element shape is `[1, 1, 4]`.
For an input tensor with larger depth, here of shape `[1, 2, 2, 3]`, e.g.
This operation, for block_size of 2, will return the following tensor of shape
`[1, 1, 1, 12]`
Similarly, for the following input of shape `[1 4 4 1]`, and a block size of 2:
the operator will return the following tensor of shape `[1 2 2 4]`:
Args: * scope: A Scope object * block_size: The size of the spatial block.
Returns: * `Output`: The output tensor. */ class SpaceToDepth { public: /// Optional attribute setters for SpaceToDepth struct Attrs { /// Defaults to "NHWC" TF_MUST_USE_RESULT Attrs DataFormat(StringPiece x) { Attrs ret = *this; ret.data_format_ = x; return ret; }
StringPiece data_format_ = "NHWC"; }; SpaceToDepth(const tensorflow::Scope& scope, tensorflow::Input input, int64 block_size); SpaceToDepth(const tensorflow::Scope& scope, tensorflow::Input input, int64 block_size, const SpaceToDepth::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs DataFormat(StringPiece x) { return Attrs().DataFormat(x); }
Operation operation; tensorflow::Output output; };
/** Splits a tensor into `num_split` tensors along one dimension.
Args: * scope: A Scope object * axis: 0-D. The dimension along which to split. Must be in the range `[-rank(value), rank(value))`. * value: The tensor to split. * num_split: The number of ways to split. Must evenly divide `value.shape[split_dim]`.
Returns: * `OutputList`: They are identically shaped tensors, whose shape matches that of `value` except along `axis`, where their sizes are `values.shape[split_dim] / num_split`. */ class Split { public: Split(const tensorflow::Scope& scope, tensorflow::Input axis, tensorflow::Input value, int64 num_split); tensorflow::Output operator[](size_t index) const { return output[index]; }
Operation operation; ::tensorflow::OutputList output; };
/** Splits a tensor into `num_split` tensors along one dimension.
Args: * scope: A Scope object * value: The tensor to split. * size_splits: list containing the sizes of each output tensor along the split dimension. Must sum to the dimension of value along split_dim. Can contain one -1 indicating that dimension is to be inferred. * axis: 0-D. The dimension along which to split. Must be in the range `[-rank(value), rank(value))`.
Returns: * `OutputList`: Tensors whose shape matches that of `value` except along `axis`, where their sizes are `size_splits[i]`. */ class SplitV { public: SplitV(const tensorflow::Scope& scope, tensorflow::Input value, tensorflow::Input size_splits, tensorflow::Input axis, int64 num_split); tensorflow::Output operator[](size_t index) const { return output[index]; }
Operation operation; ::tensorflow::OutputList output; };
/** Removes dimensions of size 1 from the shape of a tensor.
Given a tensor `input`, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don't want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying `axis`.
For example:
't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
Or, to remove specific size 1 dimensions:
't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
Args: * scope: A Scope object * input: The `input` to squeeze.
Optional attributes (see `Attrs`): * axis: If specified, only squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. Must be in the range `[-rank(input), rank(input))`.
Returns: * `Output`: Contains the same data as `input`, but has one or more dimensions of size 1 removed. */ class Squeeze { public: /// Optional attribute setters for Squeeze struct Attrs { /** If specified, only squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. Must be in the range `[-rank(input), rank(input))`.
Defaults to [] */ TF_MUST_USE_RESULT Attrs Axis(const gtl::ArraySlice& x) { Attrs ret = *this; ret.axis_ = x; return ret; }
gtl::ArraySliceaxis_ = {}; }; Squeeze(const ::tensorflow::Scope& scope, ::tensorflow::Input input); Squeeze(const tensorflow::Scope& scope, tensorflow::Input input, const Squeeze::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs Axis(const gtl::ArraySlice& x) { return Attrs().Axis(x); }
Operation operation; tensorflow::Output output; };
/** Stops gradient computation.
When executed in a graph, this op outputs its input tensor as-is.
When building ops to compute gradients, this op prevents the contribution of its inputs to be taken into account. Normally, the gradient generator adds ops to a graph to compute the derivatives of a specified 'loss' by recursively finding out inputs that contributed to its computation. If you insert this op in the graph it inputs are masked from the gradient generator. They are not taken into account for computing gradients.
This is useful any time you want to compute a value with TensorFlow but need to pretend that the value was a constant. For example, the softmax function for a vector x can be written as
def softmax(x): numerator = tf.exp(x) denominator = tf.reduce_sum(numerator) return numerator / denominator
This however is susceptible to overflow if the values in x are large. An alternative more stable way is to subtract the maximum of x from each of the values.
def stable_softmax(x): z = x - tf.reduce_max(x) numerator = tf.exp(z) denominator = tf.reduce_sum(numerator) return numerator / denominator
However, when we backprop through the softmax to x, we dont want to backprop
through the tf.reduce_max(x)
(if the max values are not unique then the
gradient could flow to the wrong input) calculation and treat that as a
constant. Therefore, we should write this out as
def stable_softmax(x): z = x - tf.stop_gradient(tf.reduce_max(x)) numerator = tf.exp(z) denominator = tf.reduce_sum(numerator) return numerator / denominator
Some other examples include:
* The *EM* algorithm where the *M-step* should not involve backpropagation through the output of the *E-step*. * Contrastive divergence training of Boltzmann machines where, when differentiating the energy function, the training must not backpropagate through the graph that generated the samples from the model. * Adversarial training, where no backprop should happen through the adversarial example generation process.
Args: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class StopGradient { public: StopGradient(const ::tensorflow::Scope& scope, ::tensorflow::Input input); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
Operation operation; tensorflow::Output output; };
/** Return a strided slice from `input`.
Note, most python users will want to use the PythonTensor.__getitem__
orVariable.__getitem__
rather than this op directly.
The goal of this op is to produce a new tensor with a subset of the elements from the `n` dimensional `input` tensor. The subset is chosen using a sequence of `m` sparse range specifications encoded into the arguments of this function. Note, in some cases `m` could be equal to `n`, but this need not be the case. Each range specification entry can be one of the following:
- An ellipsis (...). Ellipses are used to imply zero or more dimensions of full-dimension selection and are produced using `ellipsis_mask`. For example, `foo[...]` is the identity slice.
- A new axis. This is used to insert a new shape=1 dimension and is produced using `new_axis_mask`. For example, `foo[:, ...]` where `foo` is shape `(3, 4)` produces a `(1, 3, 4)` tensor.
- A range `begin:end:stride`. This is used to specify how much to choose from a given dimension. `stride` can be any integer but 0. `begin` is an integer which represents the index of the first value to select while `end` represents the index of the last value to select. The number of values selected in each dimension is `end - begin` if `stride > 0` and `begin - end` if `stride < 0`. `begin` and `end` can be negative where `-1` is the last element, `-2` is the second to last. `begin_mask` controls whether to replace the explicitly given `begin` with an implicit effective value of `0` if `stride > 0` and `-1` if `stride < 0`. `end_mask` is analogous but produces the number required to create the largest open interval. For example, given a shape `(3,)` tensor `foo[:]`, the effective `begin` and `end` are `0` and `3`. Do not assume this is equivalent to `foo[0:-1]` which has an effective `begin` and `end` of `0` and `2`. Another example is `foo[-2::-1]` which reverses the first dimension of a tensor while dropping the last two (in the original order elements). For example `foo = [1,2,3,4]; foo[-2::-1]` is `[4,3]`.
- A single index. This is used to keep only elements that have a given index. For example (`foo[2, :]` on a shape `(5,6)` tensor produces a shape `(6,)` tensor. This is encoded in `begin` and `end` and `shrink_axis_mask`.
Each conceptual range specification is encoded in the op's argument. This encoding is best understand by considering a non-trivial example. In particular, `foo[1, 2:4, None, ..., :-3:-1, :]` will be encoded as
In this case if foo.shape
is (5, 5, 5, 5, 5, 5) the final shape of
the slice becomes (2, 1, 5, 5, 2, 5).
Let us walk step by step through each argument specification.
1. The first argument in the example slice is turned intobegin = 1
andend = begin + 1 = 2
. To disambiguate from the original spec2:4
we also set the appropriate bit inshrink_axis_mask
.
2. 2:4
is contributes 2, 4, 1 to begin, end, and stride. All masks have
zero bits contributed.
3. None is a synonym for tf.newaxis
. This means insert a dimension of size 1
dimension in the final shape. Dummy values are contributed to begin,
end and stride, while the new_axis_mask bit is set.
4. ...
grab the full ranges from as many dimensions as needed to
fully specify a slice for every dimension of the input shape.
5.:-3:-1
shows the use of negative indices. A negative indexi
associated with a dimension that has shapes
is converted to a positive indexs + i
. So-1
becomess-1
(i.e. the last element). This conversion is done internally so begin, end and strides receive x, -3, and -1. The appropriate begin_mask bit is set to indicate the start range is the full range (ignoring the x).
6.:
indicates that the entire contents of the corresponding dimension is selected. This is equivalent to::
or0::1
. begin, end, and strides receive 0, 0, and 1, respectively. The appropriate bits inbegin_mask
andend_mask
are also set.
Requirements:0 != strides[i] for i in [0, m)
ellipsis_mask must be a power of two (only one ellipsis)
Args: * scope: A Scope object * begin:begin[k]
specifies the offset into thek
th range specification. The exact dimension this corresponds to will be determined by context. Out-of-bounds values will be silently clamped. If thek
th bit ofbegin_mask
thenbegin[k]
is ignored and the full range of the appropriate dimension is used instead. Negative values causes indexing to start from the highest element e.g. Iffoo==[1,2,3]
thenfoo[-1]==3
. * end:end[i]
is likebegin
with the exception thatend_mask
is used to determine full ranges. * strides:strides[i]
specifies the increment in thei
th specification after extracting a given element. Negative indices will reverse the original order. Out or range values are clamped to[0,dim[i]) if slice[i]>0
or[-1,dim[i]-1] if slice[i] < 0
Optional attributes (seeAttrs
): * begin_mask: a bitmask where a bit i being 1 means to ignore the begin value and instead use the largest interval possible. At runtime begin[i] will be replaced with[0, n-1)
ifstride[i] > 0
or[-1, n-1]
ifstride[i] < 0
* end_mask: analogous tobegin_mask
* ellipsis_mask: a bitmask where biti
being 1 means thei
th position is actually an ellipsis. One bit at most can be 1. Ifellipsis_mask == 0
, then an implicit ellipsis mask of1 << (m+1)
is provided. This means thatfoo[3:5] == foo[3:5, ...]
. An ellipsis implicitly creates as many range specifications as necessary to fully specify the sliced range for every dimension. For example for a 4-dimensional tensorfoo
the slicefoo[2, ..., 5:8]
impliesfoo[2, :, :, 5:8]
. * new_axis_mask: a bitmask where biti
being 1 means thei
th specification creates a new shape 1 dimension. For examplefoo[:4, tf.newaxis, :2]
would produce a shape(4, 1, 2)
tensor. * shrink_axis_mask: a bitmask where biti
implies that thei
th specification should shrink the dimensionality. begin and end must imply a slice of size 1 in the dimension. For example in python one might dofoo[:, 3, :]
which would result inshrink_axis_mask
being 2.
Returns: *Output
: The output tensor. */ class StridedSlice { public: /// Optional attribute setters for StridedSlice struct Attrs { /** a bitmask where a bit i being 1 means to ignore the begin value and instead use the largest interval possible. At runtime begin[i] will be replaced with[0, n-1)
ifstride[i] > 0
or[-1, n-1]
ifstride[i] < 0
Defaults to 0 */ TF_MUST_USE_RESULT Attrs BeginMask(int64 x) { Attrs ret = *this; ret.begin_mask_ = x; return ret; }
/** analogous to begin_mask
Defaults to 0 */ TF_MUST_USE_RESULT Attrs EndMask(int64 x) { Attrs ret = *this; ret.end_mask_ = x; return ret; }
/** a bitmask where biti
being 1 means thei
th position is actually an ellipsis. One bit at most can be 1. Ifellipsis_mask == 0
, then an implicit ellipsis mask of1 << (m+1)
is provided. This means thatfoo[3:5] == foo[3:5, ...]
. An ellipsis implicitly creates as many range specifications as necessary to fully specify the sliced range for every dimension. For example for a 4-dimensional tensorfoo
the slicefoo[2, ..., 5:8]
impliesfoo[2, :, :, 5:8]
.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs EllipsisMask(int64 x) { Attrs ret = *this; ret.ellipsis_mask_ = x; return ret; }
/** a bitmask where biti
being 1 means thei
th specification creates a new shape 1 dimension. For examplefoo[:4, tf.newaxis, :2]
would produce a shape(4, 1, 2)
tensor.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs NewAxisMask(int64 x) { Attrs ret = *this; ret.new_axis_mask_ = x; return ret; }
/** a bitmask where biti
implies that thei
th specification should shrink the dimensionality. begin and end must imply a slice of size 1 in the dimension. For example in python one might dofoo[:, 3, :]
which would result inshrink_axis_mask
being 2.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs ShrinkAxisMask(int64 x) { Attrs ret = *this; ret.shrink_axis_mask_ = x; return ret; }
int64 begin_mask_ = 0; int64 end_mask_ = 0; int64