] Tensorflow로 간단한 Linear Regression 구현(2)
본문 바로가기

머신러닝/텐서플로우

Tensorflow로 간단한 Linear Regression 구현(2)

(1)에서의 코드와는 약간 다르다.

tf.placeholder를 사용해서 구현했다.

placeholder사용시  원하는 데이터를 feed_dict를 통해서 바로 넣어줄 수 있다는 장점이 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import tensorflow as tf
= tf.Variable(tf.random_normal([1]), name = 'weight')
= tf.Variable(tf.random_normal([1]), name= 'bias')
 
= tf.placeholder(tf.float32, shape=[None])
= tf.placeholder(tf.float32, shape=[None])
 
hypothesis = X * W + b
 
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
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(2001):
    cost_val, W_val, b_val, _= sess.run([cost, W, b, train]
                    , feed_dict = { X : [12345], 
                      Y : [2.13.14.15.16.1]})
#fee_dict를 통해서 값을 넣어준다.
    if step % 20 == 0 :
        print(step, cost_val, W_val, b_val)
cs