DeepLearning_Lab02_1 - 8BitsCoding/RobotMentor GitHub Wiki
# Lab 2 Linear Regression
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
# Try to find values for W and b to compute Y = W * X + b
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")
์๋ ๋ถ๋ถ์ด๋ผ ์ค๋ช ์๋ต
# placeholders for a tensor that will be always fed using feed_dict
# See http://stackoverflow.com/questions/36693740/
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
Placeholder : X, Y๊ฐ์ runtime์ ๋ฃ๊ณ ์ถ๋ค
# Our hypothesis is X * W + b
hypothesis = X * W + b
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# optimizer
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
์๋ ๋ถ๋ถ์ด๋ผ ์ค๋ช ์๋ต
# Launch the graph in a session.
with tf.Session() as sess:
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
# Fit the line
for step in range(2001):
_, cost_val, W_val, b_val = sess.run(
[train, cost, W, b], feed_dict={X: [1, 2, 3], Y: [1, 2, 3]}
)
if step % 20 == 0:
print(step, cost_val, W_val, b_val)
# Testing our model
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]}))
ํ์ต๋ฐ์ดํฐ๋ฅผ ์ ์ฝ๊ฒ ์์ ํ ์ ์๊ฒ ๋์๋ค!
# Fit the line with new training data
for step in range(2001):
_, cost_val, W_val, b_val = sess.run(
[train, cost, W, b],
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)
# Testing our model
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]}))