-
Notifications
You must be signed in to change notification settings - Fork 87
/
lab6_runTFMultiANN_spiraldata.py
206 lines (157 loc) · 7.08 KB
/
lab6_runTFMultiANN_spiraldata.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#-*- coding: utf-8 -*-
#! /usr/bin/env python
'''
#------------------------------------------------------------
filename: lab6_runTFMultiANN_spiraldata.py
A Multi-Hidden Layers Fully Connected Neural Network implementation with TensorFlow.
This example is using two class spiral data
written by Jaewook Kang @ Sep 2017
#------------------------------------------------------------
'''
from os import getcwd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import DataFrame
from sklearn import metrics
import tensorflow as tf
from tensorflow.contrib.learn.python.learn import learn_io
# reading data set from csv file ==========================
xsize = 2
ysize = 2
data = pd.read_csv('./data/twospirals_N5000.csv')
data.columns=['xdata1','xdata2','tdata']
permutation_index = np.random.permutation(data.index)
permutated_data = data.reindex(permutation_index)
permutated_data.columns=['xdata1','xdata2','tdata']
x_data = np.zeros([permutated_data.xdata1.size,xsize])
x_data[:,0] = permutated_data.xdata1.values
x_data[:,1] = permutated_data.xdata2.values
t_data = np.zeros([permutated_data.tdata.size,ysize])
t_data[:,0] = permutated_data.tdata.values
t_data[:,1] = np.invert(permutated_data.tdata.values) + 2
total_size = permutated_data.xdata1.size
training_size = int(np.floor(permutated_data.xdata1.size * 0.8))
validation_size = total_size - training_size
# data dividing
x_training_data = x_data[0:training_size,:]
t_training_data = t_data[0:training_size,:]
x_validation_data = x_data[training_size:-1,:]
t_validation_data = t_data[training_size:-1,:]
# #data plot
hfig1= plt.figure(1,figsize=[10,10])
plt.scatter(data.xdata1.values[0:int(data.xdata1.size/2)],\
data.xdata2.values[0:int(data.xdata1.size/2)], \
color='b',label='class0')
plt.scatter(data.xdata1.values[int(data.xdata1.size/2)+2:-1],\
data.xdata2.values[int(data.xdata1.size/2)+2:-1], \
color='r',label='class1')
plt.title('Two Spiral data Example')
plt.legend()
# configure training parameters =====================================
learning_rate = 5E-3
training_epochs = 500
batch_size = 100
display_step = 1
# computational TF graph construction ================================
# Network Parameters
n_hidden_1 = 7 # 1st layer number of neurons
n_hidden_2 = 4 # 2nd layer number of neurons
n_hidden_3 = 4 # 3rd layer number of neurons
num_input = xsize # two-dimensional input X = [1x2]
num_classes = ysize # 2 class
# tf Graph input
X = tf.placeholder(tf.float32, [None, num_input])
Y = tf.placeholder(tf.float32, [None, num_classes])
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'h3': tf.Variable (tf.random_normal([n_hidden_2, n_hidden_3])),
'out': tf.Variable(tf.random_normal([n_hidden_3, num_classes]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'b3': tf.Variable(tf.random_normal([n_hidden_3])),
'out': tf.Variable(tf.random_normal([num_classes]))
}
# Create model
def neural_net(x):
# Hidden fully connected layer with 7 neurons
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden fully connected layer with 7 neurons
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Hidden fully connected layer with 4 neurons
layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
layer_3 = tf.nn.relu(layer_3)
# Output fully connected layer with a neuron for each class
out_layer = tf.matmul(layer_3, weights['out']) + biases['out']
return out_layer
# Construct model
#graph0 = tf.Graph()
#with graph0.as_default():
logits = neural_net(X)
prediction = tf.nn.softmax(logits)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
#optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
errRateTraining = np.zeros(training_epochs)
errRateValidation = np.zeros(training_epochs)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# Start training ===============================================
with tf.Session() as sess:
# Run the initializer
sess.run(init)
print("--------------------------------------------")
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(training_size/batch_size)
for i in range(total_batch):
data_start_index = i * batch_size
data_end_index = (i + 1) * batch_size
# feed traing data --------------------------
batch_xs = x_training_data[data_start_index:data_end_index, :]
batch_ts = t_training_data[data_start_index:data_end_index, :]
#----------------------------------------------
# Run optimization op (backprop) and cost op (to get loss value)
# feedign training data
_, local_batch_cost = sess.run([optimizer,cost], feed_dict={X: batch_xs,
Y: batch_ts})
# Compute average loss
avg_cost += local_batch_cost / total_batch
# print ("At %d-th batch in %d-epoch, avg_cost = %f" % (i,epoch,avg_cost) )
# Display logs per epoch step
if display_step == 0:
continue
elif (epoch + 1) % display_step == 0:
# print("Iteration:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
batch_train_xs = x_training_data
batch_train_ys = t_training_data
batch_valid_xs = x_validation_data
batch_valid_ys = t_validation_data
errRateTraining[epoch] = 1.0 - accuracy.eval({X: batch_train_xs, \
Y: batch_train_ys}, session=sess)
errRateValidation[epoch] = 1.0 - accuracy.eval({X: batch_valid_xs, \
Y: batch_valid_ys}, session=sess)
print("Training set Err rate: %s" % errRateTraining[epoch])
print("Validation set Err rate: %s" % errRateValidation[epoch])
print("--------------------------------------------")
print("Optimization Finished!")
# Training result visualization ===============================================
hfig2 = plt.figure(2,figsize=(10,10))
epoch_index = np.array([elem for elem in range(training_epochs)])
plt.plot(epoch_index,errRateTraining,label='Training data',color='r',marker='o')
plt.plot(epoch_index,errRateValidation,label='Validation data',color='b',marker='x')
plt.legend()
plt.title('Classification Error Rate of prediction:')
plt.xlabel('Iteration epoch')
plt.ylabel('error Rate')
plt.show()