如何计算TensorFlow元组?

2024-04-16 17:08:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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')

在这个例子中,我得到以下错误:

^{pr2}$

当我传递张量时,它起作用,但当我传递名字时就不行了。 为什么我不能把名字传给你?在


Tags: runnameimportsessiontftensorflowaswith
2条回答

@E逯net4 will behave的答案很好。但首先,你应该知道怎么做tf.元组工作。 就像在tf.元组文件上说

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 :

^{pr2}$

这里ops = tf.tuple([tensor1,tensor2,...],control_inputs=c_ops)

输出显示:

[array([11], dtype=int32), array([30], dtype=int32), array([0.83333333])]`

TensorFlow中的元组不是张量,而是张量的列表,因此不能通过图中的操作作为一个整体获取。tf.tuple将创建一些分组和依赖关系控制操作(initial_state/group_depsinitial_state/control_dependency和{}),但仅此而已。在

由于state是一个列表,因此它是Session#run的有效fetches参数。还可以从每个元组元素构建一个操作名列表,并使用该列表。在

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)
# ]

相关问题 更多 >