

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

# 使用 additionalParams 中的参数调整模型训练配置的示例
<a name="machine-learning-data-export-additionalParams-examples"></a>

 以下示例演示了如何利用 property-graph 中的“additionalParams”功能和 RDF 数据模型为 Neptune ML 应用程序配置模型训练过程的各个方面。这些示例涵盖了广泛的功能，包括指定training/validation/test数据的默认拆分率、定义节点分类、回归和链接预测任务，以及配置不同的特征类型，例如数字存储桶、文本嵌入、日期时间和分类数据。这些详细的配置使您可以根据自己的特定数据和建模要求定制机器学习管道，从而充分发挥 Neptune ML 功能的潜力。

**Contents**
+ [使用 additionalParams 的 property-graph 示例](#machine-learning-property-graph-additionalParams-examples)
  + [为模型训练配置指定默认分割率](#machine-learning-property-graph-additionalParams-default-split-rate-example)
  + [为模型训练配置指定节点分类任务](#machine-learning-property-graph-additionalParams-node-classification-example)
  + [为模型训练配置指定多类别节点分类任务](#machine-learning-property-graph-additionalParams-multi-class-example)
  + [为模型训练配置指定节点回归任务](#machine-learning-property-graph-additionalParams-node-regression-example)
  + [为模型训练配置指定边缘分类任务](#machine-learning-property-graph-additionalParams-edge-classification-example)
  + [为模型训练配置指定多类别边缘分类任务](#machine-learning-property-graph-additionalParams-multi-edge-classification-example)
  + [为模型训练配置指定边缘回归](#machine-learning-property-graph-additionalParams-edge-regression-example)
  + [为模型训练配置指定链接预测任务](#machine-learning-property-graph-additionalParams-link-prediction-example)
  + [指定数值桶特征](#machine-learning-property-graph-additionalParams-numeric-bucket-example)
  + [指定 `Word2Vec` 特征](#machine-learning-property-graph-additionalParams-word2vec-example)
  + [指定 `FastText` 特征](#machine-learning-property-graph-additionalParams-fasttext-example)
  + [指定 `Sentence BERT` 特征](#machine-learning-property-graph-additionalParams-sbert-example)
  + [指定 `TF-IDF` 特征](#machine-learning-property-graph-additionalParams-tf-idf-example)
  + [指定 `datetime` 特征](#machine-learning-property-graph-additionalParams-datetime-example)
  + [指定 `category` 特征](#machine-learning-property-graph-additionalParams-category-example)
  + [指定 `numerical` 特征](#machine-learning-property-graph-additionalParams-numerical-example)
  + [指定 `auto` 特征](#machine-learning-property-graph-additionalParams-auto-example)
+ [使用 `additionalParams` 的 RDF 示例](#machine-learning-RDF-additionalParams-examples)
  + [为模型训练配置指定默认分割率](#machine-learning-RDF-additionalParams-default-split-rate-example)
  + [为模型训练配置指定节点分类任务](#machine-learning-RDF-additionalParams-node-classification-example)
  + [为模型训练配置指定节点回归任务](#machine-learning-RDF-additionalParams-node-regression-example)
  + [为特定边缘指定链接预测任务](#machine-learning-RDF-additionalParams-link-prediction-example)
  + [为所有边缘指定链接预测任务](#machine-learning-RDF-additionalParams-link-prediction-example)

## 使用 additionalParams 的 property-graph 示例
<a name="machine-learning-property-graph-additionalParams-examples"></a>

### 为模型训练配置指定默认分割率
<a name="machine-learning-property-graph-additionalParams-default-split-rate-example"></a>

在以下示例中，`split_rate` 参数设置模型训练的默认分割率。如果未指定默认分割率，则训练将使用 [0.9、0.1、0.0] 的值。您可以通过为每个目标指定一个 `split_rate` 来覆盖每个目标的默认值。

在以下示例中，`default split_rate` 字段表示应使用分割率 `[0.7,0.1,0.2]`，除非针对每个目标进行覆盖：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "split_rate": [0.7,0.1,0.2],
    "targets": [
      (...)
    ],
    "features": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定节点分类任务
<a name="machine-learning-property-graph-additionalParams-node-classification-example"></a>

要指明哪个节点属性包含用于训练的带标签的示例，请使用 `"type" : "classification"` 向 `targets` 数组中添加节点分类元素。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

在以下示例中，`node` 目标表示应将每个 `Movie` 节点的 `genre` 属性视为节点类别标签。`split_rate` 值将覆盖默认分割率：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "node": "Movie",
        "property": "genre",
        "type": "classification",
        "split_rate": [0.7,0.1,0.2]
      }
    ],
    "features": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定多类别节点分类任务
<a name="machine-learning-property-graph-additionalParams-multi-class-example"></a>

要指明哪个节点属性包含用于训练的多个带标签的示例，请使用 `"type" : "classification"` 以及 `separator`（指定一个可用于将目标属性值拆分为多个分类值的字符），从而将节点分类元素添加到目标数组中。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

在以下示例中，`node` 目标表示应将每个 `Movie` 节点的 `genre` 属性视为节点类别标签。`separator` 字段表示每个流派属性都包含多个以分号分隔的值：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "node": "Movie",
        "property": "genre",
        "type": "classification",
        "separator": ";"
      }
    ],
    "features": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定节点回归任务
<a name="machine-learning-property-graph-additionalParams-node-regression-example"></a>

要指明哪个节点属性包含用于训练目的的带标签的回归，请使用 `"type" : "regression"` 将节点回归元素添加到目标数组中。如果您想覆盖默认分割率，则可添加 split\$1rate 字段。

以下 `node` 目标表示应将每个 `Movie` 节点的 `rating` 属性视为节点回归标签：

```
    "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "node": "Movie",
        "property": "rating",
        "type" : "regression",
        "split_rate": [0.7,0.1,0.2]
      }
    ],
    "features": [
      ...
    ]
  }
}
```

### 为模型训练配置指定边缘分类任务
<a name="machine-learning-property-graph-additionalParams-edge-classification-example"></a>

要指明哪个边缘属性包含用于训练目的的带标签的示例，请使用 `"type" : "regression"` 向 `targets` 数组中添加边缘元素。如果您想覆盖默认分割率，则可添加 split\$1rate 字段。

以下 `edge` 目标表示应将每个 `knows` 边缘的 `metAtLocation` 属性视为边缘类别标签：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "edge": ["Person", "knows", "Person"],
        "property": "metAtLocation",
        "type": "classification"
      }
    ],
    "features": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定多类别边缘分类任务
<a name="machine-learning-property-graph-additionalParams-multi-edge-classification-example"></a>

要指明哪个边缘属性包含用于训练目的的多个带标签的示例，请使用 `"type" : "classification"` 和 `separator` 字段（此字段指定用于将目标属性值拆分为多个类别值的字符），向 `targets` 数组添加一个边缘元素。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

以下 `edge` 目标表示应将每个 `repliedTo` 边缘的 `sentiment` 属性视为边缘类别标签。分隔符字段表示每个情绪属性都包含多个以逗号分隔的值：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "edge": ["Person", "repliedTo", "Message"],
        "property": "sentiment",
        "type": "classification",
        "separator": ","
      }
    ],
    "features": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定边缘回归
<a name="machine-learning-property-graph-additionalParams-edge-regression-example"></a>

要指明哪个边缘属性包含用于训练目的的带标签的回归示例，请使用 `"type" : "regression"` 向 `targets` 数组中添加 `edge` 元素。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

以下 `edge` 目标表示应将每个 `reviewed` 边缘的 `rating` 属性视为边缘回归：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "edge": ["Person", "reviewed", "Movie"],
        "property": "rating",
        "type" : "regression"
      }
    ],
    "features": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定链接预测任务
<a name="machine-learning-property-graph-additionalParams-link-prediction-example"></a>

要指示应使用哪些边缘进行链接预测训练目的，请使用 `"type" : "link_prediction"` 向目标数组添加边缘元素。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

以下 `edge` 目标表示应使用 `cites` 边缘进行链接预测：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "edge": ["Article", "cites", "Article"],
        "type" : "link_prediction"
      }
    ],
    "features": [
      (...)
    ]
  }
}
```

### 指定数值桶特征
<a name="machine-learning-property-graph-additionalParams-numeric-bucket-example"></a>

您可以通过将 `"type": "bucket_numerical"` 添加到 `features` 数组中来为节点属性指定数值数据特征。

以下 `node` 特征表示应将每个 `Person` 节点的 `age` 属性视为数字桶特征：

```
  "additionalParams": {
  "neptune_ml": {
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Person",
        "property": "age",
        "type": "bucket_numerical",
        "range": [1, 100],
        "bucket_cnt": 5,
        "slide_window_size": 3,
        "imputer": "median"
      }
    ]
  }
}
```

### 指定 `Word2Vec` 特征
<a name="machine-learning-property-graph-additionalParams-word2vec-example"></a>

您可以通过将 `"type": "text_word2vec"` 添加到 `features` 数组中来为节点属性指定 `Word2Vec` 特征。

以下 `node` 特征表示应将每个 `Movie` 节点的 `description` 属性视为 `Word2Vec` 特征：

```
"additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Movie",
        "property": "description",
        "type": "text_word2vec",
        "language": "en_core_web_lg"
      }
    ]
  }
}
```

### 指定 `FastText` 特征
<a name="machine-learning-property-graph-additionalParams-fasttext-example"></a>

您可以通过将 `"type": "text_fasttext"` 添加到 `features` 数组中来为节点属性指定 `FastText` 特征。`language` 字段为必需字段，并且必须指定以下语言之一：
+ `en`（英语）
+ `zh`（中文）
+ `hi`（印地语）
+ `es`（西班牙语）
+ `fr`（法语）

请注意，在特征中，`text_fasttext` 编码不能同时处理多种语言。

以下 `node` 特征表示应将每个 `Movie` 节点的法语 `description` 属性视为 `FastText` 特征：

```
"additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Movie",
        "property": "description",
        "type": "text_fasttext",
        "language": "fr",
        "max_length": 1024
      }
    ]
  }
}
```

### 指定 `Sentence BERT` 特征
<a name="machine-learning-property-graph-additionalParams-sbert-example"></a>

您可以通过将 `"type": "text_sbert"` 添加到 `features` 数组中来为节点属性指定 `Sentence BERT` 特征。您无需指定语言，因为该方法使用多语言模型自动对文本特征进行编码。

以下 `node` 特征表示应将每个 `Movie` 节点的 `description` 属性视为 `Sentence BERT` 特征：

```
"additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Movie",
        "property": "description",
        "type": "text_sbert128",
      }
    ]
  }
}
```

### 指定 `TF-IDF` 特征
<a name="machine-learning-property-graph-additionalParams-tf-idf-example"></a>

您可以通过将 `"type": "text_tfidf"` 添加到 `features` 数组中来为节点属性指定 `TF-IDF` 特征。

以下 `node` 特征表示应将每个 `Person` 节点的 `bio` 属性视为 `TF-IDF` 特征：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Movie",
        "property": "bio",
        "type": "text_tfidf",
        "ngram_range": [1, 2],
        "min_df": 5,
        "max_features": 1000
      }
    ]
  }
}
```

