Error when calling global

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:programdataanaconda3libsite-packagestensorflowpythonclientsession.py in _do_call(self, fn, *args) 1138 try: -> 1139 return fn(*args) 1140 except errors.OpError as e:

c:programdataanaconda3libsite-packagestensorflowpythonclientsession.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:programdataanaconda3libcontextlib.py in exit (self, type, value, traceback) 88 try: ---> 89 next(self.gen) 90 except StopIteration:

c:programdataanaconda3libsite-packagestensorflowpythonframeworkerrors_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:programdataanaconda3libsite-packagestensorflowpythonclientsession.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:programdataanaconda3libsite-packagestensorflowpythonclientsession.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:programdataanaconda3libsite-packagestensorflowpythonclientsession.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:programdataanaconda3libsite-packagestensorflowpythonclientsession.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):


In your first piece of code you don't use the tf.Variable(10) so it doesn't matter if it hasn't been initialized, while in your second piece of code you do try to evaluate it, and so TensorFlow complains that it hasn't been initialized.

In your code the Variable is defined (when the func method is called) after the initialization is done.

def func(a):
    b = tf.Variable(10) * a
    return b

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) # At this stage the TensorFlow graph is empty
    print(sess.run(func(tf.constant(4)))) # The func method is called, it defines the `tf.Variable(10)`
                                          # and tries to evaluate `b` which depends on it.

In the following piece, the tf.Variable(10) is defined before the initializing op is run.

b = func(tf.constant(4)) # tf.Variable(10) is defined
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) # tf.Variable(10) is initialized
    print(sess.run(b))
链接地址: http://www.djcxy.com/p/32064.html

上一篇: 什么是学习人工神经网络的好资源?

下一篇: 调用全局时出错