示例#1
0
def epnet_run():
    epnet = EPNet(10, 5, 5, 1)
    genp5 = ParityNGenerator(5)
    _, res, vec = map(np.array, zip(*genp5.all()))
    top, i = epnet.run(vec, res, num_hid=2, lr=0.5, temperature=1.0)
    print(top)
    print(i)
示例#2
0
 def test_evaluate_given7(self):
     weights_out = '''-42.8  -32.4   0      -5.6  -23.6  -33.6  -33.6   41.6      0      0     0
                      -75.3  -32.0   43.2  -41.1  -34.5  -34.8  -34.8   39.8  -58.9      0     0
                      -85.0  -28.1   28.6  -28.0  -28.0  -28.2  -28.2   29.3  -47.6  -41.3     0
                       59.9   12.9  -13.5   13.0   13.0   13.0   13.0  -13.4      0      0  81.8
                   '''
     weights = np.fromstring(weights_out, sep=' ').reshape(4, -1)
     ann = ForwardArtificialNeuralNectwork(7, 7, 1)
     ann.construct(weights)
     genp7 = ParityNGenerator(7)
     for _, res, vec in genp7.all():
         result = ann.evaluate(vec)
         self.assertEqual(result>0.5, not res)   # use not res since the condition is negating in paper
示例#3
0
 def test_evaluate_given8(self):
     weights_out = '''-12.4   25.2   27.7  -29.4  -28.9  -29.7  -25.4  -28.5   27.8      0      0     0
                      -40.4   19.6   18.9  -18.1  -19.1  -18.5  -17.3  -18.8   20.4  -67.6      0     0
                      -48.1   16.0   16.1  -15.9  -16.3  -15.8  -15.9  -15.8   16.7  -55.0  -26.7     0
                       45.7  -10.0  -11.0   10.0    9.9    9.4   10.0    9.6  -11.4    6.8    2.3  76.3
                   '''
     weights = np.fromstring(weights_out, sep=' ').reshape(4, -1)
     ann = ForwardArtificialNeuralNectwork(8, 8, 1)
     ann.construct(weights)
     genp8 = ParityNGenerator(8)
     _, res, vec = zip(*genp8.all())
     result = ann.evaluate(np.array(vec))
     for yhat, y in zip(result, res):
         self.assertEqual(yhat>0.5, not y)   # the same as the case given7
示例#4
0
 def test_train_given6(self):
     # mean square error function
     mse = lambda y1,y2: ((y1-y2)**2/2).sum()
     for _ in range(10):
         ann = ForwardArtificialNeuralNectwork(6, 6, 1)
         ann.initialize(3, 1.0, 0, 1)
         genp6 = ParityNGenerator(6)
         _, res, vec = map(np.array, zip(*genp6.all()))
         before = ann.evaluate(vec)
         be_mse = mse(before, res)
         ann.train(vec, res, 0.1, 1000)
         after = ann.evaluate(vec)
         af_mse = mse(after, res)
         self.assertLessEqual(af_mse, be_mse)
示例#5
0
 def test_call(self):
     pn = ParityNGenerator(4)
     vec, exp = pn(11)
     self.assertTupleEqual(tuple(vec), (True, False, True, True))
     self.assertEqual(exp, False)
     with self.assertRaises(ParityNGenerator.ParityNException):
         pn(16)
     with self.assertRaises(ParityNGenerator.ParityNException):
         pn(4, 1, a=3)
示例#6
0
def ann_run():
    ann = ForwardArtificialNeuralNectwork(5, 5, 1)
    ann.initialize(2, 1.0, 0.0, 1.0)
    genp5 = ParityNGenerator(5)
    _, res, vec = map(np.array, zip(*genp5.all()))

    ann.train(vec, res, lr=0.3, epoch=1000)

    result = (ann.evaluate(vec) > 0.5)
    for y, yhat in zip(res, result):
        if y != yhat:
            print(y, yhat)

    print()

    ann.simul_anneal(vec, res, temperature=1.0, steps=1000)

    result = (ann.evaluate(vec) > 0.5)
    for y, yhat in zip(res, result):
        if y != yhat:
            print(y, yhat)
示例#7
0
 def test_all(self):
     it = ParityNGenerator(2).all()
     _, out, vec = next(it)
     self.assertEqual(out, np.array([True]))
     self.assertTupleEqual(tuple(vec), (False, False))
     _, out, vec = next(it)
     self.assertEqual(out[0], False)
     self.assertTupleEqual(tuple(vec), (False, True))
     _, out, vec = next(it)
     self.assertEqual(out[0], False)
     self.assertTupleEqual(tuple(vec), (True, False))
     _, out, vec = next(it)
     self.assertEqual(out[0], True)
     self.assertTupleEqual(tuple(vec), (True, True))
示例#8
0
 def test_constructor(self):
     pn = ParityNGenerator(6)
     self.assertEqual(pn.bound, 64)
     with self.assertRaises(ParityNGenerator.ParityNException):
         ParityNGenerator(-1)