### 指定 `datetime` 特征
<a name="machine-learning-property-graph-additionalParams-datetime-example"></a>

导出过程会自动推理日期属性的 `datetime` 特征。但是，如果要限制用于 `datetime` 特征的 `datetime_parts`，或者覆盖特征规范，以便将通常被视为 `auto` 特征的属性显式地视为 `datetime` 特征，则可以通过向特征数组中添加 `"type": "datetime"` 来实现。

以下 `node` 特征表示应将每个 `Post` 节点的 `createdAt` 属性视为 `datetime` 特征：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Post",
        "property": "createdAt",
        "type": "datetime",
        "datetime_parts": ["month", "weekday", "hour"]
      }
    ]
  }
}
```

### 指定 `category` 特征
<a name="machine-learning-property-graph-additionalParams-category-example"></a>

导出过程会自动推理字符串属性和包含多个值的数值属性的 `auto` 特征。对于包含单个值的数值属性，它会推理 `numerical` 特征。对于日期属性，它会推理 `datetime` 特征。

如果要覆盖特征规范以便将属性视为分类特征，请向特征数组中添加 `"type": "category"`。如果该属性包含多个值，请包含 `separator` 字段。例如：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Post",
        "property": "tag",
        "type": "category",
        "separator": "|"
      }
    ]
  }
}
```

