Modifying a tensor using a placeholder value

I want to change the tensor using the [int, -1] notation (for example, to smooth the image). But I do not know the first dimension ahead of time. One use case is to train in a large batch, then evaluate for a smaller batch.

Why does this lead to the following error: got list containing Tensors of type '_Message' ?

 import tensorflow as tf import numpy as np x = tf.placeholder(tf.float32, shape=[None, 28, 28]) batch_size = tf.placeholder(tf.int32) def reshape(_batch_size): return tf.reshape(x, [_batch_size, -1]) reshaped = reshape(batch_size) with tf.Session() as sess: sess.run([reshaped], feed_dict={x: np.random.rand(100, 28, 28), batch_size: 100}) # Evaluate sess.run([reshaped], feed_dict={x: np.random.rand(8, 28, 28), batch_size: 8}) 

Note: when I have a change outside the function, it seems to work, but I have very large models that I use several times, so I need to save them in the function and pass dim with an argument.

+7
tensorflow
source share
2 answers

To do this, replace the function:

 def reshape(_batch_size): return tf.reshape(x, [_batch_size, -1]) 

... with function:

 def reshape(_batch_size): return tf.reshape(x, tf.pack([_batch_size, -1])) 

The cause of the error is that tf.reshape() expects a value that can be converted to tf.Tensor as the second argument. TensorFlow will automatically convert the list of Python numbers to tf.Tensor , but will not automatically convert a mixed list of numbers and tensors (e.g. tf.placeholder() ) - instead, a slightly unintuitive error message appears that you saw.

tf.pack() op displays a list of objects convertible to a tensor and converts each element individually, so it can handle a combination of placeholder and integer.

+9
source share

hi the whole problem is with the Keras version. I tried above all without success. Uninstall Keras and install via pip. It worked for me.

I ran into this error with Keras 1.0.2 and decided with Keras 1.2.0

Hope this helps. Thanks you

0
source share

All Articles