1

I'm working in python/tensorflow. I have a tensor x (4 dimensions) and want to take the sum over a slice of the tensor

np.sum(x[:,:,:,0])

I get the error

ValueError: setting an array element with a sequence.

What am I doing wrong?

SteC
  • 841
  • 1
  • 6
  • 8
  • This code works fine for me when I generate the np array using x = `np.random.random_sample((5,3,2,2))` and other numbers. What does `x` look like? – Rahul P Dec 23 '19 at 14:50
  • The tensor x is the output of a convolutional neural network. – SteC Dec 23 '19 at 14:58
  • Tell us about `x`, `shape` and `dtype`. – hpaulj Dec 23 '19 at 17:57
  • x = {Tensor} Tensor("concatenate_16/concat:0", shape=(?,?,?,64),dtype=float32) The ? are because the resolution of the images is different for all the images in the dataset I am using. I need this summation operation in the evaluation of the loss function during training. – SteC Dec 24 '19 at 08:00
  • When I write K.sum(x[:,:,:,0]), I get a tensor of shape () and dtype float32, probably this will have the correct value. But I want a number instead of a tensor because I want to do calculations with it... – SteC Dec 24 '19 at 08:20

1 Answers1

0

Assuming you're using TF 1.x, you can't do simple numpy operations on TF tensors (in TF 1.x). Instead do the following.

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)
thushv89
  • 10,865
  • 1
  • 26
  • 39