1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow as tf
x_data = [1.2.3.]
y_data = [1.2.3.]
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
hypothesis = w * x_data + b
cost = tf.reduce_mean(tf.square(hypothesis - y_data)) # Cost를 구한다
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(cost)  #가장 작은 cost륵 가져온다
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(2001):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(cost), sess.run(w), sess.run(b))
 
cs


위 코드는 하드코딩으로 값을 주고 있음으로 placeholder를 사용해서 재사용할 수 있도록 만들어 준다 :


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
import tensorflow as tf
 
x_data = [1.2.3.]
y_data = [1.2.3.]
 
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
 
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
 
hypothesis = w * X + b
cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Cost를 구한다
 
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(cost)  #가장 작은 cost륵 가져온다
 
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
 
for step in range(2001):
    sess.run(train, feed_dict={X:x_data, Y:y_data})
    if step % 20 == 0:
        print(step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(w), sess.run(b))
cs



위 내용을 비교를 하면 :




위 코드에서 Hypothesis를 구하려면 X(x_data) 만 있으면 된다(Y는 필요없음) 

코드 맨 아래줄에 아래와 같이 추가해준다 :


1
print(sess.run(hypothesis, feed_dict={X:5}))
cs


전체 코드 :


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
import tensorflow as tf
 
x_data = [1.2.3.]
y_data = [1.2.3.]
 
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
 
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
 
hypothesis = w * X + b
cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Cost를 구한다
 
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(cost)  #가장 작은 cost륵 가져온다
 
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
 
for step in range(2001):
    sess.run(train, feed_dict={X:x_data, Y:y_data})
    if step % 20 == 0:
        print(step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(w), sess.run(b))
 
print(sess.run(hypothesis, feed_dict={X:5}))
cs





*Cost의 최소화 방법 :


H(x) = Wx + b


Wx 와 H(x) 값이 거의 비슷( = 예측값)


cost(W,b)  = W와 b에 대한 함수 

cost(W) = b가 값이 minor함으로 W에 대한 함수로 변경


cost(W) = 1/m 평균 (Wx - y)제곱


cost가 가장 작을 때 W와 b의 값을 구하기 위해서

Gradient descent algorithm을 사용한다.

Local Minimum에 도달할 때까지 줄여나간다...


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
import tensorflow as tf
import matplotlib.pyplot as plt
tf.set_random_seed(777)  # for reproducibility - seed를 주는 것은 random variable을 일정하게 주기위함

= [123]
= [123]
 
= tf.placeholder(tf.float32)
 
# Our hypothesis for linear model X * W
hypothesis = X * W
 
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
 
# Variables for plotting cost function
W_vals = []
cost_vals = []
 
for i in range(-3050):
    curr_W = i * 0.1 # 촘촘하게 그려주기 위해 0.1을 준다.
    curr_cost = sess.run(cost, feed_dict={W: curr_W})
    W_vals.append(curr_W)
    cost_vals.append(curr_cost)
 
# Show the cost function
plt.plot(W_vals, cost_vals) # X= W_vals, Y=cost_vals
plt.show() # 화면에 보여주기
 
cs


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
52
53
54
55
56
57
58
59
import tensorflow as tf
tf.set_random_seed(777)  # for reproducibility
 
x_data = [123]
y_data = [123]
 
# Try to find values for W and b to compute y_data = W * x_data + b
# We know that W should be 1 and b should be 0
# But let's use TensorFlow to figure it out
= tf.Variable(tf.random_normal([1]), name='weight')
 
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
 
# Our hypothesis for linear model X * W
hypothesis = X * W # Simplified Hypothesis
 
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
# Minimize: Gradient Descent using derivative: W -= learning_rate * derivative
# Gradient Descent Optimizer 대신에 아래와 같은 공식을 사용한다 :
learning_rate = 0.1
gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_rate * gradient
update = W.assign(descent)
 
# 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(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))
 
'''
0 5.81756 [ 1.64462376]
1 1.65477 [ 1.34379935]
2 0.470691 [ 1.18335962]
3 0.133885 [ 1.09779179]
4 0.0380829 [ 1.05215561]
5 0.0108324 [ 1.0278163]
6 0.00308123 [ 1.01483536]
7 0.000876432 [ 1.00791216]
8 0.00024929 [ 1.00421977]
9 7.09082e-05 [ 1.00225055]
10 2.01716e-05 [ 1.00120032]
11 5.73716e-06 [ 1.00064015]
12 1.6319e-06 [ 1.00034142]
13 4.63772e-07 [ 1.00018203]
14 1.31825e-07 [ 1.00009704]
15 3.74738e-08 [ 1.00005174]
16 1.05966e-08 [ 1.00002754]
17 2.99947e-09 [ 1.00001466]
18 8.66635e-10 [ 1.00000787]
19 2.40746e-10 [ 1.00000417]
20 7.02158e-11 [ 1.00000226]
'''
 
cs






+ Recent posts