

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

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

对象回归与对象分类类似，不同之处在于从回归模型中推理的每个节点的数值谓词值。除了 `the Neptune#ml.limit` 和 `Neptune#ml.threshold` 谓词不适用之外，您可以对于对象回归和对象分类使用相同的 SPARQL 查询。

下面的查询试图预测类型为 `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)`。