티스토리 뷰

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
import tensorflow as tf
 
x_data = [1,2,3]
y_data = [1,2,3]
 
= tf.Variable(tf.random_normal([1]), name = 'weight')
 
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
 
hypothesis = W * X
 
cost = tf.reduce_sum(tf.square(hypothesis -Y))
 
learning_rate = 0.1
gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_rate * gradient
update = W.assign(descent) 
 
sess = tf.Session()
 
sess.run(tf.global_variables_initializer())
 
for step in range(21):
    sess.run(update, feed_dict={X: x_data, Y: y_data})
    print(step, sess.run(cost, feed_dict{X: x_data, Y: y_data}), sess.run(W))
 
 
cs


위 코드는 경사하강법 알고리즘을 구현한 예제이다.



1
2
optimizer = tf.train.GradientDesecdentOptimizer(learning rate = 0.1)
train = optimizer.minimize(cost)

cs



물론 이렇게 쉽게 


1
2
3
4
learning_rate = 0.1
gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_rate * gradient
update = W.assign(descent)
cs


이거 대신 사용 할 수 있다

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/05   »
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 30 31
글 보관함