在TensorFlow

时间:2018-02-27 15:59:49

标签: python tensorflow

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

效果很好。但是当我用a替换b时,如下所示:

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 \站点包\ tensorflow \ python的\客户端\ session.py   在_do_call(self,fn,* args)1138尝试:    - > 1139返回fn(* args)1140除了errors.OpError为e:

     

C:\ programdata \ anaconda3 \ lib中\站点包\ tensorflow \蟒\客户\ session.py   在_run_fn(session,feed_dict,fetch_list,target_list,options,   run_metadata)1120 feed_dict,   fetch_list,target_list,    - > 1121状态,run_metadata)1122

     退出中的

\ programdata \ anaconda3 \ lib \ contextlib.py(自我,类型,   价值,追溯)        88尝试:   ---> 89下一个(self.gen)        90除StopIteration外:

     

C:\ programdata \ anaconda3 \ lib中\站点包\ tensorflow \蟒\框架\ errors_impl.py   在raise_exception_on_not_ok_status()中       465 compat.as_text(pywrap_tensorflow.TF_Message(status)),    - > 466 pywrap_tensorflow.TF_GetCode(status))       467终于:

     

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(最近一次调用   最后)in()         4使用tf.Session()作为sess:         5 sess.run(tf.global_variables_initializer())   ----> 6打印(sess.run(func(tf.constant(4))))

     

C:\ programdata \ anaconda3 \ lib中\站点包\ tensorflow \蟒\客户\ session.py   在运行中(self,fetches,feed_dict,options,run_metadata)       787尝试:       788 result = self._run(None,fetches,feed_dict,options_ptr,    - > 789 run_metadata_ptr)       790如果run_metadata:       791 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

     

C:\ programdata \ anaconda3 \ lib中\站点包\ tensorflow \蟒\客户\ session.py   在_run中(self,handle,fetches,feed_dict,options,run_metadata)       995如果是final_fetches或final_targets:       996结果= self._do_run(句柄,final_targets,final_fetches,    - > 997 feed_dict_string,options,run_metadata)       998其他:       999结果= []

     

C:\ programdata \ anaconda3 \ lib中\站点包\ tensorflow \蟒\客户\ session.py   在_do_run中(self,handle,target_list,fetch_list,feed_dict,options,   run_metadata)1130如果句柄为无:1131返回   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中\站点包\ tensorflow \蟒\客户\ session.py   在_do_call(self,fn,* args)1150中除了KeyError:1151   通过    - > 1152引发类型(e)(node_def,op,message)1153 1154 def _extend_graph(self):

1 个答案:

答案 0 :(得分: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))