#include <array_ops.h>
Packs a list of N
rank-R
tensors into one rank-(R+1)
tensor.
Packs the N
tensors in values
into a tensor with rank one higher than each tensor in values
, by packing them along the axis
dimension. Given a list of tensors of shape (A, B, C)
;
if axis == 0
then the output
tensor will have the shape (N, A, B, C)
. if axis == 1
then the output
tensor will have the shape (A, N, B, C)
. Etc.
For example:
pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]
This is the opposite of `unpack`.
Arguments: * scope: A Scope object * values: Must be of same shape and type.
Optional attributes (see `Attrs`): * axis: Dimension along which to pack. Negative values wrap around, so the valid range is `[-(R+1), R+1)`.
Returns: * `Output`: The packed tensor. */ class Stack { public: /// Optional attribute setters for Stack struct Attrs { /** Dimension along which to pack. Negative values wrap around, so the valid range is `[-(R+1), R+1)`.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs Axis(int64 x) { Attrs ret = *this; ret.axis_ = x; return ret; }
int64 axis_ = 0; }; Stack(const ::tensorflow::Scope& scope, ::tensorflow::InputList values); Stack(const tensorflow::Scope& scope, tensorflow::InputList values, const Stack::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs Axis(int64 x) { return Attrs().Axis(x); }
tensorflow::Output output; };
/** Pads a tensor with zeros.
This operation pads a `input` with zeros according to the `paddings` you specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is the rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates how many zeros to add before the contents of `input` in that dimension, and `paddings[D, 1]` indicates how many zeros to add after the contents of `input` in that dimension.
The padded size of each dimension D of the output is:
`paddings(D, 0) + input.dim_size(D) + paddings(D, 1)`
For example:
pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] [0, 0, 1, 1, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 0, 0, 0, 0]]
Arguments: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class Pad { public: Pad(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input paddings); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
tensorflow::Output output; };
/** Pads a tensor.
This operation pads `input` according to the `paddings` and `constant_values` you specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is the rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates how many padding values to add before the contents of `input` in that dimension, and `paddings[D, 1]` indicates how many padding values to add after the contents of `input` in that dimension. `constant_values` is a scalar tensor of the same type as `input` that indicates the value to use for padding `input`.
The padded size of each dimension D of the output is:
`paddings(D, 0) + input.dim_size(D) + paddings(D, 1)`
For example:
pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] [0, 0, 1, 1, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 0, 0, 0, 0]]
Arguments: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class PadV2 { public: PadV2(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input paddings, tensorflow::Input constant_values); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
tensorflow::Output output; };
/** Concatenates a list of `N` tensors along the first dimension.
The input tensors are all required to have size 1 in the first dimension.
For example:
parallel_concat([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim.
The difference between concat and parallel_concat is that concat requires all of the inputs be computed before the operation will begin but doesn't require that the input shapes be known during graph construction. Parallel concat will copy pieces of the input into the output as they become available, in some situations this can provide a performance benefit.
Arguments: * scope: A Scope object * values: Tensors to be concatenated. All must have size 1 in the first dimension and same shape. * shape: the final shape of the result; should be equal to the shapes of any input but with the number of input values in the first dimension.
Returns: * `Output`: The concatenated tensor. */ class ParallelConcat { public: ParallelConcat(const tensorflow::Scope& scope, tensorflow::InputList values, PartialTensorShape shape); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
tensorflow::Output output; };
/** A placeholder op for a value that will be fed into the computation.
N.B. This operation will fail with an error if it is executed. It is intended as a way to represent a value that will always be fed, and to provide attrs that enable the fed value to be checked at runtime.
Arguments: * scope: A Scope object * dtype: The type of elements in the tensor.
Optional attributes (see `Attrs`): * shape: (Optional) The shape of the tensor. If the shape has 0 dimensions, the shape is unconstrained.
Returns: * `Output`: A placeholder tensor that must be replaced using the feed mechanism. */ class Placeholder { public: /// Optional attribute setters for Placeholder struct Attrs { /** (Optional) The shape of the tensor. If the shape has 0 dimensions, the shape is unconstrained.
Defaults to */ TF_MUST_USE_RESULT Attrs Shape(PartialTensorShape x) { Attrs ret = *this; ret.shape_ = x; return ret; }
PartialTensorShape shape_ = ::tensorflow::PartialTensorShape() /* unknown */; }; Placeholder(const ::tensorflow::Scope& scope, DataType dtype); Placeholder(const tensorflow::Scope& scope, DataType dtype, const Placeholder::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs Shape(PartialTensorShape x) { return Attrs().Shape(x); }
tensorflow::Output output; };
/** A placeholder op that passes through `input` when its output is not fed.
Arguments: * scope: A Scope object * input: The default value to produce when `output` is not fed. * shape: The (possibly partial) shape of the tensor.
Returns: * `Output`: A placeholder tensor that defaults to `input` if it is not fed. */ class PlaceholderWithDefault { public: PlaceholderWithDefault(const tensorflow::Scope& scope, tensorflow::Input input, PartialTensorShape shape); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
tensorflow::Output output; };
/** An identity op that triggers an error if a gradient is requested.
When executed in a graph, this op outputs its input tensor as-is.
When building ops to compute gradients, the TensorFlow gradient system will return an error when trying to lookup the gradient of this op, because no gradient must ever be registered for this function. This op exists to prevent subtle bugs from silently returning unimplemented gradients in some corner cases.
Arguments: * scope: A Scope object * input: any tensor.
Optional attributes (see `Attrs`): * message: Will be printed in the error when anyone tries to differentiate this operation.
Returns: * `Output`: the same input tensor. */ class PreventGradient { public: /// Optional attribute setters for PreventGradient struct Attrs { /** Will be printed in the error when anyone tries to differentiate this operation.
Defaults to "" */ TF_MUST_USE_RESULT Attrs Message(StringPiece x) { Attrs ret = *this; ret.message_ = x; return ret; }
StringPiece message_ = ""; }; PreventGradient(const ::tensorflow::Scope& scope, ::tensorflow::Input input); PreventGradient(const tensorflow::Scope& scope, tensorflow::Input input, const PreventGradient::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs Message(StringPiece x) { return Attrs().Message(x); }
tensorflow::Output output; };
/** Quantizes then dequantizes a tensor.
This op simulates the precision loss from the quantized forward pass by: 1. Quantizing the tensor to fixed point numbers, which should match the target quantization method when it is used in inference. 2. Dequantizing it back to floating point numbers for the following ops, most likely matmul.
There are different ways to quantize. This version does not use the full range of the output type, choosing to elide the lowest possible value for symmetry (e.g., output range is -127 to 127, not -128 to 127 for signed 8 bit quantization), so that 0.0 maps to 0.
To perform this op, we first find the range of values in our tensor. The range we use is always centered on 0, so we find m such that
1. m = max(abs(input_min), abs(input_max)) if range_given is true, 2. m = max(abs(min_elem(input)), abs(max_elem(input))) otherwise.
Our input tensor range is then [-m, m].
Next, we choose our fixed-point quantization buckets, [min_fixed, max_fixed]. If signed_input is true, this is
[min_fixed, max_fixed ] = [-(1 << (num_bits - 1) - 1), (1 << (num_bits - 1)) - 1].
Otherwise, if signed_input is false, the fixed-point range is
[min_fixed, max_fixed] = [0, (1 << num_bits) - 1].
From this we compute our scaling factor, s:
s = (max_fixed - min_fixed) / (2 * m).
Now we can quantize and dequantize the elements of our tensor. An element e is transformed into e':
e' = (e * s).round_to_nearest() / s.
Note that we have a different number of buckets in the signed vs. unsigned cases. For example, if num_bits == 8, we get 254 buckets in the signed case vs. 255 in the unsigned case.
For example, suppose num_bits = 8 and m = 1. Then
[min_fixed, max_fixed] = [-127, 127], and s = (127 + 127) / 2 = 127.
Given the vector {-1, -0.5, 0, 0.3}, this is quantized to {-127, -63, 0, 38}, and dequantized to {-1, -63.0/127, 0, 38.0/127}.
Arguments: * scope: A Scope object * input: Tensor to quantize and then dequantize. * input_min: If range_given, this is the min of the range, otherwise this input will be ignored. * input_max: If range_given, this is the max of the range, otherwise this input will be ignored.
Optional attributes (see `Attrs`): * signed_input: If the quantization is signed or unsigned. * num_bits: The bitwidth of the quantization. * range_given: If the range is given or should be computed from the tensor.
Returns: * `Output`: The output tensor. */ class QuantizeAndDequantizeV2 { public: /// Optional attribute setters for QuantizeAndDequantizeV2 struct Attrs { /** If the quantization is signed or unsigned.
Defaults to true */ TF_MUST_USE_RESULT Attrs SignedInput(bool x) { Attrs ret = *this; ret.signed_input_ = x; return ret; }
/** The bitwidth of the quantization.
Defaults to 8 */ TF_MUST_USE_RESULT Attrs NumBits(int64 x) { Attrs ret = *this; ret.num_bits_ = x; return ret; }
/** If the range is given or should be computed from the tensor.
Defaults to false */ TF_MUST_USE_RESULT Attrs RangeGiven(bool x) { Attrs ret = *this; ret.range_given_ = x; return ret; }
bool signed_input_ = true; int64 num_bits_ = 8; bool range_given_ = false; }; QuantizeAndDequantizeV2(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input input_min, tensorflow::Input input_max); QuantizeAndDequantizeV2(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input input_min, tensorflow::Input input_max, const QuantizeAndDequantizeV2::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs SignedInput(bool x) { return Attrs().SignedInput(x); } static Attrs NumBits(int64 x) { return Attrs().NumBits(x); } static Attrs RangeGiven(bool x) { return Attrs().RangeGiven(x); }
tensorflow::Output output; };
/** Quantizes then dequantizes a tensor.
This is almost identical to QuantizeAndDequantizeV2, except that num_bits is a tensor, so its value can change during training.
Arguments: * scope: A Scope object
Returns: * `Output`: The output tensor. */ class QuantizeAndDequantizeV3 { public: /// Optional attribute setters for QuantizeAndDequantizeV3 struct Attrs { /// Defaults to true TF_MUST_USE_RESULT Attrs SignedInput(bool x) { Attrs ret = *this; ret.signed_input_ = x; return ret; }
/// Defaults to true TF_MUST_USE_RESULT Attrs RangeGiven(bool x) { Attrs ret = *this; ret.range_given_ = x; return ret; }
bool signed_input_ = true; bool range_given_ = true; }; QuantizeAndDequantizeV3(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input input_min, tensorflow::Input input_max, tensorflow::Input num_bits); QuantizeAndDequantizeV3(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input input_min, tensorflow::Input input_max, tensorflow::Input num_bits, const QuantizeAndDequantizeV3::Attrs& attrs); operator ::tensorflow::Output() const { return output; } operator ::tensorflow::Input() const { return output; } ::tensorflow::Node* node() const { return output.node(); }
static Attrs SignedInput(bool x) { return Attrs().SignedInput(x); } static Attrs RangeGiven(bool x) { return Attrs().RangeGiven(x); }
tensorflow::Output output; };
/** Quantize the 'input' tensor of type float to 'output' tensor of type 'T'.
[min_range, max_range] are scalar floats that specify the range for the 'input' data. The 'mode' attribute controls exactly which calculations are used to convert the float values to their quantized equivalents. The 'round_mode' attribute controls which rounding tie-breaking algorithm is used when rounding float values to their quantized equivalents.
In 'MIN_COMBINED' mode, each value of the tensor will undergo the following:out[i] = (in[i] - min_range) * range(T) / (max_range - min_range) if T == qint8, out[i] -= (range(T) + 1) / 2.0
here `range(T) = numeric_limits::max() - numeric_limits::min()`
*MIN_COMBINED Mode Example*
Assume the input is type float and has a possible range of [0.0, 6.0] and the output type is quint8 ([0, 255]). The min_range and max_range values should be specified as 0.0 and 6.0. Quantizing from float to quint8 will multiply each value of the input by 255/6 and cast to quint8.
If the output type was qint8 ([-128, 127]), the operation will additionally subtract each value by 128 prior to casting, so that the range of values aligns with the range of qint8.
If the mode is 'MIN_FIRST', then this approach is used:num_discrete_values = 1 << (# of bits in T) range_adjust = num_discrete_values / (num_discrete_values - 1) range = (range_max - range_min) * range_adjust range_scale = num_discrete_values / range quantized = round(input * range_scale) - round(range_min * range_scale) + numeric_limits
The biggest difference between this and MIN_COMBINED is that the minimum range is rounded first, before it's subtracted from the rounded value. With MIN_COMBINED, a small bias is introduced where repeated iterations of quantizing and dequantizing will introduce a larger and larger error.
*SCALED mode Example*
`SCALED` mode matches the quantization approach used in `QuantizeAndDequantize{V2|V3}`.
If the mode is `SCALED`, we do not use the full range of the output type, choosing to elide the lowest possible value for symmetry (e.g., output range is -127 to 127, not -128 to 127 for signed 8 bit quantization), so that 0.0 maps to 0.
We first find the range of values in our tensor. The range we use is always centered on 0, so we find m such thatc++ m = max(abs(input_min), abs(input_max))
Our input tensor range is then `[-m, m]`.
Next, we choose our fixed-point quantization buckets, `[min_fixed, max_fixed]`. If T is signed, this isnum_bits = sizeof(T) * 8 [min_fixed, max_fixed] = [-(1 << (num_bits - 1) - 1), (1 << (num_bits - 1)) - 1]
Otherwise, if T is unsigned, the fixed-point range is[min_fixed, max_fixed] = [0, (1 << num_bits) - 1]
From this we compute our scaling factor, s:c++ s = (max_fixed - min_fixed) / (2 * m)
Now we can quantize the elements of our tensor:c++ result = round(input * s)
One thing to watch out for is that the operator may choose to adjust the requested minimum and maximum values slightly during the quantization process, so you should always use the output ports as the range for further calculations. For example, if the requested minimum and maximum values are close to equal, they will be separated by a small epsilon value to prevent ill-formed quantized buffers from being created. Otherwise, you can end up with buffers where all the quantized values map to the same float value, which causes problems for operations that have to perform further calculations on them.
Arguments: * scope: A Scope object * min_range: The minimum scalar value possibly produced for the input. * max_range: The maximum scalar value possibly produced for the input.
Returns: * `Output` output: The quantized data produced from the float input. * `Output` output_min: The actual minimum scalar value used for the output. * `Output` output_max: The actual maximum scalar value used for the output. */ class QuantizeV2 { public: /// Optional attribute setters for QuantizeV2 struct Attrs { /// Defaults to "MIN_COMBINED" TF_MUST_USE_RESULT Attrs Mode(StringPiece x) { Attrs ret = *this; ret.mode_ = x; return ret; }
/// Defaults to "HALF_AWAY_FROM_ZERO" TF_MUST_USE_RESULT Attrs RoundMode(StringPiece x) { Attrs ret = *this; ret.round_mode_ = x; return ret; }
StringPiece mode_ = "MIN_COMBINED"; StringPiece round_mode_ = "HALF_AWAY_FROM_ZERO"; }; QuantizeV2(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input min_range, tensorflow::Input max_range, DataType T); QuantizeV2(const tensorflow::Scope& scope, tensorflow::Input input, tensorflow::Input min_range, tensorflow::Input max_range, DataType T, const QuantizeV2::Attrs& attrs);
static Attrs Mode(StringPiece x) { return Attrs().Mode(x); } static Attrs RoundMode(StringPiece x) { return Attrs().RoundMode(x); }
tensorflow::Output output; tensorflow::Output output_min; tensorflow::Output output_max; };
/** Concatenates quantized tensors along one dimension.
Arguments: * scope: A Scope object * concat_dim: 0-D. The dimension along which to concatenate. Must be in the range [0, rank(values)). * values: The `N` Tensors to concatenate. Their ranks and types must match, and their sizes must match in all dimensions except `concat_dim`. * input_mins: The minimum scalar values for each of the input tensors. * input_maxes: The maximum scalar values for each of the input tensors.
Returns: * `Output` output: A `Tensor` with the concatenation of values stacked along the `concat_dim` dimension. This tensor's shape matches that of `values` except in `concat_dim` where it has the sum of the sizes. * `Output` output_min: The float value that the minimum quantized output value represents. * `Output` output_max: The float value that the maximum quantized output value represents. */ class QuantizedConcat { public: QuantizedConcat(const tensorflow::Scope& scope, tensorflow::Input concat_dim, tensorflow::InputList values, tensorflow::InputList input_mins, tensorflow::InputList input_maxes);
tensorflow::Output output; tensorflow::Output output_min; tensorflow::Output output_max; };
/** Quantized Instance normalization.
Arguments: * scope: A Scope object * x: A 4D input Tensor. * x_min: The value represented by the lowest quantized input. * x_max: The value represented by the highest quantized input.
Optional attributes (see `Attrs`): * output_range_given: If True, `given_y_min` and `given_y_min` and `given_y_max` are used as the output range. Otherwise, the implementation computes the output range. * given_y_min: Output in `y_min` if `output_range_given` is True. * given_y_max: Output in `y_max` if `output_range_given` is True. * variance_epsilon: A small float number to avoid dividing by 0. * min_separation: Minimum value of `y_max - y_min`
Returns: * `Output` y: A 4D Tensor. * `Output` y_min: The value represented by the lowest quantized output. * `Output` y_max: The value represented by the highest quantized output. */ class QuantizedInstanceNorm { public: /// Optional attribute setters for QuantizedInstanceNorm struct Attrs { /** If True, `given_y_min` and `given_y_min` and `given_y_max` are used as the output range. Otherwise, the implementation computes the output range.
Defaults to false */ TF_MUST_USE_RESULT Attrs OutputRangeGiven(bool x) { Attrs ret = *this; ret.output_range_given_ = x; return ret; }
/** Output in `y_min` if `output_range_given` is True.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs GivenYMin(float x) { Attrs ret = *this; ret.given_y_min_ = x; return ret; }
/** Output in `y_max` if `output_range_given` is True.
Defaults to 0 */ TF_MUST_USE_RESULT Attrs GivenYMax(float x) { Attrs ret = *this; ret.given_y_max_ = x; return ret; }
/** A small float number to avoid dividing by 0.
Defaults to 1e-05 */ TF_MUST_USE_RESULT Attrs VarianceEpsilon(float x) { Attrs ret = *this; ret.variance_epsilon_ = x; return ret; }
/** Minimum value of `y_max - y_min`
Defaults to 0.001 */ TF_MUST_USE_RESULT Attrs MinSeparation(float x) { Attrs ret = *this; ret.min_separation_ = x; return ret; }
bool output_range_given_ = false; float given_y_min_ = 0.0f; float given_y_max_ = 0.0f; float variance_epsilon_ = 1e-05f; float min_separation_ = 0.001f; }; QuantizedInstanceNorm(const tensorflow::Scope& scope, tensorflow::Input x, tensorflow::Input x_min, tensorflow::Input x_max); QuantizedInstanceNorm(const tensorflow::Scope& scope, tensorflow::Input x, tensorflow::Input x_min, tensorflow::Input x_max, const QuantizedInstanceNorm::Attrs& attrs);
static Attrs OutputRangeGiven(bool x) { return Attrs().OutputRangeGiven(x); } static Attrs GivenYMin(float x) { return Attrs().GivenYMin(x); } static Attrs GivenYMax(float x) { return Attrs().GivenYMax(x); } static Attrs VarianceEpsilon(float x) { return Attrs().VarianceEpsilon(x); } static Attrs MinSeparation(float x) { return Attrs().MinSeparation(x); }
tensorflow::Output y; tensorflow::Output y_min; tensorflow::Output y_max; };
/** Reshapes a quantized tensor as per the Reshape op.
Arguments:
Returns:
Output
outputOutput
output_min: This value is copied from input_min.Output
output_max: This value is copied from input_max. Constructors and Destructors | |
---|---|
QuantizedReshape(const ::tensorflow::Scope & scope, ::tensorflow::Input tensor, ::tensorflow::Input shape, ::tensorflow::Input input_min, ::tensorflow::Input input_max) |
Public attributes | |
---|---|
output | |
output_max | |
output_min |
::tensorflow::Output output
::tensorflow::Output output_max
::tensorflow::Output output_min
QuantizedReshape( const ::tensorflow::Scope & scope, ::tensorflow::Input tensor, ::tensorflow::Input shape, ::tensorflow::Input input_min, ::tensorflow::Input input_max )
© 2018 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/quantized-reshape.html