python - tf.reduce_sum 对 axis = -1 做了什么?

标签 python tensorflow

我不明白为什么下面代码的输出是[7 56]

import tensorflow as tf

x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, -1)  # [ 9 18 36]

with tf.Session() as sess:
  output_a = sess.run(a)
  print(output_a)

我知道按行添加已经完成。但是有人可以阐明为什么 reduce_sum 函数中的 -1 被视为对一行中的所有值求和吗?

最佳答案

-1表示最后一个轴;因为你有一个 2 阶张量,所以最后一个轴是第二个轴,也就是沿着行; tf.reduce_sumaxis=-1 将因此减少(求和)第二个维度。

关于python - tf.reduce_sum 对 axis = -1 做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53486527/

相关文章:

python - 不是 "\u": How to Unescape Unicode in JSON?

python - 从出队的一批值、索引、形状创建 SparseTensor

python - 为什么 Keras ImageDataGenerator 会抛出内存错误?

python - Tensorflow 图像分类脚本

python - QGraphicsPathItem .setPen() 影响文本样式

python - Django 在 pop() 上请求 QueryDict 错误

python - 我如何选择将数据放入模型或不对其进行处理?

python - Alpine docker : installing pandas/numpy

tensorflow - Keras错误 “You must feed a value for placeholder tensor ' bidirectional_1/keras_learning_phase'与dtype bool”

tensorflow - 在 Pytorch 中计算空间轴上的方差