最近幾年,隨着AlphaGo的崛起,深度學習開始出如今各個領域,好比無人車、圖像識別、物體檢測、推薦系統、語音識別、聊天問答等等。所以具有深度學習的知識並能應用實踐,已經成爲不少開發者包括博主本人的下一個目標了。python
目前最流行的框架莫過於Tensorflow了,可是隻要接觸過它的人,就知道它使用起來是多麼讓人恐懼。Tensorflow對咱們來講,彷彿是一門高深的Deep Learning學習語言,須要具有很深的機器學習和深度學習功底,才能玩得轉。linux
Keras正是在這種背景下應運而生的,它是一個對開發者很友好的框架,底層能夠基於TensorFlow和Theano,使用起來彷彿是在搭積木。只要不停的添加已有的「層」,就能夠實現各類複雜的深度網絡模型。git
所以,開發者須要熟悉的不過是兩點:如何搭建積木?都有什麼積木能夠用?github
安裝
安裝的步驟直接按照官方文檔來就好了,我筆記本的環境已經雜亂不堪,沒有辦法一步一步記錄安裝配置了。主要是安裝python3.6,而後各類pip install就好了。bootstrap
參考文檔:http://keras-cn.readthedocs.io/en/latest/for_beginners/keras_linux/網絡
基礎概念
在使用Keras前,首先要了解Keras裏面關於模型如何建立。在上面可愛的小盆友的圖片中,想要把積木羅列在一塊兒,須要一箇中心的木棍。那麼Sequential就能夠看作是這個木棍。框架
剩下的工做就是add不一樣的層就好了:機器學習
model = Sequential() model.add(Dense(32, input_shape=(784,))) model.add(Activation('relu'))
創建好model後,至關於咱們定義好了邏輯模型。此時就須要編譯模型,生成對應的代碼:函數
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
其中optimizer是參數優化的方法,loss是損失函數的定義,metrics是衡量模型效果的指標。學習
最後再灌入數據進行訓練便可:
model.fit(data, labels, epochs=10, batch_size=32)
完整的例子
代碼已經上傳到github:https://github.com/xinghalo/keras-examples/blob/master/keras-cn/mnist/mnist_mlp.py 不少人hello world跑不通是由於網絡問題,不能下載到對應的數據集。我這裏把數據集也上傳到對應的目錄下了,修改對應的path便可。
from __future__ import print_function import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout from keras.optimizers import RMSprop batch_size = 128 num_classes = 10 epochs = 3 # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data("/Users/xingoo/PycharmProjects/keras-examples/keras-cn/mnist/mnist.npz") x_train = x_train.reshape(60000, 784) x_test = x_test.reshape(10000, 784) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 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) model = Sequential() model.add(Dense(512, activation='relu', input_shape=(784,))) model.add(Dropout(0.2)) model.add(Dense(512, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(num_classes, activation='softmax')) model.summary() model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy']) history = 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. /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6 return f(*args, **kwds) 60000 train samples 10000 test samples _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_1 (Dense) (None, 512) 401920 _________________________________________________________________ dropout_1 (Dropout) (None, 512) 0 _________________________________________________________________ dense_2 (Dense) (None, 512) 262656 _________________________________________________________________ dropout_2 (Dropout) (None, 512) 0 _________________________________________________________________ dense_3 (Dense) (None, 10) 5130 ================================================================= Total params: 669,706 Trainable params: 669,706 Non-trainable params: 0 _________________________________________________________________ 2018-05-25 17:15:22.294036: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA Train on 60000 samples, validate on 10000 samples Epoch 1/3 128/60000 [..............................] - ETA: 166s - loss: 2.4237 - acc: 0.0703 640/60000 [..............................] - ETA: 38s - loss: 1.8688 - acc: 0.3812 1152/60000 [..............................] - ETA: 23s - loss: 1.5497 - acc: 0.5087 1664/60000 [..............................] - ETA: 18s - loss: 1.3466 - acc: 0.5655 2176/60000 [>.............................] - ETA: 15s - loss: 1.1902 - acc: 0.6167 2688/60000 [>.............................] - ETA: 13s - loss: 1.0736 - acc: 0.6536 3200/60000 [>.............................] - ETA: 12s - loss: 0.9968 - acc: 0.6778 3712/60000 [>.............................] - ETA: 11s - loss: 0.9323 - acc: 0.7002 4096/60000 [=>............................] - ETA: 11s - loss: 0.8971 - acc: 0.7109 ... 51328/60000 [========================>.....] - ETA: 0s - loss: 0.0733 - acc: 0.9775 51840/60000 [========================>.....] - ETA: 0s - loss: 0.0733 - acc: 0.9774 52352/60000 [=========================>....] - ETA: 0s - loss: 0.0735 - acc: 0.9774 52864/60000 [=========================>....] - ETA: 0s - loss: 0.0733 - acc: 0.9775 53376/60000 [=========================>....] - ETA: 0s - loss: 0.0736 - acc: 0.9774 53888/60000 [=========================>....] - ETA: 0s - loss: 0.0734 - acc: 0.9775 54400/60000 [==========================>...] - ETA: 0s - loss: 0.0736 - acc: 0.9774 54912/60000 [==========================>...] - ETA: 0s - loss: 0.0740 - acc: 0.9773 55424/60000 [==========================>...] - ETA: 0s - loss: 0.0744 - acc: 0.9772 55936/60000 [==========================>...] - ETA: 0s - loss: 0.0746 - acc: 0.9771 56448/60000 [===========================>..] - ETA: 0s - loss: 0.0749 - acc: 0.9771 56960/60000 [===========================>..] - ETA: 0s - loss: 0.0751 - acc: 0.9772 57472/60000 [===========================>..] - ETA: 0s - loss: 0.0756 - acc: 0.9772 57984/60000 [===========================>..] - ETA: 0s - loss: 0.0754 - acc: 0.9772 58496/60000 [============================>.] - ETA: 0s - loss: 0.0750 - acc: 0.9773 59008/60000 [============================>.] - ETA: 0s - loss: 0.0750 - acc: 0.9773 59520/60000 [============================>.] - ETA: 0s - loss: 0.0749 - acc: 0.9774 60000/60000 [==============================] - 7s - loss: 0.0749 - acc: 0.9774 - val_loss: 0.0819 - val_acc: 0.9768 Test loss: 0.0819479118524 Test accuracy: 0.9768
參考
- Keras中文官方文檔:http://keras-cn.readthedocs.io/en/latest/getting_started/sequential_model/
- Keras github examples:https://github.com/keras-team/keras/blob/master/examples/mnist_mlp.py
- 神經網絡(一):概念:https://blog.csdn.net/xierhacker/article/details/51771428
- 神經網絡(二):感知機:https://blog.csdn.net/xierhacker/article/details/51816484
- 深度學習筆記二:多層感知機(MLP)與神經網絡結構:https://blog.csdn.net/xierhacker/article/details/53282038
- 多層感知機:Multi-Layer Perceptron:https://blog.csdn.net/xholes/article/details/78461164