

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

# Neptune ML 中自訂模型的概觀
<a name="machine-learning-custom-model-overview"></a>

## 何時在 Neptune ML 中使用自訂模型
<a name="machine-learning-custom-models-when-to-use"></a>

Neptune ML 的內建模型會處理 Neptune ML 支援的所有標準任務，但是在有些情況，您可能想要對特定任務的模型進行更精細的控制，或者需要自訂模型訓練程序。例如，自訂模型適用於下列情況：
+ 對於非常大文字模型的文字特徵，必須在 GPU 上執行其特徵編碼。
+ 您想要使用自己的自訂圖形神經網路（GNN）模型，這是在 Deep Graph Library (DGL) 中開發的模型。
+ 您想要使用表格式模型或整體模型進行節點分類和迴歸。

## 在 Neptune ML 中開發和使用自訂模型的工作流程
<a name="machine-learning-custom-model-workflow"></a>

Neptune ML 中的自訂模型支援旨在無縫整合至現有的 Neptune ML 工作流程。它的運作方式是在 Neptune ML 基礎結構上的來源模組中執行自訂程式碼，以訓練模型。如同內建模式一樣，Neptune ML 會自動啟動 SageMaker AI HyperParameter 調校任務，並根據評估指標選取最佳模型。然後，它會使用來源模組中提供的實作來產生模型成品進行部署。

自訂模型的資料匯出、訓練組態和資料預先處理與內建模型相同。

在資料預先處理之後，您就可以反覆且以互動方式使用 Python，來開發並測試您的自訂模型實作。當您的模型已準備好生產時，您可以將產生的 Python 模組上傳到 Amazon S3，如下所示：

```
aws s3 cp --recursive {{(source path to module)}} s3://{{(bucket name)}}/{{(destination path for your module)}}
```

