Python 如何修复:“;预期形状TensorShape([Dimension(None),Dimension(785)])获得了带有形状TensorShape([Dimension(785)])的数据集;
因此,我尝试使用tensorflow构建时尚MNIST CNN分类器。我得到了这个维度错误。我是tensorflow的新手 我阅读了tensorflow文档,但没有找到问题的解决方案。 我正在共享我发现的与错误相关的代码。 以下是整个回溯Python 如何修复:“;预期形状TensorShape([Dimension(None),Dimension(785)])获得了带有形状TensorShape([Dimension(785)])的数据集;,python,tensorflow,Python,Tensorflow,因此,我尝试使用tensorflow构建时尚MNIST CNN分类器。我得到了这个维度错误。我是tensorflow的新手 我阅读了tensorflow文档,但没有找到问题的解决方案。 我正在共享我发现的与错误相关的代码。 以下是整个回溯 Traceback (most recent call last): File "<ipython-input-2-8a4292875dbe>", line 208, in <module> model
Traceback (most recent call last):
File "<ipython-input-2-8a4292875dbe>", line 208, in <module>
model.build()
File "<ipython-input-2-8a4292875dbe>", line 133, in build
self.getdata()
File "<ipython-input-2-8a4292875dbe>", line 65, in getdata
self.test_data_init = iterator.make_initializer(test_data)
# initializer for test_data
File "/home/aditya/anaconda3/envs/train/lib/python3.7/site-
packages/tensorflow/python/data/ops/iterator_ops.py", line 369,
in make_initializer
(self.output_shapes, dataset_output_shapes))
TypeError: Expected output shapes compatible with
TensorShape([Dimension(None), Dimension(785)]) but got dataset
with output shapes TensorShape([Dimension(785)]).
我的getdata()
我本想找到一种重塑的方法,但我做不到。任何帮助都将不胜感激
def build(self):
'''
Build the computation graph
'''
self.getdata()
self.inference()
self.loss()
self.optimize()
self.eval()
self.summary()
def getdata(self):
with tf.name_scope('data'):
train_data=tf.data.Dataset.from_tensor_slices(train)
train_data = train_data.shuffle(10000) # if you want to shuffle your data
train_data = train_data.batch(self.batch_size)
test_data=tf.data.Dataset.from_tensor_slices(test)
iterator=tf.data.Iterator.from_structure(train_data.output_types,train_data.output_shapes)
self.train_data_init = iterator.make_initializer(train_data) # initializer for train_data
self.test_data_init = iterator.make_initializer(test_data) # initializer for test_data
img, self.label = iterator.get_next()
self.img = tf.reshape(img, shape=[-1, 28, 28, 1])
# reshape the image to make it work with tf.nn.conv2d