from keras.datasets import mnist
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.optimizers import RMSprop
from keras.utils import np_utils
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
model = Sequential()
model.add(Dense(output_dim=625, input_dim=784, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(output_dim=625, input_dim=625, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(output_dim=10, input_dim=625, activation='softmax'))
model.compile(optimizer=RMSprop(), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
model.fit(X_train, Y_train, nb_epoch=100, batch_size=128, verbose=2,
validation_data=(X_test,Y_test))
依然是我们熟悉的MNIST数据集,这次的全连接层采用了relu
这个激活函数,relu函数的形态如下,他会让小于0的值都变为0
这一次的新面孔是Dropout
层,这一层主要是用来随机断开一些神经元,让他们不要往下传播。
另外一个新面孔是RMSprop
这个优化器,他是为了解决 Adagrad 学习率急剧下降问题的,那究竟是怎么回事呢,暂且先跳过,留到后续再讨论。