### Install AutoKeras and Set Backend Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/export.ipynb Before running the example, install AutoKeras and set the Keras backend to torch. ```bash !export KERAS_BACKEND="torch" !pip install autokeras ``` -------------------------------- ### Install AutoKeras Source: https://context7.com/keras-team/autokeras/llms.txt Use pip to install the AutoKeras library. ```bash pip install autokeras ``` -------------------------------- ### Install AutoKeras with Pip Source: https://github.com/keras-team/autokeras/blob/master/docs/templates/install.md Use these commands to install AutoKeras and its dependencies when not using a virtual environment and executing with 'pip'. ```bash pip install git+https://github.com/keras-team/keras-tuner.git pip install autokeras ``` -------------------------------- ### Run Autokeras container tasks via Makefile Source: https://github.com/keras-team/autokeras/blob/master/docker/README.md Standard commands to build and start the container in different modes. ```bash make notebook ``` ```bash make ipython ``` ```bash make bash ``` -------------------------------- ### Install AutoKeras with Pip (Python 3) Source: https://github.com/keras-team/autokeras/blob/master/docs/templates/install.md Use these commands to install AutoKeras and its dependencies when not using a virtual environment and executing with 'python3 -m pip'. ```bash python3 -m pip install git+https://github.com/keras-team/keras-tuner.git python3 -m pip install autokeras ``` -------------------------------- ### Import AutoKeras Dependencies Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Standard imports required for running AutoKeras examples. ```python import keras import numpy as np import tree from keras.datasets import mnist import autokeras as ak ``` -------------------------------- ### Install AutoKeras Source: https://github.com/keras-team/autokeras/blob/master/README.md Use this command to install the AutoKeras package via pip. Ensure you are using Python 3.7 or later and TensorFlow 2.8.0 or later. ```shell pip3 install autokeras ``` -------------------------------- ### Custom Search Space with ImageBlock Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Defines a custom search space using AutoModel and ImageBlock. This example configures the ImageBlock to specifically search for ResNet architectures, normalize data, and disable augmentation. ```python input_node = ak.ImageInput() output_node = ak.ImageBlock( # Only search ResNet architectures. block_type="resnet", # Normalize the dataset. normalize=True, # Do not do data augmentation. 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) ``` -------------------------------- ### Use custom metrics for model selection Source: https://github.com/keras-team/autokeras/blob/master/docs/templates/tutorial/faq.md Wraps a custom metric function into a Keras Tuner Objective to guide the model selection process. ```python # Implement your customized metric according to the tutorial. # https://keras.io/api/metrics/#creating-custom-metrics import autokeras as ak def f1_score(y_true, y_pred): ... clf = ak.ImageClassifier( max_trials=3, # Wrap the function into a Keras Tuner Objective # and pass it to AutoKeras. # Direction can be 'min' or 'max' # meaning we want to minimize or maximize the metric. # 'val_f1_score' is just add a 'val_' prefix # to the function name or the metric name. objective=kerastuner.Objective('val_f1_score', direction='max'), # Include it as one of the metrics. metrics=[f1_score], ) ``` -------------------------------- ### List available make tasks Source: https://github.com/keras-team/autokeras/blob/master/docker/README.md Display all supported commands defined in the Makefile. ```bash make help ``` -------------------------------- ### Load and Prepare IMDB Dataset Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_regression.ipynb Downloads the IMDB dataset, extracts it, and loads training and testing data. It then selects a subset of the data for demonstration purposes. ```python dataset = keras.utils.get_file( fname="aclImdb.tar.gz", origin="http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz", extract=True, ) # set path to dataset IMDB_DATADIR = os.path.join( os.path.dirname(dataset), "aclImdb_extracted", "aclImdb" ) classes = ["pos", "neg"] train_data = load_files( os.path.join(IMDB_DATADIR, "train"), shuffle=True, categories=classes ) test_data = load_files( os.path.join(IMDB_DATADIR, "test"), shuffle=False, categories=classes ) x_train = np.array(train_data.data)[:100] y_train = np.array(train_data.target)[:100] x_test = np.array(test_data.data)[:100] y_test = np.array(test_data.target)[:100] print(x_train.shape) # (25000,) print(y_train.shape) # (25000, 1) print(x_train[0][:50]) # this film was just brilliant casting ``` -------------------------------- ### Prepare Training and Testing Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_regression.ipynb Loads the California housing dataset and splits it into training and testing sets. The training set comprises 90% of the data, and the testing set comprises the remaining 10%. ```python house_dataset = fetch_california_housing() train_size = int(house_dataset.data.shape[0] * 0.9) x_train = house_dataset.data[:train_size] y_train = house_dataset.target[:train_size] x_test = house_dataset.data[train_size:] y_test = house_dataset.target[train_size:] ``` -------------------------------- ### Prepare MNIST Dataset for Training Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Loads the MNIST dataset and subsets it for a quick demonstration. Prints the shape and a sample of the training data. ```python (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train[:100] y_train = y_train[:100] x_test = x_test[:100] y_test = y_test[:100] print(x_train.shape) # (60000, 28, 28) print(y_train.shape) # (60000,) print(y_train[:3]) # array([7, 2, 1], dtype=uint8) ``` -------------------------------- ### Customized Search Space with AutoModel Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_regression.ipynb Demonstrates how to customize the search space using AutoModel, TextInput, TextBlock, and RegressionHead. This allows for more granular control over the model architecture. ```python input_node = ak.TextInput() output_node = ak.TextBlock()(input_node) output_node = ak.RegressionHead()(output_node) reg = ak.AutoModel( inputs=input_node, outputs=output_node, overwrite=True, max_trials=1 ) reg.fit(x_train, y_train, epochs=1, batch_size=2) ``` -------------------------------- ### Implement Multi-Input Multi-Output Models Source: https://context7.com/keras-team/autokeras/llms.txt Supports complex architectures for multi-modal and multi-task learning scenarios. ```python import numpy as np import autokeras as ak # Multi-modal data: images + text -> classification + regression image_data = np.random.rand(100, 32, 32, 3).astype(np.float32) text_data = np.array(['sample text ' + str(i) for i in range(100)]) class_labels = np.random.randint(0, 5, 100) regression_targets = np.random.rand(100, 1).astype(np.float32) # Define multi-input architecture image_input = ak.ImageInput() text_input = ak.TextInput() # Process each input image_output = ak.ImageBlock()(image_input) text_output = ak.TextBlock()(text_input) # Merge features merged = ak.Merge()([image_output, text_output]) # Multiple outputs classification_output = ak.ClassificationHead(num_classes=5)(merged) regression_output = ak.RegressionHead()(merged) # Create multi-input multi-output model model = ak.AutoModel( inputs=[image_input, text_input], outputs=[classification_output, regression_output], max_trials=3, overwrite=True ) # Train with multiple inputs and outputs model.fit( [image_data, text_data], [class_labels, regression_targets], epochs=5 ) ``` -------------------------------- ### Initialize and Fit AutoModel for Multi-task/Multi-modal Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Initializes an AutoModel with multiple inputs (ImageInput, Input) and multiple outputs (RegressionHead, ClassificationHead). The model is configured with max_trials=2 and trained for 1 epoch with batch_size=3. ```python # Initialize the multi with multiple inputs and outputs. model = ak.AutoModel( inputs=[ak.ImageInput(), ak.Input()], outputs=[ ak.RegressionHead(metrics=["mae"]), ak.ClassificationHead( loss="categorical_crossentropy", metrics=["accuracy"] ), ], overwrite=True, max_trials=2, ) # Fit the model with prepared data. model.fit( [image_data, numerical_data], [regression_target, classification_target], epochs=1, batch_size=3, ) ``` -------------------------------- ### Initialize and Train StructuredDataRegressor Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_regression.ipynb Initializes a StructuredDataRegressor with overwrite=True and max_trials=3, then trains it on the training data for 10 epochs. Finally, it predicts on the test data and prints the evaluation results. ```python # Initialize the structured data regressor. reg = ak.StructuredDataRegressor( overwrite=True, max_trials=3 ) # It tries 3 different models. # Feed the structured data regressor with training data. reg.fit( x_train, y_train, epochs=10, ) # Predict with the best model. predicted_y = reg.predict(x_test) # Evaluate the best model with testing data. print(reg.evaluate(x_test, y_test)) ``` -------------------------------- ### Run Autokeras with GPU support Source: https://github.com/keras-team/autokeras/blob/master/docker/README.md Execute the container with NVIDIA GPU access using the GPU=all flag. ```bash make notebook GPU=all # or [ipython, bash] ``` -------------------------------- ### Import Libraries Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_regression.ipynb Imports necessary libraries: fetch_california_housing from sklearn.datasets and autokeras as ak. ```python from sklearn.datasets import fetch_california_housing import autokeras as ak ``` -------------------------------- ### Image Classification with AutoKeras Source: https://github.com/keras-team/autokeras/blob/master/README.md This snippet demonstrates a basic workflow for image classification using AutoKeras. It involves initializing an ImageClassifier, training it with training data, and making predictions on test data. ```python import autokeras as ak clf = ak.ImageClassifier() clf.fit(x_train, y_train) results = clf.predict(x_test) ``` -------------------------------- ### Initialize and Train TextClassifier Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_classification.ipynb Initialize an AutoKeras TextClassifier with overwrite enabled and a maximum of 1 trial for a quick demo. Then, fit the classifier with the prepared training data and predict on the test data, followed by evaluating the model. ```python # Initialize the text classifier. clf = ak.TextClassifier( overwrite=True, max_trials=1 ) # It only tries 1 model as a quick demo. # 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)) ``` -------------------------------- ### Import Libraries Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Imports necessary libraries, including numpy for numerical operations and autokeras for building the model. ```python import numpy as np import autokeras as ak ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_classification.ipynb Import the required libraries for AutoKeras, Keras, NumPy, and scikit-learn's load_files function. ```python import os import keras import numpy as np from sklearn.datasets import load_files import autokeras as ak ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Imports the necessary AutoKeras and Keras datasets modules. This snippet prepares the environment for data loading and model initialization. ```python from keras.datasets import mnist import autokeras as ak ``` -------------------------------- ### Initialize and Fit TextRegressor Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_regression.ipynb Initializes a TextRegressor with overwrite enabled and a maximum of 1 trial. It then fits the model to the training data and predicts on the test data. ```python # Initialize the text regressor. reg = ak.TextRegressor( overwrite=True, max_trials=1 # It tries 10 different models. ) # Feed the text regressor with training data. reg.fit(x_train, y_train, epochs=1, batch_size=2) # Predict with the best model. predicted_y = reg.predict(x_test) # Evaluate the best model with testing data. print(reg.evaluate(x_test, y_test)) ``` -------------------------------- ### Initialize and Train TextRegressor Source: https://context7.com/keras-team/autokeras/llms.txt Configures and trains a text regression model using AutoKeras. ```python # Initialize the TextRegressor reg = ak.TextRegressor( max_trials=3, loss='mean_squared_error', overwrite=True ) # Train the model reg.fit( x_train, y_train, epochs=5, batch_size=2 ) # Predict ratings predictions = reg.predict(x_test) print(f"Predicted ratings: {predictions}") ``` -------------------------------- ### Run GPU container manually Source: https://github.com/keras-team/autokeras/blob/master/docker/README.md Legacy method for running GPU-enabled containers without nvidia-docker. ```bash export CUDA_SO=$(\ls /usr/lib/x86_64-linux-gnu/libcuda.* | xargs -I{} echo '-v {}:{}') export DEVICES=$(\ls /dev/nvidia* | xargs -I{} echo '--device {}:{}') docker run -it -p 8888:8888 $CUDA_SO $DEVICES gcr.io/tensorflow/tensorflow:latest-gpu ``` -------------------------------- ### Initialize and Train ImageClassifier Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Initializes an AutoKeras ImageClassifier and trains it on the prepared data. Sets max_trials to 1 and epochs to 1 for a quick demo. The model will overwrite previous results. ```python # Initialize the image classifier. clf = ak.ImageClassifier(overwrite=True, max_trials=1) # Feed the image classifier with training data. clf.fit(x_train, y_train, epochs=1) # Predict with the best model. predicted_y = clf.predict(x_test) print(predicted_y) # Evaluate the best model with testing data. print(clf.evaluate(x_test, y_test)) ``` -------------------------------- ### Initialize and Train ImageRegressor Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_regression.ipynb Initializes an AutoKeras ImageRegressor with overwrite enabled and a maximum of 1 trial. It then trains the regressor with the prepared training data for 1 epoch and predicts/evaluates on the test data. ```python # Initialize the image regressor. reg = ak.ImageRegressor(overwrite=True, max_trials=1) # Feed the image regressor with training data. reg.fit(x_train, y_train, epochs=1) # Predict with the best model. predicted_y = reg.predict(x_test) print(predicted_y) # Evaluate the best model with testing data. print(reg.evaluate(x_test, y_test)) ``` -------------------------------- ### Import Libraries Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/export.ipynb Import necessary libraries including numpy, mnist dataset from keras.datasets, load_model from keras.models, and autokeras. ```python import numpy as np from keras.datasets import mnist from keras.models import load_model import autokeras as ak ``` -------------------------------- ### Define a Custom Search Space with AutoModel Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Construct a multi-input, multi-output model by chaining AutoKeras blocks and passing them to the AutoModel class. ```python input_node1 = ak.ImageInput() output_node = ak.Normalization()(input_node1) output_node = ak.ImageAugmentation()(output_node) output_node1 = ak.ConvBlock()(output_node) output_node2 = ak.ResNetBlock(version="v2")(output_node) output_node1 = ak.Merge()([output_node1, output_node2]) input_node2 = ak.Input() output_node2 = ak.DenseBlock()(input_node2) output_node = ak.Merge()([output_node1, output_node2]) output_node1 = ak.ClassificationHead()(output_node) output_node2 = ak.RegressionHead()(output_node) auto_model = ak.AutoModel( inputs=[input_node1, input_node2], outputs=[output_node1, output_node2], overwrite=True, max_trials=2, ) image_data = np.random.rand(num_instances, 32, 32, 3).astype(np.float32) numerical_data = np.random.rand(num_instances, 20).astype(np.float32) regression_target = np.random.rand(num_instances, 1).astype(np.float32) classification_target = np.random.randint(5, size=num_instances) auto_model.fit( [image_data, numerical_data], [classification_target, regression_target], batch_size=3, epochs=1, ) ``` -------------------------------- ### Preprocessors Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Components for preprocessing and augmenting input data. ```APIDOC ## Preprocessors ### Description These components are used for preprocessing and augmenting the input data before it is fed into the model. ### Preprocessors: - **FeatureEngineering**: Performs automated feature engineering. - **ImageAugmentation**: Applies various augmentation techniques to image data. - **LightGBM**: Integrates LightGBM for gradient boosting. - **Normalization**: Standardizes data by scaling it to a specific range. ### Endpoint /block/#featureengineering-class, /block/#imageaugmentation-class, /block/#lightgbm-class, /block/#normalization-class ``` -------------------------------- ### Prepare IMDB Dataset for Text Classification Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_classification.ipynb Download, extract, and load the IMDB movie reviews dataset. It prepares training and testing data by selecting a subset and printing their shapes and a sample of the first text entry. ```python dataset = keras.utils.get_file( fname="aclImdb.tar.gz", origin="http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz", extract=True, ) # set path to dataset IMDB_DATADIR = os.path.join( os.path.dirname(dataset), "aclImdb_extracted", "aclImdb" ) classes = ["pos", "neg"] train_data = load_files( os.path.join(IMDB_DATADIR, "train"), shuffle=True, categories=classes ) test_data = load_files( os.path.join(IMDB_DATADIR, "test"), shuffle=False, categories=classes ) x_train = np.array(train_data.data)[:100] y_train = np.array(train_data.target)[:100] x_test = np.array(test_data.data)[:100] y_test = np.array(test_data.target)[:100] print(x_train.shape) # (25000,) print(y_train.shape) # (25000, 1) print(x_train[0][:50]) # this film was just brilliant casting ``` -------------------------------- ### Custom Search Space with Fine-Grained Blocks Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_regression.ipynb Builds a custom search space using AutoModel with more granular blocks, including Normalization, ImageAugmentation (with horizontal flip disabled), and a ResNetBlock (version 'v2'). The model is then trained. ```python 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.RegressionHead()(output_node) reg = ak.AutoModel( inputs=input_node, outputs=output_node, overwrite=True, max_trials=1 ) reg.fit(x_train, y_train, epochs=1) ``` -------------------------------- ### Train and Export Image Classifier Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/export.ipynb Initialize an ImageClassifier, train it on the MNIST dataset, export it as a Keras Model, save it, and then load it back for prediction. ```python (x_train, y_train), (x_test, y_test) = mnist.load_data() # Initialize the image classifier. clf = ak.ImageClassifier( overwrite=True, max_trials=1 ) # Try only 1 model.(Increase accordingly) # Feed the image classifier with training data. clf.fit(x_train, y_train, epochs=1) # Change no of epochs to improve the model # Export as a Keras Model. model = clf.export_model() print(type(model)) # model.save("model_autokeras.keras") loaded_model = load_model( "model_autokeras.keras", custom_objects=ak.CUSTOM_OBJECTS ) predicted_y = loaded_model.predict(np.expand_dims(x_test, -1)) print(predicted_y) ``` -------------------------------- ### Implement a Custom Block Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Create a new block by extending the base Block class and overriding the build method for hyperparameter tuning. ```python class SingleDenseLayerBlock(ak.Block): def build(self, hp, inputs=None): # Get the input_node from inputs. input_node = tree.flatten(inputs)[0] layer = keras.layers.Dense( hp.Int("num_units", min_value=32, max_value=512, step=32) ) output_node = layer(input_node) return output_node ``` -------------------------------- ### Import Libraries Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_classification.ipynb Imports necessary libraries including Keras, pandas, and AutoKeras. Ensure these are imported before using any AutoKeras functionalities. ```python import keras import pandas as pd import autokeras as ak ``` -------------------------------- ### Initialize and Train StructuredDataClassifier Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_classification.ipynb Initializes a StructuredDataClassifier with overwrite enabled and sets the maximum number of trials. It then fits the model to the training data and predicts on the test data, followed by evaluation. ```python # Initialize the structured data classifier. clf = ak.StructuredDataClassifier( overwrite=True, max_trials=3 ) # It tries 3 different models. # Feed the structured data classifier with training data. clf.fit( x_train, y_train, epochs=10, ) # 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)) ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_regression.ipynb Imports necessary libraries from Keras and AutoKeras. Loads the MNIST dataset and preprocesses it for a regression task by selecting a subset and printing its shape. ```python from keras.datasets import mnist import autokeras as ak ``` ```python (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train[:100] y_train = y_train[:100] x_test = x_test[:100] y_test = y_test[:100] print(x_train.shape) # (60000, 28, 28) print(y_train.shape) # (60000,) print(y_train[:3]) # array([7, 2, 1], dtype=uint8) ``` -------------------------------- ### Custom Search Space with Fine-Grained Blocks Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Builds a custom search space using AutoModel with more granular blocks like Normalization, ImageAugmentation, and ResNetBlock. This provides finer control over the model architecture and preprocessing steps. ```python 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) ``` -------------------------------- ### Train and Evaluate AutoModel Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Load MNIST data and execute the training and evaluation workflow for an AutoModel instance. ```python (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape) # (60000, 28, 28) print(y_train.shape) # (60000,) print(y_train[:3]) # array([7, 2, 1], dtype=uint8) # Feed the AutoModel with training data. auto_model.fit(x_train[:100], y_train[:100], epochs=1) # Predict with the best model. predicted_y = auto_model.predict(x_test) # Evaluate the best model with testing data. print(auto_model.evaluate(x_test, y_test)) ``` -------------------------------- ### Configure Hyperparameter Tuners Source: https://context7.com/keras-team/autokeras/llms.txt Select different tuning strategies for AutoKeras classifiers to optimize search efficiency. ```python import autokeras as ak # Greedy tuner (default) - fast, good for initial exploration clf_greedy = ak.ImageClassifier( tuner='greedy', max_trials=10 ) # Bayesian optimization - efficient for expensive evaluations clf_bayesian = ak.ImageClassifier( tuner='bayesian', max_trials=20 ) # Hyperband - efficient budget allocation clf_hyperband = ak.ImageClassifier( tuner='hyperband', max_trials=30 ) # Random search - simple baseline clf_random = ak.ImageClassifier( tuner='random', max_trials=50 ) ``` -------------------------------- ### Build Custom Architectures with AutoModel Source: https://context7.com/keras-team/autokeras/llms.txt Uses the functional API to define custom search spaces for model architectures. ```python import numpy as np import autokeras as ak # Prepare sample data x_train = np.random.rand(100, 28, 28, 1).astype(np.float32) y_train = np.random.randint(0, 10, 100) # Build custom architecture with functional API input_node = ak.ImageInput() output_node = ak.ImageBlock( block_type='vanilla', # Use vanilla ConvNet normalize=True, augment=True )(input_node) output_node = ak.ClassificationHead(num_classes=10)(output_node) # Create AutoModel with custom architecture auto_model = ak.AutoModel( inputs=input_node, outputs=output_node, max_trials=5, overwrite=True ) # Train auto_model.fit(x_train, y_train, epochs=5) # Export best Keras model best_model = auto_model.export_model() best_model.summary() ``` -------------------------------- ### Customize metrics and loss in AutoKeras Source: https://github.com/keras-team/autokeras/blob/master/docs/templates/tutorial/faq.md Configures an ImageClassifier with specific metrics and loss functions. ```python import autokeras as ak clf = ak.ImageClassifier( max_trials=3, metrics=['mse'], loss='mse', ) ``` -------------------------------- ### Train with Callbacks and Export Models Source: https://context7.com/keras-team/autokeras/llms.txt Integrate Keras callbacks during training and save the resulting model for future use. ```python import numpy as np import keras import autokeras as ak # Training data x_train = np.random.rand(500, 28, 28).astype(np.float32) y_train = np.random.randint(0, 10, 500) # Define callbacks callbacks = [ keras.callbacks.EarlyStopping( monitor='val_loss', patience=5, restore_best_weights=True ), keras.callbacks.ReduceLROnPlateau( monitor='val_loss', factor=0.2, patience=3 ), keras.callbacks.ModelCheckpoint( filepath='best_model.keras', save_best_only=True ) ] # Train with callbacks clf = ak.ImageClassifier(max_trials=5, overwrite=True) history = clf.fit( x_train, y_train, epochs=20, callbacks=callbacks, validation_split=0.2 ) # Access training history print(f"Final validation loss: {history.history['val_loss'][-1]}") # Export the best model best_model = clf.export_model() best_model.save('exported_model.keras') # Load and use the exported model loaded_model = keras.models.load_model('exported_model.keras') predictions = loaded_model.predict(x_train[:5]) ``` -------------------------------- ### Construct Custom Architectures with Building Blocks Source: https://context7.com/keras-team/autokeras/llms.txt Utilize specific blocks to define custom neural network architectures for various data types. ```python import numpy as np import autokeras as ak # ConvBlock for custom CNN architecture input_node = ak.Input() output_node = ak.ConvBlock( kernel_size=3, # Fixed kernel size (or tuned if None) num_blocks=2, # Number of conv blocks filters=64, # Number of filters max_pooling=True, # Use max pooling dropout=0.25 # Dropout rate )(input_node) output_node = ak.ClassificationHead(num_classes=10)(output_node) model = ak.AutoModel( inputs=input_node, outputs=output_node, max_trials=3, overwrite=True ) ``` ```python import autokeras as ak # DenseBlock for fully connected layers input_node = ak.Input() output_node = ak.DenseBlock( num_layers=3, # Number of dense layers num_units=128, # Units per layer use_batchnorm=True, # Use batch normalization dropout=0.5 # Dropout rate )(input_node) output_node = ak.ClassificationHead(num_classes=2)(output_node) ``` ```python import autokeras as ak # Use pretrained ResNet input_node = ak.ImageInput() output_node = ak.ResNetBlock( version='v2', # ResNet v1 or v2 pretrained=True # Use ImageNet weights )(input_node) output_node = ak.ClassificationHead(num_classes=10)(output_node) # Or use EfficientNet input_node = ak.ImageInput() output_node = ak.EfficientNetBlock( version='b0', # EfficientNet version (b0-b7) pretrained=True )(input_node) output_node = ak.ClassificationHead(num_classes=10)(output_node) ``` ```python import numpy as np import autokeras as ak # RNNBlock for sequence data input_node = ak.Input() output_node = ak.RNNBlock( layer_type='lstm', # 'lstm' or 'gru' num_layers=2, # Number of RNN layers bidirectional=True, # Use bidirectional RNN return_sequences=False # Return last output only )(input_node) output_node = ak.ClassificationHead(num_classes=5)(output_node) ``` -------------------------------- ### Custom Search Space with ImageBlock Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_regression.ipynb Defines a custom search space using AutoModel, specifying an ImageBlock with 'resnet' block_type, and disabling normalization and augmentation. The model is then trained. ```python input_node = ak.ImageInput() output_node = ak.ImageBlock( # Only search ResNet architectures. block_type="resnet", # Normalize the dataset. normalize=False, # Do not do data augmentation. augment=False, )(input_node) output_node = ak.RegressionHead()(output_node) reg = ak.AutoModel( inputs=input_node, outputs=output_node, overwrite=True, max_trials=1 ) reg.fit(x_train, y_train, epochs=1) ``` -------------------------------- ### Generate Multi-task Targets Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Generates random regression targets and classification labels for multi-task output. Regression targets are floats, and classification labels are integers representing 5 classes. ```python # Generate regression targets. regression_target = np.random.rand(num_instances, 1).astype(np.float32) # Generate classification labels of five classes. classification_target = np.random.randint(5, size=num_instances) ``` -------------------------------- ### Initialize Regressor with Custom Column Names and Types Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_regression.ipynb Initializes a StructuredDataRegressor specifying column names and types. 'MedInc' and 'Latitude' are explicitly set as numerical. Other columns will have their types inferred. ```python # Initialize the structured data regressor. reg = ak.StructuredDataRegressor( column_names=[ "MedInc", "HouseAge", "AveRooms", "AveBedrms", "Population", "AveOccup", "Latitude", "Longitude", ], column_types={"MedInc": "numerical", "Latitude": "numerical"}, max_trials=10, # It tries 10 different models. overwrite=True, ) ``` -------------------------------- ### Define Custom Search Space with AutoModel Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Construct a custom neural network topology using AutoKeras functional API blocks. ```python 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 ) ``` -------------------------------- ### Load and Prepare Titanic Dataset Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_classification.ipynb Loads the Titanic dataset from URLs using keras.utils.get_file and pandas. It separates the features (x) from the target variable (y) for both training and testing sets. ```python TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv" TEST_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/eval.csv" train_file_path = keras.utils.get_file("train.csv", TRAIN_DATA_URL) test_file_path = keras.utils.get_file("eval.csv", TEST_DATA_URL) # Load data into numpy arrays train_df = pd.read_csv(train_file_path) test_df = pd.read_csv(test_file_path) y_train = train_df["survived"].values x_train = train_df.drop("survived", axis=1).values y_test = test_df["survived"].values x_test = test_df.drop("survived", axis=1).values ``` -------------------------------- ### Mount external data volume Source: https://github.com/keras-team/autokeras/blob/master/docker/README.md Pass a local directory to the container using the DATA variable. ```bash make DATA=~/mydata ``` -------------------------------- ### Initialize Classifier with Column Specifications Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_classification.ipynb Initializes a StructuredDataClassifier, explicitly defining column names and types. This is useful when the data source lacks headers or when specific type inference is desired. AutoKeras will infer types for unspecified columns. ```python # Initialize the structured data classifier. clf = ak.StructuredDataClassifier( column_names=[ "sex", "age", "n_siblings_spouses", "parch", "fare", "class", "deck", "embark_town", "alone", ], column_types={"sex": "categorical", "fare": "numerical"}, max_trials=10, # It tries 10 different models. overwrite=True, ) ``` -------------------------------- ### Blocks Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Reusable building blocks for constructing neural network architectures. ```APIDOC ## Blocks ### Description Blocks are modular components that can be combined to create complex neural network architectures. ### Blocks: - **ConvBlock**: Convolutional block for image processing. - **DenseBlock**: Fully connected dense block. - **Embedding**: Embedding layer for categorical data. - **Merge**: Merges outputs from multiple layers. - **ResNetBlock**: Residual network block. - **RNNBlock**: Recurrent neural network block. - **SpatialReduction**: Reduces spatial dimensions. - **TemporalReduction**: Reduces temporal dimensions. - **XceptionBlock**: Xception architecture block. - **ImageBlock**: A pre-defined block for image tasks. - **TextBlock**: A pre-defined block for text tasks. - **StructuredDataBlock**: A pre-defined block for structured data tasks. ### Endpoint /block/#convblock-class, /block/#denseblock-class, /block/#embedding-class, /block/#merge-class, /block/#resnetblock-class, /block/#rnnblock-class, /block/#spatialreduction-class, /block/#temporalreduction-class, /block/#xceptionblock-class, /block/#imageblock-class, /block/#textblock-class, /block/#structureddatablock-class ``` -------------------------------- ### Generate Multi-modal Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Generates random image and numerical data for multi-modal input. The image data is shaped as (num_instances, 32, 32, 3) and numerical data as (num_instances, 20). ```python num_instances = 10 # Generate image data. image_data = np.random.rand(num_instances, 32, 32, 3).astype(np.float32) # Generate numerical data. numerical_data = np.random.rand(num_instances, 20).astype(np.float32) ``` -------------------------------- ### Image Classification with ImageClassifier Source: https://context7.com/keras-team/autokeras/llms.txt Automates the search for the best convolutional neural network architecture for image classification tasks. ```python import numpy as np import autokeras as ak from keras.datasets import mnist # Load and prepare the MNIST dataset (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape) # (60000, 28, 28) print(y_train.shape) # (60000,) # Initialize the ImageClassifier clf = ak.ImageClassifier( max_trials=10, # Number of different models to try overwrite=True, # Overwrite previous search results objective='val_accuracy' # Metric to optimize ) # Search for the best model clf.fit( x_train, y_train, epochs=10, validation_split=0.2 # Use 20% of data for validation ) # Evaluate on test data accuracy = clf.evaluate(x_test, y_test) print(f"Test accuracy: {accuracy}") # Make predictions predictions = clf.predict(x_test) # Export the best model as a Keras model best_model = clf.export_model() best_model.summary() ``` -------------------------------- ### Integrate Custom Block into AutoModel Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Use a custom block within an AutoModel pipeline and train it on synthetic data. ```python # Build the AutoModel input_node = ak.Input() output_node = SingleDenseLayerBlock()(input_node) output_node = ak.RegressionHead()(output_node) auto_model = ak.AutoModel(input_node, output_node, overwrite=True, max_trials=1) # Prepare Data num_instances = 100 x_train = np.random.rand(num_instances, 20).astype(np.float32) y_train = np.random.rand(num_instances, 1).astype(np.float32) x_test = np.random.rand(num_instances, 20).astype(np.float32) y_test = np.random.rand(num_instances, 1).astype(np.float32) # Train the model auto_model.fit(x_train, y_train, epochs=1) print(auto_model.evaluate(x_test, y_test)) ``` -------------------------------- ### AutoModel Class Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb The AutoModel class is the primary interface for building and training AutoKeras models. ```APIDOC ## AutoModel Class ### Description The AutoModel class serves as the main entry point for users to define, search, and train machine learning models automatically. ### Endpoint /auto_model/#automodel-class ### Parameters This class has various parameters for configuring the search space, objective, and other training aspects. Please refer to the AutoModel documentation for detailed parameter information. ``` -------------------------------- ### Input Nodes Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/customized.ipynb Defines the different types of input data that AutoKeras can handle. ```APIDOC ## Input Nodes ### Description These classes represent the different types of input data that can be fed into an AutoKeras model. ### Nodes: - **ImageInput**: For image data. - **Input**: A generic input node. - **TextInput**: For text data. - **StructuredDataInput**: For structured (tabular) data. ### Endpoint /node/#imageinput-class, /node/#input-class, /node/#textinput-class, /node/#structureddatainput-class ``` -------------------------------- ### Train with Validation Split Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_regression.ipynb Trains the regressor using a portion of the training data as validation data. The last 15% of the training data is used for validation. ```python reg.fit( x_train, y_train, # Split the training data and use the last 15% as validation data. validation_split=0.15, epochs=10, ) ``` -------------------------------- ### Define and Train Custom AutoModel for Text Classification Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_classification.ipynb Define a custom search space for a text classification task using AutoModel. It sets up input and output nodes and a classification head, then fits the model with the training data. ```python input_node = ak.TextInput() output_node = ak.TextBlock()(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, batch_size=2) ``` -------------------------------- ### Train with Custom Validation Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_regression.ipynb Trains the regressor using a custom validation set provided via the `validation_data` argument. The training data is first split, and then the custom validation set is used. ```python split = 500 x_val = x_train[split:] y_val = y_train[split:] x_train = x_train[:split] y_train = y_train[:split] reg.fit( x_train, y_train, # Use your own validation set. validation_data=(x_val, y_val), epochs=10, ) ``` -------------------------------- ### Fit TextRegressor with Validation Split Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_regression.ipynb Fits the TextRegressor model, specifying a validation split of 15% from the training data. This is an alternative to providing a separate validation set. ```python reg.fit( x_train, y_train, # Split the training data and use the last 15% as validation data. validation_split=0.15, ) ``` -------------------------------- ### Train with Custom Validation Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Trains the ImageClassifier using a custom validation set provided via the validation_data parameter. This allows for more control over the validation process. ```python split = 50000 x_val = x_train[split:] y_val = y_train[split:] x_train = x_train[:split] y_train = y_train[:split] clf.fit( x_train, y_train, # Use your own validation set. validation_data=(x_val, y_val), epochs=1, ) ``` -------------------------------- ### Fit TextRegressor with Custom Validation Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_regression.ipynb Fits the TextRegressor model using a custom validation set. The training data is split into training and validation subsets before fitting. ```python split = 5 x_val = x_train[split:] y_val = y_train[split:] x_train = x_train[:split] y_train = y_train[:split] reg.fit( x_train, y_train, epochs=1, # Use your own validation set. validation_data=(x_val, y_val), batch_size=2, ) ``` -------------------------------- ### Fit Model with Validation Split Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Fits the AutoModel using a portion of the training data as validation data. The `validation_split` parameter is set to 0.15, using the last 15% of the data for validation. ```python model.fit( [image_data, numerical_data], [regression_target, classification_target], # Split the training data and use the last 15% as validation data. validation_split=0.15, epochs=1, batch_size=3, ) ``` -------------------------------- ### Text Classification with TextClassifier Source: https://context7.com/keras-team/autokeras/llms.txt Handles text tokenization and embedding while searching for optimal text processing architectures. ```python import numpy as np import autokeras as ak # Prepare text classification data x_train = np.array([ "This movie was excellent and entertaining", "Terrible film, waste of time", "Great acting and wonderful story", "Boring and predictable plot", "Absolutely loved this masterpiece", "Disappointing and poorly directed" ]) y_train = np.array([1, 0, 1, 0, 1, 0]) # 1: positive, 0: negative x_test = np.array([ "Amazing performance by the cast", "Would not recommend this movie" ]) y_test = np.array([1, 0]) # Initialize the TextClassifier clf = ak.TextClassifier( max_trials=3, overwrite=True ) # Search for the best model clf.fit( x_train, y_train, epochs=5, batch_size=2 ) # Evaluate and predict accuracy = clf.evaluate(x_test, y_test) print(f"Accuracy: {accuracy}") predictions = clf.predict(x_test) print(f"Predictions: {predictions}") ``` -------------------------------- ### Text Regression with TextRegressor Source: https://context7.com/keras-team/autokeras/llms.txt Predicts continuous values from text input for tasks like sentiment scoring. ```python import numpy as np import autokeras as ak # Prepare text regression data x_train = np.array([ "Outstanding product, highly recommend", "Average quality, nothing special", "Poor quality and overpriced", "Excellent value for money", "Mediocre at best" ]) y_train = np.array([5.0, 3.0, 1.0, 4.5, 2.5]) # Rating scores x_test = np.array([ "Best purchase ever made", "Not worth the price" ]) y_test = np.array([5.0, 1.5]) ``` -------------------------------- ### Fit Model with Custom Validation Data Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/multi.ipynb Fits the AutoModel using a custom validation set provided via the `validation_data` parameter. The training data is split, and the latter part is used for validation. ```python split = 5 image_val = image_data[split:] numerical_val = numerical_data[split:] regression_val = regression_target[split:] classification_val = classification_target[split:] image_data = image_data[:split] numerical_data = numerical_data[:split] regression_target = regression_target[:split] classification_target = classification_target[:split] model.fit( [image_data, numerical_data], [regression_target, classification_target], # Use your own validation set. validation_data=( [image_val, numerical_val], [regression_val, classification_val], ), epochs=1, batch_size=3, ) ``` -------------------------------- ### BibTeX Entry for AutoKeras Source: https://github.com/keras-team/autokeras/blob/master/README.md This is the BibTeX entry for citing the AutoKeras paper. It includes author information, title, journal, year, volume, number, pages, and a URL for the paper. ```bibtex @article{JMLR:v24:20-1355, author = {Haifeng Jin and François Chollet and Qingquan Song and Xia Hu}, title = {AutoKeras: An AutoML Library for Deep Learning}, journal = {Journal of Machine Learning Research}, year = {2023}, volume = {24}, number = {6}, pages = {1--6}, url = {http://jmlr.org/papers/v24/20-1355.html} } ``` -------------------------------- ### Train with Validation Split Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/image_classification.ipynb Trains the ImageClassifier using a portion of the training data as validation data. The validation_split parameter specifies the fraction of training data to use for validation. ```python clf.fit( x_train, y_train, # Split the training data and use the last 15% as validation data. validation_split=0.15, epochs=1, ) ``` -------------------------------- ### Train with Validation Split Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/structured_data_classification.ipynb Trains the classifier using a portion of the training data as validation data. The `validation_split` parameter specifies the fraction of training data to be used for validation. ```python clf.fit( x_train, y_train, # Split the training data and use the last 15% as validation data. validation_split=0.15, epochs=10, ) ``` -------------------------------- ### Fit TextClassifier with Validation Split Source: https://github.com/keras-team/autokeras/blob/master/docs/ipynb/text_classification.ipynb Fit the TextClassifier using a portion of the training data as validation data. The `validation_split` parameter is set to 0.15, indicating that the last 15% of the training data will be used for validation. ```python clf.fit( x_train, y_train, # Split the training data and use the last 15% as validation data. validation_split=0.15, epochs=1, batch_size=2, ) ```