

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 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 上运行。
+ 您想使用自己在深度图表库 (DGL) 中开发的自定义图形神经网络 (GNN) 模型。
+ 您想使用表格模型或集合模型进行节点分类和回归。

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

Neptune ML 中的自定义模型支持旨在无缝集成到现有的 Neptune ML 工作流程中。它的工作原理是在 Neptune ML 基础设施上的源模块中运行自定义代码来训练模型。与内置模式一样，Neptune ML 会自动启动 A SageMaker I 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)数据工作流程将模型部署到生产环境中，但有一些区别。

要使用自定义模型进行模型训练，必须向 Neptune ML 模型训练 API 提供 `customModelTrainingParameters` JSON 对象，以确保使用您的自定义代码。`customModelTrainingParameters` 对象中的字段如下所示：
+ **`sourceS3DirectoryPath`** –（*必需*）实现您的模型的 Python 模块所在的 Amazon S3 位置的路径。这必须指向有效的现有 Amazon S3 位置，其中至少包含训练脚本、转换脚本和 `model-hpo-configuration.json` 文件。
+ **`trainingEntryPointScript`** –（*可选*）执行模型训练并将超参数作为命令行参数（包括固定的超参数）的脚本模块中入口点的名称。

  *默认值*：`training.py`。
+ **`transformEntryPointScript`** –（*可选*）脚本模块中入口点的名称，该脚本应在确定超参数搜索中的最佳模型之后运行，以计算模型部署所需的模型构件。它应该能够在没有命令行参数的情况下运行。

  *默认值*：`transform.py`。

例如：

------
#### [ Amazon 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)}}"
  }'
```

有关更多信息，请参阅《 Amazon CLI 命令参考》中的 [start-ml-model-training-job](https://docs.amazonaws.cn/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)}}"
        }
      }'
```

**注意**  
此示例假设您的 Amazon 证书是在您的环境中配置的。{{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)}}"
        }
      }'
```

------

同样，要启用自定义模型转换，您必须向 Neptune ML 模型转换 API 提供一个 `customModelTransformParameters` JSON 对象，其字段值必须与训练任务中保存的模型参数兼容。`customModelTransformParameters` 对象包含以下字段：
+ **`sourceS3DirectoryPath`** –（*必需*）实现您的模型的 Python 模块所在的 Amazon S3 位置的路径。这必须指向有效的现有 Amazon S3 位置，其中至少包含训练脚本、转换脚本和 `model-hpo-configuration.json` 文件。
+ **`transformEntryPointScript`** –（*可选*）脚本模块中入口点的名称，该脚本应在确定超参数搜索中的最佳模型之后运行，以计算模型部署所需的模型构件。它应该能够在没有命令行参数的情况下运行。

  *默认值*：`transform.py`。

例如：

------
#### [ Amazon 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)}}"
  }'
```

有关更多信息，请参阅《 Amazon CLI 命令参考》中的 [start-ml-model-transform-job](https://docs.amazonaws.cn/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)}}"
        }
      }'
```

**注意**  
此示例假设您的 Amazon 证书是在您的环境中配置的。{{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)}}"
        }
      }'
```

------