1, LeNet-1
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
batch_size = 256
num_classes = 10
epochs = 10
img_rows, img_cols = 28, 28
input_shape = (img_rows, img_cols, 1)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 处理 x
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)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# 处理 y
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
model = Sequential()
model.add(Conv2D(4, (5, 5), activation = 'relu', padding = 'same', input_shape = input_shape))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(12, (5, 5), activation = 'relu', padding = 'same'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(10, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics = ['accuracy'])
model.fit(x_train, y_train, validation_data = (x_test, y_test), batch_size = batch_size, epochs = epochs, verbose = 1)
score = model.evaluate(x_test, y_test, verbose = 2)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
Test loss: 0.0458575173373
Test accuracy: 0.985
2,LeNet-4
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
batch_size = 256
num_classes = 10
epochs = 10
img_rows, img_cols = 28, 28
input_shape = (img_rows, img_cols, 1)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 处理 x
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)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# 处理 y
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
model = Sequential()
model.add(Conv2D(6, (5, 5), activation = 'relu', padding = 'same', input_shape = input_shape))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(16, (5, 5), activation = 'relu', padding = 'same'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(84, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics = ['accuracy'])
model.fit(x_train, y_train, validation_data = (x_test, y_test), batch_size = batch_size, epochs = epochs, verbose = 1)
score = model.evaluate(x_test, y_test, verbose = 0)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
Test loss: 0.0271811319855
Test accuracy: 0.9898
3,LeNet-5
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
batch_size = 256
num_classes = 10
epochs = 10
img_rows, img_cols = 28, 28
input_shape = (img_rows, img_cols, 1)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 处理 x
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)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# 处理 y
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
model = Sequential()
model.add(Conv2D(6, (5, 5), activation = 'relu', padding = 'same', input_shape = input_shape))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(16, (5, 5), activation = 'relu', padding = 'same'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(120, activation = 'relu'))
model.add(Dense(84, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics = ['accuracy'])
model.fit(x_train, y_train, validation_data = (x_test, y_test), batch_size = batch_size, epochs = epochs, verbose = 1)
score = model.evaluate(x_test, y_test, verbose = 0)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
Test loss: 0.0322017334626
Test accuracy: 0.9901