Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 246
voice Avatar asked Jul 20 '18 13:07

voice


People also ask

How do you flatten a tensor in tf?

To flatten the tensor, we're going to use the TensorFlow reshape operation. So tf. reshape, we pass in our tensor currently represented by tf_initial_tensor_constant, and then the shape that we're going to give it is a -1 inside of a Python list.


1 Answers

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)
like image 138
Dinesh Avatar answered Oct 21 '22 18:10

Dinesh