Reputation: 2607
I am a newbie in Tensorflow and trying out the code on linear Regression given below:
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
boston=load_boston()
type(boston)
boston.feature_names
bd=pd.DataFrame(data=boston.data,columns=boston.feature_names)
bd['Price']=pd.DataFrame(data=boston.target)
np.random.shuffle(bd.values)
W=tf.Variable(0.0)
b=tf.Variable(0.0)
#print(bd.shape[1])
tf.summary.histogram('Weights', W)
tf.summary.histogram('Biases', b)
dataset_input=bd.iloc[:, 0 : bd.shape[1]-1];
#dataset_input.head(2)
dataset_output=bd.iloc[:, bd.shape[1]-1]
dataset_output=dataset_output.values
dataset_output=dataset_output.reshape((bd.shape[0],1)) #converted (506,) to (506,1) because in pandas
#the shape was not changing and it was needed later in feed_dict
dataset_input=dataset_input.values #only dataset_input is in DataFrame form and converting it into np.ndarray
X=tf.placeholder(tf.float32, shape=(None,bd.shape[1]-1))
Y=tf.placeholder(tf.float32, shape=(None,1))
Y_=W*X+b
print(X.shape)
print(Y.shape)
loss=tf.reduce_mean(tf.square(Y_-Y))
tf.summary.scalar('loss',loss)
optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
init=tf.global_variables_initializer()#tf.global_variables_initializer()#tf.initialize_all_variables()
sess=tf.Session()
sess.run(init)
wb_=[]
with tf.Session() as sess:
summary_merge = tf.summary.merge_all()
writer=tf.summary.FileWriter("Users/ajay/Documents",sess.graph)
epochs=10
sess.run(init)
for i in range(epochs):
s_mer=sess.run(summary_merge,feed_dict={X:dataset_input,Y:dataset_output}) #ERROR________ERROR
sess.run(train,feed_dict={X:dataset_input,Y:dataset_output})
sess.run(loss)
writer.add_summary(s_mer,i)
#tf.summary.histogram(name="loss",values=loss)
if(i%5==0):
print(i, sess.run([W,b]))
wb_.append(sess.run([W,b]))
print(writer.get_logdir())
print(writer.close())
As the loop runs the line :
s_mer=sess.run(summary_merge,feed_dict={X:dataset_input,Y:dataset_output})
is giving me error.
I really don't understand why feed_dict
is present inside the above line.
What would happen if it is not there ?
Also, why are we passing s_mer
into add_summary() ? I didn't understand its role. Can't we just pass "summary_merge" into the add_summary().
Please, explain the role of add_summary().
The error I'm getting in my IPython NB is :
InvalidArgumentError Traceback (most recent call last)
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1038 try:
-> 1039 return fn(*args)
1040 except errors.OpError as e:
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1020 feed_dict, fetch_list, target_list,
-> 1021 status, run_metadata)
1022
~/anaconda3/envs/Tensorflow/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
87 try:
---> 88 next(self.gen)
89 except StopIteration:
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
465 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466 pywrap_tensorflow.TF_GetCode(status))
467 finally:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_56' with dtype float
[[Node: Placeholder_56 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-48-ca71ed6cdb0f> in <module>()
68
69 for i in range(epochs):
---> 70 s_mer=sess.run(summary_merge,feed_dict={X:dataset_input,Y:dataset_output}) #ERROR________ERROR
71 sess.run(train,feed_dict={X:dataset_input,Y:dataset_output})
72
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
980 if final_fetches or final_targets:
981 results = self._do_run(handle, final_targets, final_fetches,
--> 982 feed_dict_string, options, run_metadata)
983 else:
984 results = []
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1030 if handle is None:
1031 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1032 target_list, options, run_metadata)
1033 else:
1034 return self._do_call(_prun_fn, self._session, handle, feed_dict,
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1050 except KeyError:
1051 pass
-> 1052 raise type(e)(node_def, op, message)
1053
1054 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_56' with dtype float
[[Node: Placeholder_56 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder_56', defined at:
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
self.io_loop.start()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 127, in start
self.asyncio_loop.run_forever()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/asyncio/base_events.py", line 421, in run_forever
self._run_once()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/asyncio/base_events.py", line 1431, in _run_once
handle._run()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 117, in _handle_events
handler_func(fileobj, events)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2903, in run_ast_nodes
if self.run_code(code, result):
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-35-a13d7623267d>", line 47, in <module>
X=tf.placeholder(tf.float32, shape=(None,bd.shape[1]-1))
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1507, in placeholder
name=name)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1997, in _placeholder
name=name)
File "/Users/ajay/anaconda3/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/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/ajay/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_56' with dtype float
[[Node: Placeholder_56 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Upvotes: 0
Views: 1983
Reputation: 126
The problem is that variables dataset_input
and dataset_output
are of dtype float64
but you use for placeholders float32
, but they need to be same. dataset_input = np.array(dataset_input, dtype=np.float32)
and dataset_output = np.array(dataset_output, dtype=np.float32)
helps on my computer.
Then when you are calling sess.run(loss)
you need to add feed_dict={X:dataset_input,Y:dataset_output}
as well because it uses X and Y placeholders.
I really don't understand why feed_dict is present inside the above line. What would happen if it is not there ?
You need to use feed_dict to feed data to your placeholders. If you don't provide it with any data then similar errors occurs, because it has no data for your computations.
Hope it helps.
Upvotes: 1