目录

简介

keras下使用mlp识别数字。

代码

# coding: utf8

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation

import numpy as np

np.random.seed(1337)
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import RMSprop

(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], -1) / 255.
Y_train = np_utils.to_categorical(Y_train, 10)
X_test = X_test.reshape(X_test.shape[0], -1) / 255.
Y_test = np_utils.to_categorical(Y_test, 10)

model = Sequential()
model.add(Dense(256, input_dim=784))
model.add(Activation('relu')) # sigmoid 0.9766 relu 0.9896
model.add(Dense(10))
model.add(Activation('softmax'))

model.compile(optimizer=RMSprop(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, Y_train, epochs=10, batch_size=128)

loss, accuracy = model.evaluate(X_test, Y_test, batch_size=32)
print(loss, accuracy)

效果

训练集:loss: 0.0651 - acc: 0.9886
测试集:loss: 0.22048894146081094 acc: 0.9731