

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

# 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 擷取中繼資料可能較慢，但當 SageMaker AI 端點在重新訓練或轉換後更新且快取過時時，此功能非常有用。

您可以在查詢中包含 `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>

`Neptune#ml.endpoint` 述詞會在 `with()` 步驟中用來指定推論端點 (如有必要)：

```
 .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()`步驟中使用，以指定 SageMaker AI 執行 IAM 角色的 ARN：

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

如需如何建立 SageMaker AI 執行 IAM 角色的資訊，請參閱 [建立自訂 NeptuneSageMakerIAMRole 角色](machine-learning-manual-setup.md#ml-manual-setup-sm-role)。

**注意**  
如果您在 Neptune 資料庫叢集參數群組中[將 `neptune_ml_iam_role` 參數](machine-learning-cluster-setup.md#machine-learning-enabling-create-param-group)設定為 SageMaker AI 執行 IAM 角色的 ARN，則不需要在每個查詢中包含`Neptune#ml.iamRoleArn`述詞。

## 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>

`Neptune#ml.score` 述詞用於 Gemlin 節點或邊緣分類查詢中，以擷取機器學習可信度分數。`Neptune#ml.score` 述詞應該與 `properties()` 步驟中的查詢述詞一起傳遞，以取得節點或邊緣分類查詢的 ML 可信度分數。

您可以在節點分類區段中找到包含[其他節點分類範例](machine-learning-gremlin-vertex-classification-queries.md#machine-learning-gremlin-node-class-other-queries)的[節點分類範例，以及邊緣](machine-learning-gremlin-edge-classification-queries.md)分類範例。