forked from GoodLuckDay/inflearn-machin_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLlab02-1.py
24 lines (19 loc) · 904 Bytes
/
MLlab02-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#gradient알고리즘을 사용하여 최적화된 W와 b값을 구하고 시험
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]})
if step %20 == 0:
print(step, cost_val, W_val, b_val)
print(sess.run(hypothesis, feed_dict={X : [5]}))
print(sess.run(hypothesis, feed_dict={X : [2.5]}))
print(sess.run(hypothesis, feed_dict={X : [1.5, 3.5]}))