-
Notifications
You must be signed in to change notification settings - Fork 8
/
pyBPN_demo.py
71 lines (57 loc) · 1.48 KB
/
pyBPN_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pyBPN import *
from optim import *
import matplotlib.pyplot as plt
from Activation import sigmoid
import numpy as np
from loadData import Data
from functools import partial
from math import sin, cos
##############################################################
##### demonstration of GradDesc for general optimization #####
# x = np.matrix([0.5, 0.7])
# def f(x):
# return np.sin(x) + 2 * x, np.cos(x) + 2
# f = partial(f, x)
# J, x_opt = gradDesc(f , init_x = x, maxEpochs = 100, lr = 0.1)
# print x_opt
#plt.plot(J)
#plt.show()
#############################################################
pat2 = [[[0, 0, 0], [0]],
[[0, 0, 1], [1]],
[[0, 1, 0], [1]],
[[0, 1, 1], [0]],
[[1, 0, 0], [2]],
[[1, 0, 1], [3]],
[[1, 1, 0], [3]],
[[1, 1, 1], [2]],
]
pat1 = [[[0, 0, 0], [0]],
[[0, 0, 1], [1]],
[[0, 1, 0], [1]],
[[0, 1, 1], [0]],
[[1, 0, 0], [1]],
[[1, 0, 1], [0]],
[[1, 1, 0], [0]],
[[1, 1, 1], [1]],
]
ActSig = sigmoid(4)
# ActSig.view()
d1 = Data()
d1.loadList(pat1)
d2 = Data()
d2.loadList(pat2, 4)
n2 = ANN([3, 3, 4], ActSig)
# n2.displaySynpWt()
arch1 = [3, 4, 1]
n1 = ANN(arch1, ActSig)
# n1.displaySynpWt()
##########################################################
########## Training With Gradient DescentPortion #########
cost = partial(n1.bpCost, data = d1, regLambda = 0.003)
J, Wt = gradDesc(cost, init_x = n1.W, maxEpochs = 500, lr = 0.8)
n1.W = Wt
##########################################################
plt.plot(J)
plt.show()
print 'Accuracy is :' , n1.test(d1), '%'