AutoKeras Documentation

repository·master·Indexed 27 days ago

https://github.com/keras-team/autokeras

AutoKeras is an AutoML system based on Keras designed to automate the process of finding optimal deep learning models. It provides high-level task classes for image, text, and structured data classification and regression, as well as a functional API using Blocks and Heads to define architectures. The system includes various tuners such as BayesianOptimization, Hyperband, and RandomSearch to optimize hyperparameters via the AutoModel class.

Tokens
11.1K
Snippets
27
Records
61
Agent score
90%

What's inside AutoKeras

  1. Generate the contributors list SVG

    master

    You can generate the contributors list image for the documentation by following these steps:

    1. Ensure Pillow is installed: pip install Pillow.
    2. Run the contributor generation script from the repository root: sh shell/contributors.sh.
    3. The resulting file will be located at docs/templates/img/contributors.svg.
    pip install Pillow
    sh shell/contributors.sh
  2. Run Autokeras via Docker using Makefile

    master

    Autokeras provides a Makefile to simplify Docker commands. You can use these commands to launch different environments like Jupyter Notebook, iPython, or a Bash shell.

    Standard Environments

    • Jupyter Notebook: Starts a container with a Jupyter Notebook server.
    • iPython: Starts an interactive iPython shell.
    • Bash: Starts an interactive bash shell.

    GPU Support

    To use GPU acceleration, you must have NVIDIA drivers installed and nvidia-docker configured. You can pass the GPU=all flag to the make commands.

    Data Persistence

    To access external datasets located on your host machine, use the DATA variable to mount a volume.

    Available Tasks

    Run make help to see a list of all available tasks.

  3. Build the AutoKeras documentation locally

    master

    To build and preview the AutoKeras documentation locally using MkDocs, follow these steps from the repository root:

    1. Install documentation dependencies: pip install -r docs/requirements.txt.
    2. Install the package in editable mode: pip install -e ..
    3. Navigate to the documentation directory: cd docs/.
    4. Generate the documentation files: python autogen.py.
    5. Start a local webserver to view the docs: mkdocs serve (available at http://localhost:8000).
    6. Alternatively, build a static site: mkdocs build (outputs to the site/ directory).
  4. Perform Text Classification with TextClassifier

    master

    Use ak.TextClassifier for a high-level automated text classification workflow. You can initialize the classifier, train it using .fit(), make predictions with .predict(), and evaluate performance with .evaluate().

    Key parameters for ak.TextClassifier:

    • overwrite: Boolean, whether to overwrite existing search results.
    • max_trials: Integer, the number of different models to try during the search.

    Key parameters for .fit():

    • epochs: Number of training epochs.
    • batch_size: Size of training batches.
    • validation_split: Float, the fraction of the training data to be used as validation data (e.g., 0.15 for 15%).
    • validation_data: A tuple (x_val, y_val) containing your own validation dataset.
    import autokeras as ak
    
    # Initialize the text classifier.
    clf = ak.TextClassifier(
        overwrite=True, max_trials=1
    )
    
    # Feed the text classifier with training data.
    clf.fit(x_train, y_train, epochs=1, batch_size=2)
    
    # Predict with the best model.
    predicted_y = clf.predict(x_test)
    
    # Evaluate the best model with testing data.
    print(clf.evaluate(x_test, y_test))
  5. Customize Image Search Space with AutoModel

    master

    For fine-grained control over the neural network architecture, use ak.AutoModel instead of ak.ImageClassifier. AutoModel uses a functional API style where you build a graph of nodes and blocks.

    High-level configuration with ImageBlock

    You can use ak.ImageBlock to constrain the search space:

    • block_type: Restrict the search to specific architectures (e.g., "resnet").
    • normalize: Boolean to enable/disable data normalization.
    • augment: Boolean to enable/disable data augmentation.

    Fine-grained configuration with individual blocks

    You can manually chain specific blocks to define a precise search space:

    • ak.ImageInput(): The starting node.
    • ak.Normalization(): For data normalization.
    • ak.ImageAugmentation(horizontal_flip=False): For data augmentation.
    • ak.ResNetBlock(version="v2"): For specific ResNet architectures.
    • ak.ClassificationHead(): To add the final classification layer.
    import autokeras as ak
    
    # Example: High-level customization
    input_node = ak.ImageInput()
    output_node = ak.ImageBlock(
        block_type="resnet",
        normalize=True,
        augment=False,
    )(input_node)
    output_node = ak.ClassificationHead()(output_node)
    
    clf = ak.AutoModel(inputs=input_node, outputs=output_node, overwrite=True, max_trials=1)
    clf.fit(x_train, y_train, epochs=1)
    
    # Example: Fine-grained customization
    input_node = ak.ImageInput()
    output_node = ak.Normalization()(input_node)
    output_node = ak.ImageAugmentation(horizontal_flip=False)(output_node)
    output_node = ak.ResNetBlock(version="v2")(output_node)
    output_node = ak.ClassificationHead()(output_node)
    
    clf = ak.AutoModel(inputs=input_node, outputs=output_node, overwrite=True, max_trials=1)
    clf.fit(x_train, y_train, epochs=1)
  6. Customize Image Regression Search Space with AutoModel

    master

    For advanced customization, use ak.AutoModel instead of ak.ImageRegressor. This allows you to build a functional graph of blocks (like ak.ImageBlock, ak.Normalization, or ak.ResNetBlock) to define exactly which components are available for the search.

    Common configuration options in ak.ImageBlock include:

    • block_type: Restrict the search to specific architectures (e.g., "resnet").
    • normalize: Boolean to enable/disable data normalization.
    • augment: Boolean to enable/disable data augmentation.
    import autokeras as ak
    
    # Define a custom search space using a functional API style
    input_node = ak.ImageInput()
    output_node = ak.ImageBlock(
        block_type="resnet",
        normalize=False,
        augment=False
    )(input_node)
    output_node = ak.RegressionHead()(output_node)
    
    # Initialize AutoModel with the defined graph
    reg = ak.AutoModel(
        inputs=input_node, 
        outputs=output_node, 
        overwrite=True, 
        max_trials=1
    )
    
    reg.fit(x_train, y_train, epochs=1)
  7. Perform Structured Data Classification

    master

    Use ak.StructuredDataClassifier to automatically find the best model architecture for tabular data. You can specify the number of model architectures to try using max_trials.

    Key methods:

    • fit(x, y, ...): Trains the classifier.
    • predict(x): Predicts labels using the best model found.
    • evaluate(x, y): Evaluates the best model on a test set.
    import autokeras as ak
    
    # Initialize the classifier
    clf = ak.StructuredDataClassifier(
        overwrite=True, 
        max_trials=3
    )
    
    # Train the model
    clf.fit(x_train, y_train, epochs=10)
    
    # Predict and evaluate
    predicted_y = clf.predict(x_test)
    print(clf.evaluate(x_test, y_test))
  8. Customize search space using AutoModel

    master

    Advanced users can define a custom neural network topology using the ak.AutoModel API. The syntax follows the Keras functional API pattern. When building a custom search space, ensure your blocks follow the topology: Preprocessor -> Block -> Head.

    Common components include:

    • Preprocessors: ak.Normalization, ak.ImageAugmentation.
    • Blocks: ak.ConvBlock, ak.ResNetBlock, ak.Merge, etc.
    • Heads: ak.ClassificationHead, ak.RegressionHead.

    Arguments for building blocks (like version in ResNetBlock) can be specified; if omitted, they are tuned automatically by AutoKeras.

    input_node = ak.ImageInput()
    output_node = ak.Normalization()(input_node)
    output_node1 = ak.ConvBlock()(output_node)
    output_node2 = ak.ResNetBlock(version="v2")(output_node)
    output_node = ak.Merge()([output_node1, output_node2])
    output_node = ak.ClassificationHead()(output_node)
    
    auto_model = ak.AutoModel(
        inputs=input_node, outputs=output_node, overwrite=True, max_trials=1
    )
    
    # Training and evaluation
    auto_model.fit(x_train[:100], y_train[:100], epochs=1)
    predicted_y = auto_model.predict(x_test)
    print(auto_model.evaluate(x_test, y_test))