-
Notifications
You must be signed in to change notification settings - Fork 51
/
iris_tf_v1.py
106 lines (85 loc) · 3.5 KB
/
iris_tf_v1.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
from __future__ import print_function
from builtins import range
"""
SECTION 1 : Load and setup data for training
the datasets separated in two files from originai datasets:
iris_train.csv = datasets for training purpose, 80% from the original data
iris_test.csv = datasets for testing purpose, 20% from the original data
"""
import pandas as pd
#load
datatrain = pd.read_csv('../Datasets/iris/iris_train.csv')
#change string value to numeric
datatrain.loc[datatrain['species']=='Iris-setosa', 'species']=0
datatrain.loc[datatrain['species']=='Iris-versicolor', 'species']=1
datatrain.loc[datatrain['species']=='Iris-virginica', 'species']=2
datatrain = datatrain.apply(pd.to_numeric)
#change dataframe to array
datatrain_array = datatrain.values
#split x and y (feature and target)
xtrain = datatrain_array[:,:4]
ytrain = datatrain_array[:,4]
"""
SECTION 2 : Build and Train Model
Multilayer perceptron model, with one hidden layer.
input layer : 4 neuron, represents the feature of Iris
hidden layer : 10 neuron, activation using ReLU
output layer : 3 neuron, represents the class of Iris, Softmax Layer
optimizer = stochastic gradient descent with no batch-size
loss function = categorical cross entropy
learning rate = 0.0001
epoch = 1000
"""
import os
import tensorflow as tf
# tensorflow configuration
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # minimzing logging
cwd = os.path.abspath(os.path.dirname(__file__)) # path for saving model
saver_path = os.path.abspath(os.path.join(cwd, 'models/model_sess.ckpt'))
tf.set_random_seed(1103) # avoiding different result of random
# tensorflow model
input = tf.placeholder(tf.float32, [None, 4])
label = tf.placeholder(tf.float32, [None])
onehot_label = tf.one_hot(tf.cast(label, tf.int32), 3)
hidden = tf.layers.dense(input, 10, tf.nn.relu, name="hidden")
# kernel_initializer=tf.initializers.random_uniform(minval=-1, maxval=1, seed=123))
output = tf.layers.dense(hidden, 3, tf.nn.relu, name="output")
# kernel_initializer=tf.initializers.random_uniform(minval=-1, maxval=1, seed=123))
soft_output = tf.nn.softmax(output)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
# optimization
loss = -tf.reduce_sum(onehot_label * tf.log(soft_output))
optimizer = tf.train.GradientDescentOptimizer(0.0001)
is_correct = tf.equal(tf.argmax(soft_output,1), tf.argmax(onehot_label,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
train_step = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(init)
# start training
for e in range(1000):
if(e%100==0):
print(e,"/ 1000 - Loss:", sess.run(loss, feed_dict={input:xtrain, label:ytrain}))
sess.run(train_step, feed_dict={input:xtrain, label:ytrain})
# save model
saver.save(sess, saver_path)
print("Train accuracy",sess.run(accuracy, feed_dict={input:xtrain, label:ytrain}))
"""
SECTION 3 : Testing model
"""
#load
datatest = pd.read_csv('../Datasets/iris/iris_test.csv')
#change string value to numeric
datatest.loc[datatest['species']=='Iris-setosa', 'species']=0
datatest.loc[datatest['species']=='Iris-versicolor', 'species']=1
datatest.loc[datatest['species']=='Iris-virginica', 'species']=2
datatest = datatest.apply(pd.to_numeric)
#change dataframe to array
datatest_array = datatest.values
#split x and y (feature and target)
xtest = datatest_array[:,:4]
ytest = datatest_array[:,4]
# get the model then test
with tf.Session() as sess:
saver.restore(sess, saver_path)
print("Test accuracy",sess.run(accuracy, feed_dict={input:xtest, label:ytest}))