-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_iris.py
71 lines (54 loc) · 2.28 KB
/
nn_iris.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
import tensorflow as tf
import numpy as np
# Translate a list of labels into an array of 0's and one 1.
# i.e.: 4 -> [0,0,0,0,1,0,0,0,0,0]
def one_hot(x, n):
"""
:param x: label (int)
:param n: number of bits
:return: one hot code
"""
if type(x) == list:
x = np.array(x)
x = x.flatten()
o_h = np.zeros((len(x), n))
o_h[np.arange(len(x)), x] = 1
return o_h
data = np.genfromtxt('iris.data', delimiter=",") # iris.data file loading
np.random.shuffle(data) # we shuffle the data
x_data = data[:, 0:4].astype('f4') # the samples are the four first rows of data
y_data = one_hot(data[:, 4].astype(int), 3) # the labels are in the last row. Then we encode them in one hot code
print("\nSome samples...")
for i in range(20):
print(x_data[i], " -> ", y_data[i])
print
x = tf.placeholder("float", [None, 4]) # samples
y_ = tf.placeholder("float", [None, 3]) # labels
W1 = tf.Variable(np.float32(np.random.rand(4, 5)) * 0.1)
b1 = tf.Variable(np.float32(np.random.rand(5)) * 0.1)
W2 = tf.Variable(np.float32(np.random.rand(5, 3)) * 0.1) #### YOu should try to quit the *0.1 and execute it
b2 = tf.Variable(np.float32(np.random.rand(3)) * 0.1)
h = tf.nn.sigmoid(tf.matmul(x, W1) + b1)
# h = tf.matmul(x, W1) + b1 # Try this!
y = tf.nn.softmax(tf.matmul(h, W2) + b2)
loss = tf.reduce_sum(tf.square(y_ - y))
opt = tf.train.GradientDescentOptimizer(0.01)
# train = tf.train.GradientDescentOptimizer(0.01).minimize(loss) # learning rate: 0.01
train = opt.minimize(loss) # learning rate: 0.01
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
print("----------------------")
print(" Start training... ")
print("----------------------")
batch_size = 20
for epoch in range(500):
for jj in range((int)(len(x_data) / batch_size)):
batch_xs = x_data[jj * batch_size: jj * batch_size + batch_size]
batch_ys = y_data[jj * batch_size: jj * batch_size + batch_size]
sess.run(train, feed_dict={x: batch_xs, y_: batch_ys})
print("Epoch #:", epoch, "Error: ", sess.run(loss, feed_dict={x: batch_xs, y_: batch_ys}))
result = sess.run(y, feed_dict={x: batch_xs})
for b, r in zip(batch_ys, result):
print(b, "-->", r)
print ("----------------------------------------------------------------------------------")