-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
81 lines (66 loc) · 2.53 KB
/
__init__.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
from pathlib import Path
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
dataset_path = Path(__file__).resolve().parent / 'iris.data'
print(dataset_path)
file_ = open(dataset_path)
data = []
label = []
# Loading data
for line in file_:
s = line.split(',')
data.append(list(map(float, s[:-1])))
if s[-1] == 'Iris-setosa\n':
label.append([1, 0, 0])
elif s[-1] == 'Iris-versicolor\n':
label.append([0, 1, 0])
elif s[-1] == 'Iris-virginica\n':
label.append([0, 0, 1])
# Shuffle data
indexes = np.arange(150)
np.random.shuffle(indexes)
# Dividing into train and test data
data_ = np.array(data)
label_ = np.array(label)
SPLIT = 105
TOTAL = 151
train_data = data_[indexes[:SPLIT]]
train_label = label_[indexes[:SPLIT]]
test_data = data_[indexes[SPLIT:TOTAL]]
test_label = label_[indexes[SPLIT:TOTAL]]
# Neural Network
sess = tf.Session()
train_data_ = tf.placeholder(dtype=tf.float32, shape=[None, 4])
train_label_ = tf.placeholder(dtype=tf.float32, shape=[None, 3])
fd = {train_data_: train_data, train_label_: train_label}
# build model in TF
Z1 = 20
Z2 = 20
W1 = tf.Variable(tf.random_uniform([4, Z1], -0.01, 0.01, dtype=tf.float32))
W2 = tf.Variable(tf.random_uniform([Z1, Z2], -0.01, 0.01, dtype=tf.float32))
W3 = tf.Variable(tf.random_uniform([Z2, 3], -0.01, 0.01, dtype=tf.float32))
b1 = tf.Variable(tf.random_uniform([1, Z1], -0.01, 0.01, dtype=tf.float32))
b2 = tf.Variable(tf.random_uniform([1, Z2], -0.01, 0.01, dtype=tf.float32))
b3 = tf.Variable(tf.random_uniform([1, 3], -0.01, 0.01, dtype=tf.float32))
a1 = tf.nn.relu(tf.matmul(train_data_, W1) + b1)
# print("a1",a1)
a2 = tf.nn.relu(tf.matmul(a1, W2) + b2)
# print("a2",a2)
model_output = tf.matmul(a2, W3) + b3
# print("mop",model_output)
sess.run(tf.global_variables_initializer())
# classification accuracy
nrCorrect = tf.reduce_mean(tf.cast(tf.equal(
tf.argmax(model_output, axis=1), tf.argmax(train_label_, axis=1)), tf.float32))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(
logits=model_output, labels=train_label_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.05)
update = optimizer.minimize(loss)
for iteration in range(0, 2000):
sess.run(update, feed_dict=fd)
correct, lossVal = sess.run([nrCorrect, loss], feed_dict=fd)
testacc = sess.run(nrCorrect, feed_dict={
train_data_: test_data, train_label_: test_label})
print("epoch ", iteration, "acc=", round(float(correct), 2),
"loss=", round(lossVal, 2), "testacc=", round(testacc, 2))