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

标签 python tensorflow

我有一个 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 适用于 3-D 张量和变量(通常是任何秩或张量或变量)。但是,如果您有二维张量(或变量)的列表,则应使用 tf.add_n() op 将列表中的值加在一起:

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

关于python - TensorFlow:如何对 tf.Variables 列表求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958139/

相关文章:

python - 有没有办法通过 zipimport 使用已编译的 C 模块?

python - Ubuntu 13.04 上的 PySide 导入错误

python - 元组索引超出 LSTM 神经网络的范围。 Python、Keras 和 Tensorflow

python - Keras 数字数据集 : expected conv2d_input to have 4 dimensions, 中出现错误,但获得形状为 (60000, 28, 28) 的数组

tensorflow - 当前一层中 mask_zero=True 时,由于连接层,Keras 图像字幕模型无法编译

Python、Keras - 张量 u'dense_2_target : Cannot feed value of shape (10, 的 ValueError : 0', which has shape ' (? 70, 1025)'

python - 使用装饰器管理函数内部的属性

Python 和 fibonnaci [列表] 生成器

python - 如何获取此嵌套字典中的最小非零值

python - 返回 Keras 中 EarlyStopping 回调的纪元数