-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmlp_soln.py
More file actions
74 lines (63 loc) · 2.08 KB
/
mlp_soln.py
File metadata and controls
74 lines (63 loc) · 2.08 KB
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
72
73
74
# MLP 3 layer network: input - hidden - outer
# based on:
# http://iamtrask.github.io/2015/07/12/basic-python-network/
import numpy as np
import pdb
def activation(x,deriv=False):
'''activation function, values for sigmoid'''
if(deriv==True):
return x*(1-x) # not technically correct
return 1/(1+np.exp(-x))
# inputs
inputs = np.array([[0,0],
[0,1],
[1,0],
[1,1]])
# targets
targets = np.array([[0],
[1],
[1],
[0]])
# make network geometry
nodes_input = 2
nodes_hidden = 4
nodes_target = 1
# initialize weights
Wxh = 2*np.random.uniform(size=(nodes_input, nodes_hidden)) - 1
Why = 2*np.random.uniform(size=(nodes_hidden, nodes_target)) - 1
# simulation parameters
np.random.seed(1)
alpha = 1 # learning rate
num_epochs = 20000 # number of epochs
# training
print("\nTraining:")
for e in range(num_epochs):
yp_lst = [] # predictions
error_lst = [] # differences between target and predictions
for X, y in zip(inputs, targets):
X = X.reshape((1, X.shape[0])) # for row, column shape
# Feed forward
H = activation(np.dot(X,Wxh))
yp = activation(np.dot(H,Why))
# Back propogate to find gradients
# Why gradients
yp_error = y - yp
yp_delta = yp_error*activation(yp,deriv=True)
grad_Why = np.dot(H.T, yp_delta)
# Wxh gradients
H_error = np.dot(yp_delta, Why.T)
H_delta = H_error * activation(H,deriv=True)
grad_Wxh = np.dot(X.T, H_delta)
# Use gradient descent to update weights
Why += alpha * grad_Why
Wxh += alpha * grad_Wxh
# save for future use
yp_lst.append(yp[0][0])
error_lst.append(yp_error[0][0])
epoch_error = np.mean(np.abs(error_lst)).round(4)
if (e % int(num_epochs/10)) == 0:
print("Epoch: {0:<8s} Error: {1}".format(str(e), epoch_error))
print("\nResults:")
print("yt\typ\typ_prob")
for yt, yp in zip(targets,yp_lst):
print("{0}\t{1}\t{2}".format(yt[0], int(yp>=0.5), round(yp,4)))