首页 文章

在TensorFlow中调用global_variables_initializer时出错

提问于
浏览
0

我在 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 _do_call中的\ client \ session.py(self,fn,* args)1138尝试: - > 1139返回fn(* args)1140除了errors.OpError为e:c:\ programdata \ anaconda3 \ lib \ site-packages \ tensorflow _run_fn中的\ python \ client \ session.py(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))467 finally:FailedPreconditionError:尝试使用未初始化的值Variable_94 [ [节点:Variable_94 / read = IdentityT = DT_INT32,class = [“loc:@ Variable_94”], device =“/ job:localhost / replica:0 / task:0 / cpu:0”]]处理上述异常时,发生了另一个异常:FailedPreconditionError Traceback(最近一次调用last)in()4,tf.Session()为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(self,fetches,feed_dict,options,run_metadata)787尝试:788结果= 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 _run中的\ session.py(self,handle,fetches,feed_dict,options,run_metadata)995 if final_fetches或final_targets:996 results = self._do_run(handle,final_targets,final_fetches, - > 997 feed_dict_string,options,run_metadata)998 else :999结果= [] 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 \ _do_call(self,fn,* args)1150中的lib \ site-packages \ tensorflow \ python \ client \ session.py除了KeyError:1151 pass - > 1152 raise type(e)(node_def,op,message)1153 1154 def _extend_graph (自):

1 回答

  • 3

    在你的第一段代码中你没有使用 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) 是在运行初始化操作之前定义的 .

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

相关问题