5
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?

pevik
  • 4,523
  • 3
  • 33
  • 44
voice
  • 93
  • 1
  • 1
  • 7

2 Answers2

4

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)
pevik
  • 4,523
  • 3
  • 33
  • 44
Dinesh
  • 1,555
  • 1
  • 16
  • 18
0

Conceptually You get error here because when you are using sess.run(x, feed_dict{x:x1}). This is trying to feed and reshape same variable. This creates a problem in run time. Thus you cannot do this using a single variable.

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)

In tensorflow, variables are place holders. So, x will hold floating point values, and another variable say y will hold values in shape [-1,28,28,1].

If same variable name is used then it has to act as placeholder for two things. This is not possible.

pevik
  • 4,523
  • 3
  • 33
  • 44
shantanu pathak
  • 2,018
  • 19
  • 26