

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

# Neptune ML での推論クエリ
<a name="machine-learning-inference-queries"></a>

Gremlin または SPARQL のいずれかを使用して、Neptune ML 推論エンドポイントを照会できます。ただし、[リアルタイムの帰納的推論](machine-learning-overview-evolving-data.md#inductive-vs-transductive-inference)は、現時点では、Gremlin クエリでのみサポートされています。

# Neptune ML の Gremlin 推論クエリ
<a name="machine-learning-gremlin-inference-queries"></a>

[Neptune ML 機能](machine-learning.md#machine-learning-capabilities) で説明しているように、Neptune ML は次の種類の推論タスクを実行できるトレーニングモデルをサポートしています。
+ **ノード分類** — 頂点プロパティのカテゴリカル特徴を予測します。
+ **ノードリグレッション** — 頂点の数値プロパティを予測します。
+ **エッジ分類** — エッジプロパティのカテゴリ別特徴を予測します。
+ **エッジリグレッション** — エッジの数値プロパティを予測します。
+ **リンク予測** — 始点ノードと送信エッジが与えられた終点ノード、または終点ノードと着信エッジを指定した始点ノードを予測します。

これらのさまざまなタスクを、[GroupLens Reserch](https://grouplens.org/datasets/movielens/) 提供の [MovieLens 100k データセット](https://grouplens.org/datasets/movielens/100k/)​ を使用する例で示すことができます。このデータセットは、映画、ユーザー、およびユーザーによる映画の評価から構成され、そこから次のようなプロパティグラフを作成しました。

![\[MovieLens 100k データセットを使用した映画プロパティグラフの例\]](http://docs.aws.amazon.com/ja_jp/neptune/latest/userguide/images/movie_property_graph_example.png)


**ノード分類**: 上記のデータセットでは、`Genre` は、エッジ `included_in` によって頂点タイプ `Movie` に接続されている頂点タイプです。ただし、データセットを微調整して `Genre` を頂点タイプ `Movie` の[カテゴリ別](https://en.wikipedia.org/wiki/Categorical_variable)特徴にすると、ナレッジグラフに追加された新しい映画の推論の問題 `Genre` は、ノード分類モデルを使用して解くことができます。

**ノードリグレッション**: 頂点タイプ `Rating` を、`timestamp` および `score` のようなプロパティを持つと考えると、`Rating` に対する数値 `Score` を推論する問題は、ノードリグレッションモデルを使用して解くことができます。

**エッジ分類**: 同様に、`Rated` エッジに関して、`Love`、`Like`、`Dislike`、`Neutral`、`Hate` のうち 1 つの値となる可能性があるプロパティ `Scale` がある場合、新しい映画/レーティング `Rated` エッジに対する推論の問題 `Scale` は、エッジ分類モデルを使用して解けます。

**エッジリグレッション**: 同様に、同じように `Rated` エッジに関して、レーティングの数値を保持する `Score` プロパティがある場合、これはエッジリグレッションモデルから推測できます。

**リンク予測**: 特定の映画を評価する可能性が最も高い上位 10 人のユーザーを見つけたり、特定のユーザーが評価する可能性が最も高い上位 10 本の映画を見つけたりするなどの問題は、リンク予測に該当します。

**注記**  
Neptune ML ユースケースについては、各ユースケースについて実践的に理解できるように設計された非常に豊富なノートブックセットがあります。Neptune [ML CloudFormation テンプレート](machine-learning-quick-start.md)を使用して Neptune ML クラスターを作成するときに、これらのノートブックを Neptune クラスターと一緒に作成できます。これらのノートブックは、[GitHub](https://github.com/aws/graph-notebook/tree/main/src/graph_notebook/notebooks/04-Machine-Learning) でも利用できます。

**Topics**
+ [Gremlin 推論クエリで使用される Neptune ML 述語](machine-learning-gremlin-inference-query-predicates.md)
+ [Neptune ML の Gremlin ノード分類クエリ](machine-learning-gremlin-vertex-classification-queries.md)
+ [Neptune ML での Gremlin ノードリグレッションクエリ](machine-learning-gremlin-vertex-regression-queries.md)
+ [Neptune ML の Gremlin エッジ分類クエリ](machine-learning-gremlin-edge-classification-queries.md)
+ [Neptune ML での Gremlin エッジリグレッションクエリ](machine-learning-gremlin-edge-regression.md)
+ [Neptune ML のリンク予測モデルを使用した Gremlin リンク予測クエリ](machine-learning-gremlin-link-prediction-queries.md)
+ [Neptune ML Gremlin 推論クエリの例外のリスト](machine-learning-gremlin-exceptions.md)

# 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 DB クラスターのパラメータグループで[`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>

必要に応じて、SageMaker AI 実行 IAM ロールの ARN を指定するために、`with()` ステップで `Neptune#ml.iamRoleArn` が使用されます。

```
 .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 DB クラスターのパラメータグループで SageMaker AI 実行 IAM ロールの ARN に [`neptune_ml_iam_role` パラメータを設定](machine-learning-cluster-setup.md#machine-learning-enabling-create-param-group)すれば、各クエリの `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` 述語は、ノード分類モデルの SageMaker AI エンドポイントから取得する必要があるプロパティを確立するために `properties()` ステップに追加されています。

```
 .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` 述語は、ノードリグレッションモデルの AI SageMaker エンドポイントから取得する必要があるプロパティを確立するために `properties()` ステップに添付されています。

```
 .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` 述語は Gremlin ノードまたはエッジ分類クエリで、機械学習の信頼スコアを取得するために使用されます。`Neptune#ml.score` 述語は、ノードまたはエッジ分類クエリの ML 信頼度スコアを取得するために、`properties()` ステップでクエリ述語とともに渡されます。

ノード分類の例については、[その他のノード分類の例](machine-learning-gremlin-vertex-classification-queries.md#machine-learning-gremlin-node-class-other-queries)で、また、[エッジ分類セクション](machine-learning-gremlin-edge-classification-queries.md)のエッジ分類の例でご確認いただけます。

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

# Neptune ML での Gremlin ノードリグレッションクエリ
<a name="machine-learning-gremlin-vertex-regression-queries"></a>

ノードリグレッションは、各ノードのリグレッションモデルから推測される値が数値であることを除いて、ノード分類に似ています。次の違いを除いて、ノード分類の場合と同じ Gremlin クエリをノードリグレッションに使用できます。
+ 繰り返しますが、Neptune ML では、ノードは頂点を指します。
+ `properties()` ステップは `properties().with("Neptune#ml.classification")` の代わりに `properties().with("Neptune#ml.regression")` 形式を取ります。
+ `"Neptune#ml.limit`" と `"Neptune#ml.threshold"` 述語は適用されません。
+ 値をフィルタリングするときは、数値を指定する必要があります。

頂点分類クエリの例を次に示します。

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

次の例に示すように、リグレッションモデルを使用して推論された値をフィルタリングできます。

```
g.with("Neptune#ml.endpoint","node-regression-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("movie_1","movie_2","movie_3")
 .properties("revenue").with("Neptune#ml.regression")
 .value().is(P.gte(1600000))

g.with("Neptune#ml.endpoint","node-regression-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("movie_1","movie_2","movie_3")
 .properties("revenue").with("Neptune#ml.regression")
 .hasValue(P.lte(1600000D))
```

## ノードリグレッションクエリでの帰納的推論の使用
<a name="machine-learning-gremlin-node-regress-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", "nr-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').properties("rating")
 .with("Neptune#ml.regression")
 .with("Neptune#ml.inductiveInference")
```

このクエリは決定論的ではないため、複数回実行すると、近傍によって多少異なる結果が返される可能性があります。

```
# First time
==>vp[rating->9.1]

# Second time
==>vp[rating->8.9]
```

より一貫性のある結果が必要な場合は、クエリを決定論的にすることもできます。

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

これで、結果は毎回ほぼ同じになります。

```
# First time
==>vp[rating->9.1]

# Second time
==>vp[rating->9.1]
```

# Neptune ML の Gremlin エッジ分類クエリ
<a name="machine-learning-gremlin-edge-classification-queries"></a>

Neptune MLにおける Gremlin エッジ分類について
+ モデルは、エッジの 1 つのプロパティで学習されます。このプロパティの一意の値のセットは、クラスのセットと呼ばれます。
+ エッジのクラスまたはカテゴリプロパティ値は、エッジ分類モデルから推測できます。これは、このプロパティがエッジにまだ添付されていない場合に便利です。
+ エッジ分類モデルから 1 つ以上のクラスを取得するには、`with()` ステップで述語 `"Neptune#ml.classification"` を使い `properties()` ステップを設定する必要があります。出力形式は、それらがエッジプロパティである場合に予想されるものと似ています。

**注記**  
エッジ分類は、文字列プロパティ値でのみ機能します。つまり、`0` や `1` などの数値プロパティ値はサポートされませんが、該当する文字列 `"0"` および `"1"` はサポートされます。同様に、ブールのプロパティ値 `true` および `false` は機能しませんが、`"true"` および `"false"` は機能します。

次に示すのは、`Neptune#ml.score` 述語を使用して信頼スコアを要求するエッジ分類クエリの例です。

```
g.with("Neptune#ml.endpoint","edge-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("relationship_1","relationship_2","relationship_3")
 .properties("knows_by", "Neptune#ml.score").with("Neptune#ml.classification")
```

また、レスポンスは次のようになります。

```
==>p[knows_by->"Family"]
==>p[Neptune#ml.score->0.01234567]
==>p[knows_by->"Friends"]
==>p[Neptune#ml.score->0.543210]
==>p[knows_by->"Colleagues"]
==>p[Neptune#ml.score->0.10101]
```

## Gremlin エッジ分類クエリの構文
<a name="machine-learning-gremlin-edge-classification-syntax"></a>

`User` は先頭と末尾のノードであり、`Relationship` はそれらを接続するエッジである単純なグラフの場合、エッジ分類クエリの例は次のとおりです。

```
g.with("Neptune#ml.endpoint","edge-classification-social-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("relationship_1","relationship_2","relationship_3")
 .properties("knows_by").with("Neptune#ml.classification")
```

このクエリの出力は、次のようになります。

```
==>p[knows_by->"Family"]
==>p[knows_by->"Friends"]
==>p[knows_by->"Colleagues"]
```

上記のクエリでは、`E()` および `properties()` ステップは次のように使用されます。
+ `E()` ステップには、エッジ分類モデルからクラスを取得するエッジのセットが含まれます。

  ```
  .E("relationship_1","relationship_2","relationship_3")
  ```
+ `properties()` ステップには、モデルがトレーニングされたキーが含まれており、これがエッジ分類 ML 推論クエリであることを示す `.with("Neptune#ml.classification")` があります。

現在、複数のプロパティキーは、`properties().with("Neptune#ml.classification")` ステップではサポートされていません。たとえば、次のクエリでは、例外がスローされます。

```
g.with("Neptune#ml.endpoint","edge-classification-social-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("relationship_1","relationship_2","relationship_3")
 .properties("knows_by", "other_label").with("Neptune#ml.classification")
```

具体的なエラーメッセージについては、[Neptune ML Gremlin 推論クエリの例外のリスト](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-edge-class-inductive"></a>

Jupyter ノートブックの既存のグラフに、次のように新しいエッジを追加するとします。

```
%%gremlin
g.V('1').as('fromV')
.V('2').as('toV')
.addE('eLabel1').from('fromV').to('toV').property(id, 'e101')
```

その場合、帰納的推論クエリを使用して、新しいエッジを考慮したスケールを得ることができます。

```
%%gremlin
g.with("Neptune#ml.endpoint", "ec-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .E('e101').properties("scale", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
```

このクエリは決定論的ではないため、複数回実行すると、ランダム近傍によって多少異なる結果が返される可能性があります。

```
# First time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.12345678]

# Second time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.21365921]
```

より一貫性のある結果が必要な場合は、クエリを決定論的にすることもできます。

```
%%gremlin
g.with("Neptune#ml.endpoint", "ec-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .E('e101').properties("scale", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
 .with("Neptune#ml.deterministic")
```

これで、クエリを実行するたびに結果がほぼ同じになります。

```
# First time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.12345678]

# Second time
==>vp[scale->Like]
==>vp[Neptune#ml.score->0.12345678]
```

# Neptune ML での Gremlin エッジリグレッションクエリ
<a name="machine-learning-gremlin-edge-regression"></a>

エッジリグレッションは、モデルから推測される値が数値であることを除いて、エッジ分類に似ています。エッジリグレッションの場合、Neptune ML は分類と同じクエリをサポートします。

注意すべき重要な点は次のとおりです。
+ このユースケースの `properties()` ステップを設定するために、ML 述語 `"Neptune#ml.regression"` を使用する必要があります。
+ `"Neptune#ml.limit"` および `"Neptune#ml.threshold"` 述語はこのユースケースでは適用されません。
+ 値をフィルタリングするには、値を数値として指定する必要があります。

## Gremlin エッジリグレッションクエリの構文
<a name="machine-learning-gremlin-edge-regression-syntax"></a>

`User` は先頭ノード、`Movie` は末尾ノード、`Rated` はそれらを接続するエッジである単純なグラフの場合、エッジ `Rated` についてスコアと呼ばれる数値評価値を見つけるエッジリグレッションクエリの例をここに示します。

```
g.with("Neptune#ml.endpoint","edge-regression-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("rating_1","rating_2","rating_3")
 .properties("score").with("Neptune#ml.regression")
```

ML リグレッションモデルから推論された値でフィルタリングすることもできます。`"rating_1"`、`"rating_2"`、`"rating_3"` によって識別される既存の `Rated` エッジ (`User` から `Movie` へ) である、ここで、エッジプロパティ `Score` はこれらの評価には存在しません。9 以上のエッジに対して、次のようなクエリを使用して `Score` を推論できます。

```
g.with("Neptune#ml.endpoint","edge-regression-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .E("rating_1","rating_2","rating_3")
 .properties("score").with("Neptune#ml.regression")
 .value().is(P.gte(9))
```

## エッジリグレッションクエリでの帰納的推論の使用
<a name="machine-learning-gremlin-edge-regression-inductive"></a>

Jupyter ノートブックの既存のグラフに、次のように新しいエッジを追加するとします。

```
%%gremlin
g.V('1').as('fromV')
.V('2').as('toV')
.addE('eLabel1').from('fromV').to('toV').property(id, 'e101')
```

この場合、帰納的推論クエリを使用して、新しいエッジを考慮したスコアを取得できます。

```
%%gremlin
g.with("Neptune#ml.endpoint", "er-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .E('e101').properties("score")
 .with("Neptune#ml.regression")
 .with("Neptune#ml.inductiveInference")
```

このクエリは決定論的ではないため、複数回実行すると、ランダム近傍によって多少異なる結果が返される可能性があります。

```
# First time
==>ep[score->96]

# Second time
==>ep[score->91]
```

より一貫性のある結果が必要な場合は、クエリを決定論的にすることもできます。

```
%%gremlin
g.with("Neptune#ml.endpoint", "er-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .E('e101').properties("score")
 .with("Neptune#ml.regression")
 .with("Neptune#ml.inductiveInference")
 .with("Neptune#ml.deterministic")
```

これで、クエリを実行するたびに結果がほぼ同じになります。

```
# First time
==>ep[score->96]

# Second time
==>ep[score->96]
```

# Neptune ML のリンク予測モデルを使用した Gremlin リンク予測クエリ
<a name="machine-learning-gremlin-link-prediction-queries"></a>

リンク予測モデルでは、次のような問題が解けます。
+ **先頭ノード予測**: 頂点とエッジタイプを指定すると、その頂点からリンクする可能性が高い頂点はどれか?
+ **末尾ノード予測**: 頂点とエッジラベルを指定すると、その頂点へとリンクする可能性が高い頂点はどれか?

**注記**  
エッジ予測は Neptune ML ではまだサポートされていません。

以下の例では、エッジ `Rated` でリンクされている `User` および `Movie` 頂点を持つ単純なグラフを考えてみましょう。

以下は、映画 `"movie_1"`、`"movie_2"` および `"movie_3"` を評価する可能性が最も高い上位 5 人のユーザーを予測するために使用されるサンプルヘッドノード予測クエリです。

```
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"` が評価する可能性が高い上位 5 本の映画を予測するために使用する、テールノード予測の類似のものです。

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

# Neptune ML Gremlin 推論クエリの例外のリスト
<a name="machine-learning-gremlin-exceptions"></a>

 これは、Neptune ML Gremlin 推論クエリの実行時に発生する可能性のある例外の包括的なリストです。これらの例外は、指定された IAM ロールまたはエンドポイントの問題から、サポートされていない Gremlin ステップやクエリあたりの ML 推論クエリ数の制限まで、さまざまな問題を対象としています。各エントリには、問題を説明する詳細なメッセージが含まれています。
+ **`BadRequestException`** — 指定されたロールの認証情報を読み込めません。

  *メッセージ*: `Unable to load credentials for role: the specified IAM Role ARN.`
+ **`BadRequestException`** — 指定された IAM ロールは SageMaker AI エンドポイントを呼び出す権限がありません。

  *メッセージ*: `User: the specified IAM Role ARN is not authorized to perform: sagemaker:InvokeEndpoint on resource: the specified endpoint.`
+ **`BadRequestException`** — 指定されたエンドポイントは存在しません。

  *メッセージ*: `Endpoint the specified endpoint not found.`
+ **`InternalFailureException`** — Neptune ML のリアルタイム帰納的推論メタデータを Amazon S3 から取得することはできません。

  *メッセージ*: `Unable to fetch Neptune ML - Real-Time Inductive Inference metadata from S3. Check the permissions of the S3 bucket or if the Neptune instance can connect to S3.`
+ **`InternalFailureException`** — Neptune ML は、Amazon S3 でリアルタイム帰納的推論のメタデータファイルを見つけることができません。

  *メッセージ*: `Neptune ML cannot find the metadata file for Real-Time Inductive Inference in S3.`
+ **`InvalidParameterException`** — 指定されたエンドポイントは構文的に有効ではありません。

  *メッセージ*: `Invalid endpoint provided for external service query.`
+ **`InvalidParameterException`** — 指定された SageMaker 実行 IAM ロール ARN は構文的に有効ではありません。

  *メッセージ*: `Invalid IAM role ARN provided for external service query.`
+ **`InvalidParameterException`** — クエリの `properties()` ステップで複数のプロパティキーが指定されます。

  *メッセージ*: `ML inference queries are currently supported for one property key.`
+ **`InvalidParameterException`** — クエリで複数のエッジラベルが指定されます。

  *メッセージ*: `ML inference are currently supported only with one edge label.`
+ **`InvalidParameterException`** — クエリで複数の頂点ラベル制約が指定されます。

  *メッセージ*: `ML inference are currently supported only with one vertex label constraint.`
+ **`InvalidParameterException`** — `Neptune#ml.classification` および `Neptune#ml.regression` 述語の両方が同じクエリに存在します。

  *メッセージ*: `Both regression and classification ML predicates cannot be specified in the query.`
+ **`InvalidParameterException`** — リンク予測クエリの `in()` または `out()` ステップで複数のエッジラベルが指定されます。

  *メッセージ*: `ML inference are currently supported only with one edge label.`
+ **`InvalidParameterException`** — Neptune\$1ml.score で複数のプロパティキーが指定されました。

  *メッセージ*: `Neptune ML inference queries are currently supported for one property key and one Neptune#ml.score property key.`
+ **`MissingParameterException`** — エンドポイントがクエリで、または DB クラスターパラメータとして指定されていません。

  *メッセージ*: `No endpoint provided for external service query.`
+ **``MissingParameterException** — SageMaker AI 実行 IAM ロールがクエリで、または DB クラスターパラメータとして指定されていません。

  *メッセージ*: `No IAM role ARN provided for external service query.`
+ **`MissingParameterException`** — プロパティキーがクエリの `properties()` ステップにありません。

  *メッセージ*: `Property key needs to be specified using properties() step for ML inference queries.`
+ **`MissingParameterException`**　— リンク予測クエリの `in()` または `out()` ステップでエッジラベルが指定されていません。

  *メッセージ*: `Edge label needs to be specified while using in() or out() step for ML inference queries.`
+ **`MissingParameterException`** — Neptune\$1ml.score でプロパティキーが指定されませんでした。

  *メッセージ*: `Property key needs to be specified along with Neptune#ml.score property key while using the properties() step for Neptune ML inference queries.`
+ **`UnsupportedOperationException`** — `both()` ステップは、リンク予測クエリで使用されます。

  *メッセージ*: `ML inference queries are currently not supported with both() step.`
+ **`UnsupportedOperationException`** — リンク予測クエリの `in()` または `out()` ステップとともに`has()` ステップで予測された頂点ラベルが指定されませんでした。

  *メッセージ*: `Predicted vertex label needs to be specified using has() step for ML inference queries.`
+ **`UnsupportedOperationException`** — Gremlin ML の帰納的推論クエリは、現在、最適化されていないステップではサポートされていません。

  *メッセージ*: `Neptune ML - Real-Time Inductive Inference queries are currently not supported with Gremlin steps which are not optimized for Neptune. Check the Neptune User Guide for a list of Neptune-optimized steps.`
+ **`UnsupportedOperationException`** — Neptune ML 推論クエリは、現在、`repeat` ステップ内ではサポートされていません。

  *メッセージ*: `Neptune ML inference queries are currently not supported inside a repeat step.`
+ **`UnsupportedOperationException`** — 現在、1 つの Gremlin クエリでサポートされている Neptune ML 推論クエリは 1 つだけです。

  *メッセージ*: `Neptune ML inference queries are currently supported only with one ML inference query per gremlin query.`

# Neptune ML での SPARQL 推論クエリ
<a name="machine-learning-sparql-inference-queries"></a>

Neptune ML は、RDF グラフをプロパティグラフにマッピングし、ML タスクをモデル化します。現在、次のユースケースをサポートしています。
+ **オブジェクト分類** — オブジェクトのカテゴリ別特徴を予測します。
+ **オブジェクト回帰** — オブジェクトの数値プロパティを予測します。
+ **オブジェクト予測**— 対象と関係が与えられたオブジェクトを予測します。
+ **件名の予測** — オブジェクトと関係が与えられた対象を予測します。

**注記**  
Neptune ML は SPARQL での主題分類と回帰のユースケースをサポートしていません。

# SPARQL 推論クエリで使用されるNeptune ML 述語
<a name="machine-learning-sparql-inference-query-predicates"></a>

SPARQL 推論では、次の述語が使用されます。

## `neptune-ml:timeout` 述語
<a name="machine-learning-sparql-inference-predicates-timeout"></a>

リモートサーバーとの接続のタイムアウトを指定します。サーバーが要求を満たすのにかかる最大時間であるクエリ要求のタイムアウトと混同しないでください。

クエリタイムアウトが、`neptune-ml:timeout` 述語発生で指定されたサービスタイムアウトの前に発生した場合は、サービス接続もキャンセルされます。

## `neptune-ml:outputClass` 述語
<a name="machine-learning-sparql-inference-predicates-outputClass"></a>

`neptune-ml:outputClass` 述語は、オブジェクト予測の予測オブジェクトのクラスまたはサブジェクト予測の予測対象を定義するためにのみ使用されます。

## `neptune-ml:outputScore` 述語
<a name="machine-learning-sparql-inference-predicates-outputScore"></a>

`neptune-ml:outputScore` 述語は、機械学習モデルの出力が正しい可能性を表す正の数です。

## `neptune-ml:modelType` 述語
<a name="machine-learning-sparql-inference-predicates-modelType"></a>

`neptune-ml:modelType` 述語は、学習する機械学習モデルのタイプを指定します。
+ `OBJECT_CLASSIFICATION`
+ `OBJECT_REGRESSION`
+ `OBJECT_PREDICTION`
+ `SUBJECT_PREDICTION`

## `neptune-ml:input` 述語
<a name="machine-learning-sparql-inference-predicates-input"></a>

`neptune-ml:input` 述語は、Neptune ML の入力として使用される URI のリストを指します。

## `neptune-ml:output` 述語
<a name="machine-learning-sparql-inference-predicates-output"></a>

`neptune-ml:output` 述語は、Neptune ML が結果を返す結合集合のリストを指します。

## `neptune-ml:predicate` 述語
<a name="machine-learning-sparql-inference-predicates-predicate"></a>

`neptune-ml:predicate` 述語は、実行されるタスクに応じて異なる方法で使用されます。
+ **オブジェクトまたはサブジェクト予測**の場合: 述語のタイプ (エッジまたはリレーションシップタイプ) を定義します。
+ **オブジェクトの分類と回帰**の場合: 予測するリテラル (プロパティ) を定義します。

## `neptune-ml:batchSize` 述語
<a name="machine-learning-sparql-inference-predicates-batchSize"></a>

`neptune-ml:batchSize` はリモートサービスコールの入力サイズを指定します。

# SPARQL オブジェクト分類の例
<a name="machine-learning-sparql-inference-object-classification"></a>

Neptune ML での SPARQL オブジェクト分類では、モデルは述語値の 1 つに基づいてトレーニングされます。これは、その述語が特定の主語にまだ存在していない場合に便利です。

オブジェクト分類モデルを使用すると、カテゴリカル述語値のみを推論できます。

次のクエリは、`foaf:Person` タイプのすべての入力の <http://www.example.org/team> 述語値を予測しようとします。

```
SELECT * WHERE { ?input a foaf:Person .
  SERVICE neptune-ml:inference {
    neptune-ml:config neptune-ml:modelType 'OBJECT_CLASSIFICATION' ;
                      neptune-ml:input ?input ;
                      neptune-ml:predicate <http://www.example.org/team> ;
                      neptune-ml:output ?output .
  }
}
```

このクエリは、次のようにカスタマイズできます。

```
SELECT * WHERE { ?input a foaf:Person .
  SERVICE neptune-ml:inference {
    neptune-ml:config neptune-ml:endpoint 'node-prediction-account-balance-endpoint' ;
                      neptune-ml:iamRoleArn 'arn:aws:iam::0123456789:role/sagemaker-role' ;

                      neptune-ml:batchSize "40"^^xsd:integer ;
                      neptune-ml:timeout "1000"^^xsd:integer ;

                      neptune-ml:modelType 'OBJECT_CLASSIFICATION' ;
                      neptune-ml:input ?input ;
                      neptune-ml:predicate <http://www.example.org/team> ;
                      neptune-ml:output ?output .
  }
}
```

# SPARQL オブジェクト回帰の例
<a name="machine-learning-sparql-inference-object-regression"></a>

オブジェクト回帰は、各ノードの回帰モデルから推定される数値の述語値を除いて、オブジェクト分類に似ています。オブジェクト回帰には、オブジェクト分類の場合と同じ SPARQL クエリを使用できます。ただし、`the Neptune#ml.limit` および `Neptune#ml.threshold` 述語は適用されません。

次のクエリは、`foaf:Person` タイプのすべての入力の <http://www.example.org/accountbalance> 述語値を予測しようとします。

```
SELECT * WHERE { ?input a foaf:Person .
  SERVICE neptune-ml:inference {
    neptune-ml:config neptune-ml:modelType 'OBJECT_REGRESSION' ;
                      neptune-ml:input ?input ;
                      neptune-ml:predicate <http://www.example.org/accountbalance> ;
                      neptune-ml:output ?output .
  }
}
```

このクエリは、次のようにカスタマイズできます。

```
SELECT * WHERE { ?input a foaf:Person .
  SERVICE neptune-ml:inference {
    neptune-ml:config neptune-ml:endpoint 'node-prediction-account-balance-endpoint' ;
                      neptune-ml:iamRoleArn 'arn:aws:iam::0123456789:role/sagemaker-role' ;

                      neptune-ml:batchSize "40"^^xsd:integer ;
                      neptune-ml:timeout "1000"^^xsd:integer ;

                      neptune-ml:modelType 'OBJECT_REGRESSION' ;
                      neptune-ml:input ?input ;
                      neptune-ml:predicate <http://www.example.org/accountbalance> ;
                      neptune-ml:output ?output .
  }
}
```

# SPARQL オブジェクト予測の例
<a name="machine-learning-sparql-inference-object-prediction"></a>

*オブジェクト予測*は、指定されたサブジェクトと述語のオブジェクト値を予測します。

次のオブジェクト予測クエリは、タイプ `foaf:Person` の入力がどの映画を好むのかを予測します。

```
?x a foaf:Person .
?x   <http://www.example.org/likes> ?m .
?m a <http://www.example.org/movie> .

## Query
SELECT * WHERE { ?input a foaf:Person .
  SERVICE neptune-ml:inference {
    neptune-ml:config neptune-ml:modelType 'OBJECT_PREDICTION' ;
                      neptune-ml:input ?input ;
                      neptune-ml:predicate <http://www.example.org/likes> ;
                      neptune-ml:output ?output ;
                      neptune-ml:outputClass <http://www.example.org/movie> .
  }
}
```

クエリ自体は、次のようにカスタマイズできます。

```
SELECT * WHERE { ?input a foaf:Person .
  SERVICE neptune-ml:inference {
    neptune-ml:config neptune-ml:endpoint 'node-prediction-user-movie-prediction-endpoint' ;
                      neptune-ml:iamRoleArn 'arn:aws:iam::0123456789:role/sagemaker-role' ;

                      neptune-ml:limit "5"^^xsd:integer ;
                      neptune-ml:batchSize "40"^^xsd:integer ;
                      neptune-ml:threshold "0.1"^^xsd:double ;
                      neptune-ml:timeout "1000"^^xsd:integer ;
                      neptune-ml:outputScore ?score ;

                      neptune-ml:modelType 'OBJECT_PREDICTION' ;
                      neptune-ml:input ?input ;
                      neptune-ml:predicate <http://www.example.org/likes> ;
                      neptune-ml:output ?output ;
                      neptune-ml:outputClass <http://www.example.org/movie> .
  }
}
```

# SPARQL サブジェクト予測の例
<a name="machine-learning-sparql-inference-subject-prediction"></a>

*件名の予測*は、述語とオブジェクトが与えられた主語を予測します。

たとえば、次のクエリは、特定の映画を視聴するのは誰か (タイプ `foaf:User`) を予測します。

```
SELECT * WHERE { ?input (a foaf:Movie) .
    SERVICE neptune-ml:inference {
        neptune-ml:config neptune-ml:modelType 'SUBJECT_PREDICTION' ;
                          neptune-ml:input ?input ;
                          neptune-ml:predicate <http://aws.amazon.com/neptune/csv2rdf/object_Property/rated> ;
                          neptune-ml:output ?output ;
                          neptune-ml:outputClass <http://aws.amazon.com/neptune/csv2rdf/class/User> ;        }
}
```

# Neptune ML SPARQL 推論クエリの例外のリスト
<a name="machine-learning-sparql-exceptions"></a>

****
+ ** `BadRequestException`** -*メッセージ*:`The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference expects at least 1 value for the parameter (parameter name), found zero.` 。
+ ** `BadRequestException`** -*メッセージ*:`The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference expects at most 1 value for the parameter (parameter name), found (a number) values.` 。
+ ** `BadRequestException`** -*メッセージ*:`Invalid predicate (predicate name) provided for external service http://aws.amazon.com/neptune/vocab/v01/services/ml#inference query.` 。
+ **`BadRequestException`** -*メッセージ*: `The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference expects the predicate (predicate name) to be defined`。
+ ** `BadRequestException`** -*メッセージ*:`The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference expects the value of (parameter) (parameter name) to be a variable, found: (type)"` 。
+ **`BadRequestException`** -*メッセージ*: `The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference expects the input (parameter name) to be a constant, found: (type)`。
+ **`BadRequestException`** -*メッセージ*: `The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference is expected to return only 1 value`。
+ **`BadRequestException`** -*メッセージ*: `"The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference only allows StatementPatternNodes`。
+ **`BadRequestException`** -*メッセージ*: `The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference does not allow the predicate (predicate name)`。
+ **`BadRequestException`** -*メッセージ*: `The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference predicates cannot be variables, found: (type)`。
+ **`BadRequestException`** -*メッセージ*: `The SERVICE http://aws.amazon.com/neptune/vocab/v01/services/ml#inference predicates are expected to be part of the namespace (namespace name), found: (namespace name)`。