본문 바로가기
머신 러닝

11.Soft_Regression 실습 (동물 구분하기)

by Jnamelight 2017. 12. 6.
import tensorflow as tf
import numpy as np

# 데이터 가져오기 ( np 를 이용해서) 
xy = np.loadtxt('data-04-zoo.csv', delimiter=',',dtype=np.float32)

x_data = xy[:,0:-1]
y_data = xy[:,[-1]]

nb_classes = 6  # 0~6 가지의 동물이 존재( 출력데이터 갯수)

# 배열 설정  (데이터수가 많음으로)
X = tf.placeholder(tf.float32,[None,x_data.shape[1]])
Y = tf.placeholder(tf.int32,[None,y_data.shape[1]])

# One_hot 으로 만들어주기 여러개의 데이터중에서 가장 가능성이 높은 데이터의 Rank 를 생성함
#근대 문제점이 있는것이, Rank의 값이 +1 이 됨으로 해결해주어야 한다.
Y_one_hot = tf.one_hot(Y, nb_classes)  
print("one_hot",Y_one_hot)

Y_one_hot = tf.reshape(Y_one_hot,[-1,nb_classes]) # 이런식으로 Rank 의 값을 하나 줄여줌 -1 은 아무거나 즉, None 과 똑같다고 보면됨
print("one_hot",Y_one_hot) 

# 이제 Y의 값을 처리해 줬으니 W, b 설계

W = tf.Variable(tf.random_normal([x_data.shape[1],nb_classes]), name = 'weight') # 여기서 nb_classes 를 넣은 이유는 위에서 원핫 처리때문
b = tf.Variable(tf.random_normal([nb_classes]))

# cost 설계

logits = tf.matmul(X,W) + b
hypothesis = tf.nn.softmax(logits)  # softmax 함수를 통해서 0~1 사이의 값으로 만들어줌 -> 확률로 만들어준다는 소리

#cost를 실제적으로 구하는 함수, 이함수는 softmax 를 알아서 해주기 떄문에 logits 의 값으로 넣음
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=Y_one_hot)
cost = tf.reduce_mean(cost_i) 

#cost의 최소화를 구해줌
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

#예측한 값과 맞는 지 확인하기 위한 작업
                                       # 현재 hypothesis 는 확률로 나타내져 있을것이다,  
prediction = tf.argmax(hypothesis,1)  # 그것을 0~6으로 나타내는 함수
correct_prediction = tf.equal(prediction,tf.argmax(Y_one_hot,1))  # 실제값과의 비교
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

#학습
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    for step in range(2001):
        sess.run(optimizer,feed_dict={X:x_data,Y:y_data})
        if step % 100 ==0:
            loss, acc = sess.run([cost,accuracy],feed_dict={X:x_data,Y:y_data})
            print("Step : {:5}\tLoss: {:.3f}\tAcc: {:.2%}".format(step,loss,acc))
        
    pred = sess.run(prediction,feed_dict={X:x_data})
    # y_data: (N,1) = flatten => (N, ) matches pred.shape
    for p, y in zip(pred, y_data.flatten()):
        print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))
one_hot Tensor("one_hot_8:0", shape=(?, 1, 6), dtype=float32)
one_hot Tensor("Reshape_24:0", shape=(?, 6), dtype=float32)
Step :     0	Loss: 2.659	Acc: 21.78%
Step :   100	Loss: 0.300	Acc: 83.17%
Step :   200	Loss: 0.201	Acc: 85.15%
Step :   300	Loss: 0.158	Acc: 87.13%
Step :   400	Loss: 0.132	Acc: 87.13%
Step :   500	Loss: 0.113	Acc: 88.12%
Step :   600	Loss: 0.100	Acc: 89.11%
Step :   700	Loss: 0.089	Acc: 89.11%
Step :   800	Loss: 0.081	Acc: 89.11%
Step :   900	Loss: 0.074	Acc: 89.11%
Step :  1000	Loss: 0.069	Acc: 89.11%
Step :  1100	Loss: 0.064	Acc: 89.11%
Step :  1200	Loss: 0.060	Acc: 90.10%
Step :  1300	Loss: 0.057	Acc: 90.10%
Step :  1400	Loss: 0.054	Acc: 89.11%
Step :  1500	Loss: 0.051	Acc: 89.11%
Step :  1600	Loss: 0.049	Acc: 89.11%
Step :  1700	Loss: 0.047	Acc: 89.11%
Step :  1800	Loss: 0.045	Acc: 89.11%
Step :  1900	Loss: 0.044	Acc: 89.11%
Step :  2000	Loss: 0.042	Acc: 90.10%
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[False] Prediction: 1 True Y: 6
[False] Prediction: 4 True Y: 6
[False] Prediction: 5 True Y: 6
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 5 True Y: 5
[True] Prediction: 4 True Y: 4
[True] Prediction: 4 True Y: 4
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 5 True Y: 5
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 5 True Y: 5
[True] Prediction: 5 True Y: 5
[True] Prediction: 1 True Y: 1
[True] Prediction: 5 True Y: 5
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[False] Prediction: 5 True Y: 6
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 5 True Y: 5
[True] Prediction: 4 True Y: 4
[False] Prediction: 3 True Y: 6
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 3 True Y: 3
[True] Prediction: 2 True Y: 2
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[False] Prediction: 2 True Y: 6
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 2 True Y: 2
[False] Prediction: 3 True Y: 6
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 2 True Y: 2
[False] Prediction: 5 True Y: 6
[True] Prediction: 3 True Y: 3
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[False] Prediction: 4 True Y: 6
[True] Prediction: 3 True Y: 3
[True] Prediction: 1 True Y: 1
[True] Prediction: 5 True Y: 5
[True] Prediction: 4 True Y: 4
[True] Prediction: 2 True Y: 2
[True] Prediction: 2 True Y: 2
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 5 True Y: 5
[True] Prediction: 0 True Y: 0
[False] Prediction: 5 True Y: 6
[True] Prediction: 1 True Y: 1


댓글