nethsix / gentle_tensorflow Goto Github PK
View Code? Open in Web Editor NEWGentle introduction to Tensorflow
Gentle introduction to Tensorflow
@nethsix Thanks for your tutorial :)
current
steps = 100
learn_rate = 0.0000001
I think it is better to adjust learn_rate or steps in the code because beginner who is not familiar with machine learning concept yet have question about value of W, b.
and what about adding the cost? I think it will show the how the cost is changed
ex) print("Cost: {0}".format(sess.run(cost, feed_dict=feed)))
There is some issue with this file. Training doesnt provide intented results. I see the actual b value is 10.
Hi Khor,
Above all, thanks for your "linear_regression_one_feature.py" as I've finally understood what running linear regression in Tensorflow is after weeks of reading and tweaking.
I've tried to reproduce your code and simply exchange X and Y with data respectively on how often students go out with friends (from 1 to 5) and on profession of the father (from 0 to 4).
Here is the code:
import numpy as np
import tensorflow as tf
import csv
x = [3.0, 2.0, 2.0, 2.0, 2.0, 4.0, 4.0, 2.0, 1.0, 3.0, 2.0, 3.0, 3.0, 2.0, 4.0, 3.0, 2.0, 5.0, 3.0, 1.0, 2.0, 1.0, 4.0, 2.0, 2.0, 2.0, 4.0, 3.0, 5.0, 2.0, 1.0, 2.0, 2.0, 3.0, 1.0, 3.0, 3.0, 2.0, 1.0, 3.0, 3.0, 3.0, 1.0, 3.0, 2.0, 5.0, 2.0, 3.0, 4.0, 3.0, 3.0, 5.0, 4.0, 4.0, 4.0, 2.0, 2.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 4.0, 3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 3.0, 4.0, 2.0, 3.0, 3.0, 3.0, 3.0, 1.0, 3.0, 2.0, 2.0, 4.0, 3.0, 2.0, 4.0, 4.0, 3.0, 2.0, 3.0, 3.0, 5.0, 3.0, 3.0, 4.0, 2.0, 3.0, 5.0, 4.0, 5.0, 5.0, 3.0, 3.0, 5.0, 4.0, 3.0, 2.0, 3.0, 5.0, 5.0, 3.0, 2.0, 2.0, 2.0, 2.0, 4.0, 3.0, 2.0, 4.0, 3.0, 3.0, 4.0, 2.0, 4.0, 4.0, 5.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.0, 4.0, 4.0, 4.0, 3.0, 1.0, 3.0, 1.0, 1.0, 4.0, 3.0, 4.0, 3.0, 1.0, 1.0, 1.0, 1.0, 3.0, 4.0, 1.0, 1.0, 4.0, 1.0, 2.0, 4.0, 3.0, 1.0, 2.0, 1.0, 4.0, 1.0, 4.0, 4.0, 3.0, 3.0, 4.0, 2.0, 2.0, 1.0, 4.0, 2.0, 4.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 3.0, 1.0]
train_x = np.array(x) #printing train_x.dtype gives "float64"
train_x.shape = (394,1) # reshaping to be sure it fits the placeholder
train_y = np.array(y) #printing train_y.dtype gives "float64"
train_y.shape = (394,1) # reshaping to be sure it fits the placeholder
x = tf.placeholder(tf.float64,shape=(None,1)) #specifying placeholder dtype to fit my arrray's one
W = tf.Variable(tf.zeros([1,1],dtype=tf.float64)) #specifying dtype to fit the training set
b = tf.Variable(tf.zeros([1],dtype=tf.float64))
product = tf.matmul(x,W)
y = product + b
y_ = tf.placeholder(tf.float64,shape=(None,1)) #specifying placeholder dtype to fit my arrray's one
cost = tf.reduce_mean(tf.square(y_-y))
gradient = tf.train.GradientDescentOptimizer(0.001).minimize(cost)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
epochs = 1000
for i in range(epochs):
feed = {x:train_x,y:train_y}
sess.run(gradient,feed_dict=feed)
if epoch % 50 == 0:
print("After {} iteration".format(i),"W= {}".format(sess.run(W),\
"b= {}".format(sess.run(b))))
print("Cost= {}".format(sess.run(cost,feed_dict=feed)))
The Traceback highlights "You must feed a value for placeholder tensor 'Placeholder_1' with dtype double and shape [?,1]" while placeholders' dtype and shape fit with those of the array. Here the full message:
2017-11-10 18:58:41.632505: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Traceback (most recent call last):
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\client\session.py", line 1323, in _do_call
return fn(*args)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\client\session.py", line 1302, in _run_fn
status, run_metadata)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in exit
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype double and shape [?,1]
[[Node: Placeholder_1 = Placeholderdtype=DT_DOUBLE, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Grégory\eclipse-workspace\tensorflow_project\Udacity_1.py", line 42, in
sess.run(gradient,feed_dict=feed)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
run_metadata_ptr)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\client\session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\client\session.py", line 1317, in _do_run
options, run_metadata)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\client\session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype double and shape [?,1]
[[Node: Placeholder_1 = Placeholderdtype=DT_DOUBLE, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
Caused by op 'Placeholder_1', defined at:
File "C:\Users\Grégory\eclipse-workspace\tensorflow_project\Udacity_1.py", line 29, in
y_ = tf.placeholder(tf.float64,shape=(None,1)) #specifying dtype to fit the training set
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1599, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 3090, in _placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\python_for_tensorflow\tensorflow_env\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in init
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype double and shape [?,1]
[[Node: Placeholder_1 = Placeholderdtype=DT_DOUBLE, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
Do you have any idea on how to fix this?
Thanks a lot!
Greg
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.