

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

# Gremlin 推理查询中使用的 Neptune ML 谓词
<a name="machine-learning-gremlin-inference-query-predicates"></a>

## `Neptune#ml.deterministic`
<a name="machine-learning-gremlin-inference-neptune-ml-deterministic-predicate"></a>

此谓词是归纳推理查询的选项，也就是说，适用于包含 [`Neptune#ml.inductiveInference`](#machine-learning-gremlin-inference-neptune-ml-inductiveInference) 谓词的查询。

使用归纳推理时，Neptune 引擎会创建相应的子图形来评估经过训练的 GNN 模型，该子图形的要求取决于最终模型的参数。具体而言，`num-layer` 参数确定从目标节点或边缘开始的遍历跳数，而 `fanouts` 参数指定在每跳处要对多少邻居进行采样（请参阅 [HPO 参数](machine-learning-customizing-hyperparams.md)）。

默认情况下，归纳推理查询在非确定性模式下运行，在这种模式下，Neptune 会随机构建邻域。在进行预测时，这种正常的随机邻居采样有时会产生不同的预测。

当您在归纳推理查询中包含 `Neptune#ml.deterministic` 时，Neptune 引擎尝试以确定性的方式对邻居进行采样，这样对同一查询的多次调用每次都会返回相同的结果。但是，不能保证结果是完全确定的，因为分布式系统的底层图形和构件的变化仍然会带来波动。

您可以在查询中加入 `Neptune#ml.deterministic` 谓词，如下所示：

```
.with("Neptune#ml.deterministic")
```

如果 `Neptune#ml.deterministic` 谓词包含在也不包含 `Neptune#ml.inductiveInference` 的查询中，则直接将其忽略。

## `Neptune#ml.disableInductiveInferenceMetadataCache`
<a name="machine-learning-gremlin-disableInductiveInferenceMetadataCache-predicate"></a>

此谓词是归纳推理查询的选项，也就是说，适用于包含 [`Neptune#ml.inductiveInference`](#machine-learning-gremlin-inference-neptune-ml-inductiveInference) 谓词的查询。

对于归纳推理查询，Neptune 使用存储在 Amazon S3 中的元数据文件来决定构建邻域时的跳数和扇出。Neptune 通常会缓存此模型的元数据，以避免重复从 Amazon S3 获取文件。可以通过在查询中包含 `Neptune#ml.disableInductiveInferenceMetadataCache` 谓词来禁用缓存。尽管 Neptune 直接从 Amazon S3 获取元数据可能会比较慢，但是当 A SageMaker I 终端节点在重新训练或转换后更新并且缓存已过时时，它会很有用。

您可以在查询中加入 `Neptune#ml.disableInductiveInferenceMetadataCache` 谓词，如下所示：

```
.with("Neptune#ml.disableInductiveInferenceMetadataCache")
```

以下是 Jupyter 笔记本中示例查询的样子：

```
%%gremlin
g.with("Neptune#ml.endpoint", "ep1")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .with("Neptune#ml.disableInductiveInferenceMetadataCache")
 .V('101').properties("rating")
 .with("Neptune#ml.regression")
 .with("Neptune#ml.inductiveInference")
```

## `Neptune#ml.endpoint`
<a name="machine-learning-gremlin-inference-neptune-ml-endpoint-predicate"></a>

必要时，在 `with()` 步骤中使用 `Neptune#ml.endpoint` 谓词来指定推理端点：

```
 .with("Neptune#ml.endpoint", "the model's SageMaker AI inference endpoint")
```

您可以通过端点的 `id` 或其 URL 来识别端点。例如：

```
 .with( "Neptune#ml.endpoint", "node-classification-movie-lens-endpoint" )
```

或：

```
 .with( "Neptune#ml.endpoint", "https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/node-classification-movie-lens-endpoint/invocations" )
```

**注意**  
如果您将 Neptune 数据库集群参数组中的 [`neptune_ml_endpoint` 参数](machine-learning-cluster-setup.md#machine-learning-set-inference-endpoint-cluster-parameter)设置为端点 `id` 或 URL，则无需在每个查询中都包含 `Neptune#ml.endpoint` 谓词。

## `Neptune#ml.iamRoleArn`
<a name="machine-learning-gremlin-inference-neptune-ml-iamRoleArn-predicate"></a>

`Neptune#ml.iamRoleArn`在`with()`步骤中用于指定 A SageMaker I 执行 IAM 角色的 ARN（如有必要）：

```
 .with("Neptune#ml.iamRoleArn", "the ARN for the SageMaker AI execution IAM role")
```

有关如何创建 A SageMaker I 执行 IAM 角色的信息，请参阅[创建自定义 NeptuneSageMakerIAMRole 角色](machine-learning-manual-setup.md#ml-manual-setup-sm-role)。

**注意**  
如果您[将 Neptune 数据库集群参数组中的参数设置](machine-learning-cluster-setup.md#machine-learning-enabling-create-param-group)为 A SageMaker I 执行 IAM 角色的 ARN，则无需在每个查询中都包含该`Neptune#ml.iamRoleArn`谓词。`neptune_ml_iam_role`

## Neptune\$1ml.inductiveInference
<a name="machine-learning-gremlin-inference-neptune-ml-inductiveInference"></a>

Gremlin 中已默认启用转导推理。要进行[实时归纳推理](machine-learning-overview-evolving-data.md#inductive-vs-transductive-inference)查询，请加入 `Neptune#ml.inductiveInference` 谓词，如下所示：

```
.with("Neptune#ml.inductiveInference")
```

如果您的图形是动态的，则归纳推理通常是最佳选择；但如果图形是静态的，则转导推理会更快、更高效。

## `Neptune#ml.limit`
<a name="machine-learning-gremlin-inference-neptune-ml-limit-predicate"></a>

`Neptune#ml.limit` 谓词可以选择限制每个实体返回的结果数：

```
 .with( "Neptune#ml.limit", 2 )
```

默认情况下，限制为 1，可设置的最大数量为 100。

## `Neptune#ml.threshold`
<a name="machine-learning-gremlin-inference-neptune-ml-threshold-predicate"></a>

`Neptune#ml.threshold` 谓词可以选择为结果分数设定截止阈值：

```
 .with( "Neptune#ml.threshold", 0.5D )
```

这使您可以丢弃分数低于指定阈值的所有结果。

## `Neptune#ml.classification`
<a name="machine-learning-gremlin-inference-neptune-ml-classification-predicate"></a>

将`Neptune#ml.classification`谓词附加到`properties()`步骤中，以确定需要从节点分类模型的 SageMaker AI 端点获取属性：

```
 .properties( "property key of the node classification model" ).with( "Neptune#ml.classification" )
```

## `Neptune#ml.regression`
<a name="machine-learning-gremlin-inference-neptune-ml-regression-predicate"></a>

将`Neptune#ml.regression`谓词附加到`properties()`步骤中，以确定需要从节点回归模型的 SageMaker AI 端点获取属性：

```
 .properties( "property key of the node regression model" ).with( "Neptune#ml.regression" )
```

## `Neptune#ml.prediction`
<a name="machine-learning-gremlin-inference-neptune-ml-prediction-predicate"></a>

`Neptune#ml.prediction` 谓词附加到 `in()` 和 `out()` 步骤，以确定这是一个链接预测查询：

```
 .in("edge label of the link prediction model").with("Neptune#ml.prediction").hasLabel("target node label")
```

## `Neptune#ml.score`
<a name="machine-learning-gremlin-inference-neptune-ml-score-predicate"></a>

在 Gremlin 节点或边缘分类查询中使用 `Neptune#ml.score` 谓词来获取机器学习置信度分数。在 `properties()` 步骤中应将 `Neptune#ml.score` 谓词与查询谓词一起传递，以获得节点或边缘分类查询的 ML 置信度分数。

您可以找到一个包含[其它节点分类示例](machine-learning-gremlin-vertex-classification-queries.md#machine-learning-gremlin-node-class-other-queries)的节点分类示例，以及[边缘分类部分](machine-learning-gremlin-edge-classification-queries.md)中的一个边缘分类示例。