# Lab 4 Multi-variable linear regression
# https://www.tensorflow.org/programmers_guide/reading_data
# tensorflow include
import tensorflow as tf
# random 난수를 위한 seed지정
tf.set_random_seed(777) # for reproducibility
# file_queue를 생성
filename_queue = tf.train.string_input_producer(
['data-01-test-score.csv'], shuffle=False, name='filename_queue')
# file이 여러개라면 여길 이렇게...
# ['data-01-test-score.csv', 'anotherfile.csv' ...]
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# filename_queue를 text형식으로 읽어오라
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [0.], [0.], [0.], [0.](/8BitsCoding/RobotMentor/wiki/0.],-[0.],-[0.],-[0.)
# 읽어온 파일을 넣을 것인데 기본 데이터형을 float32로 하자([0.])
xy = tf.decode_csv(value, record_defaults=record_defaults)
# 파일을 읽어오는데 csv형으로 파싱(tf.decode_csv)
# collect batches of csv in
train_x_batch, train_y_batch = \
tf.train.batch([xy[0:-1], xy[-1:]], batch_size=10)
# 읽어온 xy를 train_x_batch, train_y_batch에 배치시키라
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 3])
Y = tf.placeholder(tf.float32, shape=[None, 1])
# X, Y 를 runtime에 지정하는 placeholder로 지정
W = tf.Variable(tf.random_normal([3, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# W, b를 랜덤 난수로 지정
# Hypothesis
hypothesis = tf.matmul(X, W) + b
# Simplified cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)
# GradientDescentOptimizer
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# 여기는 받아들이자1
for step in range(2001):
x_batch, y_batch = sess.run([train_x_batch, train_y_batch])
# batch_size 10개씩 받아와서 x_batch, y_batch넣고
cost_val, hy_val, _ = sess.run(
[cost, hypothesis, train], feed_dict={X: x_batch, Y: y_batch})
if step % 10 == 0:
print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
coord.request_stop()
coord.join(threads)
# 여기는 받아들이자2
# Ask my score
print("Your score will be ",
sess.run(hypothesis, feed_dict={X: [100, 70, 101](/8BitsCoding/RobotMentor/wiki/100,-70,-101)}))
print("Other scores will be ",
sess.run(hypothesis, feed_dict={X: [60, 70, 110], [90, 100, 80](/8BitsCoding/RobotMentor/wiki/60,-70,-110],-[90,-100,-80)}))
'''
Your score will be [185.33531](/8BitsCoding/RobotMentor/wiki/185.33531)
Other scores will be [[178.36246]
[177.03687]]
'''