본문 바로가기

잡다한 IT/머신러닝 & 딥러닝

03-3 Minimizing Cost tf optimizer

■ 텐서플로우에서 제공하는 Minimizing Cost 함수를 이해한다.


▶ 내장 함수를 사용하여 목적함수를 구함

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)

train = optimizer.minimize(cost)

- tf->train->GradientDescentOptimizer->minimize 와 같은 계통으로 사용을 함

- learning_rate=0.1 과 cost 함수의 인자로 사용하는 위치를 익힌다.


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Lab03-3 Minimizing Cost tf optimizer
 
import tensorflow as tf
 
tf.set_random_seed(777)  # for reproducibility
 
 
# tf Graph Input
 
= [123]
 
= [123]
 
 
# Set wrong model weights
 
= tf.Variable(5.0)
 
 
# Linear model
 
hypothesis = X * W
 
 
# cost/loss function
 
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
 
# Minimize: Gradient Descent Magic
 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
 
train = optimizer.minimize(cost)
 
 
# Launch the graph in a session.
 
sess = tf.Session()
 
# Initializes global variables in the graph.
 
sess.run(tf.global_variables_initializer())
 
 
for step in range(100):
 
    print(step, sess.run(W))
 
    sess.run(train)
 
cs


반응형