반응형
경사 하강 알고리즘, 경사 하강법
Cost(W,b)의 값을 최소화 하는 W,b값을 구함.
import tensorflow as tf
x_data = [?, ?, ?, ?, ?]
y_data = [?, ?, ?, ?, ?]
with tf.GradientTape() as tape:
hypo = W * x_data + b
cost = tf.reduce_mean(tf.square(hypo - y_data))
W_grad, b_grad = tape.gradient(cost, [W,b])
with tf.GradientTape() as tape:
- Gradient(기울기) Tape(기록)
- 이후 tape에 gradient메서드를 실행하여 cost에 대한 W와b의 편미분 값을 tuple로 전달.
learning rate
기울기 값에대한 변화량을 얼마만큼 기여할것인가
보통 0.01, 0.0001등 작은값이 사용됨.
tf.Variable().assign_sub()
변수에 assign_sub()만큼의 값을 뺀 나머지를 할당
더보기
A.assign_sub(B)
A = A - B
A -= B
tensorflow에서 사용할수 없는 연산이기때문에
assign_sub메서드 사용.
코드
import tensorflow as tf
learning_rate = 0.01
x_data = [?, ?, ?, ?, ?]
y_data = [?, ?, ?, ?, ?]
# step 시작 #
with tf.GradientTape() as tape:
hypo = W * x_data + b
cost = tf.reduce_mean(tf.square(hypo - y_data))
W_grad, b_grad = tape.gradient(cost, [W,b])
W.assign_sub(learning_rate * W_grad)
b.assign_sub(learning_rate * b_grad)
# step 끝 #
반응형
'Python > Python_tensorflow | 텐서플로우' 카테고리의 다른 글
Cost(비용) 최소의 구체적인 방법 - 텐서플로우(tensorflow) (0) | 2020.05.21 |
---|---|
Parameter(W,b) Update - 텐서플로우(tensorflow) (0) | 2020.05.21 |
Linear Regression(선형회귀) 구현 및 Cost의 최소화 - 텐서플로우(tensorflow) (0) | 2020.05.19 |
Regression(회귀) - 텐서플로우(tensorflow) (0) | 2020.05.19 |
머신러닝 용어 및 개념 - 텐서플로우(tensorflow) (0) | 2020.05.18 |
댓글