【发布时间】:2020-04-03 21:49:49
【问题描述】:
我正在从《用 Python 进行深度学习》一书中学习深度学习。我有以下代码,
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images.shape
len(train_labels)
train_labels
test_images.shape
len(test_labels)
test_labels
from keras import models
from keras import layers
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc',test_acc)
到目前为止一切正常。当我运行以下代码时出现错误
digit = train_images[4]
import matplotlib.pyplot as plt
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()
它给出以下错误
C:\ProgramData\Anaconda\lib\site-packages\matplotlib\image.py in set_data(self, A)
688 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
689 raise TypeError("Invalid shape {} for image data"
--> 690 .format(self._A.shape))
691
692 if self._A.ndim == 3:
TypeError: Invalid shape (784,) for image data
这是什么意思?你能详细解释一下,因为我是深度学习的初学者。其次,我也在形状/尺寸和轴之间感到困惑。您能否解释一下以及如何解决上述错误?
【问题讨论】:
-
只是一个建议,试试 input_shape(28, 28, 1)。并且还尝试在 reshape 语句中使用 , 而不是 * 。不确定,但我就是这样做的。但是我的代码和你的不一样。
-
它不工作它给出一个错误检查输入时出错:预期dense_3_input有4维,但得到的数组形状为(10000, 784)
-
嗨朋友们,有什么帮助吗?
-
嗨!我也在学习这个东西。是你想要的,对一些图像进行分类吗?然后可以使用示例数据集的代码。这会有帮助吗?
-
是的@StackerRook,请分享代码
标签: python matplotlib deep-learning