Skip to content

Commit 00e2dfb

Browse files
committed
First commit
0 parents  commit 00e2dfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+2466
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Numpy, Scipy, and Matplotlib are required.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
4+
def DeltaBatch(W, X, D):
5+
alpha = 0.9
6+
dWsum = np.zeros(3)
7+
8+
N = 4
9+
for k in range(N):
10+
x = X[k, :].T
11+
d = D[k]
12+
13+
v = np.matmul(W, x)
14+
y = Sigmoid(v)
15+
16+
e = d - y
17+
delta = y*(1-y) * e
18+
dW = alpha * delta * x
19+
20+
dWsum = dWsum + dW
21+
22+
dWavg = dWsum / N
23+
24+
W[0][0] = W[0][0] + dWavg[0]
25+
W[0][1] = W[0][1] + dWavg[1]
26+
W[0][2] = W[0][2] + dWavg[2]
27+
28+
return W
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
4+
5+
def DeltaSGD(W, X, D):
6+
alpha = 0.9
7+
8+
N = 4
9+
for k in range(N):
10+
x = X[k, :].T
11+
d = D[k]
12+
13+
v = np.matmul(W, x)
14+
y = Sigmoid(v)
15+
16+
e = d - y
17+
delta = y*(1-y) * e
18+
19+
dW = alpha*delta*x
20+
21+
W[0][0] = W[0][0] + dW[0]
22+
W[0][1] = W[0][1] + dW[1]
23+
W[0][2] = W[0][2] + dW[2]
24+
25+
return W
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
4+
5+
def DeltaXOR(W, X, D):
6+
alpha = 0.9
7+
8+
N = 4
9+
for k in range(N):
10+
x = X[k,:].T
11+
d = D[k]
12+
13+
v = np.matmul(W, x)
14+
y = Sigmoid(v)
15+
16+
e = d - y
17+
delta = y*(1-y) * e
18+
19+
dW = alpha*delta*x
20+
21+
W[0][0] = W[0][0] + dW[0]
22+
W[0][1] = W[0][1] + dW[1]
23+
W[0][2] = W[0][2] + dW[2]
24+
25+
return W
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from DeltaSGD import *
2+
from DeltaBatch import *
3+
from Sigmoid import *
4+
import matplotlib.pyplot as plt
5+
6+
7+
X = np.array([[0, 0, 1],
8+
[0, 1, 1],
9+
[1, 0, 1],
10+
[1, 1, 1]])
11+
12+
D = np.array([[0],
13+
[0],
14+
[1],
15+
[1]])
16+
17+
E1 = np.zeros(1000)
18+
E2 = np.zeros(1000)
19+
20+
W1 = 2*np.random.random((1, 3)) - 1
21+
W2 = np.array(W1)
22+
23+
for epoch in range(1000):
24+
W1 = DeltaSGD(W1, X, D)
25+
W2 = DeltaBatch(W2, X, D)
26+
27+
es1 = 0
28+
es2 = 0
29+
N = 4
30+
for k in range(N):
31+
x = X[k, :].T
32+
d = D[k]
33+
34+
v1 = np.matmul(W1, x)
35+
y1 = Sigmoid(v1)
36+
es1 = es1 + (d - y1)**2
37+
38+
v2 = np.matmul(W2, x)
39+
y2 = Sigmoid(v2)
40+
es2 = es2 + (d - y2)**2
41+
42+
E1[epoch] = es1/N
43+
E2[epoch] = es2/N
44+
45+
46+
SGD, = plt.plot(E1, 'r')
47+
Batch, = plt.plot(E2, 'b:')
48+
plt.xlabel("Epoch")
49+
plt.ylabel("Average of Training Error")
50+
plt.legend([SGD, Batch], ['SGD', 'Batch'])
51+
plt.show()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import numpy as np
2+
3+
4+
def Sigmoid(x):
5+
return 1.0 / (1.0 + np.exp(-x))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
from DeltaBatch import *
4+
5+
6+
def TestDeltaBatch():
7+
X = np.array([[0, 0, 1],
8+
[0, 1, 1],
9+
[1, 0, 1],
10+
[1, 1, 1]])
11+
12+
D = np.array([[0],
13+
[0],
14+
[1],
15+
[1]])
16+
17+
W = 2*np.random.random((1, 3)) - 1
18+
19+
for _epoch in range(40000):
20+
W = DeltaBatch(W, X, D)
21+
22+
N = 4
23+
for k in range(N):
24+
x = X[k,:].T
25+
v = np.matmul(W, x)
26+
y = Sigmoid(v)
27+
print(y)
28+
29+
if __name__ == '__main__':
30+
TestDeltaBatch()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
from DeltaSGD import *
4+
5+
6+
def TestDeltaSGD():
7+
X = np.array([[0, 0, 1],
8+
[0, 1, 1],
9+
[1, 0, 1],
10+
[1, 1, 1]])
11+
12+
D = np.array([[0],
13+
[0],
14+
[1],
15+
[1]])
16+
17+
W = 2*np.random.random((1, 3)) - 1
18+
19+
for _epoch in range(10000):
20+
W = DeltaSGD(W, X, D)
21+
22+
N = 4
23+
for k in range(N):
24+
x = X[k,:].T
25+
v = np.matmul(W, x)
26+
y = Sigmoid(v)
27+
print(y)
28+
29+
if __name__ == '__main__':
30+
TestDeltaSGD()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
from DeltaXOR import *
4+
5+
6+
def TestDeltaXOR():
7+
X = np.array([[0, 0, 1],
8+
[0, 1, 1],
9+
[1, 0, 1],
10+
[1, 1, 1]])
11+
12+
D = np.array([[0],
13+
[1],
14+
[1],
15+
[0]])
16+
17+
W = 2*np.random.random((1, 3)) - 1
18+
19+
for _epoch in range(40000): #train
20+
W = DeltaXOR(W, X, D)
21+
22+
N = 4 #inference
23+
for k in range(N):
24+
x = X[k,:].T
25+
v = np.matmul(W, x)
26+
y = Sigmoid(v)
27+
print(y)
28+
29+
if __name__ == '__main__':
30+
TestDeltaXOR()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
from Sigmoid import *
3+
4+
5+
def BackpropCE(W1, W2, X, D):
6+
alpha = 0.9
7+
8+
N = 4
9+
for k in range(N):
10+
x = X[k, :].T
11+
d = D[k]
12+
13+
v1 = np.matmul(W1, x)
14+
y1 = Sigmoid(v1)
15+
v = np.matmul(W2, y1)
16+
y = Sigmoid(v)
17+
18+
e = d - y
19+
delta = e
20+
21+
e1 = np.matmul(W2.T, delta)
22+
delta1 = y1*(1-y1) * e1
23+
24+
dW1 = (alpha*delta1).reshape(4, 1) * x.reshape(1, 3)
25+
W1 = W1 + dW1
26+
27+
dW2 = alpha * delta * y1
28+
W2 = W2 + dW2
29+
30+
return W1, W2

0 commit comments

Comments
 (0)