-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnp_nn_iris_classification.py
More file actions
191 lines (138 loc) · 4.74 KB
/
np_nn_iris_classification.py
File metadata and controls
191 lines (138 loc) · 4.74 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Import dependencies
import numpy as np
import random
import urllib.request
# Download iris dataset
urllib.request.urlretrieve(
"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
"iris-data.txt")
# Pre-process data
# seed random-generators
random.seed(0)
np.random.seed(0)
train_test_ratio = 0.8
tmp_list = []
tmp_set = set()
features = []
labels = []
# text-file to numpy arrays
with open("iris-data.txt") as f:
for line in f.readlines():
if not line.isspace():
tmp_list.append(line)
random.shuffle(tmp_list)
for line in tmp_list:
split_line = line.strip().split(',')
length_line = len(split_line)
for i in range(length_line - 1):
split_line[i] = float(split_line[i])
label = split_line[length_line - 1]
tmp_set.add(label)
features.append(split_line[:length_line - 1])
labels.append(label)
# Scale data
max_val = max([item for i in features for item in i])
min_val = min([item for i in features for item in i])
for i in range(len(features)):
for j in range(len(features[0])):
features[i][j] = (features[i][j] - min_val) / (max_val - min_val)
# One-hot encoding
tmp_list = list(tmp_set)
for i in range(len(labels)):
labels[i] = tmp_list.index(labels[i])
label_idx = np.array(labels)
labels = np.zeros((len(labels), len(tmp_list)))
labels[np.arange(len(labels)), label_idx] = 1
# split into train-test set
features_train = np.array(features[:int(train_test_ratio * len(features))])
features_test = np.array(features[int(train_test_ratio * len(features)):])
labels_train = labels[:int(train_test_ratio * len(labels))]
labels_test = labels[int(train_test_ratio * len(labels)):]
# Neural Network
# hyper-parameters
n_input_layers = len(features_test[0])
n_hidden_layers = 5
n_output_layers = len(tmp_list)
learning_rate = 0.01
momentum = 0.9
n_epoch = 100
# Activation Functions and their derivative
activation_f = {
'identity': lambda x: x,
'sigmoid': lambda x: 1.0 / (1.0 + np.exp(-x)),
'tanh': lambda x: np.tanh(x),
'relu': lambda x: x * (x > 0),
}
activation_f_prime = {
'identity': lambda x: 1,
'sigmoid': lambda x: x * (1.0 - x),
'tanh': lambda x: 1 - x**2,
'relu': lambda x: 1.0 * (x > 0),
}
# Activation Function Parameters
f1 = 'tanh'
f2 = 'sigmoid'
act_f1 = activation_f[f1]
act_f2 = activation_f[f2]
act_f1_prime = activation_f_prime[f1]
act_f2_prime = activation_f_prime[f2]
# Training Function
def train(input_features, output_label, i_h_weights, h_o_weights):
input_features = input_features.reshape(1, -1)
# forward prop
h_inter = np.dot(input_features, i_h_weights)
h_result = act_f1(h_inter)
o_inter = np.dot(h_result, h_o_weights)
o_result = act_f2(o_inter)
error = np.mean(0.5 * np.square(o_result - output_label))
# back prop
del_h_o = -np.multiply(output_label - o_result, act_f2_prime(o_result))
change_h_o = np.dot(h_result.T, del_h_o)
del_i_h = np.dot(del_h_o, h_o_weights.T) * act_f1_prime(h_result)
change_i_h = np.dot(input_features.T, del_i_h)
return error, change_i_h, change_h_o
# Predict Function
def predict(input_features, i_h_weights, h_o_weights):
# uses just forward prop
h_inter = np.dot(input_features, i_h_weights)
h_result = act_f1(h_inter)
o_inter = np.dot(h_result, h_o_weights)
o_result = act_f2(o_inter)
return (o_result >= max(o_result)).astype(int)
# Train Neural Network
print("*********** Train ***********")
# Initial Random Weights
V = np.random.normal(scale=0.1, size=(n_input_layers, n_hidden_layers))
W = np.random.normal(scale=0.1, size=(n_hidden_layers, n_output_layers))
# Training-set
X = features_train
T = labels_train
# Epoch-training
for epoch in range(n_epoch):
tr_err = []
for i in range(X.shape[0]):
loss, grad_V, grad_W = train(X[i], T[i], V, W)
# Adjust Weights
V -= learning_rate * grad_V + momentum * grad_V
W -= learning_rate * grad_W + momentum * grad_W
tr_err.append(loss)
if epoch % 10 == 0:
val_err = []
# use test set as validiation set
for i in range(features_test.shape[0]):
loss, _, _ = train(features_test[i], labels_test[i], V, W)
val_err.append(loss)
train_error = sum(tr_err) / len(tr_err)
valid_error = sum(val_err) / len(val_err)
print("Epoch:", epoch, " Train-error:", train_error,
" Validation-error:", valid_error)
# Test Neural Network
print("*********** Test ***********")
success = 0
for i in range(len(features_test)):
a = predict(features_test[i], V, W)
b = labels_test[i]
if np.array_equal(a, b):
success += 1
print("Total = %d Success = %d Accuracy = %f" %
(len(features_test), success, success * 100 / len(features_test)))