Tensor代表操作的输出结果 ,Operation表示一个操作,
c = tf.matmul(a, b)
c是Tensor对象,还是Operation对象?
这里c应该是Tensor对象。
有些操作没有返回值,此时该对象就是一个Operation对象。如
optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(self.loss)
optimizer是一个Operation对象,因为minimize函数没有输出值。
附api:
-
Class
Operation
Defined in tensorflow/python/framework/ops.py
.
Represents a graph node that performs computation on tensors.
An Operation
is a node in a TensorFlow Graph
that takes zero or more Tensor
objects as input, and produces zero or more Tensor
objects as output. Objects of type Operation
are created by calling a Python op constructor (such as tf.matmul
) or tf.Graph.create_op
.
For example c = tf.matmul(a, b)
creates an Operation
of type "MatMul" that takes tensors a
and b
as input, and produces c
as output.
After the graph has been launched in a session, an Operation
can be executed by passing it to tf.Session.run
. op.run()
is a shortcut for calling tf.get_default_session().run(op)
.
Operation
类
表示图中的节点,实现对Tensor的计算。
一个Operation对象是Tensorflow计算图中的一个节点,接收0或多个Tensor对象作输入,输出0或多个Tensor对象。
-
Class
Tensor
Defined in tensorflow/python/framework/ops.py
.
Represents one of the outputs of an Operation
..
Operation可以有多个输出,Tensor表示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.Session
.
This class has two primary purposes:
-
A
Tensor
can be passed as an input to anotherOperation
. This builds a dataflow connection between operations, which enables TensorFlow to execute an entireGraph
that represents a large, multi-step computation. -
After the graph has been launched in a session, the value of the
Tensor
can be computed by passing it totf.Session.run
.t.eval()
is a shortcut for callingtf.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:
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)