iris_alpha_01.py 코드의 함수 모델에서 처음 Covariance 처리 후 ReLU 처리를 거쳐 다시 중복하여 Covariance 처리하였다. Covariance 처리에 따른 feature extraction 효과를 확인 할 수 있도록 ReLU 처리와 뒷부분의 중복을 제거한 아주 간략한 상태의 코드로 Accuracy를 계산해 보자. 결과 5회에 3번은 1.0, 2회는 98.3 이 출력된다.
#iris_alpha_02.py
#import os
#os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'
import tensorflow as tf
import numpy as np
import time
start_time = time.time()
def label_encode(label):
val=[]
if label == "Iris-setosa":
val = [1,0,0]
elif label == "Iris-versicolor":
val = [0,1,0]
elif label == "Iris-virginica":
val = [0,0,1]
return val
def data_encode(file):
X = []
Y = []
train_file = open(file, 'r')
for line in train_file.read().strip().split('\n'):
line = line.split(',')
X.append([line[0], line[1], line[2], line[3]])
Y.append(label_encode(line[4]))
return X, Y
#Defining a Multilayer Neural Network Model
def model(x, alpha, weights1, bias1,weights2, bias2,weights3, bias3):
layer_1 = tf.add(tf.matmul(x, weights1["hidden1"]), bias1["hidden1"]) + alpha * tf.add(tf.matmul(x, weights2["hidden2"]), bias2["hidden2"]) * tf.add(tf.matmul(x, weights3["hidden3"]), bias3["hidden3"])
#layer_1 = tf.nn.relu(layer_1)
output_layer = tf.matmul(layer_1, weights1["output1"]) + bias1["output1"]
return output_layer
#Training and Testing Data
train_X , train_Y = data_encode('iris.train')
test_X , test_Y = data_encode('iris.test')
#hyperparameter
learning_rate = 0.01
training_epochs = 10000
display_steps = 2000
#Network parameters
n_input = 4
n_hidden = 10
n_output = 3
#alpha = 0.0238
#alpha=0.3
alpha=0.2
#Graph Nodes
X = tf.placeholder("float", [None, n_input])
Y = tf.placeholder("float", [None, n_output])
#Weights and Biases
weights1 = {
"hidden1" : tf.Variable(tf.random_normal([n_input, n_hidden]), name="weight_hidden1"),
"output1" : tf.Variable(tf.random_normal([n_hidden, n_output]), name="weight_output1")
}
bias1 = {
"hidden1" : tf.Variable(tf.random_normal([n_hidden]), name="bias_hidden1"),
"output1" : tf.Variable(tf.random_normal([n_output]), name="bias_output1")
}
weights2 = {
"hidden2" : tf.Variable(tf.random_normal([n_input, n_hidden]), name="weight_hidden2"),
"output2" : tf.Variable(tf.random_normal([n_hidden, n_output]), name="weight_output2")
}
bias2 = {
"hidden2" : tf.Variable(tf.random_normal([n_hidden]), name="bias_hidden2"),
"output2" : tf.Variable(tf.random_normal([n_output]), name="bias_output2")
}
weights3 = {
"hidden3" : tf.Variable(tf.random_normal([n_input, n_hidden]), name="weight_hidden3"),
"output3" : tf.Variable(tf.random_normal([n_hidden, n_output]), name="weight_output3")
}
bias3 = {
"hidden3" : tf.Variable(tf.random_normal([n_hidden]), name="bias_hidden3"),
"output3" : tf.Variable(tf.random_normal([n_output]), name="bias_output3")
}
#Define model
pred = model(X,alpha, weights1, bias1,weights2, bias2,weights3, bias3)
#Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
#Initializing global variables
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
_, c = sess.run([optimizer, cost], feed_dict={X: train_X, Y: train_Y})
if(epoch + 1) % display_steps == 0:
print( "Epoch: ", (epoch+1), "Cost: ", c )
print("Optimization Finished!")
test_result = sess.run(pred, feed_dict={X: train_X})
correct_pred = tf.equal(tf.argmax(test_result, 1), tf.argmax(train_Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, "float"))
print( "Accuracy:", accuracy.eval({X: test_X, Y: test_Y}) )
end_time = time.time()
print( "Completed in ", end_time - start_time , " seconds")
'''
runfile('D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/iris_alpha_02.py', wdir='D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master')
Epoch: 2000 Cost: 0.047372095
Epoch: 4000 Cost: 0.046686634
Epoch: 6000 Cost: 0.046483833
Epoch: 8000 Cost: 0.042684656
Epoch: 10000 Cost: 0.014836952
Optimization Finished!
Accuracy: 1.0
Completed in 15.510505199432373 seconds
runfile('D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/iris_alpha_02.py', wdir='D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master')
Epoch: 2000 Cost: 0.048434567
Epoch: 4000 Cost: 0.04761433
Epoch: 6000 Cost: 0.047844656
Epoch: 8000 Cost: 0.046645913
Epoch: 10000 Cost: 0.04332118
Optimization Finished!
Accuracy: 0.98333335
Completed in 15.306052207946777 seconds
runfile('D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/iris_alpha_02.py', wdir='D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master')
Epoch: 2000 Cost: 0.047547735
Epoch: 4000 Cost: 0.046159886
Epoch: 6000 Cost: 0.044194356
Epoch: 8000 Cost: 0.036898416
Epoch: 10000 Cost: 0.016994698
Optimization Finished!
Accuracy: 1.0
Completed in 15.98526382446289 seconds
runfile('D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/iris_alpha_02.py', wdir='D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master')
Epoch: 2000 Cost: 0.047301635
Epoch: 4000 Cost: 0.046075907
Epoch: 6000 Cost: 0.02632461
Epoch: 8000 Cost: 0.0058051967
Epoch: 10000 Cost: 0.0029842153
Optimization Finished!
Accuracy: 1.0
Completed in 15.768858909606934 seconds
runfile('D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/iris_alpha_02.py', wdir='D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master')
Epoch: 2000 Cost: 0.048431214
Epoch: 4000 Cost: 0.047390338
Epoch: 6000 Cost: 0.04381287
Epoch: 8000 Cost: 0.013681986
Epoch: 10000 Cost: 0.0051795444
Optimization Finished!
Accuracy: 1.0
Completed in 15.8097403049469 seconds
runfile('D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/iris_alpha_02.py', wdir='D:/아두이노자료_17_03_24/아나콘다머신러닝/Iris-Data-Set-Classification-using-TensorFlow-MLP-master/Iris-Data-Set-Classification-using-TensorFlow-MLP-master')
Epoch: 2000 Cost: 0.04816631
Epoch: 4000 Cost: 0.046676736
Epoch: 6000 Cost: 0.04431151
Epoch: 8000 Cost: 0.03839687
Epoch: 10000 Cost: 0.029822191
Optimization Finished!
Accuracy: 0.98333335
Completed in 15.983241319656372 seconds
'''
'머신러닝' 카테고리의 다른 글
Covariance 항을 포함한 Softmax Classifier의 XOR 로직 적용 (0) | 2019.08.03 |
---|---|
Covariance 항을 포함한 Softmax MNIST 적용 (0) | 2019.08.02 |
Application of Statistical softmax To TensorFlow Iris flower data classification (0) | 2019.07.23 |
Scikit-learn 라이브러리 지원 Iris flowers 예제 (0) | 2019.07.22 |
이미지 Classification: Convolutionary Digital Filter→AlexNet→∙∙∙→ResNet (0) | 2019.07.21 |