【问题标题】:Tensorflow model fit method throws exception for dimensionsTensorflow 模型拟合方法引发尺寸异常
【发布时间】:2020-01-13 02:05:41
【问题描述】:

我想练习一下这个关于图像分类的tensorflow教程https://www.tensorflow.org/tutorials/keras/basic_classification

并通过本教程将我的数据加载到数据集中 https://www.tensorflow.org/tutorials/load_data/images

如果我调用模型拟合方法,我会得到以下异常: 检查输入时出错:预期 flatten_6_input 有 4 个维度,但得到的数组形状为 (28, 28, 3)

图像的形状为 (28, 28, 3)

我已经尝试将几个维度作为输入参数,并从数据集中读取数据以验证数据。

image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64))
image_label_ds = tf.data.Dataset.zip((image_ds, label_ds))

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28, 3)),
    keras.layers.Dense(256, activation=tf.nn.relu),
    keras.layers.Dense(2, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(image_label_ds.shuffle(5), epochs=5)
Epoch 1/5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-114-a638eaff329f> in <module>()
----> 1 model.fit(train_dataset.shuffle(5), epochs=5)

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    374                            ': expected ' + names[i] + ' to have ' +
    375                            str(len(shape)) + ' dimensions, but got array '
--> 376                            'with shape ' + str(data_shape))
    377         if not check_batch_axis:
    378           data_shape = data_shape[1:]

ValueError: Error when checking input: expected flatten_6_input to have 4 dimensions, but got array with shape (28, 28, 3)

【问题讨论】:

标签: tensorflow model dataset


【解决方案1】:

我刚刚包含了命令

''' image_label_ds= image_label_ds.batch(2) '''

成功了

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 2015-03-08
    • 2013-03-09
    • 2014-02-15
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多