

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

# 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 物件分類，模型會根據其中一個述詞值進行訓練。在該述詞尚未與給定主旨一起存在時，這很有用。

只能使用物件分類模型推斷類別述詞值。

下列查詢試圖為類型 `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)`。