[英]ValueError: Error when checking input: expected dense_16_input to have 2 dimensions, but got array with shape (60000, 28, 28)
Trying to compile code from a google Tensorflow tutorial, and eventually make new code to identify digits from the mnist data set. 尝试从Google Tensorflow教程中编译代码,并最终制作新代码以识别mnist数据集中的数字。 Not sure as to why this code wont compile.
不知道为什么此代码无法编译。
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))
Getting this error message: 收到此错误消息:
---------------------------------------------------------------------------
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'
Looks like the problem is something within tensorflow / Keras?? 看起来问题在于tensorflow / Keras内的东西吗? Or have I got something wrong in this code?
还是我的代码有问题?
UPDATE!!! 更新!!! Changed the first few lines of code to:
将代码的前几行更改为:
(train_images, train_labels), (test_images, test_labels) =
tf.keras.datasets.mnist.load_data()
And am now getting the error: 现在出现错误:
ValueError: Error when checking input: expected dense_16_input to have 2
dimensions, but got array with shape (60000, 28, 28)
You must reshape the train and test dataset since the input shape of network is (?, 784)
: 由于网络的输入形状为
(?, 784)
必须重塑训练和测试数据集的形状:
train_images = train_images.reshape((-1, 28*28))
test_images = test_images.reshape((-1, 28*28))
You may also want to normalize the images to help the optimization process: 您可能还需要规范化图像以帮助优化过程:
train_images = train_images.astype('float32') / 255.
test_images = test_images.astype('float32') / 255.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.