调用全局时出错

我在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))))

它运作良好。 但是当我用b代替a时:

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))))

它得到以下错误:

-------------------------------------------------- ------------------------- FailedPreconditionError Traceback(最近调用最后一个)c: programdata anaconda3 lib site-packages tensorflow python 客户端 session.py在_do_call(self,fn,* args)1138尝试: - > 1139返回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 退出 (self,type,value,traceback)88试试:---> 89 next(self.gen)90除了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))终于:

FailedPreconditionError:尝试使用未初始化的值Variable_94 [[Node:Variable_94 / read = IdentityT = DT_INT32,_class = [“loc:@ Variable_94”],_device =“/ job:localhost / replica:0 / task:0 / cpu:0 “]]

在处理上述异常期间,发生了另一个异常:

()中的FailedPreconditionError Traceback(最近一次调用最后一次)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或final_targets:996 results = self._do_run(句柄,final_targets,final_fetches, - > 997 feed_dict_string,options,run_metadata)998 else:999 results = []

在_do_run(self,handle,target_list,fetch_list,feed_dict,options,run_metadata)中,如果handle是None,则返回1130: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,

在_do_call(self,fn,* args)1150中除了KeyError:1151 pass - > 1152 raise type(e)(node_def,op ,留言)1153 1154 def _extend_graph(self):


在你的第一段代码中,你不使用tf.Variable(10)因此它是否没有被初始化并不重要,而在你的第二段代码中你尝试去评估它,所以TensorFlow抱怨它尚未初始化。

在你的代码中, Variable是在完成初始化后定义的(当func方法时)。

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.

在下面的部分中, tf.Variable(10)在初始化op运行之前定义。

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/32063.html

上一篇: Error when calling global

下一篇: Tensorflow freeze model but backpropagate error and update input layer