繁体   English   中英

ValueError:检查输入时出错:预期density_16_input具有2维,但数组的形状为(60000,28,28)

[英]ValueError: Error when checking input: expected dense_16_input to have 2 dimensions, but got array with shape (60000, 28, 28)

尝试从Google Tensorflow教程中编译代码,并最终制作新代码以识别mnist数据集中的数字。 不知道为什么此代码无法编译。

import tensorflow as tf

(train_images, train_labels) = tf.keras.datasets.mnist.load_data()
(test_images, test_labels) = tf.keras.datasets.mnist.load_data()

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(512, activation = tf.nn.relu, 
input_shape = (784,)))
model.add(tf.keras.layers.Dense(10, activation = tf.nn.softmax))
model.compile(loss = 'categorical_crossentropy', optimizer = 'rmsprop')

model.fit(train_images, train_labels, epochs=5)

loss, accuracy = model.evaluate(test_images, test_labels)
print('Accuracy', test_accuracy)

scores = model.predict(test_images[0:1])
print(np.argmax(scores)) 

收到此错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-5c0c03bc04c4> in <module>()
     10 model.compile(loss = 'categorical_crossentropy', optimizer = 'rmsprop')
     11 
---> 12 model.fit(train_images, train_labels, epochs=5)
     13 
     14 loss, accuracy = model.evaluate(test_images, test_labels)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1261         steps_name='steps_per_epoch',
   1262         steps=steps_per_epoch,
-> 1263         validation_split=validation_split)
   1264 
   1265     # Prepare validation data.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split)
    866         feed_input_shapes,
    867         check_batch_axis=False,  # Don't enforce the batch size.
--> 868         exception_prefix='input')
    869 
    870     if y is not None:

/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)
    141     data = data.values if data.__class__.__name__ == 'DataFrame' else data
    142     data = [data]
--> 143   data = [standardize_single_array(x) for x in data]
    144 
    145   if len(data) != len(names):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_utils.py in <listcomp>(.0)
    141     data = data.values if data.__class__.__name__ == 'DataFrame' else data
    142     data = [data]
--> 143   data = [standardize_single_array(x) for x in data]
    144 
    145   if len(data) != len(names):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_utils.py in standardize_single_array(x)
     79   elif tensor_util.is_tensor(x):
     80     return x
---> 81   elif x.ndim == 1:
     82     x = np.expand_dims(x, 1)
     83   return x

AttributeError: 'tuple' object has no attribute 'ndim'

看起来问题在于tensorflow / Keras内的东西吗? 还是我的代码有问题?

更新!!! 将代码的前几行更改为:

(train_images, train_labels), (test_images, test_labels) = 
tf.keras.datasets.mnist.load_data()

现在出现错误:

ValueError: Error when checking input: expected dense_16_input to have 2 
dimensions, but got array with shape (60000, 28, 28)

由于网络的输入形状为(?, 784)必须重塑训练和测试数据集的形状:

train_images = train_images.reshape((-1, 28*28))
test_images = test_images.reshape((-1, 28*28))

您可能还需要规范化图像以帮助优化过程:

train_images = train_images.astype('float32') / 255.
test_images = test_images.astype('float32') / 255.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM