I have the following code in TensorFlow
:
def func(a):
b = tf.Variable(10) * a
return a
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(func(tf.constant(4))))
It works well. But when I substitute a
with b
as follows:
def func(a):
b = tf.Variable(10) * a
return b
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(func(tf.constant(4))))
It gets the following error:
--------------------------------------------------------------------------- FailedPreconditionError Traceback (most recent call last) c:\programdata\anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 1138 try: -> 1139 return fn(*args) 1140 except errors.OpError as e:
c:\programdata\anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 1120 feed_dict, fetch_list, target_list, -> 1121 status, run_metadata) 1122
c:\programdata\anaconda3\lib\contextlib.py in exit(self, type, value, traceback) 88 try: ---> 89 next(self.gen) 90 except StopIteration:
c:\programdata\anaconda3\lib\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:
FailedPreconditionError: Attempting to use uninitialized value Variable_94 [[Node: Variable_94/read = IdentityT=DT_INT32, _class=["loc:@Variable_94"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
During handling of the above exception, another exception occurred:
FailedPreconditionError Traceback (most recent call last) in () 4 with tf.Session() as sess: 5 sess.run(tf.global_variables_initializer()) ----> 6 print(sess.run(func(tf.constant(4))))
c:\programdata\anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata) 787 try: 788 result = self._run(None, fetches, feed_dict, options_ptr, --> 789 run_metadata_ptr) 790 if run_metadata: 791 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
c:\programdata\anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 995 if final_fetches or final_targets: 996 results = self._do_run(handle, final_targets, final_fetches, --> 997 feed_dict_string, options, run_metadata) 998 else: 999 results = []
c:\programdata\anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1130 if handle is None: 1131 return self._do_call(_run_fn, self._session, feed_dict, fetch_list, -> 1132 target_list, options, run_metadata) 1133 else: 1134 return self._do_call(_prun_fn, self._session, handle, feed_dict,
c:\programdata\anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 1150 except KeyError: 1151 pass -> 1152 raise type(e)(node_def, op, message) 1153 1154 def _extend_graph(self):