### 指定 `numerical` 特征
<a name="machine-learning-property-graph-additionalParams-numerical-example"></a>

导出过程会自动推理字符串属性和包含多个值的数值属性的 `auto` 特征。对于包含单个值的数值属性，它会推理 `numerical` 特征。对于日期属性，它会推理 `datetime` 特征。

如果要覆盖特征规范以便将属性视为 `numerical` 特征，请向特征数组中添加 `"type": "numerical"`。如果该属性包含多个值，请包含 `separator` 字段。例如：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "Recording",
        "property": "duration",
        "type": "numerical",
        "separator": ","
      }
    ]
  }
}
```

### 指定 `auto` 特征
<a name="machine-learning-property-graph-additionalParams-auto-example"></a>

导出过程会自动推理字符串属性和包含多个值的数值属性的 `auto` 特征。对于包含单个值的数值属性，它会推理 `numerical` 特征。对于日期属性，它会推理 `datetime` 特征。

如果要覆盖特征规范以便将属性视为 `auto` 特征，请向特征数组中添加 `"type": "auto"`。如果该属性包含多个值，请包含 `separator` 字段。例如：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      ...
    ],
    "features": [
      {
        "node": "User",
        "property": "role",
        "type": "auto",
        "separator": ","
      }
    ]
  }
}
```

