我正在尝试将手写数字(28 x 28像素)的灰度图像分为10类。
我已经在此站点上检查了类似的问题,但是我无法解决为什么得到此错误的原因:
ValueError:无法将大小为7840000的数组重塑为形状(60000,784)
如果可以,请帮助我解决此问题。
from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical
def load_dataset():
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28 * 28))
test_images = test_images.reshape((60000, 28 * 28))
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
return train_images, train_labels, test_images, test_labels
def prep_pixels(train, test):
train_images = train_images.astype('float32') / 255
test_images = test_images.astype('float32') / 255
return train_images, test_images
def define_model():
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
return network
def compile(network):
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
def run():
(train_images, train_labels), (test_images, test_labels) = load_dataset()
train_images, test_images = prep_pixels(test_images, test_images)
network = define_model()
compiled_network = compile(network)
compiled_network.fit(train_images, train_labels, epochs=5, batch_size=128)
run()
最佳答案
MNIST数据集由60000个训练图像和10000个测试图像组成。重塑为:
test_images = test_images.reshape((10000, 28 * 28))
关于python - ValueError:无法将大小为7840000的数组重塑为形状(60000,784),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58644645/