"TensorFlow 선형회귀"의 두 판 사이의 차이

(새 문서: ==개요== ;GradientDescentOptimizer <source lang='python'> import tensorflow as tf x_train = [1,2,3] y_train = [1,2,3] W = tf.Variable(tf.random_normal([1]), name='weight') b = t...)
 
2번째 줄: 2번째 줄:
;GradientDescentOptimizer  
;GradientDescentOptimizer  


==예제 1==
<source lang='python'>
<source lang='python'>
import tensorflow as tf
import tensorflow as tf
37번째 줄: 38번째 줄:
1800 1.4661e-05 [ 0.99555296] [ 0.01010923]
1800 1.4661e-05 [ 0.99555296] [ 0.01010923]
2000 5.59837e-06 [ 0.99725193] [ 0.00624691]
2000 5.59837e-06 [ 0.99725193] [ 0.00624691]
</source>
==예제 2==
<source lang='python'>
import tensorflow as tf
x_data = [[1,1],[2,2],[3,3]]
y_data = [[1],[2],[3]]
X = tf.placeholder(tf.float32, shape=[None,2])
Y = tf.placeholder(tf.float32, shape=[None,1])
W = tf.Variable(tf.random_normal([2,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
hypothesis = tf.matmul(X,W) + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(2001):
        cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val, W_val, b_val)
</source>
{{소스헤더|실행결과}}
<source lang='text'>
0 0.630719 [[ 1.96448588]
[-1.46632338]] [ 0.50560945]
200 0.0159201 [[ 2.1430881 ]
[-1.28772187]] [ 0.33275333]
400 0.00558863 [[ 2.17255759]
[-1.25825167]] [ 0.19715267]
600 0.00196186 [[ 2.19001746]
[-1.24079025]] [ 0.11681108]
800 0.000688694 [[ 2.20036244]
[-1.23044467]] [ 0.06920907]
1000 0.000241762 [[ 2.20649195]
[-1.22431529]] [ 0.04100555]
1200 8.48685e-05 [[ 2.21012259]
[-1.22068286]] [ 0.02429538]
1400 2.97948e-05 [[ 2.21227384]
[-1.21853077]] [ 0.01439506]
1600 1.04602e-05 [[ 2.21354699]
[-1.21725464]] [ 0.00852949]
1800 3.67325e-06 [[ 2.2142992 ]
[-1.21649647]] [ 0.00505449]
2000 1.29013e-06 [[ 2.21474481]
[-1.21604705]] [ 0.0029954]
</source>
</source>


[[분류: TensorFlow]]
[[분류: TensorFlow]]

2017년 12월 6일 (수) 16:44 판

1 개요

GradientDescentOptimizer

2 예제 1

import tensorflow as tf

x_train = [1,2,3]
y_train = [1,2,3]

W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = x_train * W + b

cost = tf.reduce_mean(tf.square(hypothesis - y_train))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

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

for step in range(2001):
    sess.run(train)
    if step % 200 == 0:
        print(step, sess.run(cost), sess.run(W), sess.run(b))
실행결과
0 10.3343 [-0.58280057] [ 0.22211805]
200 0.0324351 [ 0.79082805] [ 0.47549695]
400 0.0123853 [ 0.87074447] [ 0.29382828]
600 0.00472931 [ 0.92012799] [ 0.18156797]
800 0.00180589 [ 0.9506439] [ 0.11219799]
1000 0.000689574 [ 0.96950084] [ 0.06933162]
1200 0.000263314 [ 0.98115343] [ 0.04284282]
1400 0.000100546 [ 0.98835391] [ 0.02647423]
1600 3.83936e-05 [ 0.99280345] [ 0.01635946]
1800 1.4661e-05 [ 0.99555296] [ 0.01010923]
2000 5.59837e-06 [ 0.99725193] [ 0.00624691]

3 예제 2

import tensorflow as tf

x_data = [[1,1],[2,2],[3,3]]
y_data = [[1],[2],[3]]
X = tf.placeholder(tf.float32, shape=[None,2])
Y = tf.placeholder(tf.float32, shape=[None,1])
W = tf.Variable(tf.random_normal([2,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.matmul(X,W) + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(2001):
        cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val, W_val, b_val)
실행결과
0 0.630719 [[ 1.96448588]
 [-1.46632338]] [ 0.50560945]
200 0.0159201 [[ 2.1430881 ]
 [-1.28772187]] [ 0.33275333]
400 0.00558863 [[ 2.17255759]
 [-1.25825167]] [ 0.19715267]
600 0.00196186 [[ 2.19001746]
 [-1.24079025]] [ 0.11681108]
800 0.000688694 [[ 2.20036244]
 [-1.23044467]] [ 0.06920907]
1000 0.000241762 [[ 2.20649195]
 [-1.22431529]] [ 0.04100555]
1200 8.48685e-05 [[ 2.21012259]
 [-1.22068286]] [ 0.02429538]
1400 2.97948e-05 [[ 2.21227384]
 [-1.21853077]] [ 0.01439506]
1600 1.04602e-05 [[ 2.21354699]
 [-1.21725464]] [ 0.00852949]
1800 3.67325e-06 [[ 2.2142992 ]
 [-1.21649647]] [ 0.00505449]
2000 1.29013e-06 [[ 2.21474481]
 [-1.21604705]] [ 0.0029954]
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}