

# Text Classification - TensorFlow
<a name="text-classification-tensorflow"></a>

The Amazon SageMaker AI Text Classification - TensorFlow algorithm is a supervised learning algorithm that supports transfer learning with many pretrained models from the [TensorFlow Hub](https://tfhub.dev/). Use transfer learning to fine-tune one of the available pretrained models on your own dataset, even if a large amount of text data is not available. The text classification algorithm takes a text string as input and outputs a probability for each of the class labels. Training datasets must be in CSV format. This page includes information about Amazon EC2 instance recommendations and sample notebooks for Text Classification - TensorFlow.

**Topics**
+ [How to use the SageMaker AI Text Classification - TensorFlow algorithm](text-classification-tensorflow-how-to-use.md)
+ [Input and output interface for the Text Classification - TensorFlow algorithm](text-classification-tensorflow-inputoutput.md)
+ [Amazon EC2 instance recommendation for the Text Classification - TensorFlow algorithm](#text-classification-tensorflow-instances)
+ [Text Classification - TensorFlow sample notebooks](#text-classification-tensorflow-sample-notebooks)
+ [How Text Classification - TensorFlow Works](text-classification-tensorflow-HowItWorks.md)
+ [TensorFlow Hub Models](text-classification-tensorflow-Models.md)
+ [Text Classification - TensorFlow Hyperparameters](text-classification-tensorflow-Hyperparameter.md)
+ [Tune a Text Classification - TensorFlow model](text-classification-tensorflow-tuning.md)

# How to use the SageMaker AI Text Classification - TensorFlow algorithm
<a name="text-classification-tensorflow-how-to-use"></a>

You can use Text Classification - TensorFlow as an Amazon SageMaker AI built-in algorithm. The following section describes how to use Text Classification - TensorFlow with the SageMaker AI Python SDK. For information on how to use Text Classification - TensorFlow from the Amazon SageMaker Studio Classic UI, see [SageMaker JumpStart pretrained models](studio-jumpstart.md).

The Text Classification - TensorFlow algorithm supports transfer learning using any of the compatible pretrained TensorFlow models. For a list of all available pretrained models, see [TensorFlow Hub Models](text-classification-tensorflow-Models.md). Every pretrained model has a unique `model_id`. The following example uses BERT Base Uncased (`model_id`: `tensorflow-tc-bert-en-uncased-L-12-H-768-A-12-2`) to fine-tune on a custom dataset. The pretrained models are all pre-downloaded from the TensorFlow Hub and stored in Amazon S3 buckets so that training jobs can run in network isolation. Use these pre-generated model training artifacts to construct a SageMaker AI Estimator.

First, retrieve the Docker image URI, training script URI, and pretrained model URI. Then, change the hyperparameters as you see fit. You can see a Python dictionary of all available hyperparameters and their default values with `hyperparameters.retrieve_default`. For more information, see [Text Classification - TensorFlow Hyperparameters](text-classification-tensorflow-Hyperparameter.md). Use these values to construct a SageMaker AI Estimator.

**Note**  
Default hyperparameter values are different for different models. For example, for larger models, the default batch size is smaller. 

This example uses the [https://www.tensorflow.org/datasets/catalog/glue#gluesst2](https://www.tensorflow.org/datasets/catalog/glue#gluesst2) dataset, which contains positive and negative movie reviews. We pre-downloaded the dataset and made it available with Amazon S3. To fine-tune your model, call `.fit` using the Amazon S3 location of your training dataset. Any S3 bucket used in a notebook must be in the same AWS Region as the notebook instance that accesses it.

```
from sagemaker import image_uris, model_uris, script_uris, hyperparameters
from sagemaker.estimator import Estimator

model_id, model_version = "tensorflow-tc-bert-en-uncased-L-12-H-768-A-12-2", "*"
training_instance_type = "ml.p3.2xlarge"

# Retrieve the Docker image
train_image_uri = image_uris.retrieve(model_id=model_id,model_version=model_version,image_scope="training",instance_type=training_instance_type,region=None,framework=None)

# Retrieve the training script
train_source_uri = script_uris.retrieve(model_id=model_id, model_version=model_version, script_scope="training")

# Retrieve the pretrained model tarball for transfer learning
train_model_uri = model_uris.retrieve(model_id=model_id, model_version=model_version, model_scope="training")

# Retrieve the default hyperparameters for fine-tuning the model
hyperparameters = hyperparameters.retrieve_default(model_id=model_id, model_version=model_version)

# [Optional] Override default hyperparameters with custom values
hyperparameters["epochs"] = "5"

# Sample training data is available in this bucket
training_data_bucket = f"jumpstart-cache-prod-{aws_region}"
training_data_prefix = "training-datasets/SST2/"

training_dataset_s3_path = f"s3://{training_data_bucket}/{training_data_prefix}"

output_bucket = sess.default_bucket()
output_prefix = "jumpstart-example-tc-training"
s3_output_location = f"s3://{output_bucket}/{output_prefix}/output"

# Create an Estimator instance
tf_tc_estimator = Estimator(
    role=aws_role,
    image_uri=train_image_uri,
    source_dir=train_source_uri,
    model_uri=train_model_uri,
    entry_point="transfer_learning.py",
    instance_count=1,
    instance_type=training_instance_type,
    max_run=360000,
    hyperparameters=hyperparameters,
    output_path=s3_output_location,
)

# Launch a training job
tf_tc_estimator.fit({"training": training_dataset_s3_path}, logs=True)
```

For more information about how to use the SageMaker Text Classification - TensorFlow algorithm for transfer learning on a custom dataset, see the [Introduction to JumpStart - Text Classification](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/jumpstart_text_classification/Amazon_JumpStart_Text_Classification.ipynb) notebook.

# Input and output interface for the Text Classification - TensorFlow algorithm
<a name="text-classification-tensorflow-inputoutput"></a>

Each of the pretrained models listed in TensorFlow Hub Models can be fine-tuned to any dataset made up of text sentences with any number of classes. The pretrained model attaches a classification layer to the Text Embedding model and initializes the layer parameters to random values. The output dimension of the classification layer is determined based on the number of classes detected in the input data. 

Be mindful of how to format your training data for input to the Text Classification - TensorFlow model.
+ **Training data input format:** A directory containing a `data.csv` file. Each row of the first column should have integer class labels between 0 and the number of classes. Each row of the second column should have the corresponding text data.

The following is an example of an input CSV file. Note that the file should not have any header. The file should be hosted in an Amazon S3 bucket with a path similar to the following: `s3://bucket_name/input_directory/`. Note that the trailing `/` is required.

```
|   |  |
|---|---|
|0 |hide new secretions from the parental units|
|0 |contains no wit , only labored gags|
|1 |that loves its characters and communicates something rather beautiful about human nature|
|...|...|
```

## Incremental training
<a name="text-classification-tensorflow-incremental-training"></a>

You can seed the training of a new model with artifacts from a model that you trained previously with SageMaker AI. Incremental training saves training time when you want to train a new model with the same or similar data.

**Note**  
You can only seed a SageMaker AI Text Classification - TensorFlow model with another Text Classification - TensorFlow model trained in SageMaker AI. 

You can use any dataset for incremental training, as long as the set of classes remains the same. The incremental training step is similar to the fine-tuning step, but instead of starting with a pretrained model, you start with an existing fine-tuned model. 

For more information on using incremental training with the SageMaker AI Text Classification - TensorFlow algorithm, see the [Introduction to JumpStart - Text Classification](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/jumpstart_text_classification/Amazon_JumpStart_Text_Classification.ipynb) sample notebook.

## Inference with the Text Classification - TensorFlow algorithm
<a name="text-classification-tensorflow-inference"></a>

You can host the fine-tuned model that results from your TensorFlow Text Classification training for inference. Any raw text formats for inference must be content type `application/x-text`.

Running inference results in probability values, class labels for all classes, and the predicted label corresponding to the class index with the highest probability encoded in JSON format. The Text Classification - TensorFlow model processes a single string per request and outputs only one line. The following is an example of a JSON format response:

```
accept: application/json;verbose

{"probabilities": [prob_0, prob_1, prob_2, ...],
"labels": [label_0, label_1, label_2, ...],
"predicted_label": predicted_label}
```

If `accept` is set to `application/json`, then the model only outputs probabilities. 

## Amazon EC2 instance recommendation for the Text Classification - TensorFlow algorithm
<a name="text-classification-tensorflow-instances"></a>

The Text Classification - TensorFlow algorithm supports all CPU and GPU instances for training, including:
+ `ml.p2.xlarge`
+ `ml.p2.16xlarge`
+ `ml.p3.2xlarge`
+ `ml.p3.16xlarge`
+ `ml.g4dn.xlarge`
+ `ml.g4dn.16.xlarge`
+ `ml.g5.xlarge`
+ `ml.g5.48xlarge`

We recommend GPU instances with more memory for training with large batch sizes. Both CPU (such as M5) and GPU (P2, P3, G4dn, or G5) instances can be used for inference. For a comprehensive list of SageMaker training and inference instances across AWS Regions, see [Amazon SageMaker Pricing](https://aws.amazon.com/sagemaker/pricing/).

## Text Classification - TensorFlow sample notebooks
<a name="text-classification-tensorflow-sample-notebooks"></a>

For more information about how to use the SageMaker AI Text Classification - TensorFlow algorithm for transfer learning on a custom dataset, see the [Introduction to JumpStart - Text Classification](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/jumpstart_text_classification/Amazon_JumpStart_Text_Classification.ipynb) notebook.

For instructions how to create and access Jupyter notebook instances that you can use to run the example in SageMaker AI, see [Amazon SageMaker notebook instances](nbi.md). After you have created a notebook instance and opened it, select the **SageMaker AI Examples** tab to see a list of all the SageMaker AI samples. To open a notebook, choose its **Use** tab and choose **Create copy**.

# How Text Classification - TensorFlow Works
<a name="text-classification-tensorflow-HowItWorks"></a>

The Text Classification - TensorFlow algorithm takes text as classifies it into one of the output class labels. Deep learning networks such as [BERT](https://arxiv.org/pdf/1810.04805.pdf) are highly accurate for text classification. There are also deep learning networks that are trained on large text datasets, such as TextNet, which has more than 11 million texts with about 11,000 categories. After a network is trained with TextNet data, you can then fine-tune the network on a dataset with a particular focus to perform more specific text classification tasks. The Amazon SageMaker AI Text Classification - TensorFlow algorithm supports transfer learning on many pretrained models that are available in the TensorFlow Hub.

According to the number of class labels in your training data, a text classification layer is attached to the pretrained TensorFlow model of your choice. The classification layer consists of a dropout layer, a dense layer, and a fully connected layer with 2-norm regularization, and is initialized with random weights. You can change the hyperparameter values for the dropout rate of the dropout layer and the L2 regularization factor for the dense layer.

You can fine-tune either the entire network (including the pretrained model) or only the top classification layer on new training data. With this method of transfer learning, training with smaller datasets is possible.

# TensorFlow Hub Models
<a name="text-classification-tensorflow-Models"></a>

The following pretrained models are available to use for transfer learning with the Text Classification - TensorFlow algorithm. 

The following models vary significantly in size, number of model parameters, training time, and inference latency for any given dataset. The best model for your use case depends on the complexity of your fine-tuning dataset and any requirements that you have on training time, inference latency, or model accuracy.


| Model Name | `model_id` | Source | 
| --- | --- | --- | 
|  BERT Base Uncased  | `tensorflow-tc-bert-en-uncased-L-12-H-768-A-12-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3) | 
|  BERT Base Cased  | `tensorflow-tc-bert-en-cased-L-12-H-768-A-12-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_en_cased_L-12_H-768_A-12/3) | 
|  BERT Base Multilingual Cased  | `tensorflow-tc-bert-multi-cased-L-12-H-768-A-12-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_multi_cased_L-12_H-768_A-12/3) | 
|  Small BERT L-2\$1H-128\$1A-2  | `tensorflow-tc-small-bert-bert-en-uncased-L-2-H-128-A-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-128_A-2/1) | 
|  Small BERT L-2\$1H-256\$1A-4 | `tensorflow-tc-small-bert-bert-en-uncased-L-2-H-256-A-4` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-256_A-4/1) | 
|  Small BERT L-2\$1H-512\$1A-8  | `tensorflow-tc-small-bert-bert-en-uncased-L-2-H-512-A-8` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-512_A-8/1) | 
|  Small BERT L-2\$1H-768\$1A-12  | `tensorflow-tc-small-bert-bert-en-uncased-L-2-H-768-A-12` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-768_A-12/1) | 
|  Small BERT L-4\$1H-128\$1A-2  | `tensorflow-tc-small-bert-bert-en-uncased-L-4-H-128-A-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-128_A-2/1) | 
|  Small BERT L-4\$1H-256\$1A-4  | `tensorflow-tc-small-bert-bert-en-uncased-L-4-H-256-A-4` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-256_A-4/1) | 
|  Small BERT L-4\$1H-512\$1A-8  | `tensorflow-tc-small-bert-bert-en-uncased-L-4-H-512-A-8` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1) | 
|  Small BERT L-4\$1H-768\$1A-12  | `tensorflow-tc-small-bert-bert-en-uncased-L-4-H-768-A-12` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-768_A-12/1) | 
|  Small BERT L-6\$1H-128\$1A-2  | `tensorflow-tc-small-bert-bert-en-uncased-L-6-H-128-A-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-128_A-2/1) | 
|  Small BERT L-6\$1H-256\$1A-4  | `tensorflow-tc-small-bert-bert-en-uncased-L-6-H-256-A-4` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-256_A-4/1) | 
|  Small BERT L-6\$1H-512\$1A-8  | `tensorflow-tc-small-bert-bert-en-uncased-L-6-H-512-A-8` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-512_A-8/1) | 
|  Small BERT L-6\$1H-768\$1A-12  | `tensorflow-tc-small-bert-bert-en-uncased-L-6-H-768-A-12` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-768_A-12/1) | 
|  Small BERT L-8\$1H-128\$1A-2  | `tensorflow-tc-small-bert-bert-en-uncased-L-8-H-128-A-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-128_A-2/1) | 
|  Small BERT L-8\$1H-256\$1A-4  | `tensorflow-tc-small-bert-bert-en-uncased-L-8-H-256-A-4` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-256_A-4/1) | 
|  Small BERT L-8\$1H-512\$1A-8  | `tensorflow-tc-small-bert-bert-en-uncased-L-8-H-512-A-8` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-512_A-8/1) | 
|  Small BERT L-8\$1H-768\$1A-12  | `tensorflow-tc-small-bert-bert-en-uncased-L-8-H-768-A-12` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-768_A-12/1) | 
|  Small BERT L-10\$1H-128\$1A-2  | `tensorflow-tc-small-bert-bert-en-uncased-L-10-H-128-A-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-128_A-2/1) | 
|  Small BERT L-10\$1H-256\$1A-4  | `tensorflow-tc-small-bert-bert-en-uncased-L-10-H-256-A-4` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-256_A-4/1) | 
|  Small BERT L-10\$1H-512\$1A-8  | `tensorflow-tc-small-bert-bert-en-uncased-L-10-H-512-A-8` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-512_A-8/1) | 
|  Small BERT L-10\$1H-768\$1A-12  | `tensorflow-tc-small-bert-bert-en-uncased-L-10-H-768-A-12` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-768_A-12/1) | 
|  Small BERT L-12\$1H-128\$1A-2  | `tensorflow-tc-small-bert-bert-en-uncased-L-12-H-128-A-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-128_A-2/1) | 
|  Small BERT L-12\$1H-256\$1A-4  | `tensorflow-tc-small-bert-bert-en-uncased-L-12-H-256-A-4` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-256_A-4/1) | 
|  Small BERT L-12\$1H-512\$1A-8  | `tensorflow-tc-small-bert-bert-en-uncased-L-12-H-512-A-8` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-512_A-8/1) | 
|  Small BERT L-12\$1H-768\$1A-12  | `tensorflow-tc-small-bert-bert-en-uncased-L-12-H-768-A-12` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-768_A-12/1) | 
|  BERT Large Uncased  | `tensorflow-tc-bert-en-uncased-L-24-H-1024-A-16-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/3) | 
|  BERT Large Cased  | `tensorflow-tc-bert-en-cased-L-24-H-1024-A-16-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_en_cased_L-24_H-1024_A-16/3) | 
|  BERT Large Uncased Whole Word Masking  | `tensorflow-tc-bert-en-wwm-uncased-L-24-H-1024-A-16-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_en_wwm_uncased_L-24_H-1024_A-16/3) | 
|  BERT Large Cased Whole Word Masking  | `tensorflow-tc-bert-en-wwm-cased-L-24-H-1024-A-16-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/bert_en_wwm_cased_L-24_H-1024_A-16/3) | 
|  ALBERT Base  | `tensorflow-tc-albert-en-base` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/albert_en_base/2) | 
|  ELECTRA Small\$1\$1  | `tensorflow-tc-electra-small-1` | [TensorFlow Hub link](https://tfhub.dev/google/electra_small/2) | 
|  ELECTRA Base  | `tensorflow-tc-electra-base-1` | [TensorFlow Hub link](https://tfhub.dev/google/electra_base/2) | 
|  BERT Base Wikipedia and BooksCorpus  | `tensorflow-tc-experts-bert-wiki-books-1` | [TensorFlow Hub link](https://tfhub.dev/google/experts/bert/wiki_books/2) | 
|  BERT Base MEDLINE/PubMed  | `tensorflow-tc-experts-bert-pubmed-1` | [TensorFlow Hub link](https://tfhub.dev/google/experts/bert/pubmed/2) | 
|  Talking Heads Base  | `tensorflow-tc-talking-heads-base` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_base/1) | 
|  Talking Heads Large  | `tensorflow-tc-talking-heads-large` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_large/1) | 

# Text Classification - TensorFlow Hyperparameters
<a name="text-classification-tensorflow-Hyperparameter"></a>

Hyperparameters are parameters that are set before a machine learning model begins learning. The following hyperparameters are supported by the Amazon SageMaker AI built-in Object Detection - TensorFlow algorithm. See [Tune a Text Classification - TensorFlow model](text-classification-tensorflow-tuning.md) for information on hyperparameter tuning. 


| Parameter Name | Description | 
| --- | --- | 
| batch\$1size |  The batch size for training. For training on instances with multiple GPUs, this batch size is used across the GPUs.  Valid values: positive integer. Default value: `32`.  | 
| beta\$11 |  The beta1 for the `"adam"` and `"adamw"` optimizers. Represents the exponential decay rate for the first moment estimates. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.9`.  | 
| beta\$12 |  The beta2 for the `"adam"` and `"adamw"` optimizers. Represents the exponential decay rate for the second moment estimates. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.999`.  | 
| dropout\$1rate | The dropout rate for the dropout layer in the top classification layer. Used only when `reinitialize_top_layer` is set to `"True"`. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.2` | 
| early\$1stopping |  Set to `"True"` to use early stopping logic during training. If `"False"`, early stopping is not used. Valid values: string, either: (`"True"` or `"False"`). Default value: `"False"`.  | 
| early\$1stopping\$1min\$1delta | The minimum change needed to qualify as an improvement. An absolute change less than the value of early\$1stopping\$1min\$1delta does not qualify as improvement. Used only when early\$1stopping is set to "True".Valid values: float, range: [`0.0`, `1.0`].Default value: `0.0`. | 
| early\$1stopping\$1patience |  The number of epochs to continue training with no improvement. Used only when `early_stopping` is set to `"True"`. Valid values: positive integer. Default value: `5`.  | 
| epochs |  The number of training epochs. Valid values: positive integer. Default value: `10`.  | 
| epsilon |  The epsilon for `"adam"`, `"rmsprop"`, `"adadelta"`, and `"adagrad"` optimizers. Usually set to a small value to avoid division by 0. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `1e-7`.  | 
| initial\$1accumulator\$1value |  The starting value for the accumulators, or the per-parameter momentum values, for the `"adagrad"` optimizer. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.0001`.  | 
| learning\$1rate | The optimizer learning rate. Valid values: float, range: [`0.0`, `1.0`].Default value: `0.001`. | 
| momentum |  The momentum for the `"sgd"` and `"nesterov"` optimizers. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.9`.  | 
| optimizer |  The optimizer type. For more information, see [Optimizers](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers) in the TensorFlow documentation. Valid values: string, any of the following: (`"adamw"`, `"adam"`, `"sgd"`, `"nesterov"`, `"rmsprop"`,` "adagrad"` , `"adadelta"`). Default value: `"adam"`.  | 
| regularizers\$1l2 |  The L2 regularization factor for the dense layer in the classification layer. Used only when `reinitialize_top_layer` is set to `"True"`. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.0001`.  | 
| reinitialize\$1top\$1layer |  If set to `"Auto"`, the top classification layer parameters are re-initialized during fine-tuning. For incremental training, top classification layer parameters are not re-initialized unless set to `"True"`. Valid values: string, any of the following: (`"Auto"`, `"True"` or `"False"`). Default value: `"Auto"`.  | 
| rho |  The discounting factor for the gradient of the `"adadelta"` and `"rmsprop"` optimizers. Ignored for other optimizers.  Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.95`.  | 
| train\$1only\$1on\$1top\$1layer |  If `"True"`, only the top classification layer parameters are fine-tuned. If `"False"`, all model parameters are fine-tuned. Valid values: string, either: (`"True"` or `"False"`). Default value: `"False"`.  | 
| validation\$1split\$1ratio |  The fraction of training data to randomly split to create validation data. Only used if validation data is not provided through the `validation` channel. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.2`.  | 
| warmup\$1steps\$1fraction |  The fraction of the total number of gradient update steps, where the learning rate increases from 0 to the initial learning rate as a warm up. Only used with the `adamw` optimizer. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.1`.  | 

