![](/img/trans.png)
[英]ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (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.