본문 바로가기
AI/Deep Learning

딥러닝: 02. 경사하강법(Gradient descent algorithm)

by KIha_Jung 2019. 7. 30.
minimizing_cost

Gradient descent algorithm


cost(W,b) fnc. 의 최소값을 찾는다.
어떤 점에서든 시작할 수 있다.
조금씩 W를 바꿔 최적의 cost(W,b)를 찾는다.

In [29]:
import tensorflow as tf
import matplotlib.pyplot as plt
In [30]:
X = [1,2,3]
Y = [1,2,3]

W = tf.placeholder(tf.float32)
hypothesis = X * W

cost = tf.reduce_mean(tf.square(hypothesis - Y))

sess = tf.Session()
sess.run(tf.global_variables_initializer())

W_val = []
cost_val = []
for i in range(-30, 50):
    feed_W = i * 0.1
    curr_cost, curr_W = sess.run([cost, W], feed_dict = {W : feed_W})
    W_val.append(curr_W)
    cost_val.append(curr_cost)
    
plt.plot(W_val, cost_val)
plt.show()
In [31]:
X_data = [1,2,3]
Y_data = [1,2,3]

W = tf.Variable(tf.random_normal([1]), name ='weight')
hypothesis = X_data * W
cost = tf.reduce_mean(tf.square(hypothesis - Y_data))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(50):
    print(step, sess.run(W))
    sess.run(train)

    
0 [1.5032543]
1 [1.4562839]
2 [1.4136975]
3 [1.3750857]
4 [1.3400778]
5 [1.3083372]
6 [1.279559]
7 [1.2534668]
8 [1.2298099]
9 [1.2083609]
10 [1.188914]
11 [1.1712819]
12 [1.1552956]
13 [1.1408013]
14 [1.1276598]
15 [1.1157448]
16 [1.104942]
17 [1.0951474]
18 [1.086267]
19 [1.0782154]
20 [1.0709152]
21 [1.0642965]
22 [1.0582955]
23 [1.0528545]
24 [1.0479214]
25 [1.0434488]
26 [1.0393935]
27 [1.0357168]
28 [1.0323832]
29 [1.0293608]
30 [1.0266204]
31 [1.0241358]
32 [1.0218831]
33 [1.0198407]
34 [1.0179889]
35 [1.01631]
36 [1.0147877]
37 [1.0134075]
38 [1.0121561]
39 [1.0110216]
40 [1.009993]
41 [1.0090603]
42 [1.0082146]
43 [1.0074478]
44 [1.0067527]
45 [1.0061225]
46 [1.0055511]
47 [1.005033]
48 [1.0045632]
49 [1.0041373]
In [38]:
X = [1,2,3]
Y = [1,2,3]

W = tf.Variable(5.)
hypothesis = X * W

gradient = tf.reduce_mean((X * W - Y) * X) * 2
cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

gvs = optimizer.compute_gradients(cost)
apply_gradients = optimizer.apply_gradients(gvs)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(50):
    print(step, sess.run([gradient, W]))
    sess.run(apply_gradients)
0 [37.333332, 5.0]
1 [33.84889, 4.6266665]
2 [30.689657, 4.2881775]
3 [27.825287, 3.9812808]
4 [25.228262, 3.703028]
5 [22.873621, 3.4507453]
6 [20.738752, 3.2220092]
7 [18.803137, 3.0146217]
8 [17.048176, 2.8265903]
9 [15.457013, 2.6561086]
10 [14.014359, 2.5015385]
11 [12.706352, 2.361395]
12 [11.520427, 2.2343314]
13 [10.445186, 2.119127]
14 [9.470302, 2.0146751]
15 [8.586407, 1.9199722]
16 [7.785009, 1.8341081]
17 [7.0584083, 1.756258]
18 [6.399624, 1.685674]
19 [5.8023257, 1.6216778]
20 [5.260776, 1.5636545]
21 [4.7697697, 1.5110468]
22 [4.324591, 1.4633491]
23 [3.9209633, 1.4201032]
24 [3.5550067, 1.3808936]
25 [3.2232056, 1.3453435]
26 [2.9223735, 1.3131114]
27 [2.6496189, 1.2838877]
28 [2.4023216, 1.2573916]
29 [2.178105, 1.2333684]
30 [1.9748148, 1.2115873]
31 [1.7904993, 1.1918392]
32 [1.623386, 1.1739342]
33 [1.4718695, 1.1577003]
34 [1.3344955, 1.1429816]
35 [1.2099417, 1.1296366]
36 [1.0970144, 1.1175373]
37 [0.9946267, 1.1065671]
38 [0.90179497, 1.0966209]
39 [0.8176275, 1.087603]
40 [0.7413151, 1.0794266]
41 [0.67212623, 1.0720135]
42 [0.609394, 1.0652922]
43 [0.5525169, 1.0591983]
44 [0.50094914, 1.0536731]
45 [0.45419374, 1.0486636]
46 [0.41180158, 1.0441216]
47 [0.37336722, 1.0400037]
48 [0.33851996, 1.03627]
49 [0.30692515, 1.0328848]
In [ ]:
 

댓글