Gradient descent algorithm 실습
- 코드1을 간략하게 코드2로 표현할 수 있다.
# 코드 1
# learning_rate : w를 얼만큼씩 옮길것인지
learning_rate = 0.1
# cost funtion을 미분한 수식
gradient = tf.reduce_mean((W * X - Y) * X)
# 다음으로 이동 될 w값 -> descent에 저장
descent = W - learning_rate * gradient
# w를 descent로 update
update = W.assign(descent)
# 코드 2
# Minimize: Gradient Descent Magic
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train = optimizer.minimize(cost)
# -*- coding: utf-8 -*-
import tensorflow as tf
# input x, y
x_data = [1, 2, 3]
y_data = [1, 2, 3]
# simplify h(x) -> h(x) = wx
# variable(trainalbe) w
W = tf.Variable(tf.random_normal([1]), name='weight')
# placeholder(변수) X, Y
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# simplify h(x)
hypothesis = X * W
# cost/loss function
cost = tf.reduce_sum(tf.square(hypothesis - Y))
# learning_rate : w를 얼만큼씩 옮길것인지
learning_rate = 0.1
# cost funtion을 미분한 수식
gradient = tf.reduce_mean((W * X - Y) * X)
# 다음으로 이동 될 w값 -> descent에 저장
descent = W - learning_rate * gradient
# w를 descent로 update
update = W.assign(descent)
# sess 생성
sess = tf.Session()
# variable 생성한다.
sess.run(tf.global_variables_initializer())
for step in range(21):
sess.run(update, feed_dict={X: x_data, Y: y_data})
print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W))'IT > 머신러닝' 카테고리의 다른 글
| [에러 슈팅] conda 파이썬 버전 변경시 (conda install python=3.6.6) (0) | 2018.11.06 |
|---|---|
| Linux ubuntu 16.04 한글 설치 후 한영 전환 (0) | 2018.11.05 |
| Tensorflow gpu 설치시 맞춰야 하는 버전 (0) | 2018.11.05 |
| 파이썬 아나콘다(anaconda) 정의 및 설치 (0) | 2018.10.09 |
| Classfication, Object Detection, Semantic or Insatance Segmentation (0) | 2018.10.08 |