본문 바로가기
Python/Python_tensorflow | 텐서플로우

Parameter(W,b) Update - 텐서플로우(tensorflow)

by Pig_CoLa 2020. 5. 21.
SMALL

전체코드(Full Code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import tensorflow as tf
 
learning_rate = 0.01
 
x_data = [12345]
y_data = [12345]
 
= tf.Variable(3.0)
= tf.Variable(1.0)
 
for i in range(100):
    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)
 
        if (i+1) % 10 == 0:
                print('{:5d}|{:10.4f}|{:10.4f}|{:10.6f}'.format(i+1, W.numpy(), b.numpy(), cost))
cs
  • 실행결과

각각 step수, W값, b값, cost값을 나타내며

step이 커짐에 따라 W값은 1에, b값은 0에, cost값은 0에 가까워지고 있다.

LIST

댓글