小试牛刀Keras之MNIST数据集识别

前言

在全世界中目前比较流行的深度学习框架主要有:Theano 、Torch 、PyTorch、Caffe、Tensorflow、MXNet、Keras。由于在笔者的工作中大部分是使用Tensorflow 和 Caffe,此外还有MXNet,但是对其他框架没有上过手,因此笔者有一个想法,就是把这些框架都体验一遍,并用这些框架都去实现一遍经典的深度学习网络模型。

关于Keras

Keras是一个高层神经网络API,Keras由纯Python编写而成并基Tensorflow、Theano以及CNTK后端,并将其函数统一封装,使得用户可以以同一个接口调用不同后端引擎的函数。Keras默认使用Tensorflow作为其后端。
– Theano是一个开源的符号主义张量操作框架,由蒙特利尔大学LISA/MILA实验室开发。
– TensorFlow是一个符号yo的商业级工具包。

时间就是金钱,Keras具有网络层、损失函数、优化器、初始化策略、激活函数和正则化方法等独立模块,能简易快速地实现原型设计,可以大大提高我们动手实现网络模型的效率。

安装Keras

建议使用PyPI的方法安装:

sudo pip install keras 

MNIST数据集识别

talk is cheap,show me the code.


from __future__ import print_function import keras import tensorflow as tf from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K batch_size = 128 num_classes = 10 epochs = 12 # input image dimensions img_rows, img_cols = 28, 28 # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() # 考虑到Theano和Tensorflow 两个框架的数据结构不同,这里需要根据不同的后端来调整数据维度顺序 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) # 把数据变成float32更精确 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 # 将标签数据处理成one-hot型数据 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) # 创建一个顺序模型,将所有涉及的计算都顺序放入这个模型中 model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) # model.add(Dense(num_classes, activation='softmax')) model.add(Dense(num_classes, activation=tf.nn.softmax)) # 构建模型,同时附带模型计算的损失函数,优化算法、评价函数 # 这里使用交叉熵损失函数,优化方法选用Adadelta model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) # 进行训练 model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) # 计算预测准确率 score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])

输出

Using TensorFlow backend.
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/12
60000/60000 [==============================] - 38s 632us/step - loss: 0.2544 - acc: 0.9204 - val_loss: 0.0552 - val_acc: 0.9823
Epoch 2/12
60000/60000 [==============================] - 17s 288us/step - loss: 0.0853 - acc: 0.9745 - val_loss: 0.0398 - val_acc: 0.9873
Epoch 3/12
60000/60000 [==============================] - 17s 285us/step - loss: 0.0660 - acc: 0.9805 - val_loss: 0.0344 - val_acc: 0.9884
Epoch 4/12
60000/60000 [==============================] - 17s 284us/step - loss: 0.0544 - acc: 0.9830 - val_loss: 0.0318 - val_acc: 0.9889
Epoch 5/12
60000/60000 [==============================] - 17s 285us/step - loss: 0.0477 - acc: 0.9857 - val_loss: 0.0280 - val_acc: 0.9896
Epoch 6/12
60000/60000 [==============================] - 17s 283us/step - loss: 0.0426 - acc: 0.9870 - val_loss: 0.0292 - val_acc: 0.9901
Epoch 7/12
60000/60000 [==============================] - 17s 286us/step - loss: 0.0371 - acc: 0.9887 - val_loss: 0.0275 - val_acc: 0.9910
Epoch 8/12
60000/60000 [==============================] - 17s 283us/step - loss: 0.0352 - acc: 0.9898 - val_loss: 0.0256 - val_acc: 0.9913
Epoch 9/12
60000/60000 [==============================] - 17s 285us/step - loss: 0.0322 - acc: 0.9901 - val_loss: 0.0285 - val_acc: 0.9903
Epoch 10/12
60000/60000 [==============================] - 17s 284us/step - loss: 0.0303 - acc: 0.9902 - val_loss: 0.0250 - val_acc: 0.9918
Epoch 11/12
60000/60000 [==============================] - 17s 286us/step - loss: 0.0279 - acc: 0.9913 - val_loss: 0.0254 - val_acc: 0.9913
Epoch 12/12
60000/60000 [==============================] - 17s 284us/step - loss: 0.0256 - acc: 0.9925 - val_loss: 0.0256 - val_acc: 0.9922
Test loss: 0.02559355384617702
Test accuracy: 0.9922

参考链接

如何入门Keras?
Keras github 源码
keras英文文档
Keras中文文档


笔者目前主要从事自动驾驶研发工作,本公众号主要关注机器人和自动驾驶方向的技术以及最新进展,闲暇时也和大家聊聊读书、生活以及最新的电子科技产品,希望大家喜欢,共同进步!
欢迎关注

Author Rewards

Leave a Reply

Your email address will not be published. Required fields are marked *