05_deep_neural_net_Costfun2

review

In [1]:
import tensorflow as tf
import numpy as np
In [2]:
# data_define

# 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

In [3]:
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

W1 = tf.Variable(tf.random_normal([2, 10], mean=0, stddev=1))
W2 = tf.Variable(tf.random_normal([10, 3], mean=0, stddev=1))

b1 = tf.Variable(tf.zeros([10]))
b2 = tf.Variable(tf.zeros([3]))

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.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))


modeling

In [5]:
optimizier = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizier.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})))
50, 1.15160
100, 1.14459
150, 1.13681
200, 1.12806
250, 1.11820
300, 1.10710
350, 1.09478
400, 1.08139
450, 1.06730
500, 1.05302
550, 1.03913
600, 1.02610
650, 1.01422
700, 1.00354
750, 0.99399
800, 0.98538
850, 0.97755
900, 0.97032
950, 0.96356
1000, 0.95717


output

In [7]:
sess.run(model, feed_dict={X: x_data, Y:y_data})
Out[7]:
array([[0.64512706, 0.08173119, 0.2731418 ],
       [0.37277853, 0.06834452, 0.55887693],
       [0.3646471 , 0.05929578, 0.57605714],
       [0.64512706, 0.08173119, 0.2731418 ],
       [0.64512706, 0.08173116, 0.2731418 ],
       [0.51281446, 0.0757072 , 0.41147828]], dtype=float32)
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})))
prediction: 	[0 2 2 0 0 0]
target: 	[0 1 2 0 0 2]


accuracy

In [9]:
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: \t{:.3f}".format(sess.run(accuracy, feed_dict={X:x_data, Y:y_data})))
accuracy: 	0.667
In [10]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

07.tensorboard02_example  (0) 2018.12.09
06.tensorboard01_example  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09
04_deep_neural_net_Costfun1

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})))
50, 1.66444
100, 1.45811
150, 1.34925
200, 1.26442
250, 1.19221
300, 1.12926
350, 1.07365
400, 1.02398
450, 0.97921
500, 0.93851
550, 0.90123
600, 0.86685
650, 0.83496
700, 0.80520
750, 0.77730
800, 0.75101
850, 0.72614
900, 0.70253
950, 0.68003
1000, 0.65854


output

In [7]:
sess.run(model, feed_dict={X:x_data})
Out[7]:
array([[0.86290747, 0.08460085, 0.05249172],
       [0.408934  , 0.25658685, 0.33447912],
       [0.03734298, 0.09417698, 0.86848   ],
       [0.86290747, 0.08460085, 0.05249172],
       [0.86290747, 0.08460083, 0.05249173],
       [0.12566108, 0.106323  , 0.7680159 ]], dtype=float32)
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})))
prediction: 	[0 0 2 0 0 2]
target: 	[0 1 2 0 0 2]


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})))
accuracy: 0.833
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
03_classification

활성화함수

@activation_fucntion
activation function은 인공신경망을 통과해온 값을 최종적으로 어떤 값으로 만들지를 결정 대표적으로 Sigmoid, ReLU, tanh함수가 있음


backpropagation

@backpropagation
출력층이 내놓은 결과의 오차를 입력층까지 전달하며 가중치를 계산


classification model

In [1]:
import tensorflow as tf
import numpy as np
In [2]:
# [털, 날개]
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]  # 조류
])

#     [0, 0] -> [1, 0, 0] # 털x, 날개x -> 기타
#     [1, 0] -> [0, 1, 0] # 털o, 날개x -> 포유류
#     [1, 1] -> [0, 0, 1] # 털o, 날개o -> 조류
#     [0, 0] -> [1, 0, 0] # 털x, 날개x -> 기타
#     [0, 0] -> [1, 0, 0] # 털x, 날개x -> 기타
#     [0, 1] -> [0, 0, 1] # 털x, 날개o -> 조류


neuralnet model setting

X, Y는 실측값groud truth를 넣어서 학습 -> placeholder

In [3]:
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

# weith와 bias 설정
W = tf.Variable(tf.random_uniform([2, 3], -1., 1.)) # 입력, 출력
b = tf.Variable(tf.zeros([3]))

L = tf.add(tf.matmul(X, W), b)
L = tf.nn.relu(L)

# 출력값을 softmax 함수를 이용하여 다듬어줌
# softmax는 결과값들을 전체 합이 1이 되도록 만들어줌 ==> 확률로 해석 가능
model = tf.nn.softmax(L)


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.001).minimize(cost)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for step in range(1000):
    sess.run(optimizer, feed_dict={X:x_data, Y:y_data})
    
    if (step+1) % 50 == 0:
        print("{}, {}".format(step+1, 
                             sess.run(cost, feed_dict={X:x_data, Y:y_data})))
50, 1.9566283226013184
100, 1.9450751543045044
150, 1.9336179494857788
200, 1.9222602844238281
250, 1.911005973815918
300, 1.899857997894287
350, 1.888819694519043
400, 1.8778947591781616
450, 1.8670859336853027
500, 1.856396198272705
550, 1.8458284139633179
600, 1.835384726524353
650, 1.8250678777694702
700, 1.8148797750473022
750, 1.804822325706482
800, 1.7948967218399048
850, 1.7851048707962036
900, 1.7754473686218262
950, 1.7659250497817993
1000, 1.7565385103225708


output

예측값인 model을 바로 출력하면 확률로 나오기 때문에 요소중 가장 큰 값의 인덱스를 찾아주는 argmax()를 이용

In [6]:
sess.run(model, feed_dict={X:x_data})
Out[6]:
array([[0.3319709 , 0.3319709 , 0.33605826],
       [0.2515205 , 0.45800376, 0.29047582],
       [0.21185754, 0.42796907, 0.36017334],
       [0.3319709 , 0.3319709 , 0.33605826],
       [0.3319709 , 0.3319709 , 0.33605826],
       [0.29241043, 0.2897259 , 0.4178636 ]], dtype=float32)
In [7]:
prediction = tf.argmax(model, axis=1)
target = tf.argmax(Y, axis=1)

print("prediction: {}".format(sess.run(prediction, feed_dict={X:x_data})))
print("ground_truth: {}".format(sess.run(target, feed_dict={Y:y_data})))
prediction: [2 1 1 2 2 2]
ground_truth: [0 1 2 0 0 2]


accuracy

In [8]:
is_correct = tf.equal(prediction, target)

# tf.cast : True, False -> 1, 0
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})))
accuracy: 0.333
In [9]:
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
04.deep_neural_net_Costfun1  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09
01.tesnsor_and_graph  (0) 2018.12.09
02_linear_regression

선형 회귀|center

  • 주어진 x와 y 값을 가지고 서로 간의 관계를 파악
  • 새로운 x값이 주어졌을 때 y값을 쉽게 알 수 있음
In [1]:
import tensorflow as tf
In [2]:
x_data = [1, 2, 3]
y_data = [1, 2, 3]


data 생성

In [3]:
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.random_uniform([1], -1.0, 1.0))


placeholder 설정

In [4]:
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")


model 설정

In [5]:
model = W*X + b


cost function

In [6]:
cost = tf.reduce_mean(tf.square(model- Y))


gradient desent

In [7]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(cost)

@경사하강법|center


modeling

In [8]:
sess =  tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(500):
    _, cost_val = sess.run([train_op, cost], feed_dict={X: x_data, Y: y_data})
    if step % 25 == 0:
        print("step: {}, cost_val: {:.5f}, W: {}, b: {}".format(step, cost_val, sess.run(W), sess.run(b)))
step: 0, cost_val: 0.45853, W: [0.71138144], b: [0.7272724]
step: 25, cost_val: 0.02285, W: [0.828638], b: [0.389546]
step: 50, cost_val: 0.00677, W: [0.9067342], b: [0.2120151]
step: 75, cost_val: 0.00201, W: [0.9492389], b: [0.11539178]
step: 100, cost_val: 0.00059, W: [0.97237265], b: [0.06280342]
step: 125, cost_val: 0.00018, W: [0.9849635], b: [0.03418154]
step: 150, cost_val: 0.00005, W: [0.99181616], b: [0.01860373]
step: 175, cost_val: 0.00002, W: [0.99554586], b: [0.01012532]
step: 200, cost_val: 0.00000, W: [0.9975758], b: [0.00551084]
step: 225, cost_val: 0.00000, W: [0.9986806], b: [0.00299932]
step: 250, cost_val: 0.00000, W: [0.99928194], b: [0.00163241]
step: 275, cost_val: 0.00000, W: [0.9996092], b: [0.00088852]
step: 300, cost_val: 0.00000, W: [0.9997873], b: [0.00048356]
step: 325, cost_val: 0.00000, W: [0.9998842], b: [0.00026316]
step: 350, cost_val: 0.00000, W: [0.99993694], b: [0.00014328]
step: 375, cost_val: 0.00000, W: [0.9999657], b: [7.794145e-05]
step: 400, cost_val: 0.00000, W: [0.99998134], b: [4.2393247e-05]
step: 425, cost_val: 0.00000, W: [0.9999898], b: [2.3081342e-05]
step: 450, cost_val: 0.00000, W: [0.99999446], b: [1.2575029e-05]
step: 475, cost_val: 0.00000, W: [0.99999696], b: [6.8529835e-06]


test

In [9]:
class prediction:
            
    def run(self, input):  
        self.input = input
        output = sess.run(model, feed_dict={X: self.input})
        print("X: {}, Y-result: {}".format(self.input, output))
        
pred = prediction()
In [10]:
pred.run(2.5)
pred.run(5)
pred.run(10)
X: 2.5, Y-result: [2.4999995]
X: 5, Y-result: [4.999995]
X: 10, Y-result: [9.999987]
In [11]:
from IPython.core.display import HTML, display

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
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
01.tesnsor_and_graph  (0) 2018.12.09
01_tesnsor_and_graph
In [1]:
import tensorflow as tf
In [2]:
hello = tf.constant("hello, Tensorflow")
print(hello)
Tensor("Const:0", shape=(), dtype=string)

1. 텐서의 그래프 실행


hello 변수의 값을 출력한 결과로, hello가 텐서플로의 텐서Tensor라는 자료형이고, 상수를 담고 있음
텐서는 텐서플로에서 다양한 수학식을 계산하기 위한 가장 기본적이고 중요한 자료형이며, rank와 shape라는 개념을 가지고 있음

In [3]:
3 # rank=0, shape=[]
[1., 2., 3.] # rank=1, shape=[3]
[[1., 2., 3.], [4., 5., 6.]] # rank=2, shape=[2, 3]
[[[1., 2., 3.,]], [[7., 8., 9.]]] # rank=3, shape=[2, 1, 3]
Out[3]:
[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]
In [4]:
a = tf.constant(10)
b = tf.constant(32)
c = tf.add(a, b)
print(c)
Tensor("Add:0", shape=(), dtype=int32)

텐서플로 프로그램 구성|center

그래프: 텐서들의 연산 모음
필요할 때 연산을 실행하는 코드를 넣어 '원하는 시점'에서 실제 연산을 수행하도록 함 --> 지연 실행lazy evaluation

In [5]:
sess = tf.Session()

print(sess.run(hello))
print(sess.run([a, b, c]))

sess.close()
b'hello, Tensorflow'
[10, 32, 42]
In [6]:
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
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09

+ Recent posts