我理解AutoKeras的ImageClassifier的作用(https://autokeras.com/image_classifier/)
clf = ImageClassifier(verbose=True, augment=False)clf.fit(x_train, y_train, time_limit=12 * 60 * 60)clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)y = clf.evaluate(x_test, y_test)
但是我无法理解AutoModel类(https://autokeras.com/auto_model/)的作用,或者它与ImageClassifier有何不同
autokeras.auto_model.AutoModel(inputs,outputs,name="auto_model",max_trials=100,directory=None,objective="val_loss",tuner="greedy",seed=None)
关于输入和输出的参数文档说明如下:
- inputs: 一个列表或一个HyperNode实例。AutoModel的输入节点。
- outputs: 一个列表或一个HyperHead实例。AutoModel的输出头部。
什么是HyperNode实例?
同样,GraphAutoModel类是什么?(https://autokeras.com/graph_auto_model/)
autokeras.auto_model.GraphAutoModel(inputs,outputs,name="graph_auto_model",max_trials=100,directory=None,objective="val_loss",tuner="greedy",seed=None)
文档说明如下:
由HyperBlocks图定义的HyperModel。GraphAutoModel是HyperModel的子类。除了HyperModel的属性外,它还具有调节HyperModel的调谐器。用户可以像使用Keras模型一样使用它,因为它也具有fit()和predict()方法。
什么是HyperBlocks?如果图像分类器自动进行超参数调优,那么GraphAutoModel的用途是什么?
任何关于更好理解AutoModel和GraphAutoModel的文档/资源的链接将不胜感激。
回答:
最近使用了autokeras,我可以分享我的一点知识。
-
任务API
当执行经典任务如图像分类/回归、文本分类/回归等时,可以使用autokeras提供的最简单的API,称为任务API
:ImageClassifier
、ImageRegressor
、TextClassifier
、TextRegressor
等。在这种情况下,你有一个输入(图像或文本或表格数据等)和一个输出(分类、回归)。 -
Automodel
然而,当你处于需要多输入/输出架构的任务时,你不能直接使用任务API,这就是Automodel
和I/O API
发挥作用的地方。你可以查看文档中提供的示例,其中有两个输入(图像和结构化数据)和两个输出(分类和回归) -
GraphAutoModel
GraphAutomodel的工作方式类似于keras的功能API。它组装不同的块(卷积、LSTM、GRU等),并使用这些块创建模型,然后根据你提供的架构寻找最佳的超参数。假设我想要使用时间序列作为输入数据进行二元分类任务。首先让我们生成一个玩具数据集:
import numpy as npimport autokeras as akx = np.random.randn(100, 7, 3)y = np.random.choice([0, 1], size=100, p=[0.5, 0.5])
这里x是一个时间序列,包含100个样本,每个样本是一个长度为7的序列,特征维度为3。相应的目标变量y是二元的(0, 1)。使用GraphAutomodel,我可以指定我想使用的架构,使用所谓的HyperBlocks
。有许多块:卷积、RNN、密集层等,完整列表请查看这里。在我的情况下,因为我有时间序列数据,所以我想使用RNN块来创建模型:
input_layer = ak.Input()rnn_layer = ak.RNNBlock(layer_type="lstm")(input_layer)dense_layer = ak.DenseBlock()(rnn_layer)output_layer = ak.ClassificationHead(num_classes=2)(dense_layer)automodel = ak.GraphAutoModel(input_layer, output_layer, max_trials=2, seed=123)automodel.fit(x, y, validation_split=0.2, epochs=2, batch_size=32)
(如果你不熟悉上述定义模型的风格,那么你应该查看keras功能API的文档)。
所以在这个例子中,我有更多的灵活性来创建我想使用的架构骨架:LSTM块后面跟随一个密集层,再后面跟随一个分类层,但是我没有指定任何超参数(LSTM层的数量、密集层的数量、LSTM层的尺寸、密集层的尺寸、激活函数、dropout、batchnorm等),Autokeras将根据我提供的架构(骨架)自动进行超参数调优。