简体   繁体   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)

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.

相关问题 ValueError:检查输入时出错:预期density_1_input具有2维,但数组的形状为(60000,28,28) - ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (60000, 28, 28) ValueError:检查输入时出错:预期dense_10_input有2维,但得到的数组形状为(60000、28、28) - ValueError: Error when checking input: expected dense_10_input to have 2 dimensions, but got array with shape (60000, 28, 28) 检查时出错:预期density_3_input具有2维,但数组的形状为(28,28,1) - Error when checking : expected dense_3_input to have 2 dimensions, but got array with shape (28, 28, 1) 如何解决:ValueError:检查输入时出错:期望flatten_input具有3个维,但数组的形状为(28,28) - How to fix: ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (28, 28) ValueError:检查时出错:预期conv2d_1_input具有形状(28,28,1),但数组的形状为(58000,28,28) - ValueError: Error when checking : expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (58000, 28, 28) ValueError:检查时出错:预期density_1_input具有2维,但数组的形状为(1,16,16,512) - ValueError: Error when checking : expected dense_1_input to have 2 dimensions, but got array with shape (1, 16, 16, 512) Keras 数字数据集错误:预期 conv2d_input 有 4 个维度,但得到的数组形状为 (60000, 28, 28) - Error in Keras Digit Dataset: expected conv2d_input to have 4 dimensions, but got array with shape (60000, 28, 28) 检查输入时出错:预期 conv2d_17_input 有 4 个维度,但得到形状为 (28, 28, 1) 的数组 - Error when checking input: expected conv2d_17_input to have 4 dimensions, but got array with shape (28, 28, 1) ValueError:检查输入时出错:预期density_11_input具有3维,但数组的形状为(0,1) - ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1) ValueError:检查输入时出错:预期density_6_input具有3维,但数组的形状 - ValueError: Error when checking input: expected dense_6_input to have 3 dimensions, but got array with shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM