Keras 訓練簡單的深度神經網絡

機器學習訓練營最近的作業都是使用Keras,所以最近去翻了下文檔,這裏記錄一下學習栗子。(官網有中文文檔)

不多BB,直接上代碼,註釋已經寫得很清楚了。

#!/usr/bin/env python  
# -*- coding: utf-8 -*-

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 = 20       # 迭代次數

# load the MNIST dataset
# 初次運行會先下載文件
(x_train, y_train), (x_test, y_test) = mnist.load_data()

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 sample')

# 類向量到二元類矩陣的轉換
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# Keras 的核心數據結構是 model,一種組織網絡層的方式。最簡單的模型是 Sequential 順序模型,它由多個網絡層線性堆疊。
model = Sequential()

# 可以簡單地使用 .add() 來堆疊模型:
# activation: 激活函數, relu: 線性修正單元, softmax 激活函數
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()

# 在完成了模型的構建後, 可以使用 .compile() 來配置學習過程
model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

# x_train 和 y_train 是 Numpy 數組 -- 就像在 Scikit-Learn API 中一樣。
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])

# 對新的數據生成預測
classes = model.predict(x_test, batch_size=128)

下面是部分輸出

Using TensorFlow backend.
60000 train samples
10000 test sample
_________________________________________________________________
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
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/20

###

Test loss:  0.11462802259046188
Test accuracy:  0.9826

對數據訓練20次得到的結果,準確率高達98.26%,還是挺6的。

但是在運行模型中,這臺13年的Mac CPU直接轉滿,處理器是2.4 GHz Intel Core i5,感覺好吃力,果然深度學習配置要求高啊。

如果代碼看不清楚,可以去閱讀原文中看。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章