Type Tensor
Namespace tensorflow
Parent PythonObjectContainer
Interfaces _TensorLike, ITensor
Represents one of the outputs of an `Operation`. A `Tensor` is a symbolic handle to one of the outputs of an
`Operation`. It does not hold the values of that operation's output,
but instead provides a means of computing those values in a
TensorFlow `tf.compat.v1.Session`. This class has two primary purposes: 1. A `Tensor` can be passed as an input to another `Operation`.
This builds a dataflow connection between operations, which
enables TensorFlow to execute an entire `Graph` that represents a
large, multi-step computation. 2. After the graph has been launched in a session, the value of the
`Tensor` can be computed by passing it to
tf.Session.run
.
`t.eval()` is a shortcut for calling
`tf.compat.v1.get_default_session().run(t)`. In the following example, `c`, `d`, and `e` are symbolic `Tensor`
objects, whereas `result` is a numpy array that stores a concrete
value:
Show Example
# Build a dataflow graph. c = tf.constant([[1.0, 2.0], [3.0, 4.0]]) d = tf.constant([[1.0, 1.0], [0.0, 1.0]]) e = tf.matmul(c, d) # Construct a `Session` to execute the graph. sess = tf.compat.v1.Session() # Execute the graph and store the value that `e` represents in `result`. result = sess.run(e)
Methods
- __array__
- __array___dyn
- __bool__
- __bool___dyn
- __copy__
- __copy___dyn
- __iter__
- __iter___dyn
- __len__
- __len___dyn
- __nonzero__
- __nonzero___dyn
- eval
- eval
- eval
- eval
- eval
- eval
- eval_dyn
- experimental_ref_dyn
- get_shape_
- get_shape__dyn
- numpy
- numpy_dyn
- set_shape_
- set_shape__dyn
Properties
Public instance methods
void __array__()
object __array___dyn()
bool __bool__()
Returns True if this shape contains non-zero information.
object __bool___dyn()
Returns True if this shape contains non-zero information.
object __copy__()
Create a copy of this subgraph. Note that this class is a "view", copying it only create another view and
does not copy the underlying part of the
tf.Graph
.
Returns
-
object
- A new identical instance of the original subgraph view.
object __copy___dyn()
Create a copy of this subgraph. Note that this class is a "view", copying it only create another view and
does not copy the underlying part of the
tf.Graph
.
Returns
-
object
- A new identical instance of the original subgraph view.
object __iter__()
Returns `self.dims` if the rank is known, otherwise raises ValueError.
object __iter___dyn()
Returns `self.dims` if the rank is known, otherwise raises ValueError.
Nullable<int> __len__()
Returns the rank of this shape, or raises ValueError if unspecified.
object __len___dyn()
Returns the rank of this shape, or raises ValueError if unspecified.
bool __nonzero__()
Dummy method to prevent a tensor from being used as a Python `bool`. This is the Python 2.x counterpart to `__bool__()` above.
object __nonzero___dyn()
Dummy method to prevent a tensor from being used as a Python `bool`. This is the Python 2.x counterpart to `__bool__()` above.
object eval(IDictionary<object, object> feed_dict, IDictionary<object, object> session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
IDictionary<object, object>
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
IDictionary<object, object>
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object eval(IDictionary<object, object> feed_dict, BaseSession session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
IDictionary<object, object>
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
BaseSession
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object eval(IDictionary<object, object> feed_dict, _MonitoredSession session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
IDictionary<object, object>
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
_MonitoredSession
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object eval(BaseSession feed_dict, IDictionary<object, object> session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
BaseSession
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
IDictionary<object, object>
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object eval(BaseSession feed_dict, BaseSession session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
BaseSession
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
BaseSession
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object eval(BaseSession feed_dict, _MonitoredSession session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
BaseSession
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
_MonitoredSession
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object eval_dyn(object feed_dict, object session)
Evaluates this tensor in a `Session`. 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.
Parameters
-
object
feed_dict - A dictionary that maps `Tensor` objects to feed values. See
tf.Session.run
for a description of the valid feed values. -
object
session - (Optional.) The `Session` to be used to evaluate this tensor. If none, the default session will be used.
Returns
-
object
- A numpy array corresponding to the value of this tensor.
object experimental_ref_dyn()
Returns a hashable reference object to this Tensor. Warning: Experimental API that could be changed or removed. The primary usecase 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.
Instead, we can use `tensor.experimental_ref()`.
Also, the reference object provides `.deref()` function that returns the
original Tensor.
Show Example
import tensorflow as tf x = tf.constant(5) y = tf.constant(10) z = tf.constant(10) # The followings will raise an exception starting 2.0 # TypeError: Tensor is unhashable if Tensor equality is enabled. tensor_set = {x, y, z} tensor_dict = {x: 'five', y: 'ten', z: 'ten'}
TensorShape get_shape_()
object get_shape__dyn()
_ArrayLike numpy()
Gets current value of the Tensor. Only works in eager mode.
For scalar tensors use scalar
object numpy_dyn()
Gets current value of the Tensor. Only works in eager mode.
void set_shape_(TensorShape shape)
object set_shape__dyn(object shape)
Public properties
object device get;
The name of the device on which this tensor will be produced, or None.
object device_dyn get;
The name of the device on which this tensor will be produced, or None.
object dtype get;
The `DType` of elements in this tensor.
object dtype_dyn get;
The `DType` of elements in this tensor.
object graph get;
The `Graph` that contains this tensor.
object graph_dyn get;
The `Graph` that contains this tensor.
Tensor Item get;
Gets tensor slice
object name get;
The string name of this tensor.
object name_dyn get;
The string name of this tensor.
object op get;
The `Operation` that produces this tensor as an output.
object op_dyn get;
The `Operation` that produces this tensor as an output.
ISet<string> OVERLOADABLE_OPERATORS get; set;
object OVERLOADABLE_OPERATORS_dyn get; set;
object PythonObject get;
TensorShape shape get;
Returns the `TensorShape` that represents the shape of this tensor. The shape is computed using shape inference functions that are
registered in the Op for each `Operation`. See
tf.TensorShape
for more details of what a shape represents. The inferred shape of a tensor is used to provide shape
information without having to launch the graph in a session. This
can be used for debugging, and providing early error messages. For
example:
In some cases, the inferred shape may have unknown dimensions. If
the caller has additional information about the values of these
dimensions, `Tensor.set_shape()` can be used to augment the
inferred shape.
Show Example
c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) print(c.shape) ==> TensorShape([Dimension(2), Dimension(3)]) d = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]]) print(d.shape) ==> TensorShape([Dimension(4), Dimension(2)]) # Raises a ValueError, because `c` and `d` do not have compatible # inner dimensions. e = tf.matmul(c, d) f = tf.matmul(c, d, transpose_a=True, transpose_b=True) print(f.shape) ==> TensorShape([Dimension(3), Dimension(4)])
object shape_dyn get;
Returns the `TensorShape` that represents the shape of this tensor. The shape is computed using shape inference functions that are
registered in the Op for each `Operation`. See
tf.TensorShape
for more details of what a shape represents. The inferred shape of a tensor is used to provide shape
information without having to launch the graph in a session. This
can be used for debugging, and providing early error messages. For
example:
In some cases, the inferred shape may have unknown dimensions. If
the caller has additional information about the values of these
dimensions, `Tensor.set_shape()` can be used to augment the
inferred shape.
Show Example
c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) print(c.shape) ==> TensorShape([Dimension(2), Dimension(3)]) d = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]]) print(d.shape) ==> TensorShape([Dimension(4), Dimension(2)]) # Raises a ValueError, because `c` and `d` do not have compatible # inner dimensions. e = tf.matmul(c, d) f = tf.matmul(c, d, transpose_a=True, transpose_b=True) print(f.shape) ==> TensorShape([Dimension(3), Dimension(4)])
int value_index get;
The index of this tensor in the outputs of its `Operation`.
object value_index_dyn get;
The index of this tensor in the outputs of its `Operation`.