

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

# Neptune ML 中的 Gremlin 節點分類查詢
<a name="machine-learning-gremlin-vertex-classification-queries"></a>

對於 Neptune ML 中的 Gremlin 節點分類：
+ 模型根據頂點的一個屬性進行訓練。這個屬性的唯一值集被稱為節點類別集，或者簡稱為類別。
+ 頂點屬性的節點類別屬性值可從節點分類模型中推斷出來。這在此屬性尚未附加至頂點時很有用。
+ 為了從節點分類模型中擷取一個或多個類別，您需要使用 `with()` 步驟搭配述詞 `Neptune#ml.classification` 來設定 `properties()` 步驟。如果輸出格式是頂點屬性，則輸出格式與您所期望的格式相似。

**注意**  
節點分類僅會使用字串屬性值。這表示不支援數值屬性值 (例如 `0` 或 `1`)，儘管字串等同於 `"0"` 和 `"1"`。同樣地，布林屬性值 `true` 和 `false` 不起作用，但 `"true"` 和 `"false"` 可以。

以下是範例節點分類查詢：

```
g.with( "Neptune#ml.endpoint","node-classification-movie-lens-endpoint" )
 .with( "Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role" )
 .with( "Neptune#ml.limit", 2 )
 .with( "Neptune#ml.threshold", 0.5D )
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
```

此查詢的輸出如下所示：

```
==>vp[genre->Action]
==>vp[genre->Crime]
==>vp[genre->Comedy]
```

在上述查詢中，`V()` 和 `properties()` 步驟的使用方式如下：

`V()` 步驟包含您要從節點分類模型中擷取其類別的頂點集：

```
 .V( "movie_1", "movie_2", "movie_3" )
```

`properties()` 步驟包含模型訓練時根據的索引鍵，而且具有 `.with("Neptune#ml.classification")` 指示這是節點分類 ML 推論查詢。

`properties().with("Neptune#ml.classification")` 步驟中目前不支援多個內容索引鍵。例如，下列查詢會導致例外狀況：

```
g.with("Neptune#ml.endpoint", "node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre", "other_label").with("Neptune#ml.classification")
```

如需特定錯誤訊息，請參閱 [Neptune ML 例外狀況清單](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-node-class-other-queries"></a>

如果推論端點和對應的 IAM 角色都已儲存在您的資料庫叢集參數群組中，則節點分類查詢可以簡單的如下所示：

```
g.V("movie_1", "movie_2", "movie_3").properties("genre").with("Neptune#ml.classification")
```

您可以使用 `union()` 步驟，在查詢中混合頂點屬性和類別：

```
g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .union(
   properties("genre").with("Neptune#ml.classification"),
   properties("genre")
 )
```

您也可以建立無限制的查詢，如下所示：

```
g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V()
 .properties("genre").with("Neptune#ml.classification")
```

您可以使用 `select()` 步驟搭配 `as()` 步驟來擷取節點類別以及頂點：

```
g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" ).as("vertex")
 .properties("genre").with("Neptune#ml.classification").as("properties")
 .select("vertex","properties")
```

您也可以根據節點類別篩選，如下列範例所示：

```
g.with("Neptune#ml.endpoint", "node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
 .has(value, "Horror")

g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
 .has(value, P.eq("Action"))

g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
 .has(value, P.within("Action", "Horror"))
```

您可以使用 `Neptune#ml.score` 述詞，取得節點分類可信度分數：

```
 g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre", "Neptune#ml.score").with("Neptune#ml.classification")
```

回應看起來像這樣：

```
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.01234567]
==>vp[genre->Crime]
==>vp[Neptune#ml.score->0.543210]
==>vp[genre->Comedy]
==>vp[Neptune#ml.score->0.10101]
```

## 在節點分類查詢中使用歸納推論
<a name="machine-learning-gremlin-node-class-inductive"></a>

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

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

您接著可以使用歸納推論查詢，取得反映了新節點的類型和可信度分數：

```
%%gremlin
g.with("Neptune#ml.endpoint", "nc-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').properties("genre", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
```

不過，如果您執行了多次查詢，則可能會得到有些不同的結果：

```
# First time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.12345678]

# Second time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.21365921]
```

你可以使相同的查詢具有確定性：

```
%%gremlin
g.with("Neptune#ml.endpoint", "nc-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').properties("genre", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
 .with("Neptune#ml.deterministic")
```

在這種情況下，每次結果將大致相同：

```
# First time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.12345678]
# Second time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.12345678]
```