-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (147 loc) · 4.59 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from network import Network, intializeRandomWeigths, intializeSameWeigths
from dataset import createDatasetEvenly, getDataset
import matplotlib.pyplot as plt
import random
import math
import numpy as np
# 1(a)
# variables
hiddenWeights = [[0.5, 0.3, -0.1], [-0.5, -0.4, 1.0]]
outputWeights = [1.0, -2.0, 0.5]
# create network and datasets
net1a = Network(hiddenWeights, outputWeights)
dataset = createDatasetEvenly()
# color the input space according to network result
plt.figure(1)
plt.title("1a")
plt.xlabel("x1")
plt.ylabel("x2")
plt.xlim(xmin=-5, xmax=5)
plt.ylim(ymin=-5, ymax=5)
for data in dataset:
result = net1a.inference(data)
if result == -1:
color = 'b'
else:
color = 'r'
plt.plot(data[0], data[1], marker='o', color=color, markersize=10)
plt.show()
# 1(b)
# create network for 1b
hiddenWeights = [[-1.0, -0.5, 1.5], [1.0, 1.5, -0.5]]
outputWeights = [0.5, -1.0, 1.0]
net1b = Network(hiddenWeights, outputWeights)
# color the input space according to network result
plt.figure(2)
plt.title("1b")
plt.xlabel("x1")
plt.ylabel("x2")
plt.xlim(xmin=-5, xmax=5)
plt.ylim(ymin=-5, ymax=5)
for data in dataset:
result = net1b.inference(data)
if result == -1:
color = 'b'
else:
color = 'r'
plt.plot(data[0], data[1], marker='o', color=color, markersize=10)
plt.show()
# Problem 2
# theta for problem 2
theta = 0.005
# 2(a)
# initialize all weights randomly
hiddenWeights, outputWeights = intializeRandomWeigths()
# create network for net2a
net2a = Network(hiddenWeights, outputWeights)
# get dataset according to the table
dataset, labels, indexes = getDataset()
# random seed for problem 2 to shuffle datasets
random.seed(2019)
# train the network and plot a learning curve
plt.figure(3)
trainingErrors = []
accuracyList = []
epochs = []
epoch = 0
while 1:
trainingError = 0.0
epochGradient = 0.0
correctNum = 0.0
# shuffle dataset
random.shuffle(indexes)
# stochastic gradient descend training
for index in indexes:
data = dataset[index]
label = labels[index]
predict = net2a.inference(data)
labelSign = math.copysign(1, label)
if (predict == labelSign):
correctNum += 1
stepTrainingError, stepGradient = net2a.train(data, label)
trainingError += stepTrainingError
epochGradient += stepGradient
trainingError = trainingError / len(dataset)
trainingErrors.append(trainingError)
epochs.append(epoch)
accuracy = correctNum / len(dataset)
accuracyList.append(accuracy)
epochGradient = epochGradient / len(dataset)
if np.absolute(epochGradient).mean() < theta:
print("Break Epoch in initialize all weights randomly:", epoch)
break
epoch += 1
plt.subplot(211)
plt.plot(epochs, trainingErrors, color='r', linestyle='solid', marker='o', linewidth=2)
plt.subplot(212)
plt.plot(epochs, accuracyList, color='r', linestyle='solid', marker='o', linewidth=2)
# 2(b)
# weights initialized to be the same throughout each level
hiddenWeights, outputWeights = intializeSameWeigths()
# create network for net2b
net2b = Network(hiddenWeights, outputWeights)
# get dataset according to the table
dataset, labels, indexes = getDataset()
# random seed for problem 2 to shuffle datasets
random.seed(2019)
# train the network and plot a learning curve
trainingErrors = []
accuracyList = []
epochs = []
epoch = 0
while 1:
trainingError = 0.0
epochGradient = 0.0
correctNum = 0.0
# shuffle dataset
random.shuffle(indexes)
# stochastic gradient descend training
for index in indexes:
data = dataset[index]
label = labels[index]
predict = net2b.inference(data)
labelSign = math.copysign(1, label)
if (predict == labelSign):
correctNum += 1
stepTrainingError, stepGradient = net2b.train(data, label)
trainingError += stepTrainingError
epochGradient += stepGradient
trainingError = trainingError / len(dataset)
trainingErrors.append(trainingError)
epochs.append(epoch)
accuracy = correctNum / len(dataset)
accuracyList.append(accuracy)
epochGradient = epochGradient / len(dataset)
if np.absolute(epochGradient).mean() < theta:
print("Break Epoch in weights initialized to be the same throughout each level:", epoch)
break
epoch += 1
plt.subplot(211)
plt.xlabel("epoch")
plt.ylabel("training_error")
plt.plot(epochs, trainingErrors, color='b', linestyle='solid', marker='o', linewidth=2)
plt.subplot(212)
plt.xlabel("epoch")
plt.ylabel("accuracy")
plt.plot(epochs, accuracyList, color='b', linestyle='solid', marker='o', linewidth=2)
plt.show()