출처 :
http://hunkim.github.io/ml/
https://www.tensorflow.org/?hl=tr
ML lab 02 - TensorFlow로 간단한 linear regression을 구현
Hypothesis and Cost function
주어진 x에 대해 예측을 어떻게 할 것인가?!
예측 H(x)와 실제 데이터 (y)의 차이는 얼만큼 나는가?!
- linear Regression의 학습은 W, b를 갱신 하면서 Cost function을 minimize한다는 것이다.
다시보는 Machine learning의 동작 원리
① Build graph using TensorFlow operations
# X data and Y label x_train = [1, 2, 3] y_train = [1, 2, 3] W = tf.Variable(tf.random_normal([1]), name = 'weight') b = tf.Variable(tf.random_normal([1]), name = 'bias') # Hypothesis hypothesis = x_train * W + b
- x_train 위치에 영상 픽셀 값들이 오고, y_train 위치에 label이 온다 생각하자
# cost / loss function cost = tf.reduce_mean(tf.square(hypothesis - y_train))
GradientDescent
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01) train = optimizer.minimize(cost)
- 최적화에는 여러가지 알고리즘이 존재한다 (진짜 엄청많음...)
- GD 알고리즘에 대한 얘기는 나~중에
②, ③ Run / update graph and get results
# Launch the graph in a session sess = tf.Session() # Initializes global variables in the graph sess.run(tf.global_variables_initializer()) # Fit the line for step in range(2001): sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W), sess.run(b))
- Variables를 사용하기 전에는 반드시
> tf.global_variables_initializer())를 해줘야 함
- train만 실행 시킨다는 것의 의미
> 그래프를 따라 들어가 W와 b를 조정할 수 있다.
Full Code
import tensorflow as tf x_train = [1, 2, 3] y_train = [1, 2, 3] W = tf.Variable(tf.random_normal([1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias') # hypothesis Wx + b hypothesis = W * x_train + b # cost / loss function cost = tf.reduce_mean(tf.square(hypothesis - y_train)) # Minimize optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost) # Launch the graph in a session sess = tf.Session() # Initializes global variables in the graph sess.run(tf.global_variables_initializer()) # Fit the line for step in rage(2001): sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W), sess.run(b))
Placeholders를 이용한 구현
# Now we can use X and Y in place of x_data and y_data # placeholders for a tensor that will be always fed using feed_dict X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) # hypothesis Wx + b # cost / loss function # Minimize sess = tf.Session() for step in range(2001): cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], feed_dict={X : [1, 2, 3], Y : [1, 2, 3]}) if step % 20 == 0 : print(step, cost_val, W_val, b_val)
도식화
! tf.Variable
- class이다. 함수인줄...
- 사용 전 초기화 해줘야 한다.
(1) 각각 (2) 전체
W.initializer tf.global_variable_initializer()
- __init__ method
> 수 많은 Argument 중 내가 신경써야 할거면 신경쓰자 (내 생각임...)
__init__(
initial_value=None,
name=None,
dtype=None
)
! tf.reduce_mean
- reduce_mean(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None
)
! tf.GradientDescentOptimizer
- 얘도 class이다.
- __init__(
learning_rate,
use_locking=False,
name='GradientDescent'
)
- minimize(
loss,
...
...
...
)
필요할 때 tensorflow 홈페이지 찾아보자~
'Season.1 > BOOK] Deep learning from scratch' 카테고리의 다른 글
딥러닝의 기초 A to Z _ 3 _ 실습 (0) | 2018.01.11 |
---|---|
딥러닝의 기초 A to Z - 3 (0) | 2018.01.11 |
딥러닝의 기초 A to Z _ 2 (0) | 2018.01.03 |
딥러닝의 기초 A to Z _ 1 _ 실습 (0) | 2018.01.02 |
딥러닝의 기초 A to Z _ 개요 (0) | 2018.01.02 |