Example #1
0
 def test_rule_notself(self):
     ctx = Backward()
     ctx.evaluate("""
         !A => A
     """)
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
Example #2
0
 def scaled_estimate(self, x, T):
     self.__init_storage(T)
     for t in range(T):
         fw, bw = Forward(self.A, self.B,
                          self.rho), Backward(self.A, self.B, self.rho)
         P, _ = fw.scaled_evaluate(x), bw.scaled_evaluate(x, fw.C)
         self.__scaled_update(fw.alpha, bw.beta, fw.C, x)
         self.__store(np.log(fw.C).sum(), t)
Example #3
0
 def estimate(self, x, T):
     self.__init_storage(T)
     for t in range(T):
         fw, bw = Forward(self.A, self.B,
                          self.rho), Backward(self.A, self.B, self.rho)
         P, _ = fw.evaluate(x), bw.evaluate(x)
         self.__update(fw.alpha, bw.beta, x)
         self.__store(np.log(P), t)
Example #4
0
 def test_not(self):
     ctx = Backward()
     ans = ctx.evaluate("= a")
     ans = ctx.evaluate("a")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("!a")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("!!a")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("!!(!a)")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("!b")
     self.assertEqual(ans, [True])
Example #5
0
 def test_rule(self):
     ctx = Backward()
     ans = ctx.evaluate("a => b")
     ans = ctx.evaluate("b => c")
     ans = ctx.evaluate("= a")
     ans = ctx.evaluate("a b c")
     self.assertEqual(ans, [True, True, True])
     ans = ctx.evaluate("a => d & e & f")
     ans = ctx.evaluate("d e f")
     self.assertEqual(ans, [True, True, True])
