Created
February 2, 2018 16:50
-
-
Save scmmishra/569fb56dbf49edb9d4dc007744c5d1b2 to your computer and use it in GitHub Desktop.
linear_regression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
W = tf.Variable(tf.zeros([2, 1]), name="weights") | |
b = tf.Variable(0., name="bias") | |
def inference(X): | |
return tf.matmul(X, W) + b | |
def loss(X,Y): | |
Y_predicted = inference(X) | |
return tf.reduce_sum(tf.squared_difference(Y, Y_predicted)) | |
def inputs(): | |
weight_age = [[84, 46], [73, 20], [65, 52], [70, 30], [76, 57], [69, 25], [63, 28], [72, 36], [79, 57], [75, 44], [27, 24], [89, 31], [65, 52], [57, 23], [59, 60], [69, 48], [60, 34], [79, 51], [75, 50], [82, 34], [59, 46], [67, 23], [85, 37], [55, 40], [63, 30]] | |
blood_fat_content = [354, 190, 405, 263, 451, 302, 288, 385, 402, 365, 209, 290, 346, 254, 395, 434, 220, 374, 308, 220, 311, 181, 274, 303, 244] | |
return tf.to_float(weight_age), tf.to_float(blood_fat_content) | |
def train(total_loss): | |
learning_rate = 0.00000001 | |
return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss) | |
def evaluates(sess, X, Y): | |
print(sess.run(inference([80. ,25.]))) | |
print(sess.run(inference([65. ,25.]))) | |
with tf.Session() as sess: | |
tf.global_variables_initializer().run() | |
X, Y = inputs() | |
print("X Rank: ",tf.rank(X)) | |
print("Y Rank: ",tf.rank(Y)) | |
total_loss = loss(X, Y) | |
train_op = train(total_loss) | |
coord = tf.train.Coordinator() | |
threads = tf.train.start_queue_runners(sess=sess, coord=coord) | |
training_steps = 1000 | |
for step in range(training_steps): | |
sess.run([train_op]) | |
if step%10 == 0: | |
print("loss: ", sess.run([total_loss])) | |
evaluates(sess, X, Y) | |
coord.request_stop() | |
coord.join(threads) | |
sess.close() | |
# Output | |
''' | |
loss: [5343514.5] | |
loss: [5343446.5] | |
loss: [5343380.0] | |
loss: [5343314.5] | |
loss: [5343250.0] | |
loss: [5343187.5] | |
loss: [5343128.0] | |
loss: [5343067.5] | |
loss: [5343010.5] | |
loss: [5342952.5] | |
loss: [5342897.5] | |
loss: [5342843.0] | |
loss: [5342791.5] | |
loss: [5342738.5] | |
loss: [5342688.5] | |
loss: [5342638.5] | |
loss: [5342589.5] | |
loss: [5342543.0] | |
loss: [5342496.5] | |
loss: [5342449.5] | |
loss: [5342406.0] | |
loss: [5342363.0] | |
loss: [5342319.5] | |
loss: [5342277.5] | |
loss: [5342236.0] | |
loss: [5342197.5] | |
loss: [5342157.0] | |
loss: [5342118.5] | |
loss: [5342080.5] | |
loss: [5342043.0] | |
loss: [5342007.5] | |
Traceback (most recent call last): | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 671, in _call_cpp_shape_fn_impl | |
input_tensors_as_shapes, status) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/contextlib.py", line 88, in __exit__ | |
next(self.gen) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status | |
pywrap_tensorflow.TF_GetCode(status)) | |
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 2 but is rank 1 for 'MatMul_1' (op: 'MatMul') with input shapes: [2], [2,1]. | |
During handling of the above exception, another exception occurred: | |
Traceback (most recent call last): | |
File "linear_regression.py", line 42, in <module> | |
evaluates(sess, X, Y) | |
File "linear_regression.py", line 23, in evaluates | |
print(sess.run(inference([80. ,25.]))) | |
File "linear_regression.py", line 7, in inference | |
return tf.matmul(X, W) + b | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 1801, in matmul | |
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1263, in _mat_mul | |
transpose_b=transpose_b, name=name) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op | |
op_def=op_def) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2338, in create_op | |
set_shapes_for_outputs(ret) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1719, in set_shapes_for_outputs | |
shapes = shape_func(op) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1669, in call_with_requiring | |
return call_cpp_shape_fn(op, require_shape_fn=True) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn | |
debug_python_shape_fn, require_shape_fn) | |
File "/Users/shivammishra/miniconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl | |
raise ValueError(err.message) | |
ValueError: Shape must be rank 2 but is rank 1 for 'MatMul_1' (op: 'MatMul') with input shapes: [2], [2,1]. | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment