python中张量切片中的值之和

python中张量切片中的值之和,python,numpy,tensorflow,Python,Numpy,Tensorflow,我在python/tensorflow中工作。我有一个张量x(4维),想求张量切片上的和 np.sum(x[:,:,:,0]) 我得到了错误 ValueError: setting an array element with a sequence. 我做错了什么?假设您使用的是TF 1.x,您不能对TF张量(在TF 1.x中)执行简单的numpy操作。相反,请执行以下操作 import tensorflow as tf x = tf.random_normal(shape=[10, 5, 3

我在python/tensorflow中工作。我有一个张量x(4维),想求张量切片上的和

np.sum(x[:,:,:,0])
我得到了错误

ValueError: setting an array element with a sequence.

我做错了什么?

假设您使用的是TF 1.x,您不能对TF张量(在TF 1.x中)执行简单的numpy操作。相反,请执行以下操作

import tensorflow as tf
x = tf.random_normal(shape=[10, 5, 3, 2])
res = tf.reduce_sum(tf.gather(x, 0, axis=-1))

with tf.Session() as sess:
  r = sess.run(res)

假设您使用的是TF1.x,就不能对TF张量(在TF1.x中)执行简单的numpy操作。相反,请执行以下操作

import tensorflow as tf
x = tf.random_normal(shape=[10, 5, 3, 2])
res = tf.reduce_sum(tf.gather(x, 0, axis=-1))

with tf.Session() as sess:
  r = sess.run(res)

当我使用x=
np.random.random\u sample((5,3,2,2))
和其他数字生成np数组时,这段代码对我来说效果很好。
x
是什么样子的?张量x是卷积神经网络的输出。告诉我们关于
x
shape
dtype
。x={tensor}张量(“concatenate_16/concat:0”,shape=(?,?,64),dtype=float32)的情况?是因为我使用的数据集中的所有图像的分辨率都不同。在训练过程中,我在计算损失函数时需要这个求和运算。当我写K.sum(x[:,:,:,0])时,我得到一个shape()和dtype float32的张量,这可能会有正确的值。但是我想要一个数字而不是张量,因为我想用它进行计算……当我使用x=
np.random.random\u sample((5,3,2,2))
和其他数字生成np数组时,这段代码对我来说很好用。
x
是什么样子的?张量x是卷积神经网络的输出。告诉我们关于
x
shape
dtype
。x={tensor}张量(“concatenate_16/concat:0”,shape=(?,?,64),dtype=float32)的情况?是因为我使用的数据集中的所有图像的分辨率都不同。在训练过程中,我在计算损失函数时需要这个求和运算。当我写K.sum(x[:,:,:,0])时,我得到一个shape()和dtype float32的张量,这可能会有正确的值。但是我想要一个数字而不是张量,因为我想用它来计算。。。