CogandKim
TensorFlow Linear Regression 본문
TensorFlow Linear Regression
이전 포스트
Lineare Regression
Hypothesis
H(x) = Wx+b ( W : weight)
Cost(Loss) Function
cost(W,b) = 1mi=1m(H(xi)-yi)2
Cost function 에서 minimize 를 하기 위해 Gradient Descent Algorithm을 사용 하였습니다.
Source
import tensorflow as tf
# Training Data
x_data = [1.,2.,3.]
y_data = [1.,2.,3.]
# Try to find values for W and b that compute y_data = W * x_data + b
# We know that W should be 1 and b 0
# random_uniform -> random function -1.0 ~ 1.0
W = tf.Variable(tf.random_uniform([1],-1.0,1.0))
b = tf.Variable(tf.random_uniform([1],-1.0,1.0))
# Place Holder
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# Our Hypothesis H(x) = Wx+b
hypothesis = W * X + b
# Simplified cost function Cost(Loss) = 1/m * Sigma (H(xi)-yi)2
# tf.square = x^2
cost = tf.reduce_mean(tf.square(hypothesis-Y))
# Minimize -> Gradient Descent Algorithm
a = tf.Variable(0.1) # Learning rate, alpha
optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)
# Before Starting, initalize the Variables
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in xrange(2001) :
sess.run(train, feed_dict={X:x_data, Y:y_data})
if step % 30 == 0 :
print step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(W), sess.run(b)
# Leanrs best fit is W : [0.1], b : [0.3]
API
tf.Variable() : 변수 생성
https://www.tensorflow.org/versions/r0.9/api_docs/python/state_ops.html#Variable
tf.random_uniform() : Random으로 실수를 뽑아낸다.
https://www.tensorflow.org/versions/r0.9/api_docs/python/constant_op.html#random_uniform
tf.placeholder() : functon 의 Parameter로 넘기지만, 함수를 실행할 때 값을 넘겨 준다.
TensorFlow provides a placeholder operation that must be fed with data on execution. For more info, see the section on Feeding data.
https://www.tensorflow.org/versions/r0.9/api_docs/python/io_ops.html#placeholders
tf.reduce_mean() : element 들의 평균을 합니다.
Computes the mean of elements across dimensions of a tensor.
https://www.tensorflow.org/versions/r0.9/api_docs/python/math_ops.html#reduce_mean
tf.square() : 제곱합니다.
Computes square of x element-wise.
I.e., y=x∗x=x2.
https://www.tensorflow.org/versions/r0.9/api_docs/python/math_ops.html#square
tf.train.GradientDescentoptimizer : Gradient descent algorithm 사용
Optimizer that implements the gradient descent algorithm.
https://www.tensorflow.org/versions/r0.9/api_docs/python/train.html#GradientDescentOptimizer
tf.initialize_all_variables() : 모든 변수 초기화
Returns an Op that initializes all variables.
https://www.tensorflow.org/versions/r0.9/api_docs/python/state_ops.html#initialize_all_variables
결과 : Step 30번을 주기로 step, cost, W, b 값을 출력합니다.
cost가 0이 되는 구간은 대략 690 step으로 W = 1 인 것을 알 수 있습니다.
이상 포스팅을 마칩니다.
출처
Sung Kim http://hunkim.github.io/ml/
TensorFlow https://www.tensorflow.org/
PyCharm https://www.jetbrains.com/pycharm/
'TensorFlow' 카테고리의 다른 글
| TensorFlow Minimize Cost, Gradient Discent Algorithm (0) | 2016.07.08 |
|---|---|
| TensorFlow 설치 및 PyCharm 설치 (0) | 2016.07.06 |