数据读取
MNIST数据本身是图像格式的,我们用mode=”vector”去读取,转变成矢量格式的。
def LoadData():print("reading data...")dr = MnistImageDataReader(mode="vector")......
搭建模型
一共4个隐层,都用ReLU激活函数连接,最后的输出层接Softmax分类函数。

以下是主要的参数设置:
if __name__ == '__main__':dataReader = LoadData()num_feature = dataReader.num_featurenum_example = dataReader.num_examplenum_input = num_featurenum_hidden1 = 128num_hidden2 = 64num_hidden3 = 32num_hidden4 = 16num_output = 10max_epoch = 10batch_size = 64learning_rate = 0.1params = HyperParameters_4_0(learning_rate, max_epoch, batch_size,net_type=NetType.MultipleClassifier,init_method=InitialMethod.MSRA,stopper=Stopper(StopCondition.StopLoss, 0.12))net = NeuralNet_4_0(params, "MNIST")fc1 = FcLayer_1_0(num_input, num_hidden1, params)net.add_layer(fc1, "fc1")r1 = ActivationLayer(Relu())net.add_layer(r1, "r1")......fc5 = FcLayer_1_0(num_hidden4, num_output, params)net.add_layer(fc5, "fc5")softmax = ClassificationLayer(Softmax())net.add_layer(softmax, "softmax")net.train(dataReader, checkpoint=0.05, need_test=True)net.ShowLossHistory(xcoord=XCoordinate.Iteration)
运行结果
我们设计的停止条件是绝对Loss值达到0.12时,所以迭代到6个epoch时,达到了0.119的损失值,就停止训练了。
图14-19 训练过程中损失函数值和准确率的变化
图14-19是训练过程图示,下面是最后几行的打印输出。
......epoch=6, total_iteration=5763loss_train=0.005559, accuracy_train=1.000000loss_valid=0.119701, accuracy_valid=0.971667time used: 17.500738859176636save parameterstesting...0.9697
最后用测试集得到的准确率为96.97%。
代码位置
原代码位置:ch14, Level6
个人代码:MnistClassifier**
keras实现
from ExtendedDataReader.MnistImageDataReader import *from keras.models import Sequentialfrom keras.layers import Denseimport matplotlib.pyplot as pltimport osos.environ['KMP_DUPLICATE_LIB_OK']='True'def load_data():dataReader = MnistImageDataReader(mode="vector")dataReader.ReadData()dataReader.NormalizeX()dataReader.NormalizeY(NetType.MultipleClassifier)dataReader.GenerateValidationSet(k=20)x_train, y_train = dataReader.XTrain, dataReader.YTrainx_test, y_test = dataReader.XTest, dataReader.YTestx_val, y_val = dataReader.XDev, dataReader.YDevx_train = x_train.reshape(x_train.shape[0], 28 * 28)x_test = x_test.reshape(x_test.shape[0], 28 * 28)x_val = x_val.reshape(x_val.shape[0], 28 * 28)return x_train, y_train, x_test, y_test, x_val, y_valdef build_model():model = Sequential()model.add(Dense(128, activation='relu', input_shape=(784, )))model.add(Dense(64, activation='relu'))model.add(Dense(32, activation='relu'))model.add(Dense(16, activation='relu'))model.add(Dense(10, activation='softmax'))model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])return model#画出训练过程中训练和验证的精度与损失def draw_train_history(history):plt.figure(1)# summarize history for accuracyplt.subplot(211)plt.plot(history.history['accuracy'])plt.plot(history.history['val_accuracy'])plt.title('model accuracy')plt.ylabel('accuracy')plt.xlabel('epoch')plt.legend(['train', 'validation'])# summarize history for lossplt.subplot(212)plt.plot(history.history['loss'])plt.plot(history.history['val_loss'])plt.title('model loss')plt.ylabel('loss')plt.xlabel('epoch')plt.legend(['train', 'validation'])plt.show()if __name__ == '__main__':x_train, y_train, x_test, y_test, x_val, y_val = load_data()# print(x_train.shape)# print(x_test.shape)# print(x_val.shape)model = build_model()history = model.fit(x_train, y_train, epochs=20, batch_size=64, validation_data=(x_val, y_val))draw_train_history(history)loss, accuracy = model.evaluate(x_test, y_test)print("test loss: {}, test accuracy: {}".format(loss, accuracy))weights = model.get_weights()print("weights: ", weights)
模型输出
test loss: 0.11646892445675121, test accuracy: 0.9768999814987183
模型损失以及准确率曲线

