首页 > 解决方案 > 如何修复:“预期的形状 TensorShape([Dimension(None), Dimension(785)]) 获得了形状为 TensorShape([Dimension(785)]) 的数据集”

问题描述

所以我正在尝试使用 tensorflow 构建 Fashion-MNIST CNN 分类器。我得到了这个维度错误。我是 tensorflow 的新手。

我阅读了 tensorflow 文档,但我没有解决我的问题。我正在分享我发现与错误相关的代码。以下是整个回溯

    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)]).

我的模型.build()

    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

我期待找到一种重塑的方法,但我做不到。任何帮助将不胜感激

标签: pythontensorflow

解决方案


推荐阅读