cha*_*e09 2 python matrix tensorflow
该tf.reduce_mean()函数以轴参数中引用的索引的方式对数组的元素求和。
在下面的代码中:
import tensorflow as tf
x = tf.Variable([1, 2, 3])
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
Run Code Online (Sandbox Code Playgroud)
所以对于线
print(sess.run(tf.reduce_sum(x)))
Run Code Online (Sandbox Code Playgroud)
输出为:6
为了生成相同的输出,我需要对所有元素求和以减少列数的方式。所以我需要设置axis = 1对吗?
print(sess.run(tf.reduce_sum(x, 1)))
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误:
ValueError:无效的归约维数1用于1维的输入
但是,如果将axis设置为0,则得到6。这是为什么?
您得到的错误是ValueError: Invalid reduction dimension 1 for input with 1 dimensions。这几乎意味着如果您无法缩小一维张量的维数。
对于N x M张量,设置轴= 0将返回1xM张量,而设置轴= 1将返回Nx1张量。考虑来自tensorflow文档的以下示例:
# 'x' is [[1, 1, 1]
# [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6589 次 |
| 最近记录: |