自动机器学习之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/

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