Search code examples
pythonnumpytensorflowreshapemnist

How do I use tf.reshape()?


import tensorflow as tf    
import random    
import numpy as np    

x = tf.placeholder('float')   
x = tf.reshape(x, [-1,28,28,1])

with tf.Session() as sess:
    x1 = np.asarray([random.uniform(0,1) for i in range(784)])
    result = sess.run(x, feed_dict={x: x1})
    print(result)

I had some problems using mnist data on reshaping, but this question is simplified version of my problem... Why actually isn't this code working?

It shows

"ValueError: Cannot feed value of shape (784,) for Tensor 'Reshape:0', which has shape '(?, 28, 28, 1)' ".

How could I solve it?


Solution

  • After you reassign, x is a tensor with shape [-1,28,28,1] and as error says, you cannot shape (784,) to (?, 28, 28, 1). You can use a different variable name:

    import tensorflow as tf
    import random
    import numpy as np
    
    x = tf.placeholder('float')
    y = tf.reshape(x, [-1,28,28,1])
    
    with tf.Session() as sess:
        x1 = np.asarray([random.uniform(0,1) for i in range(784)])
        result = sess.run(y, feed_dict={x: x1})
        print(result)