tf.tuple()用于组合多个张量输入

版权声明:本文为博主原创文章,保留著作权,未经博主允许不得转载。 https://blog.csdn.net/LoseInVain/article/details/81704072

tf.tuple()用于组合多个张量输入组成的列表[tensor1,tensor2,...],然后返回一个计算过后的张量列表[cal_tensor1,cal_tensor2,...],这点和tf.group()是不同的,API手册如:

tf.tuple(
    tensors,
    name=None,
    control_inputs=None
)

ops = tf.tuple([tensor1,tensor2,...],control_inputs=c_ops)
其中tensors是由多个tensor组成的列表,其中的control_inputs是添加额外的控制输入,添加的输入c_ops必须在整个ops完成之前得到执行,但是c_ops的输出是不会返回的。

API上描述,这个可以作为提供一种并行处理的机制,所有的输入的tensor可以并行计算,但是所有tensor的计算出来的值将会以tuple的形式返回,并且这个只能在并行计算完成之后得到。(This can be used as a “join” mechanism for parallel computations: all the argument tensors can be computed in parallel, but the values of any tensor returned by tuple are only available after all the parallel computations are done.)

使用例子:

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)

输出

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

可以和tf.group()用于组合多个操作的例子进行对比。

猜你喜欢

转载自blog.csdn.net/LoseInVain/article/details/81704072