Reputation: 55
import tensorflow as tf
a = tf.zeros([10])
b = tf.zeros([10])
state = tf.tuple([a, b], name='initial_state')
with tf.Session() as sess:
s = sess.run('initial_state:0')
I get the following error with this example:
ValueError: Fetch argument 'initial_state' cannot be interpreted as a Tensor.
("The name 'initial_state' refers to an Operation not in the graph.")`
It works when I just pass the tensor, but not when I pass the name. Why can't I pass the name in this case?
Upvotes: 0
Views: 2360
Reputation: 152
The answer of @E_net4 will behave is nice. But First, you should know how tf.tuple work. As in the tf.tuple documentation says
tf.tuple( tensors, control_inputs=None, name=None )
You have to sometime follow the instruction. But it hard to understand there as no example shown so see mine :
import tensorflow as tf
a = tf.Variable([5])
b = tf.Variable([6])
c = a+b
d = a*b
e = a/b
ops = tf.tuple([c,d,e])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ee = sess.run(ops)
print(ee)
Here ops = tf.tuple([tensor1,tensor2,...],control_inputs=c_ops)
The output shows :
[array([11], dtype=int32), array([30], dtype=int32), array([0.83333333])]`
Upvotes: 1
Reputation: 30042
Tuples in TensorFlow are not tensors, but a list of tensors, and so cannot be fetched as a whole through an operation in the graph. tf.tuple
will create a few grouping and dependency control operations (initial_state/group_deps
, initial_state/control_dependency
and initial_state/control_dependency_1
in this case), but that's about it.
Since state
is a list, it is a valid fetches
argument to Session#run
. One can also build a list of operation names from each tuple element and use that instead.
s = sess.run(['zeros:0', 'zeros_1:0'])
# [
# array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32),
# array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
# ]
Upvotes: 1