然後，您可以使用一般[預設](machine-learning-overview.md#machine-learning-overview-starting-workflow)或[增量](machine-learning-overview-evolving-data-incremental.md#machine-learning-overview-incremental)資料工作流程，將模型部署至生產環境，但有一些差異。

對於使用自訂模型的模型訓練，您必須將 `customModelTrainingParameters` JSON 物件提供給 Neptune ML 模型訓練 API，以確保使用您的自訂程式碼。`customModelTrainingParameters` 物件中的欄位如下所示：
+ **`sourceS3DirectoryPath`** – (*必要*) 此路徑通往實作您模型之 Python 模組所在的 Amazon S3 位置。這必須指向有效的現有 Amazon S3 位置，其中至少包含訓練指令碼、轉換指令碼和 `model-hpo-configuration.json` 檔案。
+ **`trainingEntryPointScript`** – (*選用*) 指令碼模組中的進入點名稱，該指令碼會執行模型訓練，並接受超參數作為命令列引數 (包括固定的超參數)。

  *預設*︰`training.py`。
+ **`transformEntryPointScript`** – (*選用*) 指令碼模組中的進入點名稱，該指令碼應在識別了超參數搜尋中的最佳模型之後執行，以計算模型部署所需的模型成品。它應該能夠在沒有命令列參數的情況下執行。

  *預設*︰`transform.py`。

例如：

------
#### [ AWS CLI ]

```
aws neptunedata start-ml-model-training-job \
  --endpoint-url https://{{your-neptune-endpoint}}:{{port}} \
  --id "{{(a unique model-training job ID)}}" \
  --data-processing-job-id "{{(the data-processing job-id of a completed job)}}" \
  --train-model-s3-location "s3://{{(your Amazon S3 bucket)}}/neptune-model-graph-autotrainer" \
  --model-name "custom" \
  --custom-model-training-parameters '{
    "sourceS3DirectoryPath": "s3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}",
    "trainingEntryPointScript": "{{(your training script entry-point name in the Python module)}}",
    "transformEntryPointScript": "{{(your transform script entry-point name in the Python module)}}"
  }'
```

如需詳細資訊，請參閱《 AWS CLI 命令參考》中的 [start-ml-model-training-job](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/start-ml-model-training-job.html)。

------
#### [ SDK ]

```
import boto3
from botocore.config import Config

client = boto3.client(
    'neptunedata',
    endpoint_url='https://{{your-neptune-endpoint}}:{{port}}',
    config=Config(read_timeout=None, retries={'total_max_attempts': 1})
)

response = client.start_ml_model_training_job(
    id='{{(a unique model-training job ID)}}',
    dataProcessingJobId='{{(the data-processing job-id of a completed job)}}',
    trainModelS3Location='s3://{{(your Amazon S3 bucket)}}/neptune-model-graph-autotrainer',
    modelName='custom',
    customModelTrainingParameters={
        'sourceS3DirectoryPath': 's3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}',
        'trainingEntryPointScript': '{{(your training script entry-point name in the Python module)}}',
        'transformEntryPointScript': '{{(your transform script entry-point name in the Python module)}}'
    }
)

print(response)
```

------
#### [ awscurl ]

```
awscurl https://{{your-neptune-endpoint}}:{{port}}/ml/modeltraining \
  --region {{us-east-1}} \
  --service neptune-db \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
        "id" : "{{(a unique model-training job ID)}}",
        "dataProcessingJobId" : "{{(the data-processing job-id of a completed job)}}",
        "trainModelS3Location" : "s3://{{(your Amazon S3 bucket)}}/neptune-model-graph-autotrainer",
        "modelName": "custom",
        "customModelTrainingParameters" : {
          "sourceS3DirectoryPath": "s3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}",
          "trainingEntryPointScript": "{{(your training script entry-point name in the Python module)}}",
          "transformEntryPointScript": "{{(your transform script entry-point name in the Python module)}}"
        }
      }'
```

**注意**  
此範例假設您的 AWS 登入資料已在您的環境中設定。將 {{us-east-1}} 取代為 Neptune 叢集的區域。

------
#### [ curl ]

```
curl \
  -X POST https://{{your-neptune-endpoint}}:{{port}}/ml/modeltraining \
  -H 'Content-Type: application/json' \
  -d '{
        "id" : "{{(a unique model-training job ID)}}",
        "dataProcessingJobId" : "{{(the data-processing job-id of a completed job)}}",
        "trainModelS3Location" : "s3://{{(your Amazon S3 bucket)}}/neptune-model-graph-autotrainer",
        "modelName": "custom",
        "customModelTrainingParameters" : {
          "sourceS3DirectoryPath": "s3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}",
          "trainingEntryPointScript": "{{(your training script entry-point name in the Python module)}}",
          "transformEntryPointScript": "{{(your transform script entry-point name in the Python module)}}"
        }
      }'
```

------

同樣地，若要啟用自訂模型轉換，您必須將 `customModelTransformParameters` JSON 物件提供給 Neptune ML 模型轉換 API，其欄位值與訓練工作中儲存的模型參數相容。`customModelTransformParameters` 物件包含下列欄位：
+ **`sourceS3DirectoryPath`** – (*必要*) 此路徑通往實作您模型之 Python 模組所在的 Amazon S3 位置。這必須指向有效的現有 Amazon S3 位置，其中至少包含訓練指令碼、轉換指令碼和 `model-hpo-configuration.json` 檔案。
+ **`transformEntryPointScript`** – (*選用*) 指令碼模組中的進入點名稱，該指令碼應在識別了超參數搜尋中的最佳模型之後執行，以計算模型部署所需的模型成品。它應該能夠在沒有命令列參數的情況下執行。

  *預設*︰`transform.py`。

例如：

------
#### [ AWS CLI ]

```
aws neptunedata start-ml-model-transform-job \
  --endpoint-url https://{{your-neptune-endpoint}}:{{port}} \
  --id "{{(a unique model-transform job ID)}}" \
  --training-job-name "{{(name of a completed SageMaker training job)}}" \
  --model-transform-output-s3-location "s3://{{(your Amazon S3 bucket)}}/neptune-model-transform/" \
  --custom-model-transform-parameters '{
    "sourceS3DirectoryPath": "s3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}",
    "transformEntryPointScript": "{{(your transform script entry-point name in the Python module)}}"
  }'
```

如需詳細資訊，請參閱《 AWS CLI 命令參考》中的 [start-ml-model-transform-job](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/start-ml-model-transform-job.html)。

------
#### [ SDK ]

```
import boto3
from botocore.config import Config

client = boto3.client(
    'neptunedata',
    endpoint_url='https://{{your-neptune-endpoint}}:{{port}}',
    config=Config(read_timeout=None, retries={'total_max_attempts': 1})
)

response = client.start_ml_model_transform_job(
    id='{{(a unique model-transform job ID)}}',
    trainingJobName='{{(name of a completed SageMaker training job)}}',
    modelTransformOutputS3Location='s3://{{(your Amazon S3 bucket)}}/neptune-model-transform/',
    customModelTransformParameters={
        'sourceS3DirectoryPath': 's3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}',
        'transformEntryPointScript': '{{(your transform script entry-point name in the Python module)}}'
    }
)

print(response)
```

------
#### [ awscurl ]

```
awscurl https://{{your-neptune-endpoint}}:{{port}}/ml/modeltransform \
  --region {{us-east-1}} \
  --service neptune-db \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
        "id" : "{{(a unique model-transform job ID)}}",
        "trainingJobName" : "{{(name of a completed SageMaker training job)}}",
        "modelTransformOutputS3Location" : "s3://{{(your Amazon S3 bucket)}}/neptune-model-transform/",
        "customModelTransformParameters" : {
          "sourceS3DirectoryPath": "s3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}",
          "transformEntryPointScript": "{{(your transform script entry-point name in the Python module)}}"
        }
      }'
```

**注意**  
此範例假設您的 AWS 登入資料已在您的環境中設定。將 {{us-east-1}} 取代為 Neptune 叢集的區域。

------
#### [ curl ]

```
curl \
  -X POST https://{{your-neptune-endpoint}}:{{port}}/ml/modeltransform \
  -H 'Content-Type: application/json' \
  -d '{
        "id" : "{{(a unique model-transform job ID)}}",
        "trainingJobName" : "{{(name of a completed SageMaker training job)}}",
        "modelTransformOutputS3Location" : "s3://{{(your Amazon S3 bucket)}}/neptune-model-transform/",
        "customModelTransformParameters" : {
          "sourceS3DirectoryPath": "s3://{{(your Amazon S3 bucket)}}/{{(path to your Python module)}}",
          "transformEntryPointScript": "{{(your transform script entry-point name in the Python module)}}"
        }
      }'
```

------