5. 파이썬
[텐서플로] 경사하강법 그래프 보기
패스트코드블로그
2020. 5. 16. 12:25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import matplotlib.pyplot as plt
class GradientDescent:
@staticmethod
def execute():
X = [1., 2., 3.]
Y = [1., 2., 3.]
m = len(X)
W = tf.placeholder(tf.float32)
hypothesis = tf.multiply(X, W)
cost = tf.reduce_mean(tf.pow(hypothesis - Y, 2)) / m
W_val = []
cost_val = []
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(-30, 50):
W_val.append(i * 0.1)
cost_val.append(sess.run(cost, {W: i * 0.1}))
plt.plot(W_val, cost_val, 'ro')
plt.ylabel('COST')
plt.xlabel('W')
plt.savefig('./data/result.svg')
print('경사하강법 종료')
return '종료'
if __name__ == '__main__':
GradientDescent.execute()
|
cs |