In [2]:
import tensorflow as tf
x_data = [[1, 2, 1, 1], [2, 1, 3, 2], [3, 1, 3, 4], [4, 1, 5, 5], [1, 7, 5, 5],
[1, 2, 5, 6], [1, 6, 6, 6], [1, 7, 7, 7]]
# one hot incoding.. 한가지만 1 나머진0
# C C C B B B A A
y_data = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]]
X = tf.placeholder("float", [None, 4])
Y = tf.placeholder("float", [None, 3])
nb_classes = 3
W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
# tf.nn.softmax = exp(Logits) / reduce_sum(exp(Logits), dim)
hypothesis = tf.nn.softmax(tf.matmul(X,W) + b)
# Cross entropy cost(loss)
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(4001):
sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
if step % 400 == 0:
print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}))
# TEST
print("---------------------\nTEST")
a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})
print(a) # 이대로 출력할 경우 arg_max 를 사용하라는 경고가 발생한다.
print(sess.run(tf.arg_max(a, 1))) # arg_max : 이 matrix 에서 제일 높은값을 갖는 인덱스 추출
'Dev > 딥러닝' 카테고리의 다른 글
07-1. Tensor Flow 에서 Learning rate 이란 (0) | 2018.08.22 |
---|---|
06-2. Tensorflow 에서 One hot 인코딩을 이용한 classification (0) | 2018.08.22 |
05. Tensor Flow 로 Classification 예제 (binary) (0) | 2018.08.17 |
04-2. Tensor Flow 에서 csv 파일 읽기 및 queue 사용법 (0) | 2018.08.17 |
04-1. Tensor Flow 변수가 여러개일때 Linear regression (0) | 2018.08.17 |
댓글