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

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

在 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")

假设您要在 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]