Example #6
0
 def test_or(self):
     ctx = Backward()
     ans = ctx.evaluate("= a c")
     ans = ctx.evaluate("!a | c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a | !c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("b | c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("!a | !c")
     self.assertEqual(ans, [False])
Example #7
0
 def test_atom(self):
     ctx = Backward()
     ans = ctx.evaluate("a")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("= a c")
     ans = ctx.evaluate("a b c")
     self.assertEqual(ans, [True, False, True])
     ans = ctx.evaluate("=")
     ans = ctx.evaluate("a b c")
     self.assertEqual(ans, [False, False, False])
     ans = ctx.evaluate("!a")
     self.assertEqual(ans, [True])
Example #8
0
 def test_and(self):
     ctx = Backward()
     ans = ctx.evaluate("= a b c")
     ans = ctx.evaluate("a & b & c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a & !b & c")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("a & !(!b & !c)")
     self.assertEqual(ans, [True])
Example #9
0
 def test_xor(self):
     ctx = Backward()
     ans = ctx.read("a ^ b")
     self.assertEqual(str(ans), "[xor((atom(a), atom(b)))]")
     ans = ctx.read("!a ^ b")
     self.assertEqual(str(ans), "[xor((not(atom(a)), atom(b)))]")
     ans = ctx.read("a ^ !b")
     self.assertEqual(str(ans), "[xor((atom(a), not(atom(b))))]")
     ans = ctx.read("a ^ b | c")
     self.assertEqual(str(ans), "[or((xor((atom(a), atom(b))), atom(c)))]")
Example #10
0
 def test_or(self):
     ctx = Backward()
     ans = ctx.read("a | b")
     self.assertEqual(str(ans), "[or((atom(a), atom(b)))]")
     ans = ctx.read("!a | b")
     self.assertEqual(str(ans), "[or((not(atom(a)), atom(b)))]")
     ans = ctx.read("a | !b")
     self.assertEqual(str(ans), "[or((atom(a), not(atom(b))))]")
     ans = ctx.read("a | b & c")
     self.assertEqual(str(ans), "[and((or((atom(a), atom(b))), atom(c)))]")
Example #11
0
 def test_xor(self):
     ctx = Backward()
     ans = ctx.evaluate("= a c")
     ans = ctx.evaluate("a ^ c")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("!a ^ c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a ^ !c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a ^ b")
     self.assertEqual(ans, [True])
Example #12
0
 def test_rule_and(self):
     ctx = Backward()
     ans = ctx.evaluate("""
         B => A
         D & E => B
         G & H => F
         I & J => G
         G => H
         L & M => K
         O & P => L & N
         N => M
     """)
     ans = ctx.evaluate("= D E I J O P")
     ans = ctx.evaluate("A F K P")
     self.assertEqual(ans, [True, True, True, True])
     ans = ctx.evaluate("= D E I J P")
     ans = ctx.evaluate("A F K P")
     self.assertEqual(ans, [True, True, False, True])
Example #13
0
 def test_rule_not1(self):
     ctx = Backward()
     ctx.evaluate("B & !C => A")
     ctx.evaluate("=")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
     ctx.evaluate("= B")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
     ctx.evaluate("= B C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
Example #14
0
 def test_atom(self):
     ctx = Backward()
     ans = ctx.read("a")
     self.assertEqual(str(ans), "[atom(a)]")
     ans = ctx.read("a b c")
     self.assertEqual(str(ans), "[atom(a), atom(b), atom(c)]")
Example #15
0
 def test_not(self):
     ctx = Backward()
     ans = ctx.read("!a")
     self.assertEqual(str(ans), "[not(atom(a))]")
     ans = ctx.read("!a !b")
     self.assertEqual(str(ans), "[not(atom(a)), not(atom(b))]")
Example #16
0
 def test_rule_samerule(self):
     ctx = Backward()
     ctx.evaluate("""
         B => A
         C => A
     """)
     ctx.evaluate("= B")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= B C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
Example #17
0
 def test_rule_xor(self):
     ctx = Backward()
     ctx.evaluate("""
         B & C => A
         D ^ E => B
         B => C
     """)
     ctx.evaluate("=")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
     ctx.evaluate("= D")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= E")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= D E")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
num_iterations = 2
np.random.seed(1)
#track the cost
costs = []
print_cost = True
#boucle de 0 à nombre d'iterations:
    
for i in range (0,num_iterations):
        
    #forward propagation l layers
    annForward.forward_layers(X)
      
    cost = annForward.compute_cost(Y)
    caches = annForward.caches
    parameters = annForward.parameters
#     print(parameters)
    AL = annForward.AL
    
   
    annBackward = Backward(AL, Y, caches, parameters)
#     #L_model_backward   
    annBackward.l_model_backward()
    newParameters = annBackward.update_parameters()
        
    # Print the cost every 100 training example
    if print_cost and i % 1000 == 0:
        print(cost)
    #print (f"Cost after iteration {i}{cost}")
    if print_cost and i % 1000 == 0:
        costs.append(cost)
                
Example #19
0
 def test_empty(self):
     ctx = Backward()
     ans = ctx.read("")
     self.assertEqual(str(ans), "[]")
Example #20
0
        If X croaks and X eats flies – Then X is a frog
        If X chirps and X sings – Then X is a canary
        If X is a frog – Then X is green
        If X is a canary – Then X is yellow

    And initial facts:

        Fritz croaks
        Fritz eats flies

    Our goal is to decide wether Fritz is green.
"""

if __name__ == "__main__":

    ctx = Backward()

    # Defining our knowledge base

    ctx.evaluate("croaks & eatsFlies => frog")
    ctx.evaluate("chirps & sings => canary")
    ctx.evaluate("frog => green")
    ctx.evaluate("canary => yellow")

    # Initial facts

    # ctx.evaluate("= croaks eatsFlies")

    # Let's bind some questions to ask the user whenever the value of the variable is unknown.

    ctx.bind_question("croaks", "Does it croak?")
Example #21
0
 def test_rule_not2(self):
     ctx = Backward()
     ctx.evaluate("!A | A => B")
     ctx.evaluate("A | !A => C")
     ans = ctx.evaluate("B C")
     self.assertEqual(ans, [True, True])
from forward import HMM, Forward
from backward import Backward
from math import log
from logsum import log_sum

f = Forward("dev.txt", "hmm-trans.txt", "hmm-emit.txt", "hmm-prior.txt")
b = Backward("dev.txt", "hmm-trans.txt", "hmm-emit.txt", "hmm-prior.txt")
dev_file = "dev.txt"

with open(dev_file, 'r') as file:
    for line in file:
        words = line.rstrip('\n\r').split(" ")
        dev_a = list()
        dev_b = list()
        # calculate conditional probability
        for i in range(len(words)):
            t = i + 1
            if t == 1:
                f.initialize_alpha(words[0], dev_a)
            else:
                f.calculate_alpha(words[i], t, dev_a)
        T = len(words)
        t = T
        for i in range(len(words)):
            if t == T:
                beta_T = [log(1) for x in range(b.hmm.N)]
                dev_b.append(beta_T)
            else:
                b.calculate_beta(dev_b, words[t], t - 1, T - 1)
            t -= 1