Image source from unsplash by Timothy L Brock
上一篇文章介紹瞭如何在 Keras 中調用 Tensorboard。這篇文章就來談談如何用 Tensorboard 幫助模型調參。
代碼repo見這裏
https://github.com/zht007/tensorflow-practice
還是用手寫數字MINST數據集爲例,之前我們通過CNN的模型將識別率提高到了99%,CNN網絡中的各個參數是怎麼得到的呢,多少層卷積層,多少層全連接層,每層神經網絡多少個神經元或者多少個Filter呢?如何調整這些參數以保證模型是具有"識別"手寫數字的能力,而並不是僅僅將每個圖片對應的數字簡單粗暴地"記"下來了呢?
這裏我們就需要遍歷不同參數的組合,然後使用 Tensorboard 可視化的工具找出最佳的參數組合。
1. 提取模型參數
最容易調節的參數:卷積層層數,每層神經元個數(Filter 數量) 和 全連接層層數,這幾個參數分別list三個數。
dense_layers = [0,1,2]
layer_sizes = [32, 64,128]
conv_layers = [1, 2, 3]
2. 建立和訓練各個模型
三個參數,三個for循環遍歷,一共建立並訓練9個模型。注意: tensorboard 需要在循環中調用。
NAME = "{}-conv-{}-notes-{}-dense-{}".format(conv_layer,layer_size,dense_layer,int(time.time()))
tensorboard = TensorBoard(log_dir='gdrive/My Drive/dataML/logs1/{}'.format(NAME))
當然爲了提高速度,我們只訓練了 30 個epoch.
完整代碼如下:
for dense_layer in dense_layers:
for layer_size in layer_sizes:
for conv_layer in conv_layers:
NAME = "{}-conv-{}-notes-{}-dense-{}".format(conv_layer,layer_size,dense_layer,int(time.time()))
tensorboard = TensorBoard(log_dir='gdrive/My Drive/dataML/logs1/{}'.format(NAME))
print(NAME)
model = models.Sequential()
model.add(layers.Conv2D(filters = layer_size, kernel_size=(6,6), strides=(1,1),
padding = 'same', activation = 'relu',
input_shape = (28,28,1)))
for l in range(conv_layer - 1):
model.add(layers.Conv2D(filters = layer_size,kernel_size=(5,5),strides=(2,2),
padding = 'same', activation = 'relu'))
model.add(layers.Flatten())
for l in range(dense_layer):
model.add(layers.Dense(units = layer_size, activation='relu'))
model.add(layers.Dense(units=10, activation='softmax'))
model.summary()
adam = keras.optimizers.Adam(lr = 0.0001)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=adam,
metrics=['accuracy'])
H = model.fit(x_train, y_train,
batch_size=50,
epochs=30,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[tensorboard])
3. 在Tensorboard 中 查看結果
當然我們最關心的是測試集的準確率和損失函數
一共9個結果,看起來比較麻煩,可以通過左下角的工具,可以勾選自己想看的結果。通過對比,可以發現卷積層操過三層,神經元或 Filter 數量操過64個,全連接層超過2個,就會出現明顯的過擬合現象。
4. 調整參數組合優化模型
通過 Tensorboard 的觀察,我們繼續優化模型參數,這次可以去掉過造成擬合的參數,增加batch size
dense_layers = [1,2]
layer_sizes = [32,64]
conv_layers = [2]
batch_sizes = [50,100,200]
重複上述過程,進一步優化參數,去掉造成過擬合的參數,增加Learning Rate
dense_layers = [1,2]
layer_sizes = [32,64]
conv_layers = [2]
batch_sizes = [50,100]
learning_rates = [0.0005,0.0001,0.00005]
進一步縮小遍歷的參數範圍,增加訓練的 epoch 數量,最終得到一組自己滿意的參數組合
dense_layers = [1]
layer_sizes = [32]
conv_layers = [2]
batch_sizes = [100]
learning_rates = [0.0005]
5. 總結
機器學習模型調參的過程實際上是一個不斷嘗試的過程,將想要調整的參數列出來一一訓練。然後藉助 Tensorboard 縮小探索的範圍,最終得到一個自己滿意的參數組合。
參考資料
[1]https://www.kaggle.com/c/digit-recognizer/data
[2]https://codelabs.developers.google.com/codelabs/cloud-tensorflow-mnist/#0
[3]https://github.com/GoogleCloudPlatform/tensorflow-without-a-phd.git
[4]https://www.tensorflow.org/api_docs/
相關文章
Tensorflow入門——單層神經網絡識別MNIST手寫數字
Tensorflow入門——多層神經網絡MNIST手寫數字識別
Tensorflow入門——分類問題cross_entropy的選擇
首發steemit
歡迎掃描二維碼關注我的微信公衆號“tensorflow機器學習”,一起學習,共同進步