算法的论文是《Towards Learning Instantiated Logical Rules from Knowledge Graphs》
https://arxiv.org/pdf/2003.06071.pdf
算法的源码来源于
https://github.com/irokin/GPFL
按target读取数据
Set<Pair> trainPairs = IO.readPair(graph, trainFile, target);
Settings.TARGET_FUNCTIONAL = IO.isTargetFunctional(trainPairs);
Set<Pair> validPairs = IO.readPair(graph, validFile, target);
Set<Pair> testPairs = IO.readPair(graph, testFile, target);
拓展path,生成Closed Rule(CAR),利用instantiatedRule(oar)生成Head Anchored Rule(HAR)和Both Anchored Rule (BAR)
1.针对每一条三元组(head,relation,tail),用head或者tail拓展path。
2.head与path组成一条规则的雏形。同时,用字母标注生成规则的雏形,生成template。
3.如果head与path[0]并且tail与path[path.size()-1]相同,则该template可以生成ClosedRule。
4.如果不相同,则是instantiatedRule,实例化生成HAR和BAR
用head或者tail拓展path
@Override
public void run() {
Random rand = new Random();
try(Transaction tx = graph.beginTx()) {
while(consumer.isAlive()) {
Pair pair = trainPairs.get(rand.nextInt(trainPairs.size()));
addVisitedPair(pair);
Traverser traverser = GraphOps.buildStandardTraverser(graph, pair, Settings.RANDOM_WALKERS);
List<Path> paths=new ArrayList<>();
for (Path path : traverser) {
paths.add(path);
Rule rule = Context.createTemplate(path, pair);
while (consumer.isAlive()) {
if (ruleQueue.offer(rule, 100, TimeUnit.MILLISECONDS))
break;
}
if (!consumer.isAlive())
break;
}
}
tx.success();
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(-1);
}
}
head与path组成一条规则的雏形。同时,用字母标注生成规则的雏形,生成template。
public static Rule createTemplate(Path path, Pair pair) {
List<Atom> bodyAtoms = buildBodyAtoms(path);
Atom head = new Atom(pair);
return new Template(head, bodyAtoms);
}
标注template是否是Closed,如果是Closed则这个template就是ClosedRule
Rule(Atom head, List<Atom> bodyAtoms) {
this.head = head;
this.bodyAtoms = new ArrayList<>(bodyAtoms);
Atom lastAtom = bodyAtoms.get(bodyAtoms.size() - 1);
closed = head.getObjectId() == lastAtom.getObjectId();
Atom firstAtom = bodyAtoms.get( 0 );
fromSubject = head.getSubjectId() == firstAtom.getSubjectId();
}
对非Closed的Rule生成HAR和BAR
/*
* 对path=1的instantied rule拓展,生成HAR或者BAR
* 定义规则的读写类
* tempFiltercontents只写入ABS:instantiated开头的,后接HAR或者BAR,对应的是各种类型的文件夹
* ruleFilecontens只写入HAR或者BAR,对应只有一个rules.txt文件夹
*
*/
if(Settings.ESSENTIAL_TIME != -1 && Settings.INS_DEPTH != 0)
EssentialRuleGenerator.generateEssentialRules(trainPairs, validPairs
, context, graph, ruletempFile, ruleFile);
/*
* 对path>1的instantied rule拓展,生成HAR或者BAR
* !!!!!!!!!!!!!!!!!!!!!!!!!!!
* generalization没把closed的规则写进去,specialization把closed的规则也写进去了
* tempFiltercontents两种方式写入,一类是ABS:closed的,另一类是ABS:instantiated开头的,后接HAR或者BAR,对应的是各种类型的文件夹
* ruleFilecontens写入HAR或者BAR以及Closed的,对应只有一个rules.txt文件夹
*/
specialization(context, trainPairs, validPairs, ruletempFile);
用生成的规则CAR,HAR,BAR,生成答案集合
/*
* !!!!!!!!!!!query中会生成newTriple
* 生成predictionMap (答案集合),key是query,value是所有的answer值
*/
ruleApplication(context, ruletempFile);
标签:head,BAR,生成,算法,GPFL,bodyAtoms,整理,path,HAR
From: https://www.cnblogs.com/csjywu01/p/17056158.html