我看到已经回答了类似的问题,这帮助我意识到输入不是模型所期望的,但我无法找到如何纠正这个问题。
我的问题是为什么它需要二维,我可以对我的代码做些什么来使这项工作分为 10 个不同的类?
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Import Libraries Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
from keras.datasets import fashion_mnist
from keras import models
from keras import layers
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Load Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Parameters Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
train_number_images = train_images.shape[0]
test_number_images = test_images.shape[0]
train_x_image_size = train_images.shape[1]
train_y_image_size = train_images.shape[2]
test_x_image_size = test_images.shape[1]
test_y_image_size = test_images.shape[2]
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Pretreat Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
train_images = train_images.reshape((train_number_images, train_x_image_size , train_y_image_size, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((test_number_images, test_x_image_size , test_y_image_size, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
gen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Define Model Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model2 = models.Sequential()
model2.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)))
model2.add(layers.MaxPooling2D((2,2)))
model2.add(layers.Conv2D(64, (3,3), activation='relu'))
model2.add(layers.MaxPooling2D((2,2)))
model2.add(layers.Conv2D(64, (3,3), activation='relu'))
model2.add(layers.MaxPooling2D((2,2)))
model2.add(layers.Flatten())
model2.add(layers.Dense(64, activation='relu'))
model2.add(layers.Dense(10, activation='sigmoid'))
model2.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
history2 = model2.fit_generator(gen.flow(train_images, train_labels, 64), epochs=15,
steps_per_epoch=len(train_images) // 64)