有两个隐层的神经网络,使用minist手写训练集。代码如下:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout,Activation
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Flatten
from keras.optimizers import SGD
from keras.datasets import mnist
from keras import backend as K
import keras
import numpy as np
import struct
def train():
model = Sequential()
model.add(Dense(input_dim=28*28, output_dim=500))
model.add(Activation("sigmoid"))
model.add(Dense(output_dim=500))
model.add(Activation("sigmoid"))
model.add(Dense(output_dim=10))
model.add(Activation("sigmoid"))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
(X_train, y_train), (X_test, y_test) = mnist.load_data() # 使用Keras自带的mnist工具读取数据(第一次需要联网)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1] * X_train.shape[
2]) # 由于mist的输入数据维度是(num, 28, 28),这里需要把后面的维度直接拼起来变成784维
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1] * X_test.shape[2])
Y_train = (np.arange(10) == y_train[:, None]).astype(int) # 参考上一篇文章,这里需要把index转换成一个one hot的矩阵
Y_test = (np.arange(10) == y_test[:, None]).astype(int)
# 开始训练,这里参数比较多。batch_size就是batch_size,nb_epoch就是最多迭代的次数, shuffle就是是否把数据随机打乱之后再进行训练
# verbose是屏显模式,官方这么说的:verbose: 0 for no logging to stdout, 1 for progress bar logging, 2 for one log line per epoch.
# 就是说0是不屏显,1是显示一个进度条,2是每个epoch都显示一行数据
# show_accuracy就是显示每次迭代后的正确率
# validation_split就是拿出百分之多少用来做交叉验证
model.fit(X_train, Y_train, batch_size=200, nb_epoch=2, shuffle=True, verbose=1,validation_split=0.3)
print('test set')
model.evaluate(X_test, Y_test, batch_size=200)
def trainCNN():
modelCNN = Sequential()
modelCNN.add(Convolution2D(24, 3, 3, input_shape=(28,28,1)))#这里使用24个filter,每个大小为3x3。输入图片大小28x28
modelCNN.add(MaxPooling2D((2,2)))#pooling为2x2,即每4个网格取一个最大值 pooling之前是24(filter)x26x26,pooling后是24(filter)x13x13
modelCNN.add(Convolution2D(48, 3, 3))#再用24x3x3filter卷积一次,大小为48(filter)x11x11
modelCNN.add(MaxPooling2D((2, 2))) # pooling为2x2,完成后成为48(filter)x5x5
modelCNN.add(Flatten())
modelCNN.add(Dense(output_dim=100))
modelCNN.add(Activation('relu'))
modelCNN.add(Dense(output_dim=10))
modelCNN.add(Activation('softmax'))
modelCNN.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# input image dimensions
img_rows, img_cols = 28, 28
batch_size = 200
num_classes = 10
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
modelCNN.fit(x_train, y_train,
batch_size=batch_size,
epochs=2,
verbose=1,
validation_data=(x_test, y_test))
score = modelCNN.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
if __name__ == "__main__":
trainCNN();
全连接和cnn都跑了两轮,结果如下:
全连接网络: loss: 0.2520 - acc: 0.9265 - val_loss: 0.2469 - val_acc: 0.9292
cnn: loss: 0.0769 - acc: 0.9770 - val_loss: 0.0605 - val_acc: 0.9785
可以看到cnn的精度,明显高于全连接网络。不过cnn相对全连接还是深了一些,训练速度慢了不少。