Skip to content

[source]

AutoModel

autokeras.AutoModel(
    inputs,
    outputs,
    preprocessors=None,
    project_name="auto_model",
    max_trials=100,
    directory=None,
    objective="val_loss",
    tuner="greedy",
    overwrite=False,
    seed=None,
    **kwargs
)

A Model defined by inputs and outputs. AutoModel combines a HyperModel and a Tuner to tune the HyperModel. The user can use it in a similar way to a Keras model since it also has fit() and predict() methods.

The AutoModel has two use cases. In the first case, the user only specifies the input nodes and output heads of the AutoModel. The AutoModel infers the rest part of the model. In the second case, user can specify the high-level architecture of the AutoModel by connecting the Blocks with the functional API, which is the same as the Keras functional API.

Exampl

Example

    # The user only specifies the input nodes and output heads.
    import autokeras as ak
    ak.AutoModel(
        inputs=[ak.ImageInput(), ak.TextInput()],
        outputs=[ak.ClassificationHead(), ak.RegressionHead()]
    )
    # The user specifies the high-level architecture.
    import autokeras as ak
    image_input = ak.ImageInput()
    image_output = ak.ImageBlock()(image_input)
    text_input = ak.TextInput()
    text_output = ak.TextBlock()(text_input)
    output = ak.Merge()([image_output, text_output])
    classification_output = ak.ClassificationHead()(output)
    regression_output = ak.RegressionHead()(output)
    ak.AutoModel(
        inputs=[image_input, text_input],
        outputs=[classification_output, regression_output]
    )

Arguments

  • inputs Union[autokeras.Input, List[autokeras.Input]]: A list of Node instances. The input node(s) of the AutoModel.
  • outputs Union[autokeras.Head, autokeras.Node, list]: A list of Node or Head instances. The output node(s) or head(s) of the AutoModel.
  • preprocessors Union[autokeras.engine.preprocessor.Preprocessor, List[autokeras.engine.preprocessor.Preprocessor], None]: An instance or list of Preprocessor objects corresponding to each AutoModel input, to preprocess a tf.data.Dataset before passing it to the model. Defaults to None (no external preprocessing).
  • project_name str: String. The name of the AutoModel. Defaults to 'auto_model'.
  • max_trials int: Int. The maximum number of different Keras Models to try. The search may finish before reaching the max_trials. Defaults to 100.
  • directory Union[str, pathlib.Path, None]: String. The path to a directory for storing the search outputs. Defaults to None, which would create a folder with the name of the AutoModel in the current directory.
  • objective str: String. Name of model metric to minimize or maximize, e.g. 'val_accuracy'. Defaults to 'val_loss'.
  • tuner Union[str, Type[autokeras.engine.tuner.AutoTuner]]: String or subclass of AutoTuner. If string, it should be one of 'greedy', 'bayesian', 'hyperband' or 'random'. It can also be a subclass of AutoTuner. Defaults to 'greedy'.
  • overwrite bool: Boolean. Defaults to False. If False, reloads an existing project of the same name if one is found. Otherwise, overwrites the project.
  • seed Optional[int]: Int. Random seed.
  • **kwargs: Any arguments supported by kerastuner.Tuner.

[source]

fit

AutoModel.fit(
    x=None,
    y=None,
    batch_size=32,
    epochs=None,
    callbacks=None,
    validation_split=0.2,
    validation_data=None,
    **kwargs
)

Search for the best model and hyperparameters for the AutoModel.

It will search for the best model based on the performances on validation data.

Arguments

  • x: numpy.ndarray or tensorflow.Dataset. Training data x.
  • y: numpy.ndarray or tensorflow.Dataset. Training data y.
  • batch_size: Int. Number of samples per gradient update. Defaults to 32.
  • epochs: Int. The number of epochs to train each model during the search. If unspecified, by default we train for a maximum of 1000 epochs, but we stop training if the validation loss stops improving for 10 epochs (unless you specified an EarlyStopping callback as part of the callbacks argument, in which case the EarlyStopping callback you specified will determine early stopping).
  • callbacks: List of Keras callbacks to apply during training and validation.
  • validation_split: Float between 0 and 1. Defaults to 0.2. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling. This argument is not supported when x is a dataset. The best model found would be fit on the entire dataset including the validation data.
  • validation_data: Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. validation_data will override validation_split. The type of the validation data should be the same as the training data. The best model found would be fit on the training dataset without the validation data.
  • **kwargs: Any arguments supported by keras.Model.fit.

[source]

predict

AutoModel.predict(x, **kwargs)

Predict the output for a given testing data.

Arguments

  • x: Any allowed types according to the input node. Testing data.
  • **kwargs: Any arguments supported by keras.Model.predict.

Returns

A list of numpy.ndarray objects or a single numpy.ndarray. The predicted results.


[source]

evaluate

AutoModel.evaluate(x, y=None, **kwargs)

Evaluate the best model for the given data.

Arguments

  • x: Any allowed types according to the input node. Testing data.
  • y: Any allowed types according to the head. Testing targets. Defaults to None.
  • **kwargs: Any arguments supported by keras.Model.evaluate.

Returns

Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs.


[source]

export_model

AutoModel.export_model()

Export the best Keras Model.

Returns

tf.keras.Model instance. The best model found during the search, loaded with trained weights.