我将通过一个示例来帮助您入门。它应该得到大约 50% 的准确率。
因此,我将代码与您的代码保持相同以加载数据。唯一的区别是我将数据标准化为介于 0 和 1 之间。通常建议这样做以更紧密地限制权重。
import numpy as np
from keras.datasets import cifar10
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from keras import backend as k
from keras.models import Sequential
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K
(x_train, y_train), (x_test, y_test)=cifar10.load_data()
从https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071下载数据[================== ===========] - 71s 0us/步
(x_train, y_train), (x_test, y_test)=cifar10.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape(50000, 3072)
x_test = x_test.reshape(10000, 3072)
# The known number of output classes.
num_classes = 10
# Channels go last for TensorFlow backend
x_train_reshaped = x_train.reshape(x_train.shape[0], x_train.shape[1],)
x_test_reshaped = x_test.reshape(x_test.shape[0], x_test.shape[1],)
input_shape = (x_train.shape[1],)
# Convert class vectors to binary class matrices. This uses 1 hot encoding.
y_train_binary = keras.utils.to_categorical(y_train, num_classes)
y_test_binary = keras.utils.to_categorical(y_test, num_classes)
现在让我们制作我们的模型。在这里,我确实使隐藏层每层有更多的神经元。
model = Sequential()
model.add(Dense(32,
activation='relu',
input_shape=input_shape))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
现在我们可以训练模型了
epochs = 4
batch_size = 128
# Fit the model weights.
model.fit(x_train_reshaped, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test_reshaped, y_test_binary))
50000/50000 [===============================] - 3s 58us/step - loss: 1.6716 - acc: 0.4038 - val_loss :1.6566 - val_acc:0.4094