# Tune a Text Classification - TensorFlow model
<a name="text-classification-tensorflow-tuning"></a>

*Automatic model tuning*, also known as hyperparameter tuning, finds the best version of a model by running many jobs that test a range of hyperparameters on your dataset. You choose the tunable hyperparameters, a range of values for each, and an objective metric. You choose the objective metric from the metrics that the algorithm computes. Automatic model tuning searches the hyperparameters chosen to find the combination of values that result in the model that optimizes the objective metric.

For more information about model tuning, see [Automatic model tuning with SageMaker AI](automatic-model-tuning.md).

## Metrics computed by the Text Classification - TensorFlow algorithm
<a name="text-classification-tensorflow-metrics"></a>

Refer to the following chart to find which metrics are computed by the Text Classification - TensorFlow algorithm.


| Metric Name | Description | Optimization Direction | Regex Pattern | 
| --- | --- | --- | --- | 
| validation:accuracy | The ratio of the number of correct predictions to the total number of predictions made. | Maximize | `val_accuracy=([0-9\\.]+)` | 

## Tunable Text Classification - TensorFlow hyperparameters
<a name="text-classification-tensorflow-tunable-hyperparameters"></a>

Tune a text classification model with the following hyperparameters. The hyperparameters that have the greatest impact on text classification objective metrics are: `batch_size`, `learning_rate`, and `optimizer`. Tune the optimizer-related hyperparameters, such as `momentum`, `regularizers_l2`, `beta_1`, `beta_2`, and `eps` based on the selected `optimizer`. For example, use `beta_1` and `beta_2` only when `adamw` or `adam` is the `optimizer`.

