TensorFlow:如何对 tf.Variables 的列表求和?

TensorFlow: how can I sum a list of tf.Variables?

我有一个 tf.Variable 类型的 3D 数组。 tf.reduce_sum 仅适用于单个张量。我尝试过:

tf.reduce_sum([tf.reduce_sum(mat) for mat in var_3Dlist])

...但是 tf.reduce_sum() 需要张量而不是列表。我可以以某种方式将它转换为张量,还是有另一种更好的方法来做到这一点?我在 API.

中没有找到任何内容

用于将列表中的值加在一起的 tf.reduce_sum() op works on 3-D tensors and variables (and in general any rank or tensor or variable). However, if you have a list of 2-D tensors (or variables), you should use the tf.add_n() 操作:

var_3Dlist = ...  # List of 3-D variables.
sum_list = [tf.reduce_sum(mat) for mat in var_3Dlist]
sum = tf.add_n(sum_list)