

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 如何使用 SageMaker AI 文字分類 - TensorFlow 演算法
<a name="text-classification-tensorflow-how-to-use"></a>

您可以使用文字分類 - TensorFlow 做為 Amazon SageMaker AI 內建演算法。下一節描述如何將文字分類 - TensorFlow 搭配 SageMaker AI Python SDK 使用。如需如何從 Amazon SageMaker Studio Classic 使用者介面使用文字分類 - TensorFlow 的資訊，請參閱[SageMaker JumpStart 預先訓練模型](studio-jumpstart.md)。

文字分類 - TensorFlow 演算法支援使用任何相容預先訓練的 TensorFlow 模型進行轉移學習。如需有關所有可用之預先訓練模型的清單，請參閱[TensorFlow Hub 模型](text-classification-tensorflow-Models.md)。每個預先訓練的模型都有唯一的 `model_id`。下方的範例會使用 BERT Base Uncased (`model_id`:`tensorflow-tc-bert-en-uncased-L-12-H-768-A-12-2`) 來微調自訂資料集。預先訓練的模型都是從 TensorFlow Hub 預先下載並儲存在 Amazon S3 儲存貯體中，以便訓練任務可以在網路隔離中執行。使用這些預先產生的模型訓練成品，來建構 SageMaker AI 估算器。

首先，檢索 Docker 映像 URI，訓練指令碼 URI 和預先訓練的模型 URI。然後，視需要變更超參數。您可以使用 `hyperparameters.retrieve_default` 查看所有可用超參數及其預設數值的 Python 字典。如需詳細資訊，請參閱[文字分類 - TensorFlow 超參數](text-classification-tensorflow-Hyperparameter.md)。使用這些值來建構 SageMaker AI 估算器。

**注意**  
對於不同的模型而言，預設超參數值是不同的。例如，對於較大的模型，預設批次大小較小。

此範例使用 [https://www.tensorflow.org/datasets/catalog/glue#gluesst2](https://www.tensorflow.org/datasets/catalog/glue#gluesst2) 資料集，其中包含正面和負面電影評論。我們已預先下載資料集，並透過 Amazon S3 提供該資料集。若要微調模型，請使用您的訓練資料集的 Amazon S3 位置呼叫 `.fit`。筆記本中使用的任何 S3 儲存貯體都必須與存取該儲存貯體的筆記本執行個體位於相同的 AWS 區域。

```
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)
```

如需如何在自訂資料集上使用 SageMaker 文字分類 - TensorFlow 演算法進行轉移學習的更多相關資訊，請參閱 [JumpStart - 文字分類](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/jumpstart_text_classification/Amazon_JumpStart_Text_Classification.ipynb)筆記本簡介。