Beispiel #1
0
def weighted_modus_ponens_formula(B, AB, A):
    tA = get_ttv(A)
    tAB = get_ttv(AB)
    #print("Input: A=", tA, " AB=", tAB);
    new_tv = torch.sigmoid(w[0] * tA * tAB + w[1] * tA + w[2] * tAB + w[3])
    set_ttv(B, new_tv)
    #print("Result: B=", new_tv)
    return B
Beispiel #2
0
 def test_modus_ponens(self):
     rule_base = ConceptNode('PLN')
     set_ttv(ConceptNode("apple"), TTruthValue((0.8, 0.9)))
     inh = InheritanceLink(ConceptNode("apple"), ConceptNode("fruit"))
     set_ttv(inh, TTruthValue(0.8, 0.4))
     bc = BackwardChainer(self.atomspace, rule_base, ConceptNode("fruit"))
     bc.do_chain()
     result = get_ttv(bc.get_results().out[0])
     self.assertTrue(abs(0.68 - float(result.mean)) < 0.0001)
Beispiel #3
0
 def test_contraposition(self):
     rule_base = ConceptNode('PLN')
     A = PredicateNode('A')
     set_ttv(A, (1.0, 1.0))
     B = PredicateNode('B')
     set_ttv(B, (1.0, 1.0))
     set_ttv(ImplicationLink(A, B), (1.0, 1.0))
     NBNA = ImplicationLink(NotLink(B), NotLink(A))
     bc = BackwardChainer(self.atomspace, rule_base, NBNA)
     bc.do_chain()
     result = bc.get_results().out[0]
     self.assertTrue(0 < get_ttv(result).confidence,
             'fails due to https://github.com/opencog/opencog/issues/3465')
Beispiel #4
0
 def test_fuzzy_conjunction(self):
     rule_base = ConceptNode('PLN')
     apple = ConceptNode('apple')
     inh_green = InheritanceLink(PredicateNode("green"), ConceptNode("color"))
     set_ttv(inh_green, torch.tensor([0.96, .9]))
     ev_green = EvaluationLink(PredicateNode('green'), apple)
     set_ttv(ev_green, TTruthValue(0.85, 0.95))
     conj = AndLink(ev_green, inh_green)
     bc = BackwardChainer(self.atomspace, rule_base, conj)
     bc.do_chain()
     result = get_ttv(bc.get_results().out[0])
     self.assertTrue(abs(result.mean - 0.85) < 0.0001)
     self.assertTrue(abs(result.confidence - 0.9) < 0.0001)
Beispiel #5
0
def main():
    build_modus_ponens()
    # train implication or just weigths of modus_ponens_formula
    train_implication = False
    if train_implication:
        print("train implication and modus_ponens_formula' weights")
    else:
        print("train only modus_ponens_formula' weights")

    optimizer = optim.SGD(params(train_implication), lr=1e-1)
    initial_w = w.clone()
    print('initial ' + res_string.format(*initial_w))
    print("inital implication links")
    for color in colors:
        for fruit in fruits:
            print(ImplicationLink(PredicateNode(fruit), PredicateNode(color)))

    for i in range(300):
        fruit_id = np.random.randint(len(fruits))
        color_id = np.random.choice(len(colors), p=Pcolorfruit[fruit_id])
        fruit = fruits[fruit_id]
        color = colors[color_id]
        x = fruit + "-" + str(i)
        set_ttv(EvaluationLink(PredicateNode(fruit), ConceptNode(x)),
                TTruthValue(1.0, 1.0))
        optimizer.zero_grad()
        loss = torch.zeros(1)
        for c in colors:
            bc = BackwardChainer(
                atomspace, rule_base,
                EvaluationLink(PredicateNode(c), ConceptNode(x)))
            bc.do_chain()
            evlink = bc.get_results().out[0]
            p = get_ttv(evlink)
            # print(evlink)
            if color == c:
                loss = loss - torch.log(p[0])
            else:
                loss = loss - torch.log(1 - p[0])
        loss.backward()
        optimizer.step()
    print("implication links")
    for color in colors:
        for fruit in fruits:
            print(ImplicationLink(PredicateNode(fruit), PredicateNode(color)))
    print("after training")
    print(res_string.format(w[0], w[1], w[2], w[2], w[3]))
Beispiel #6
0
    def test_cons_disj_elimination(self):
        rule_base = ConceptNode('PLN')
        A = PredicateNode('A')
        set_ttv(A, (1.0, 1.0))
        B = PredicateNode('B')
        set_ttv(B, (1.0, 1.0))
        C = PredicateNode('C')
        set_ttv(C, (1.0, 1.0))
        set_ttv(ImplicationLink(A, C), (0.55, 0.55))
        set_ttv(ImplicationLink(A, OrLink(B, C)), (1.0, 1.0))

        impl = ImplicationLink(A, B)
        bc = BackwardChainer(self.atomspace, rule_base, impl)
        bc.do_chain()
        result = get_ttv(bc.get_results().out[0])
        self.assertTrue(result.mean == 1.0)
        alpha = 0.9
        self.assertTrue(result.confidence == alpha * 0.55)