首页 文章

我如何使用tf.reshape()?

提问于
浏览
2
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)

我在重塑时使用mnist数据时遇到了一些问题,但这个问题是我的问题的简化版本......为什么这个代码实际上不起作用?

表明

“ValueError:无法为Tensor'重塑:0'提供形状值(784,),其形状为'(?,28,28,1)'” .

我该怎么解决?

1 回答

  • 2

    重新分配后,x是一个形状为 [-1,28,28,1] 的张量,如错误所示,您无法将 (784,) 整形为 (?, 28, 28, 1) . 您可以使用其他变量名称:

    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)
    

相关问题