반응형
전체코드(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 = [1, 2, 3, 4, 5]
y_data = [1, 2, 3, 4, 5]
W = tf.Variable(3.0)
b = 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에 가까워지고 있다.
반응형
'Python > Python_tensorflow | 텐서플로우' 카테고리의 다른 글
Cost(비용) 최소의 구체적인 방법 - 텐서플로우(tensorflow) (0) | 2020.05.21 |
---|---|
Gradient descent(경사하강법, 경사하강알고리즘) - 텐서플로우(tensorflow) (0) | 2020.05.20 |
Linear Regression(선형회귀) 구현 및 Cost의 최소화 - 텐서플로우(tensorflow) (0) | 2020.05.19 |
Regression(회귀) - 텐서플로우(tensorflow) (0) | 2020.05.19 |
머신러닝 용어 및 개념 - 텐서플로우(tensorflow) (0) | 2020.05.18 |
댓글