test.csv :
73,80,75,152
93,88,93,185
89,91,90,180
96,98,100,196
73,66,70,142
53,46,55,101
import tensorflow as tf
filename_queue = tf.train.string_input_producer(\
['/Users/sh/Documents/_iPython/TensorFlow/test.csv'], shuffle=False, name='filename_queue')
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the decoded result.
record_defaults = [[0.], [0.],[0.],[0.]]
xy = tf.decode_csv(value, record_defaults = record_defaults)
#collect batches of csv in
train_x_batch, train_y_batch = tf.train.batch([xy[0:-1], xy[-1:]], batch_size=10)
#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])
W = tf.Variable(tf.random_normal([3, 1]), name = 'weight')
b = tf.Variable(tf.random_normal([1]), name = 'bias')
# 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)
# Launch the graph in a sesseion.
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)
for step in range(2001):
x_batch, y_batch = sess.run([train_x_batch, train_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)
RESULT :
2000 Cost: 1.20003
Prediction:
[[ 181.07557678]
[ 195.87307739]
[ 140.93286133]
[ 102.22296906]
[ 153.24723816]
[ 183.31776428]
[ 181.07557678]
[ 195.87307739]
[ 140.93286133]
[ 102.22296906]]
-Reference-
https://github.com/hunkim/DeepLearningZeroToAll/
'Programming > TensorFlow' 카테고리의 다른 글
Logistic Classification on TensorFlow (0) | 2017.06.11 |
---|---|
Multi Variables Linear Regression을 Tensor Flow 에서 구현하기 (0) | 2017.04.09 |
TensorFlow Linear Regression with place holder (0) | 2017.04.08 |
Linear Regression Ex1 (0) | 2017.03.28 |
아나콘다(Anaconda)에 TensorFlow 설치하기 on Mac | Installing TensorFlow at Anaconda on MAC OS X (0) | 2017.03.26 |