自動機器學習之Auto-Keras入門

對於訓練深度學習,設計神經網絡結構是其中技術含高最高的任務,優秀的網絡架構往往依賴建構模型的經驗,專業領域知識,以及大量的算力試錯。實際應用中往往基於類似功能的神經網絡微調生成新的網絡結構。

Auto-Keras是一個離線使用的開源庫,用於構建神經網絡結構和搜索超參數,支持RNN,CNN神經網絡,它使用了高效神經網絡搜索ENAS,利用遷移學習的原理將在前面任務中學到的權值應用於後期的模型中,效率相對較高。除了支持keras,它還提供TensorFlow他PyTorch的版本。

1. 安裝
由於需要把輸出的神經網絡結構保存成圖片,使用了pydot和graphviz圖像工具支持,auto-keras支持torch等多種工具支持,因此,安裝時會下載大量依賴軟件。使用以下方法安裝auto-keras:

conda create --name automl python=3.6
conda activate autokeras

# yum install graphviz  (root用戶搞一下)
$ pip install pydot
$ pip install autokeras
$ pip install keras

 

2. 舉例

本例中使用了mnist數據集,它是一個入門級的圖像識別數據集,用於訓練手寫數字識別模型,例程自動下載訓練數據,然後創建了圖片分類器,訓練時間設置爲10分鐘,模型在測試集上的正確率爲99.21%。建議使用帶GPU的機器訓練模型,它比使用CPU訓練速度快幾十倍。

from keras.datasets import mnist
from autokeras import ImageClassifier
from autokeras.constant import Constant
import autokeras
from keras.utils import plot_model
    
if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.reshape(x_train.shape + (1,))
    x_test = x_test.reshape(x_test.shape + (1,))
    clf = ImageClassifier(verbose=True, augment=False)
    clf.fit(x_train, y_train, time_limit=10 * 60)
    clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
    y = clf.evaluate(x_test, y_test)
    print(y * 100)
    clf.export_keras_model('model.h5')
    plot_model(clf, to_file='model.png')
# 返回值: 99.21

上述程序在我的機器上運行後訓練出了17層網絡,其中包括dropout層,池化層,卷積層,全連接層等,程序以圖片方式將描述信息保存在model.png中,下面截取了圖片中的部分層作爲示例,如下圖所示:


參考:

https://www.jianshu.com/p/4e499201816f
官網 https://autokeras.com/

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