(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 W = tf.Variable(tf.random_normal([1]), name = 'weight') b = tf.Variable(tf.random_normal([1]), name= 'bias') X = tf.placeholder(tf.float32, shape=[None]) Y = 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 : [1, 2, 3, 4, 5], Y : [2.1, 3.1, 4.1, 5.1, 6.1]}) #fee_dict를 통해서 값을 넣어준다. if step % 20 == 0 : print(step, cost_val, W_val, b_val) | cs |
'머신러닝 > 텐서플로우' 카테고리의 다른 글
Linear Regression의 cost 최소화 알고리즘의 원리 설명 (0) | 2018.07.27 |
---|---|
Tensorflow로 간단한 linear regression 구현(1) (0) | 2018.07.26 |