

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

# Neptune ML 中的 Gremlin 边缘分类查询
<a name="machine-learning-gremlin-edge-classification-queries"></a>

对于 Neptune ML 中的 Gremlin 边缘分类：
+ 模型是在边缘的一个属性上训练的。此属性的唯一值集合称为一组类。
+ 可以从边缘分类模型中推理边缘的类或分类属性值，当该属性尚未附加到边缘时，该模型很有用。
+ 要从边缘分类模型中获取一个或多个类，您需要使用带有谓词 `"Neptune#ml.classification"` 的 `with()` 步骤来配置 `properties()` 步骤。如果这些是边缘属性，则输出格式与您期望的格式类似。

**注意**  
边缘分类仅适用于字符串属性值。这意味着不支持诸如 `0` 或 `1` 之类的数值属性值，但支持等同的字符串 `"0"` 和 `"1"`。同样，布尔属性值 `true` 和 `false` 不起作用，但 `"true"` 和 `"false"` 起作用。

以下是使用 `Neptune#ml.score` 谓词请求置信度分数的边缘分类查询的示例：

```
g.with("Neptune#ml.endpoint","edge-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("relationship_1","relationship_2","relationship_3")
 .properties("knows_by", "Neptune#ml.score").with("Neptune#ml.classification")
```

响应将如下所示：

```
==>p[knows_by->"Family"]
==>p[Neptune#ml.score->0.01234567]
==>p[knows_by->"Friends"]
==>p[Neptune#ml.score->0.543210]
==>p[knows_by->"Colleagues"]
==>p[Neptune#ml.score->0.10101]
```

## Gremlin 边缘分类查询的语法
<a name="machine-learning-gremlin-edge-classification-syntax"></a>

对于简单的图形，其中 `User` 是头部和尾部节点，`Relationship` 是连接它们的边缘，边缘分类查询的示例为：

```
g.with("Neptune#ml.endpoint","edge-classification-social-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("relationship_1","relationship_2","relationship_3")
 .properties("knows_by").with("Neptune#ml.classification")
```

此查询的输出如下所示：

```
==>p[knows_by->"Family"]
==>p[knows_by->"Friends"]
==>p[knows_by->"Colleagues"]
```

在上面的查询中，`E()` 和 `properties()` 步骤的使用方式如下：
+ `E()` 步骤包含要从边缘分类模型中获取类的一组边缘：

  ```
  .E("relationship_1","relationship_2","relationship_3")
  ```
+ `properties()` 步骤包含训练模型所依据的键，并具有 `.with("Neptune#ml.classification")` 以表明这是边缘分类机器学习推理查询。

目前不支持在 `properties().with("Neptune#ml.classification")` 步骤中使用多个属性键。例如，以下查询会导致引发异常：

```
g.with("Neptune#ml.endpoint","edge-classification-social-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("relationship_1","relationship_2","relationship_3")
 .properties("knows_by", "other_label").with("Neptune#ml.classification")
```

有关具体的错误消息，请参阅[Neptune ML Gremlin 推理查询的异常列表](machine-learning-gremlin-exceptions.md)。

`properties().with("Neptune#ml.classification")` 步骤可以与以下任何步骤结合使用：
+ `value()`
+ `value().is()`
+ `hasValue()`
+ `has(value,"")`
+ `key()`
+ `key().is()`
+ `hasKey()`
+ `has(key,"")`
+ `path()`

## 在边缘分类查询中使用归纳推理
<a name="machine-learning-gremlin-edge-class-inductive"></a>

假设您要在 Jupyter 笔记本的现有图形中添加一个新边缘，如下所示：

```
%%gremlin
g.V('1').as('fromV')
.V('2').as('toV')
.addE('eLabel1').from('fromV').to('toV').property(id, 'e101')
```

然后，您可以使用归纳推理查询来获得一个考虑了新边缘的比例：

```
%%gremlin
g.with("Neptune#ml.endpoint", "ec-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .E('e101').properties("scale", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
```

由于查询不是确定性的，因此，如果您根据随机邻域多次运行该查询，结果会有所不同：

```
# First time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.12345678]

# Second time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.21365921]
```

如果您需要更一致的结果，可以使查询具有确定性：

```
%%gremlin
g.with("Neptune#ml.endpoint", "ec-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .E('e101').properties("scale", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
 .with("Neptune#ml.deterministic")
```

现在，每次运行查询时，结果都会大致相同：

```
# First time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.12345678]

# Second time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.12345678]
```