在 Neptune ML 中使用链接预测模型的 Gremlin 链接预测查询 - Amazon Neptune
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

在 Neptune ML 中使用链接预测模型的 Gremlin 链接预测查询

链接预测模型可以解决以下问题:

  • 头节点预测:给定顶点和边缘类型,该顶点可能与哪些顶点链接?

  • 尾节点预测:给定顶点和边缘标签,那个顶点可能会链接到哪些顶点?

  • 边缘类型预测:鉴于头部和尾部顶点,什么边缘可能会将它们连接起来?

前两种情况属于节点预测问题,最后一种是边缘类型预测。

注意

Neptune ML 中尚不支持边缘预测。

对于以下示例,请考虑一个带有顶点的简单图表UserMovie被边缘链接Rated.

以下是一个示例头节点预测查询,用于预测最有可能评分电影的前五名用户,"movie_1""movie_2", 和"movie_3"

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"很可能会评分:

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 例外列表.

您可以使用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")