

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

# 在 Neptune ML 中使用連結預測模型進行 Gremlin 連結預測查詢
<a name="machine-learning-gremlin-link-prediction-queries"></a>

連結預測模型可以解決如下問題：
+ **前端節點預測**：鑑於頂點和邊緣類型，該頂點可能從哪些頂點連結？
+ **尾端節點預測**：鑑於頂點和邊緣標籤，該頂點可能連結至哪些頂點？

**注意**  
Neptune ML 中尚未支援邊緣預測。

對於下面範例，考慮其中頂點 `User` 和 `Movie` 是由邊緣 `Rated` 連結的簡單圖形。

以下是範例前端節點預測查詢，用來預測最有可能評分電影 (`"movie_1"`、`"movie_2"` 和 `"movie_3"`) 的前五名使用者：

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .with("Neptune#ml.limit", 5)
 .V("movie_1", "movie_2", "movie_3")
 .in("rated").with("Neptune#ml.prediction").hasLabel("user")
```

以下是用於尾端節點預測的類似查詢，用來預測使用者 `"user_1"` 可能評分的前五名電影：

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out("rated").with("Neptune#ml.prediction").hasLabel("movie")
```

邊緣標籤和預測的頂點標籤都是必要的。如果省略其中一個，則會擲出例外狀況。例如，以下查詢沒有預測的頂點標籤，其會擲回例外狀況：

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out("rated").with("Neptune#ml.prediction")
```

同樣地，沒有邊緣標籤的下列查詢也會擲回例外狀況：

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out().with("Neptune#ml.prediction").hasLabel("movie")
```

如需這些例外狀況傳回的特定錯誤訊息，請參閱 [Neptune ML 例外狀況清單](machine-learning-gremlin-exceptions.md)。

## 其他連結預測查詢
<a name="machine-learning-gremlin-other-link-prediction-queries"></a>

您可以使用 `select()` 步驟搭配 `as(`) 步驟，以同時輸出預測的頂點與輸入頂點：

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("movie_1").as("source")
 .in("rated").with("Neptune#ml.prediction").hasLabel("user").as("target")
 .select("source","target")

g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1").as("source")
 .out("rated").with("Neptune#ml.prediction").hasLabel("movie").as("target")
 .select("source","target")
```

您可以進行無限制查詢，如下所示：

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out("rated").with("Neptune#ml.prediction").hasLabel("movie")

g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("movie_1")
 .in("rated").with("Neptune#ml.prediction").hasLabel("user")
```

## 在連結預測查詢中使用歸納推論
<a name="machine-learning-gremlin-link-predict-inductive"></a>

假設您要在 Jupyter 筆記本中將新節點新增至現有圖形，如下所示：

```
%%gremlin
g.addV('label1').property(id,'101').as('newV1')
 .addV('label2').property(id,'102').as('newV2')
 .V('1').as('oldV1')
 .V('2').as('oldV2')
 .addE('eLabel1').from('newV1').to('oldV1')
 .addE('eLabel2').from('oldV2').to('newV2')
```

然後，您可以使用歸納推論查詢來預測前端節點，同時考慮到新節點：

```
%%gremlin
g.with("Neptune#ml.endpoint", "lp-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').out("eLabel1")
 .with("Neptune#ml.prediction")
 .with("Neptune#ml.inductiveInference")
 .hasLabel("label2")
```

結果：

```
==>V[2]
```

同樣地，您可以使用歸納推論查詢來預測尾端節點，同時考慮到新節點：

```
%%gremlin
g.with("Neptune#ml.endpoint", "lp-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('102').in("eLabel2")
 .with("Neptune#ml.prediction")
 .with("Neptune#ml.inductiveInference")
 .hasLabel("label1")
```

結果：

```
==>V[1]
```