### Set Backend and Install AutoKeras Source: https://autokeras.com/tutorial/export Before running the export example, set the Keras backend to 'torch' and install the AutoKeras library. This ensures compatibility and availability of necessary packages. ```bash !export KERAS_BACKEND="torch" !pip install autokeras ``` -------------------------------- ### Install Development Dependencies Source: https://autokeras.com/contributing After activating the virtual environment, install AutoKeras with its test dependencies and then replace the installed package with the local version. ```bash workon ak pip install -e ".[tests]" pip uninstall autokeras add2virtualenv . ``` -------------------------------- ### Install AutoKeras with Pip (Virtualenv) Source: https://autokeras.com/install Use these commands to install AutoKeras and Keras Tuner if you are using a virtual environment. ```bash pip install git+https://github.com/keras-team/keras-tuner.git ``` ```bash pip install autokeras ``` -------------------------------- ### Install AutoKeras with Pip (No Virtualenv) Source: https://autokeras.com/install Use these commands to install AutoKeras and Keras Tuner if you are not using a virtual environment and use the 'python3' command. ```bash python3 -m pip install git+https://github.com/keras-team/keras-tuner.git ``` ```bash python3 -m pip install autokeras ``` -------------------------------- ### Install AutoKeras Package Source: https://autokeras.com/ Use this command to install the AutoKeras package via pip. Ensure your Python version is 3.7 or higher and TensorFlow is 2.8.0 or higher. ```bash pip3 install autokeras ``` -------------------------------- ### Define Customized Search Space with AutoModel Source: https://autokeras.com/tutorial/customized Build a neural network using AutoKeras building blocks and the AutoModel API, similar to the Keras functional API. This example uses a custom topology of Preprocessor -> Block -> Head. ```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 ) ``` -------------------------------- ### Train and Export AutoKeras ImageClassifier Source: https://autokeras.com/tutorial/export This example trains an AutoKeras ImageClassifier on the MNIST dataset and then exports the best found model as a Keras Model. The exported model is then saved and loaded back for making predictions. ```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 Dense Layer Block Source: https://autokeras.com/tutorial/customized Extend the `ak.Block` class to create a custom building block. This example implements a single Dense layer with a tunable number of neurons using KerasTuner's `hp` object. ```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 ``` -------------------------------- ### Initialize ImageClassifier Source: https://autokeras.com/image_classifier Instantiate the ImageClassifier with various configuration options. Defaults are used if parameters are not specified. ```python autokeras.ImageClassifier( num_classes=None, multi_label=False, loss=None, metrics=None, project_name="image_classifier", max_trials=100, directory=None, objective="val_loss", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Initialize StructuredDataRegressor Source: https://autokeras.com/structured_data_regressor Instantiate StructuredDataRegressor with optional column configurations, loss function, and tuner settings. Specify project name and directory for saving search outputs. Overwrite existing projects if needed. ```python autokeras.StructuredDataRegressor( column_names=None, column_types=None, output_dim=None, loss="mean_squared_error", metrics=None, project_name="structured_data_regressor", max_trials=100, directory=None, objective="val_loss", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Format Code Source: https://autokeras.com/contributing Run this script to automatically format your code according to the project's style guidelines. ```bash shell/format.sh ``` -------------------------------- ### Prepare Training and Testing Data Source: https://autokeras.com/tutorial/structured_data_regression Prepares the California housing dataset by splitting it into training and testing sets. 90% of the data is used for training and 10% for testing. ```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:] ``` -------------------------------- ### Initialize and Fit AutoModel for Multi-Task/Modal Source: https://autokeras.com/tutorial/multi Initializes an AutoModel with multiple inputs (image and numerical) and multiple outputs (regression and classification). Fits the model with the prepared data. ```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 ClassificationHead Source: https://autokeras.com/block Sets up a ClassificationHead for classification tasks. It automatically configures loss and metrics based on the number of classes and multi-label settings. Dropout can be specified or tuned. ```python autokeras.ClassificationHead( num_classes=None, multi_label=False, loss=None, metrics=None, dropout=None, **kwargs ) ``` -------------------------------- ### Initialize and Train TextClassifier Source: https://autokeras.com/tutorial/text_classification Initialize a TextClassifier and train it with the prepared data. Set epochs and batch_size for training. The max_trials parameter limits the number of models to try. ```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)) ``` -------------------------------- ### Prepare Data and Train AutoModel Source: https://autokeras.com/tutorial/customized Load the MNIST dataset, print its shape, and then feed the data to the AutoModel for training and prediction. This demonstrates fitting the customized model and evaluating its performance. ```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)) ``` -------------------------------- ### Run Integration Tests Source: https://autokeras.com/contributing Execute only the integration tests found in the 'tests/integration_tests' directory. These tests verify the interaction between different components. ```bash pytest tests/integration_tests ``` -------------------------------- ### Create Virtual Environment Source: https://autokeras.com/contributing Use this command to create a new virtual environment named 'ak' for Python 3 development. ```bash mkvirtualenv -p python3 ak ``` -------------------------------- ### Initialize ImageRegressor Source: https://autokeras.com/image_regressor Instantiate the ImageRegressor class with various configuration options. Defaults are provided for most parameters. ```python autokeras.ImageRegressor( output_dim=None, loss="mean_squared_error", metrics=None, project_name="image_regressor", max_trials=100, directory=None, objective="val_loss", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Initialize StructuredDataClassifier Source: https://autokeras.com/structured_data_classifier Instantiate the StructuredDataClassifier with various configuration options for column handling, model search, and training objectives. Defaults are provided for most parameters. ```python autokeras.StructuredDataClassifier( column_names=None, column_types=None, num_classes=None, multi_label=False, loss=None, metrics=None, project_name="structured_data_classifier", max_trials=100, directory=None, objective="val_accuracy", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Import Libraries and Load Data Source: https://autokeras.com/tutorial/structured_data_regression Imports necessary libraries, including scikit-learn for dataset fetching and AutoKeras for the regression model. Loads the California housing dataset. ```python from sklearn.datasets import fetch_california_housing import autokeras as ak ``` -------------------------------- ### Prepare IMDB Dataset for Regression Source: https://autokeras.com/tutorial/text_regression Downloads and prepares the IMDB dataset, extracting text data and labels for training and testing. It then samples a subset for demonstration. ```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 ``` -------------------------------- ### Load and Prepare MNIST Data Source: https://autokeras.com/tutorial/image_regression Loads the MNIST dataset and prepares a small subset for regression. It 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) ``` -------------------------------- ### Initialize and Train ImageRegressor Source: https://autokeras.com/tutorial/image_regression Initializes an ImageRegressor with overwrite enabled and a maximum of 1 trial. It then trains the regressor on the prepared data for 1 epoch. ```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)) ``` -------------------------------- ### Initialize ImageAugmentation Source: https://autokeras.com/block Initializes the ImageAugmentation block with various parameters for image transformations. Parameters can be set directly or left unspecified to be tuned automatically. ```python autokeras.ImageAugmentation( translation_factor=None, vertical_flip=None, horizontal_flip=None, rotation_factor=None, zoom_factor=None, contrast_factor=None, **kwargs ) ``` -------------------------------- ### Import Necessary Libraries Source: https://autokeras.com/tutorial/customized Import core Keras, NumPy, tree, MNIST dataset, and AutoKeras libraries. These are essential for building and training models. ```python import keras import numpy as np import tree from keras.datasets import mnist import autokeras as ak ``` -------------------------------- ### Initialize TextClassifier in AutoKeras Source: https://autokeras.com/text_classifier Instantiate the TextClassifier with various configuration options. Customize parameters like the number of classes, loss function, metrics, and search objectives. ```python autokeras.TextClassifier( num_classes=None, multi_label=False, loss=None, metrics=None, project_name="text_classifier", max_trials=100, directory=None, objective="val_loss", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Initialize TextRegressor Source: https://autokeras.com/text_regressor Instantiate TextRegressor with customizable parameters for output dimensions, loss functions, metrics, and search configurations. ```python autokeras.TextRegressor( output_dim=None, loss="mean_squared_error", metrics=None, project_name="text_regressor", max_trials=100, directory=None, objective="val_loss", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Run All Tests Source: https://autokeras.com/contributing Execute all tests in the project using pytest. Ensure your virtual environment is activated and you are in the repository directory. ```bash pytest tests ``` -------------------------------- ### Initialize and train ImageClassifier Source: https://autokeras.com/tutorial/image_classification Initialize an AutoKeras ImageClassifier and train it with the prepared training data. Set max_trials to 1 for a quick demo and epochs to 10. Predictions and evaluation are then performed. ```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)) ``` -------------------------------- ### Image Classification with AutoKeras Source: https://autokeras.com/ This snippet demonstrates a basic image classification task using AutoKeras. It initializes an ImageClassifier, fits it to training data, and then predicts 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 TextRegressor Source: https://autokeras.com/tutorial/text_regression Initializes a TextRegressor with overwrite enabled and a maximum of 1 trial. It then fits the regressor to the training data and makes predictions and evaluations 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)) ``` -------------------------------- ### ImageClassifier Constructor Source: https://autokeras.com/image_classifier Initializes the ImageClassifier with specified parameters for image classification. ```APIDOC ## ImageClassifier Constructor ### Description Initializes the ImageClassifier with specified parameters for image classification. ### Arguments - **num_classes** (int | None): Number of classes. Defaults to None (inferred from data). - **multi_label** (bool): Whether the classification is multi-label. Defaults to False. - **loss** (str | Callable | keras.losses.Loss | None): Loss function for training. Defaults to binary or categorical crossentropy. - **metrics** (List | None): List of metrics to evaluate. Defaults to 'accuracy'. - **project_name** (str): Name for the AutoModel project. Defaults to 'image_classifier'. - **max_trials** (int): Maximum number of different Keras Models to try. Defaults to 100. - **directory** (str | pathlib.Path | None): Directory to store search outputs. Defaults to None. - **objective** (str): Metric to minimize or maximize. Defaults to 'val_loss'. - **tuner** (str | Type[autokeras.engine.tuner.AutoTuner] | None): Type of tuner to use. Defaults to a task-specific tuner. - **overwrite** (bool): Whether to overwrite an existing project. Defaults to False. - **seed** (int | None): Random seed for reproducibility. - **max_model_size** (int | None): Maximum size of the model parameters. Models larger than this are rejected. - ****kwargs**: Any arguments supported by AutoModel. ``` -------------------------------- ### Load and Prepare Titanic Dataset Source: https://autokeras.com/tutorial/structured_data_classification Load the Titanic dataset from URLs using keras.utils.get_file and pandas. Prepare the data by separating features (x) and target (y) for training and testing. ```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 ``` -------------------------------- ### Import Necessary Libraries Source: https://autokeras.com/tutorial/export Import required libraries including NumPy for numerical operations, datasets from Keras, load_model for loading saved models, and AutoKeras for model building. ```python import numpy as np from keras.datasets import mnist from keras.models import load_model import autokeras as ak ``` -------------------------------- ### Import Libraries Source: https://autokeras.com/tutorial/image_regression Imports necessary libraries, including the MNIST dataset from Keras and the AutoKeras library. ```python from keras.datasets import mnist import autokeras as ak ``` -------------------------------- ### Initialize and Train StructuredDataClassifier Source: https://autokeras.com/tutorial/structured_data_classification Initialize the StructuredDataClassifier with overwrite=True and max_trials=3. Train the classifier using the prepared training data and evaluate its performance on the test data. ```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)) ``` -------------------------------- ### TextClassifier Constructor Source: https://autokeras.com/text_classifier Initializes the TextClassifier with various configuration options for model search and training. ```APIDOC ## TextClassifier Constructor ### Description Initializes the TextClassifier with various configuration options for model search and training. ### Signature ```python autokeras.TextClassifier( num_classes=None, multi_label=False, loss=None, metrics=None, project_name="text_classifier", max_trials=100, directory=None, objective="val_loss", tuner=None, overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` ### Arguments * **num_classes** (int | None): Number of classes. Defaults to None, inferred from data. * **multi_label** (bool): Whether the classification is multi-label. Defaults to False. * **loss** (str | Callable | keras.losses.Loss | None): Keras loss function. Defaults to binary or categorical crossentropy based on num_classes. * **metrics** (List[str | Callable | keras.metrics.Metric] | List[List[str | Callable | keras.metrics.Metric]] | Dict[str, str | Callable | keras.metrics.Metric] | None): List of Keras metrics. Defaults to 'accuracy'. * **project_name** (str): Name of the AutoModel project. Defaults to 'text_classifier'. * **max_trials** (int): Maximum number of different Keras Models to try. Defaults to 100. * **directory** (str | pathlib.Path | None): Path to store search outputs. Defaults to None. * **objective** (str): Name of model metric to minimize or maximize. Defaults to 'val_loss'. * **tuner** (str | Type[autokeras.engine.tuner.AutoTuner] | None): Tuner to use for the search. Can be 'greedy', 'bayesian', 'hyperband', 'random', or a subclass of AutoTuner. Defaults to a task-specific tuner. * **overwrite** (bool): Whether to overwrite an existing project. Defaults to False. * **seed** (int | None): Random seed for reproducibility. * **max_model_size** (int | None): Maximum number of scalars in model parameters. Models larger than this are rejected. ``` -------------------------------- ### Build and Train AutoModel with Custom Block Source: https://autokeras.com/tutorial/customized Integrate the custom `SingleDenseLayerBlock` into an AutoModel for regression tasks. This involves defining input and output nodes, preparing synthetic data, and training the model. ```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)) ``` -------------------------------- ### Customized search space with ImageBlock Source: https://autokeras.com/tutorial/image_classification Customize the search space using AutoModel and ImageBlock. Configure block_type, normalize, and augment for specific search strategies. ```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) ``` -------------------------------- ### Run Unit Tests Source: https://autokeras.com/contributing Run only the unit tests located within the 'tests/autokeras' directory. This is useful for quickly verifying core functionality. ```bash pytest tests/autokeras ``` -------------------------------- ### Import Necessary Libraries Source: https://autokeras.com/tutorial/text_classification Import essential libraries for AutoKeras, Keras, NumPy, and scikit-learn. These are needed for data manipulation and model building. ```python import os import keras import numpy as np from sklearn.datasets import load_files import autokeras as ak ``` -------------------------------- ### Import Libraries Source: https://autokeras.com/tutorial/multi Imports necessary libraries, including NumPy for data manipulation and AutoKeras for model building. ```python import numpy as np import autokeras as ak ``` -------------------------------- ### Customize Search Space with AutoModel Source: https://autokeras.com/tutorial/text_regression Demonstrates how to customize the search space for text regression using AutoModel. It defines input and output nodes and configures the TextBlock for high-level settings. ```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) ``` -------------------------------- ### AutoModel Constructor Source: https://autokeras.com/auto_model Initializes an AutoModel instance. This class combines a HyperModel and a Tuner to tune the HyperModel, offering a user experience similar to Keras models with `fit()` and `predict()` methods. ```APIDOC ## AutoModel Constructor ### Description Initializes an AutoModel instance. This class combines a HyperModel and a Tuner to tune the HyperModel, offering a user experience similar to Keras models with `fit()` and `predict()` methods. ### Signature ```python autokeras.AutoModel( inputs, outputs, project_name="auto_model", max_trials=100, directory=None, objective="val_loss", tuner="greedy", overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` ### Arguments * **inputs** (`autokeras.Input` | `List[autokeras.Input]`): A list of Node instances. The input node(s) of the AutoModel. * **outputs** (`autokeras.Head` | `autokeras.Node` | `list`): A list of Node or Head instances. The output node(s) or head(s) of the AutoModel. * **project_name** (`str`): The name of the AutoModel. Defaults to 'auto_model'. * **max_trials** (`int`): The maximum number of different Keras Models to try. The search may finish before reaching the max_trials. Defaults to 100. * **directory** (`str` | `pathlib.Path` | `None`): 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`): Name of model metric to minimize or maximize, e.g. 'val_accuracy'. Defaults to 'val_loss'. * **tuner** (`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`): Defaults to `False`. If `False`, reloads an existing project of the same name if one is found. Otherwise, overwrites the project. * **seed** (`int` | `None`): Random seed. * **max_model_size** (`int` | `None`): Maximum number of scalars in the parameters of a model. Models larger than this are rejected. * ****kwargs** : Any arguments supported by keras_tuner.Tuner. ``` -------------------------------- ### Initialize Head in AutoKeras Source: https://autokeras.com/base Initializes a Head object, serving as the base class for output layers like classification or regression. Accepts optional loss and metrics. ```python autokeras.Head(loss=None, metrics=None, **kwargs) ``` -------------------------------- ### Prepare IMDB Dataset for Text Classification Source: https://autokeras.com/tutorial/text_classification Download and prepare the IMDB dataset for text classification. This involves loading the data, setting the directory, and extracting text and target labels. ```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 ``` -------------------------------- ### RegressionHead Constructor Source: https://autokeras.com/block Initializes a RegressionHead for regression tasks. The targets should be numerical numpy arrays. ```APIDOC ## RegressionHead ### Description Regression Dense layers. The targets passing to the head would have to be np.ndarray. It can be single-column or multi-column. The values should all be numerical. ### Arguments - **output_dim** (int | None): Int. The number of output dimensions. Defaults to None. If None, it will be inferred from the data. - **multi_label** (Boolean): Defaults to False. - **loss** (str | Callable | keras.losses.Loss): A Keras loss function. Defaults to use `mean_squared_error`. - **metrics** (List[str | Callable | keras.metrics.Metric] | List[List[str | Callable | keras.metrics.Metric]] | Dict[str, str | Callable | keras.metrics.Metric] | None): A list of Keras metrics. Defaults to use `mean_squared_error`. - **dropout** (float | None): Float. The dropout rate for the layers. If left unspecified, it will be tuned automatically. ``` -------------------------------- ### Import Libraries Source: https://autokeras.com/tutorial/structured_data_classification Import necessary libraries including Keras, pandas, and AutoKeras. These are essential for data manipulation and model building. ```python import keras import pandas as pd import autokeras as ak ``` -------------------------------- ### Set Custom Metrics and Loss in AutoKeras Source: https://autokeras.com/tutorial/faq Specify custom metrics and loss functions when initializing an AutoKeras model. Ensure the metrics are compatible with the task. ```python import autokeras as ak clf = ak.ImageClassifier( max_trials=3, metrics=['mse'], loss='mse', ) ``` -------------------------------- ### Train and Evaluate StructuredDataRegressor Source: https://autokeras.com/tutorial/structured_data_regression Initializes and trains a StructuredDataRegressor with specified epochs and trials. It then predicts values for the test set and evaluates the model's performance. ```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)) ``` -------------------------------- ### Lint Code Source: https://autokeras.com/contributing Run this script to check your code for style violations and potential errors. ```bash shell/lint.sh ``` -------------------------------- ### Customized search space with fine-grained blocks Source: https://autokeras.com/tutorial/image_classification Further customize the search space using fine-grained blocks like Normalization, ImageAugmentation, and ResNetBlock within AutoModel. This allows for more detailed control over the model architecture. ```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) ``` -------------------------------- ### Initialize Node in AutoKeras Source: https://autokeras.com/base Initializes a Node object, which represents connections between blocks in a network. Accepts arbitrary keyword arguments. ```python autokeras.Node(**kwargs) ``` -------------------------------- ### Customized Search Space with ImageBlock Source: https://autokeras.com/tutorial/image_regression Defines a custom search space using AutoModel, specifying an ImageBlock with 'resnet' type, no normalization, and no 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) ``` -------------------------------- ### ImageRegressor Constructor Source: https://autokeras.com/image_regressor Initializes the ImageRegressor with specified parameters for image regression tasks. ```APIDOC ## ImageRegressor ### Description AutoKeras image regression class. ### Parameters #### Arguments - **output_dim** (int | None) - Optional - The number of output dimensions. Defaults to None. If None, it will be inferred from the data. - **loss** (str | Callable | keras.losses.Loss) - Optional - A Keras loss function. Defaults to use 'mean_squared_error'. - **metrics** (List[str | Callable | keras.metrics.Metric] | List[List[str | Callable | keras.metrics.Metric]] | Dict[str, str | Callable | keras.metrics.Metric] | None) - Optional - A list of Keras metrics. Defaults to use 'mean_squared_error'. - **project_name** (str) - Optional - String. The name of the AutoModel. Defaults to 'image_regressor'. - **max_trials** (int) - Optional - Int. The maximum number of different Keras Models to try. The search may finish before reaching the max_trials. Defaults to 100. - **directory** (str | pathlib.Path | None) - Optional - 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) - Optional - String. Name of model metric to minimize or maximize, e.g. 'val_accuracy'. Defaults to 'val_loss'. - **tuner** (str | Type[autokeras.engine.tuner.AutoTuner] | None) - Optional - 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. If left unspecified, it uses a task specific tuner, which first evaluates the most commonly used models for the task before exploring other models. - **overwrite** (bool) - Optional - Boolean. Defaults to `False`. If `False`, reloads an existing project of the same name if one is found. Otherwise, overwrites the project. - **seed** (int | None) - Optional - Int. Random seed. - **max_model_size** (int | None) - Optional - Int. Maximum number of scalars in the parameters of a model. Models larger than this are rejected. - ****kwargs** - Optional - Any arguments supported by AutoModel. ``` -------------------------------- ### Initialize Block in AutoKeras Source: https://autokeras.com/base Initializes a Block object, the base class for network components. Many arguments default to tunable variables, allowing for flexible search space construction. ```python autokeras.Block(**kwargs) ``` -------------------------------- ### AutoModel with Inferred Architecture Source: https://autokeras.com/auto_model Use this when you want AutoModel to infer the model architecture based on the provided input and output types. ```python import autokeras as ak ak.AutoModel( inputs=[ak.ImageInput(), ak.TextInput()], outputs=[ak.ClassificationHead(), ak.RegressionHead()] ) ``` -------------------------------- ### TextRegressor Constructor Source: https://autokeras.com/text_regressor Initializes the TextRegressor model with specified parameters for automated text regression. ```APIDOC ## TextRegressor ### Description AutoKeras text regression class. ### Arguments - **output_dim** (int | None): The number of output dimensions. Defaults to None. If None, it will be inferred from the data. - **loss** (str | Callable | keras.losses.Loss): A Keras loss function. Defaults to use 'mean_squared_error'. - **metrics** (List[str | Callable | keras.metrics.Metric] | List[List[str | Callable | keras.metrics.Metric]] | Dict[str, str | Callable | keras.metrics.Metric] | None): A list of Keras metrics. Defaults to use 'mean_squared_error'. - **project_name** (str): The name of the AutoModel. Defaults to 'text_regressor'. - **max_trials** (int): The maximum number of different Keras Models to try. The search may finish before reaching the max_trials. Defaults to 100. - **directory** (str | pathlib.Path | None): 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): Name of model metric to minimize or maximize, e.g. 'val_accuracy'. Defaults to 'val_loss'. - **tuner** (str | Type[autokeras.engine.tuner.AutoTuner] | None): 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. If left unspecified, it uses a task specific tuner, which first evaluates the most commonly used models for the task before exploring other models. - **overwrite** (bool): Defaults to `False`. If `False`, reloads an existing project of the same name if one is found. Otherwise, overwrites the project. - **seed** (int | None): Random seed. - **max_model_size** (int | None): Maximum number of scalars in the parameters of a model. Models larger than this are rejected. - **kwargs**: Any arguments supported by AutoModel. ``` -------------------------------- ### StructuredDataRegressor Constructor Source: https://autokeras.com/structured_data_regressor Initializes the StructuredDataRegressor with specified parameters for data handling, model search, and training objectives. ```APIDOC ## StructuredDataRegressor ### Description AutoKeras structured data regression class. ### Parameters - **column_names** (List[str] | None) - Optional - A list of strings specifying the names of the columns. Defaults to None. - **column_types** (Dict[str, str] | None) - Optional - Dict specifying the type of each column ('numerical' or 'categorical'). Defaults to None. - **output_dim** (int | None) - Optional - The number of output dimensions. Defaults to None. - **loss** (str | Callable | keras.losses.Loss) - Optional - A Keras loss function. Defaults to 'mean_squared_error'. - **metrics** (List[str | Callable | keras.metrics.Metric] | List[List[str | Callable | keras.metrics.Metric]] | Dict[str, str | Callable | keras.metrics.Metric] | None) - Optional - A list of Keras metrics. Defaults to 'mean_squared_error'. - **project_name** (str) - Optional - The name of the AutoModel. Defaults to 'structured_data_regressor'. - **max_trials** (int) - Optional - The maximum number of different Keras Models to try. Defaults to 100. - **directory** (str | pathlib.Path | None) - Optional - The path to a directory for storing the search outputs. Defaults to None. - **objective** (str) - Optional - Name of model metric to minimize or maximize. Defaults to 'val_loss'. - **tuner** (str | Type[autokeras.engine.tuner.AutoTuner] | None) - Optional - String or subclass of AutoTuner. Defaults to a task specific tuner. - **overwrite** (bool) - Optional - Defaults to `False`. If `False`, reloads an existing project. Otherwise, overwrites the project. - **seed** (int | None) - Optional - Random seed. - **max_model_size** (int | None) - Optional - Maximum number of scalars in the parameters of a model. Models larger than this are rejected. - ****kwargs** : Any arguments supported by AutoModel. ``` -------------------------------- ### Initialize Classifier with Column Specifications Source: https://autokeras.com/tutorial/structured_data_classification Initialize the StructuredDataClassifier by explicitly providing column names and types. This is useful when the data source does not have headers or when you need to enforce specific data types. ```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, ) ``` -------------------------------- ### ConvBlock for Vanilla ConvNets Source: https://autokeras.com/block Use for vanilla Convolutional Neural Networks. Parameters like kernel_size, filters, and pooling can be tuned automatically. ```python autokeras.ConvBlock( kernel_size=None, num_blocks=None, num_layers=None, filters=None, max_pooling=None, separable=None, dropout=None, **kwargs ) ``` -------------------------------- ### StructuredDataClassifier Constructor Source: https://autokeras.com/structured_data_classifier Initializes the StructuredDataClassifier with various configuration options for data handling, model search, and training. ```APIDOC ## StructuredDataClassifier ### Description AutoKeras structured data classification class. ### Parameters #### Arguments - **column_names** (List[str] | None) - Optional - A list of strings specifying the names of the columns. The length of the list should be equal to the number of columns of the data excluding the target column. Defaults to None. - **column_types** (Dict | None) - Optional - Dict. The keys are the column names. The values should either be 'numerical' or 'categorical', indicating the type of that column. Defaults to None. If not None, the column_names need to be specified. If None, it will be inferred from the data. - **num_classes** (int | None) - Optional - Int. Defaults to None. If None, it will be inferred from the data. - **multi_label** (bool) - Optional - Boolean. Defaults to False. - **loss** (str | Callable | keras.losses.Loss | None) - Optional - A Keras loss function. Defaults to use 'binary_crossentropy' or 'categorical_crossentropy' based on the number of classes. - **metrics** (List[str | Callable | keras.metrics.Metric] | List[List[str | Callable | keras.metrics.Metric]] | Dict[str, str | Callable | keras.metrics.Metric] | None) - Optional - A list of Keras metrics. Defaults to use 'accuracy'. - **project_name** (str) - Optional - String. The name of the AutoModel. Defaults to 'structured_data_classifier'. - **max_trials** (int) - Optional - Int. The maximum number of different Keras Models to try. The search may finish before reaching the max_trials. Defaults to 100. - **directory** (str | pathlib.Path | None) - Optional - 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) - Optional - String. Name of model metric to minimize or maximize. Defaults to 'val_accuracy'. - **tuner** (str | Type[autokeras.engine.tuner.AutoTuner] | None) - Optional - 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. If left unspecified, it uses a task specific tuner, which first evaluates the most commonly used models for the task before exploring other models. - **overwrite** (bool) - Optional - Boolean. Defaults to `False`. If `False`, reloads an existing project of the same name if one is found. Otherwise, overwrites the project. - **seed** (int | None) - Optional - Int. Random seed. - **max_model_size** (int | None) - Optional - Int. Maximum number of scalars in the parameters of a model. Models larger than this are rejected. - **kwargs** - Optional - Any arguments supported by AutoModel. ``` -------------------------------- ### AutoModel Constructor Source: https://autokeras.com/auto_model The AutoModel constructor defines the inputs, outputs, and various tuning parameters for the model search. ```python autokeras.AutoModel( inputs, outputs, project_name="auto_model", max_trials=100, directory=None, objective="val_loss", tuner="greedy", overwrite=False, seed=None, max_model_size=None, **kwargs ) ``` -------------------------------- ### Generate Multi-Task Targets Source: https://autokeras.com/tutorial/multi Generates random regression targets and classification labels for multi-task learning. Classification labels are integers. ```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) ``` -------------------------------- ### Block Class Source: https://autokeras.com/base The base class for different blocks, which can be connected to build search spaces. ```APIDOC ## Block ### Description The base class for different Block. The Block can be connected together to build the search space for an AutoModel. Notably, many args in the **init** function are defaults to be a tunable variable when not specified by the user. ### Class Signature ```python autokeras.Block(**kwargs) ``` ``` -------------------------------- ### evaluate Source: https://autokeras.com/structured_data_regressor Evaluate the best model for the given data. ```APIDOC ## evaluate ### Description Evaluate the best model for the given data. ### Method Signature `StructuredDataRegressor.evaluate(x, y=None, **kwargs)` ### Parameters * **x** (numpy.ndarray) - Testing data x. It can be string or numerical data. * **y** (numpy.ndarray) - Testing data y. It can be string labels or numerical values. * **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. ```