review¶
In [1]:
import tensorflow as tf
import numpy as np
In [2]:
# data_define
# [털, 날개]
x_data = np.array([
[0, 0],
[1, 0],
[1, 1],
[0, 0],
[0, 0],
[0, 1]
])
# [기타, 포유류, 조류]
y_data = np.array([
[1, 0, 0], # 기타
[0, 1, 0], # 포유류
[0, 0, 1], # 조류
[1, 0, 0], # 기타
[1, 0, 0], # 기타
[0, 0, 1] # 조류
])
## model setting
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
add layer¶
In [3]:
# weighted
W1 = tf.Variable(tf.random_normal([2, 10], mean=0, stddev=1)) # [특징, hidden_layer]
W2 = tf.Variable(tf.random_normal([10, 3], mean=0, stddev=1)) # [hidden_layer, output]
# biased
b1 = tf.Variable(tf.zeros([10])) # ["hidden layer"]
b2 = tf.Variable(tf.zeros([3])) # [output]
L1 = tf.add(tf.matmul(X, W1), b1)
L1 = tf.nn.sigmoid(L1)
model = tf.add(tf.matmul(L1, W2), b2)
model = tf.nn.softmax(model)
cost function¶
- one-hot encoding을 이용한 대부분의 모델에서는 cross-entropy를 사용
$E(w,\quad b)\quad =\quad -\sum _{ n=1 }^{ N } \left\{ { t_{ n }logy_{ n }+(1-t_{ n })log(1-y_{ n }) } \right\} $
In [4]:
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(model) + (1-Y)*tf.log(1-model), axis=1))
modeling¶
In [5]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(cost)
In [6]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(1000):
sess.run(train_op, feed_dict={X:x_data, Y:y_data})
if (step+1) % 50 == 0:
print("{}, {:.5f}".format(step+1,
sess.run(cost, feed_dict={X: x_data, Y: y_data})))
output¶
In [7]:
sess.run(model, feed_dict={X:x_data})
Out[7]:
In [8]:
prediction = tf.argmax(model, axis=1)
target = tf.argmax(Y, axis=1)
print("prediction: \t{}".format(sess.run(prediction, feed_dict={X:x_data})))
print("target: \t{}".format(sess.run(target, feed_dict={Y:y_data})))
accuracy¶
In [9]:
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: {:.3f}".format(sess.run(accuracy, feed_dict={X:x_data, Y:y_data})))
In [10]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))
'Deep_Learning' 카테고리의 다른 글
06.tensorboard01_example (0) | 2018.12.09 |
---|---|
05.deep_neural_net_Costfun2 (0) | 2018.12.09 |
03.classification (0) | 2018.12.09 |
02.linear_regression (0) | 2018.12.09 |
01.tesnsor_and_graph (0) | 2018.12.09 |