Float tensor
Inherits From: Tensor
TFSimilarity.callbacks.FloatTensor(
op, value_index, dtype
)
op | An Operation. Operation that computes this tensor. |
value_index | An int. Index of the operation's endpoint that produces this tensor. |
dtype | A DType. Type of elements stored in this tensor. |
TypeError | If the op is not an Operation. |
device | The name of the device on which this tensor will be produced, or None. |
dtype | The DType of elements in this tensor. |
graph | The Graph that contains this tensor. |
name | The string name of this tensor. |
op | The Operation that produces this tensor as an output. |
shape |
Returns a tf.TensorShape that represents the shape of this tensor.
tf.Tensor.shape is equivalent to tf.Tensor.get_shape(). In a tf.function or when building a model using tf.keras.Input, they return the build-time shape of the tensor, which may be partially unknown. A tf.TensorShape is not a tensor. Use tf.shape(t) to get a tensor containing the shape, calculated at runtime. See tf.Tensor.get_shape(), and tf.TensorShape for details and examples. |
value_index | The index of this tensor in the outputs of its Operation. |
consumers()
Returns a list of Operations that consume this tensor.
Returns | |
---|---|
A list of Operations. |
eval(
feed_dict=None, session=None
)
Evaluates this tensor in a Session.
Note: If you are not using compat.v1 libraries, you should not need this, (or feed_dict or Session). In eager execution (or within tf.function) you do not need to call eval.
Calling this method will execute all preceding operations that produce the inputs needed for the operation that produces this tensor.
N.B. Before invoking Tensor.eval()``` , its graph must have been launched in a session, and either a default session must be available, or session must be specified explicitly.
Args | |
---|---|
feed_dict | A dictionary that maps Tensor objects to feed values. See tf.Session.run for a description of the valid feed values. |
session | (Optional.) The Session to be used to evaluate this tensor. If none, the default session will be used. |
Returns | |
---|---|
A numpy array corresponding to the value of this tensor. |
experimental_ref()
DEPRECATED FUNCTION
Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use ref() instead.
get_shape()
Returns a tf.TensorShape that represents the shape of this tensor.
In eager execution the shape is always fully-known.
>>> a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> print(a.shape)
(2, 3)
tf.Tensor.get_shape() is equivalent to tf.Tensor.shape.
When executing in a tf.function or building a model using tf.keras.Input, <a href="../../TFSimilarity/callbacks/Tensor.md## # the print executes during tracing. ... print("Result shape: ", result.shape) ... return result
The shape inference functions propagate shapes to the extent possible:
f = my_matmul.get_concrete_function( ... tf.TensorSpec([None,3]), ... tf.TensorSpec([3,5])) Result shape: (None, 5)
Tracing may fail if a shape missmatch can be detected:
cf = my_matmul.get_concrete_function( ... tf.TensorSpec([None,3]), ... tf.TensorSpec([4,5])) Traceback (most recent call last): ... ValueError: Dimensions must be equal, but are 3 and 4 for 'matmul' (op: 'MatMul') with input shapes: [?,3], [4,5].
In some cases, the inferred shape may have unknown dimensions. If
the caller has additional information about the values of these
dimensions, <b>tf.ensure_shape</b> or <a href="../../TFSimilarity/callbacks/Tensor.md## the <b>print</b> executes during tracing.
... print("Result shape: ", a.shape)
... return a
>>> cf = my_fun.get_concrete_function(
... tf.TensorSpec([None, None]))
Result shape: (5, 5)
Returns | |
---|---|
A tf.TensorShape representing the shape of this tensor. |
ref()
Returns a hashable reference object to this Tensor.
The primary use case for this API is to put tensors in a set/dictionary. We can't put tensors in a set/dictionary as tensor.hash() is no longer available starting Tensorflow 2.0.
The following will raise an exception starting 2.0
>>> x = tf.constant(5)
>>> y = tf.constant(10)
>>> z = tf.constant(10)
>>> tensor_set = <i>x, y, z</i>
Traceback (most recent call last):
...
TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.
>>> tensor_dict = <i>x: 'five', y: 'ten'</i>
Traceback (most recent call last):
...
TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.
Instead, we can use tensor.ref().
>>> tensor_set = <i>x.ref(), y.ref(), z.ref()</i>
>>> x.ref() in tensor_set
True
>>> tensor_dict = <i>x.ref(): 'five', y.ref(): 'ten', z.ref(): 'ten'</i>
>>> tensor_dict[y.ref()]
'ten'
Also, the reference object provides .deref() function that returns the original Tensor.
>>> x = tf.constant(5)
>>> x.ref().deref()
<tf.Tensor: shape=(), dtype=int32, numpy=5>
set_shape(
shape
)
Updates the shape of this tensor.
Note: It is recommended to use tf.ensure_shape instead of Tensor.set_shape`` , because tf.ensure_shape` provides better checking for programming errors and can create guarantees for compiler optimization.
With eager execution this operates as a shape assertion. Here the shapes match:
>>> t = tf.constant([[1,2,3]])
>>> t.set_shape([1, 3])
Passing a None in the new shape allows any value for that axis:
>>> t.set_shape([1,None])
An error is raised if an incompatible shape is passed.
>>> t.set_shape([1,5])
Traceback (most recent call last):
...
ValueError: Tensor's shape (1, 3) is not compatible with supplied
shape [1, 5]
When executing in a tf.function, or building a model using tf.keras.Input, <a href="../../TFSimilarity/callbacks/Tensor.md## # the print executes during tracing. ... print("Initial shape: ", result.shape) ... result.set_shape([None, None]) ... print("Final shape: ", result.shape) ... return result
Trace the function
concrete_parse = my_parse.get_concrete_function( ... tf.TensorSpec([], dtype=tf.string)) Initial shape: Final shape: (None, None)
##### # The function still runs, even though it <b>set_shape([None,None])</b>
>>> t2 = concrete_parse(serialized)
>>> print(t2.shape)
(5, 5, 5)
Args | |
---|---|
shape | A TensorShape representing the shape of this tensor, a TensorShapeProto, a list, a tuple, or None. |
Raises | |
---|---|
ValueError | If shape is not compatible with the current shape of this tensor. |
__abs__(
name=None
)
Computes the absolute value of a tensor.
Given a tensor of integer or floating-point values, this operation returns a tensor of the same type, where each element contains the absolute value of the corresponding element in the input.
Given a tensor x of complex numbers, this operation returns a tensor of type float32 or float64 that is the absolute value of each element in x. For a complex number \(a + bj\), its absolute value is computed as \(\sqrta^2 + b^2\).
x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]]) tf.abs(x) <tf.Tensor: shape=(2, 1), dtype=float64, numpy= array([[5.25594901], [6.60492241]])>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Args</th></tr>
<tr>
<td>
<b>x</b>
</td>
<td>
A <b>Tensor</b> or <b>SparseTensor</b> of type <b>float16</b>, <b>float32</b>, <b>float64</b>,
<b>int32</b>, <b>int64</b>, <b>complex64</b> or <b>complex128</b>.
</td>
</tr><tr>
<td>
<b>name</b>
</td>
<td>
A name for the operation (optional).
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Returns</th></tr>
<tr class="alt">
<td colspan="2">
A <b>Tensor</b> or <b>SparseTensor</b> of the same size, type and sparsity as <b>x</b>,
with absolute values. Note, for <b>complex64</b> or <b>complex128</b> input, the
returned <b>Tensor</b> will be of type <b>float32</b> or <b>float64</b>, respectively.
If <b>x</b> is a <b>SparseTensor</b>, returns
<b>SparseTensor(x.indices, tf.math.abs(x.values, ...), x.dense_shape)</b>
</td>
</tr>
</table>
<h3 id="__add__">__add__</h3>
```python
__add__(
y
)
The operation invoked by the Tensor.add``` operator.
This method is exposed in TensorFlow's API so that library developers can register dispatching for <a href="../../TFSimilarity/callbacks/Tensor.md## # # # ...
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Raises</th></tr>
<tr class="alt">
<td colspan="2">
<b>TypeError</b>.
</td>
</tr>
</table>
<h3 id="__div__">__div__</h3>
```python
__div__(
y
)
Divides x / y elementwise (using Python 2 division operator semantics). (deprecated)
Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Deprecated in favor of operator or tf.math.divide.
This function divides x and y, forcing Python 2 semantics. That is, if x and y are both integers then the result will be an integer. This is in contrast to Python 3, where division with / is always a float while division with // is always an integer.
Args | |
---|---|
x | Tensor numerator of real numeric type. |
y | Tensor denominator of real numeric type. |
name | A name for the operation (optional). |
Returns | |
---|---|
x / y returns the quotient of x and y. |
This function is deprecated in TF2. Prefer using the Tensor division operator, tf.divide, or tf.math.divide, which obey the Python 3 division operator semantics.
__eq__(
other
)
The operation invoked by the Tensor.eq``` operator.
Compares two tensors element-wise for equality if they are broadcast-compatible; or returns False if they are not broadcast-compatible. (Note that this behavior differs from tf.math.equal, which raises an exception if the two tensors are not broadcast-compatible.)
This method is exposed in TensorFlow's API so that library developers can register dispatching for Tensor.eq``` to allow it to handle custom composite tensors & other custom objects.
The API symbol is not intended to be called by users directly and does appear in TensorFlow's generated documentation.
Args | |
---|---|
self | The left-hand side of the == operator. |
other | The right-hand side of the == operator. |
Returns | |
---|---|
The result of the elementwise == operation, or False if the arguments are not broadcast-compatible. |
__floordiv__(
y
)
Divides x / y elementwise, rounding toward the most negative integer.
Mathematically, this is equivalent to floor(x / y). For example: floor(8.4 / 4.0) = floor(2.1) = 2.0 floor(-8.4 / 4.0) = floor(-2.1) = -3.0 This is equivalent to the '//' operator in Python 3.0 and above.
Note: x and y must have the same type, and the result will have the same type as well.
Args | |
---|---|
x | Tensor numerator of real numeric type. |
y | Tensor denominator of real numeric type. |
name | A name for the operation (optional). |
Returns | |
---|---|
x / y rounded toward -infinity. |
Raises | |
---|---|
TypeError | If the inputs are complex. |
__ge__(
y, name=None
)
Returns the truth value of (x >= y) element-wise.
NOTE: math.greater_equal supports broadcasting. More about broadcasting
x = tf.constant([5, 4, 6, 7])
y = tf.constant([5, 2, 5, 10])
tf.math.greater_equal(x, y) ==> [True, True, True, False]
x = tf.constant([5, 4, 6, 7])
y = tf.constant([5])
tf.math.greater_equal(x, y) ==> [True, False, True, True]
Args | |
---|---|
x | A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64. |
y | A Tensor. Must have the same type as x. |
name | A name for the operation (optional). |
Returns | |
---|---|
A Tensor of type bool. |
__getitem__(
slice_spec, var=None
)
Overload for Tensor.getitem.
This operation extracts the specified region from the tensor. The notation is similar to NumPy with the restriction that currently only support basic indexing. That means that using a non-scalar tensor as input is not currently allowed.
#### Notes:
- <b>tf.newaxis</b> is <b>None</b> as in NumPy.
- An implicit ellipsis is placed at the end of the <b>slice_spec</b>
- NumPy advanced indexing is currently not supported.
#### Purpose in the API:
This method is exposed in TensorFlow's API so that library developers
can register dispatching for <a href="../../TFSimilarity/callbacks/Tensor.md#__getitem__">Tensor.__getitem__```
</a> to allow it to handle
custom composite tensors & other custom objects.
The API symbol is not intended to be called by users directly and does
appear in TensorFlow's generated documentation.
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Args</th></tr>
<tr>
<td>
<b>tensor</b>
</td>
<td>
An ops.Tensor object.
</td>
</tr><tr>
<td>
<b>slice_spec</b>
</td>
<td>
The arguments to Tensor.__getitem__.
</td>
</tr><tr>
<td>
<b>var</b>
</td>
<td>
In the case of variable slice assignment, the Variable object to slice
(i.e. tensor is the read-only view of this variable).
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Returns</th></tr>
<tr class="alt">
<td colspan="2">
The appropriate slice of "tensor", based on "slice_spec".
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Raises</th></tr>
<tr>
<td>
<b>ValueError</b>
</td>
<td>
If a slice range is negative size.
</td>
</tr><tr>
<td>
<b>TypeError</b>
</td>
<td>
If the slice indices aren't int, slice, ellipsis,
tf.newaxis or scalar int32/int64 tensors.
</td>
</tr>
</table>
<h3 id="__gt__">__gt__</h3>
```python
__gt__(
y, name=None
)
Returns the truth value of (x > y) element-wise.
NOTE: math.greater supports broadcasting. More about broadcasting
x = tf.constant([5, 4, 6])
y = tf.constant([5, 2, 5])
tf.math.greater(x, y) ==> [False, True, True]
x = tf.constant([5, 4, 6])
y = tf.constant([5])
tf.math.greater(x, y) ==> [False, False, True]
Args | |
---|---|
x | A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64. |
y | A Tensor. Must have the same type as x. |
name | A name for the operation (optional). |
Returns | |
---|---|
A Tensor of type bool. |
__invert__(
name=None
)
__iter__()
__le__(
y, name=None
)
Returns the truth value of (x <= y) element-wise.
NOTE: math.less_equal supports broadcasting. More about broadcasting
x = tf.constant([5, 4, 6])
y = tf.constant([5])
tf.math.less_equal(x, y) ==> [True, True, False]
x = tf.constant([5, 4, 6])
y = tf.constant([5, 6, 6])
tf.math.less_equal(x, y) ==> [True, True, True]
Args | |
---|---|
x | A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64. |
y | A Tensor. Must have the same type as x. |
name | A name for the operation (optional). |
Returns | |
---|---|
A Tensor of type bool. |
__len__()
__lt__(
y, name=None
)
Returns the truth value of (x < y) element-wise.
NOTE: math.less supports broadcasting. More about broadcasting
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy= array([[[ 94, 100], [229, 244]], [[508, 532], [697, 730]]], dtype=int32)>
Since python >= 3.5 the @ operator is supported
(see [PEP 465](https://www.python.org/dev/peps/pep-0465/)). In TensorFlow,
it simply calls the <b>tf.matmul()</b> function, so the following lines are
equivalent:
d = a @ b @ [[10], [11]] d = tf.matmul(tf.matmul(a, b), [[10], [11]])
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Args</th></tr>
<tr>
<td>
<b>a</b>
</td>
<td>
<b>tf.Tensor</b> of type <b>float16</b>, <b>float32</b>, <b>float64</b>, <b>int32</b>,
<b>complex64</b>, <b>complex128</b> and rank > 1.
</td>
</tr><tr>
<td>
<b>b</b>
</td>
<td>
<b>tf.Tensor</b> with same type and rank as <b>a</b>.
</td>
</tr><tr>
<td>
<b>transpose_a</b>
</td>
<td>
If <b>True</b>, <b>a</b> is transposed before multiplication.
</td>
</tr><tr>
<td>
<b>transpose_b</b>
</td>
<td>
If <b>True</b>, <b>b</b> is transposed before multiplication.
</td>
</tr><tr>
<td>
<b>adjoint_a</b>
</td>
<td>
If <b>True</b>, <b>a</b> is conjugated and transposed before
multiplication.
</td>
</tr><tr>
<td>
<b>adjoint_b</b>
</td>
<td>
If <b>True</b>, <b>b</b> is conjugated and transposed before
multiplication.
</td>
</tr><tr>
<td>
<b>a_is_sparse</b>
</td>
<td>
If <b>True</b>, <b>a</b> is treated as a sparse matrix. Notice, this
**does not support <b>tf.sparse.SparseTensor</b>**, it just makes optimizations
that assume most values in <b>a</b> are zero.
See <b>tf.sparse.sparse_dense_matmul</b>
for some support for <b>tf.sparse.SparseTensor</b> multiplication.
</td>
</tr><tr>
<td>
<b>b_is_sparse</b>
</td>
<td>
If <b>True</b>, <b>b</b> is treated as a sparse matrix. Notice, this
**does not support <b>tf.sparse.SparseTensor</b>**, it just makes optimizations
that assume most values in <b>a</b> are zero.
See <b>tf.sparse.sparse_dense_matmul</b>
for some support for <b>tf.sparse.SparseTensor</b> multiplication.
</td>
</tr><tr>
<td>
<b>output_type</b>
</td>
<td>
The output datatype if needed. Defaults to None in which case
the output_type is the same as input type. Currently only works when input
tensors are type (u)int8 and output_type can be int32.
</td>
</tr><tr>
<td>
<b>name</b>
</td>
<td>
Name for the operation (optional).
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Returns</th></tr>
<tr class="alt">
<td colspan="2">
A <b>tf.Tensor</b> of the same type as <b>a</b> and <b>b</b> where each inner-most matrix
is the product of the corresponding matrices in <b>a</b> and <b>b</b>, e.g. if all
transpose or adjoint attributes are <b>False</b>:
<b>output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j])</b>,
for all indices <b>i</b>, <b>j</b>.
</td>
</tr>
<tr>
<td>
<b>Note</b>
</td>
<td>
This is matrix product, not element-wise product.
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Raises</th></tr>
<tr>
<td>
<b>ValueError</b>
</td>
<td>
If <b>transpose_a</b> and <b>adjoint_a</b>, or <b>transpose_b</b> and
<b>adjoint_b</b> are both set to <b>True</b>.
</td>
</tr><tr>
<td>
<b>TypeError</b>
</td>
<td>
If output_type is specified but the types of <b>a</b>, <b>b</b> and
<b>output_type</b> is not (u)int8, (u)int8 and int32.
</td>
</tr>
</table>
<h3 id="__mod__">__mod__</h3>
```python
__mod__(
y
)
Returns element-wise remainder of division. When x < 0 xor y < 0 is
true, this follows Python semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.
NOTE: math.floormod supports broadcasting. More about broadcasting
Args | |
---|---|
x | A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, bfloat16, half, float32, float64. |
y | A Tensor. Must have the same type as x. |
name | A name for the operation (optional). |
Returns | |
---|---|
A Tensor. Has the same type as x. |
__mul__(
y
)
Dispatches cwise mul for "DenseDense" and "DenseSparse".
__ne__(
other
)
The operation invoked by the Tensor.ne``` operator.
Compares two tensors element-wise for inequality if they are broadcast-compatible; or returns True if they are not broadcast-compatible. (Note that this behavior differs from tf.math.not_equal, which raises an exception if the two tensors are not broadcast-compatible.)
This method is exposed in TensorFlow's API so that library developers can register dispatching for <a href="../../TFSimilarity/callbacks/Tensor.md## [[256, 65536], [9, 27]]
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Args</th></tr>
<tr>
<td>
<b>x</b>
</td>
<td>
A <b>Tensor</b> of type <b>float16</b>, <b>float32</b>, <b>float64</b>, <b>int32</b>, <b>int64</b>,
<b>complex64</b>, or <b>complex128</b>.
</td>
</tr><tr>
<td>
<b>y</b>
</td>
<td>
A <b>Tensor</b> of type <b>float16</b>, <b>float32</b>, <b>float64</b>, <b>int32</b>, <b>int64</b>,
<b>complex64</b>, or <b>complex128</b>.
</td>
</tr><tr>
<td>
<b>name</b>
</td>
<td>
A name for the operation (optional).
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Returns</th></tr>
<tr class="alt">
<td colspan="2">
A <b>Tensor</b>.
</td>
</tr>
</table>
<h3 id="__radd__">__radd__</h3>
```python
__radd__(
x
)
The operation invoked by the Tensor.add``` operator.
This method is exposed in TensorFlow's API so that library developers can register dispatching for <a href="../../TFSimilarity/callbacks/Tensor.md## # # # # # # [[256, 65536], [9, 27]]
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Args</th></tr>
<tr>
<td>
<b>x</b>
</td>
<td>
A <b>Tensor</b> of type <b>float16</b>, <b>float32</b>, <b>float64</b>, <b>int32</b>, <b>int64</b>,
<b>complex64</b>, or <b>complex128</b>.
</td>
</tr><tr>
<td>
<b>y</b>
</td>
<td>
A <b>Tensor</b> of type <b>float16</b>, <b>float32</b>, <b>float64</b>, <b>int32</b>, <b>int64</b>,
<b>complex64</b>, or <b>complex128</b>.
</td>
</tr><tr>
<td>
<b>name</b>
</td>
<td>
A name for the operation (optional).
</td>
</tr>
</table>
<!-- Tabular view -->
<table class="responsive fixed orange">
<colgroup><col width="214px"><col></colgroup>
<tr><th colspan="2">Returns</th></tr>
<tr class="alt">
<td colspan="2">
A <b>Tensor</b>.
</td>
</tr>
</table>
<h3 id="__rsub__">__rsub__</h3>
```python
__rsub__(
x
)
Returns x - y element-wise.
NOTE: tf.subtract supports broadcasting. More about broadcasting
Both input and output have a range (-inf, inf).
Example usages below.
Subtract operation between an array and a scalar:
>>> x = [1, 2, 3, 4, 5]
>>> y = 1
>>> tf.subtract(x, y)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
>>> tf.subtract(y, x)
<tf.Tensor: shape=(5,), dtype=int32,
numpy=array([ 0, -1, -2, -3, -4], dtype=int32)>
Note that binary - operator can be used instead:
>>> x = tf.convert_to_tensor([1, 2, 3, 4, 5])
>>> y = tf.convert_to_tensor(1)
>>> x - y
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
Subtract operation between an array and a tensor of same shape:
>>> x = [1, 2, 3, 4, 5]
>>> y = tf.constant([5, 4, 3, 2, 1])
>>> tf.subtract(y, x)
<tf.Tensor: shape=(5,), dtype=int32,
numpy=array([ 4, 2, 0, -2, -4], dtype=int32)>
Warning: If one of the inputs (x or y) is a tensor and the other is a non-tensor, the non-tensor input will adopt (or get casted to) the data type of the tensor input. This can potentially cause unwanted overflow or underflow conversion.
For example,
>>> x = tf.constant([1, 2], dtype=tf.int8)
>>> y = [2**8 + 1, 2**8 + 2]
>>> tf.subtract(x, y)
<tf.Tensor: shape=(2,), dtype=int8, numpy=array([0, 0], dtype=int8)>
When subtracting two input values of different shapes, tf.subtract follows the
- general broadcasting rules . The two input array shapes are compared element-wise. Starting with the trailing dimensions, the two dimensions either have to be equal or one of them needs to be 1.
For example,
>>> x = np.ones(6).reshape(2, 3, 1)
>>> y = np.ones(6).reshape(2, 1, 3)
>>> tf.subtract(x, y)
<tf.Tensor: shape=(2, 3, 3), dtype=float64, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]])>
Example with inputs of different dimensions:
>>> x = np.ones(6).reshape(2, 3, 1)
>>> y = np.ones(6).reshape(1, 6)
>>> tf.subtract(x, y)
<tf.Tensor: shape=(2, 3, 6), dtype=float64, numpy=
array([[[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]]])>
Args | |
---|---|
x | A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128, uint32, uint64. |
y | A Tensor. Must have the same type as x. |
name | A name for the operation (optional). |
Returns | |
---|---|
A Tensor. Has the same type as x. |
__rtruediv__(
x
)
Divides x / y elementwise (using Python 3 division operator semantics).
NOTE: Prefer using the Tensor operator or tf.divide which obey Python division operator semantics.
This function forces Python 3 division operator semantics where all integer arguments are cast to floating types first. This op is generated by normal x / y division in Python 3 and in Python 2.7 with from future import division. If you want integer division that rounds down, use x // y or tf.math.floordiv.
x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).
Args | |
---|---|
x | Tensor numerator of numeric type. |
y | Tensor denominator of numeric type. |
name | A name for the operation (optional). |
Returns | |
---|---|
x / y evaluated in floating point. |
Raises | |
---|---|
TypeError | If x and y have different dtypes. |
__rxor__(
x
)
__sub__(
y
)
Returns x - y element-wise.
NOTE: tf.subtract supports broadcasting. More about broadcasting
Both input and output have a range (-inf, inf).
Example usages below.
Subtract operation between an array and a scalar:
>>> x = [1, 2, 3, 4, 5]
>>> y = 1
>>> tf.subtract(x, y)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
>>> tf.subtract(y, x)
<tf.Tensor: shape=(5,), dtype=int32,
numpy=array([ 0, -1, -2, -3, -4], dtype=int32)>
Note that binary - operator can be used instead:
>>> x = tf.convert_to_tensor([1, 2, 3, 4, 5])
>>> y = tf.convert_to_tensor(1)
>>> x - y
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
Subtract operation between an array and a tensor of same shape:
>>> x = [1, 2, 3, 4, 5]
>>> y = tf.constant([5, 4, 3, 2, 1])
>>> tf.subtract(y, x)
<tf.Tensor: shape=(5,), dtype=int32,
numpy=array([ 4, 2, 0, -2, -4], dtype=int32)>
Warning: If one of the inputs (x or y) is a tensor and the other is a non-tensor, the non-tensor input will adopt (or get casted to) the data type of the tensor input. This can potentially cause unwanted overflow or underflow conversion.
For example,
>>> x = tf.constant([1, 2], dtype=tf.int8)
>>> y = [2**8 + 1, 2**8 + 2]
>>> tf.subtract(x, y)
<tf.Tensor: shape=(2,), dtype=int8, numpy=array([0, 0], dtype=int8)>
When subtracting two input values of different shapes, tf.subtract follows the
- general broadcasting rules . The two input array shapes are compared element-wise. Starting with the trailing dimensions, the two dimensions either have to be equal or one of them needs to be 1.
For example,
>>> x = np.ones(6).reshape(2, 3, 1)
>>> y = np.ones(6).reshape(2, 1, 3)
>>> tf.subtract(x, y)
<tf.Tensor: shape=(2, 3, 3), dtype=float64, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]])>
Example with inputs of different dimensions:
>>> x = np.ones(6).reshape(2, 3, 1)
>>> y = np.ones(6).reshape(1, 6)
>>> tf.subtract(x, y)
<tf.Tensor: shape=(2, 3, 6), dtype=float64, numpy=
array([[[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]]])>
Args | |
---|---|
x | A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128, uint32, uint64. |
y | A Tensor. Must have the same type as x. |
name | A name for the operation (optional). |
Returns | |
---|---|
A Tensor. Has the same type as x. |
__truediv__(
y
)
Divides x / y elementwise (using Python 3 division operator semantics).
NOTE: Prefer using the Tensor operator or tf.divide which obey Python division operator semantics.
This function forces Python 3 division operator semantics where all integer arguments are cast to floating types first. This op is generated by normal x / y division in Python 3 and in Python 2.7 with from future import division. If you want integer division that rounds down, use x // y or tf.math.floordiv.
x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).
Args | |
---|---|
x | Tensor numerator of numeric type. |
y | Tensor denominator of numeric type. |
name | A name for the operation (optional). |
Returns | |
---|---|
x / y evaluated in floating point. |
Raises | |
---|---|
TypeError | If x and y have different dtypes. |
__xor__(
y
)