For more information about which hyperparameters are used for each `optimizer`, see [Text Classification - TensorFlow Hyperparameters](text-classification-tensorflow-Hyperparameter.md).


| Parameter Name | Parameter Type | Recommended Ranges | 
| --- | --- | --- | 
| batch\$1size | IntegerParameterRanges | MinValue: 4, MaxValue: 128 | 
| beta\$11 | ContinuousParameterRanges | MinValue: 1e-6, MaxValue: 0.999 | 
| beta\$12 | ContinuousParameterRanges | MinValue: 1e-6, MaxValue: 0.999 | 
| eps | ContinuousParameterRanges | MinValue: 1e-8, MaxValue: 1.0 | 
| learning\$1rate | ContinuousParameterRanges | MinValue: 1e-6, MaxValue: 0.5 | 
| momentum | ContinuousParameterRanges | MinValue: 0.0, MaxValue: 0.999 | 
| optimizer | CategoricalParameterRanges | ['adamw', 'adam', 'sgd', 'rmsprop', 'nesterov', 'adagrad', 'adadelta'] | 
| regularizers\$1l2 | ContinuousParameterRanges | MinValue: 0.0, MaxValue: 0.999 | 
| train\$1only\$1on\$1top\$1layer | CategoricalParameterRanges | ['True', 'False'] | 