

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Neptune ML の Gremlin ノード分類クエリ
<a name="machine-learning-gremlin-vertex-classification-queries"></a>

Neptune ML の Gremlin ノード分類について
+ モデルは、頂点の 1 つのプロパティで学習されます。このプロパティの一意の値のセットは、ノードクラスのセット、または単にクラスと呼ばれます。
+ 頂点のプロパティのノードクラスまたはカテゴリプロパティ値は、ノード分類モデルから推測できます。これは、このプロパティがまだ頂点にアタッチされていない場合に便利です。
+ ノード分類モデルから 1 つ以上のクラスを取得するには、`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()` ステップには、モデルがトレーニングされたキーが含まれており、これがノード分類 ML 推論クエリであることを示す `.with("Neptune#ml.classification")` があります。

現在、複数のプロパティキーは、`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 ロールの両方が DB クラスターパラメータグループに保存されている場合、ノード分類クエリは次のようになります。

```
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]
```