## 使用 `additionalParams` 的 RDF 示例
<a name="machine-learning-RDF-additionalParams-examples"></a>

### 为模型训练配置指定默认分割率
<a name="machine-learning-RDF-additionalParams-default-split-rate-example"></a>

在以下示例中，`split_rate` 参数设置模型训练的默认分割率。如果未指定默认分割率，则训练将使用 [0.9、0.1、0.0] 的值。您可以通过为每个目标指定一个 `split_rate` 来覆盖每个目标的默认值。

在以下示例中，`default split_rate` 字段表示应使用分割率 `[0.7,0.1,0.2]`，除非针对每个目标进行覆盖：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "split_rate": [0.7,0.1,0.2],
    "targets": [
      (...)
    ]
  }
}
```

### 为模型训练配置指定节点分类任务
<a name="machine-learning-RDF-additionalParams-node-classification-example"></a>

要指明哪个节点属性包含用于训练的带标签的示例，请使用 `"type" : "classification"` 向 `targets` 数组中添加节点分类元素。添加节点字段以指示目标节点的节点类型。添加 `predicate` 字段以定义哪些文本数据用作目标节点的目标节点特征。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

在以下示例中，`node` 目标表示应将每个 `Movie` 节点的 `genre` 属性视为节点类别标签。`split_rate` 值将覆盖默认分割率：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "node": "http://aws.amazon.com/neptune/csv2rdf/class/Movie",
        "predicate": "http://aws.amazon.com/neptune/csv2rdf/datatypeProperty/genre",
        "type": "classification",
        "split_rate": [0.7,0.1,0.2]
      }
    ]
  }
}
```

### 为模型训练配置指定节点回归任务
<a name="machine-learning-RDF-additionalParams-node-regression-example"></a>

要指明哪个节点属性包含用于训练目的的带标签的回归，请使用 `"type" : "regression"` 将节点回归元素添加到目标数组中。添加 `node` 字段以指示目标节点的节点类型。添加 `predicate` 字段以定义哪些文本数据用作目标节点的目标节点特征。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

以下 `node` 目标表示应将每个 `Movie` 节点的 `rating` 属性视为节点回归标签：

```
    "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "node": "http://aws.amazon.com/neptune/csv2rdf/class/Movie",
        "predicate": "http://aws.amazon.com/neptune/csv2rdf/datatypeProperty/rating",
        "type": "regression",
        "split_rate": [0.7,0.1,0.2]
      }
    ]
  }
}
```

### 为特定边缘指定链接预测任务
<a name="machine-learning-RDF-additionalParams-link-prediction-example"></a>

要指示应使用哪些边缘进行链接预测训练目的，请使用 `"type" : "link_prediction"` 向目标数组添加边缘元素。添加 `subject`、`predicate` 和 `object` 字段以指定边缘类型。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

以下 `edge` 目标表示应使用将 `Directors` 连接到 `Movies` 的 `directed` 边缘进行链路预测：

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "subject": "http://aws.amazon.com/neptune/csv2rdf/class/Director",
        "predicate": "http://aws.amazon.com/neptune/csv2rdf/datatypeProperty/directed",
        "object": "http://aws.amazon.com/neptune/csv2rdf/class/Movie",
        "type" : "link_prediction"
      }
    ]
  }
}
```

### 为所有边缘指定链接预测任务
<a name="machine-learning-RDF-additionalParams-link-prediction-example"></a>

要指示应使用所有边缘进行链接预测训练目的，请使用 `"type" : "link_prediction"` 向目标数组添加 `edge` 元素。请勿添加 `subject`、`predicate` 或 `object` 字段。如果您想覆盖默认分割率，则可添加 `split_rate` 字段。

```
  "additionalParams": {
  "neptune_ml": {
    "version": "v2.0",
    "targets": [
      {
        "type" : "link_prediction"
      }
    ]
  }
}
```