解决tensorflow由于未初始化变量而导致的错误问题


Posted in Python onJanuary 06, 2020

我写的这个程序

import tensorflow as tf

sess=tf.InteractiveSession()
x=tf.Variable([1.0,2.0])
a=tf.constant([3.0,3.0])
x.initializer.run()
sun=tf.div(x,a)
print(sub.eval())
sess.close()

出现了如下所示的错误:

原因是倒数第二行的sub没有初始化,倒数第三行应该是初始化sub的,但是打错了,成了sun,这样后面出现的sub就相当于没有初始化,所以出现了变量没有初始化的错误。

FailedPreconditionError          Traceback (most recent call last)
C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
  1360   try:
-> 1361    return fn(*args)
  1362   except errors.OpError as e:

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
  1339      return tf_session.TF_Run(session, options, feed_dict, fetch_list,
-> 1340                  target_list, status, run_metadata)
  1341 

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
  515       compat.as_text(c_api.TF_Message(self.status.status)),
--> 516       c_api.TF_GetCode(self.status.status))
  517   # Delete the underlying status object from memory otherwise it stays alive

FailedPreconditionError: Attempting to use uninitialized value Variable_1
	 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_1)]]

During handling of the above exception, another exception occurred:

FailedPreconditionError          Traceback (most recent call last)
<ipython-input-3-cac34f40642f> in <module>()
   5 x.initializer.run()
   6 sun=tf.div(x,a)
----> 7 print(sub.eval())
   8 sess.close()

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in eval(self, feed_dict, session)
  654 
  655   """
--> 656   return _eval_using_default_session(self, feed_dict, self.graph, session)
  657 
  658 

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
  4899            "the tensor's graph is different from the session's "
  4900            "graph.")
-> 4901  return session.run(tensors, feed_dict)
  4902 
  4903 

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
  903   try:
  904    result = self._run(None, fetches, feed_dict, options_ptr,
--> 905             run_metadata_ptr)
  906    if run_metadata:
  907     proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
  1135   if final_fetches or final_targets or (handle and feed_dict_tensor):
  1136    results = self._do_run(handle, final_targets, final_fetches,
-> 1137               feed_dict_tensor, options, run_metadata)
  1138   else:
  1139    results = []

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
  1353   if handle is None:
  1354    return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1355              options, run_metadata)
  1356   else:
  1357    return self._do_call(_prun_fn, self._session, handle, feeds, fetches)

C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
  1372     except KeyError:
  1373      pass
-> 1374    raise type(e)(node_def, op, message)
  1375 
  1376  def _extend_graph(self):

FailedPreconditionError: Attempting to use uninitialized value Variable_1
	 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_1)]]

Caused by op 'Variable_1/read', defined at:
 File "C:\Users\SKJ\Anaconda3\lib\runpy.py", line 184, in _run_module_as_main
  "__main__", mod_spec)
 File "C:\Users\SKJ\Anaconda3\lib\runpy.py", line 85, in _run_code
  exec(code, run_globals)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
  app.launch_new_instance()
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\traitlets\config\application.py", line 653, in launch_instance
  app.start()
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
  ioloop.IOLoop.instance().start()
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 162, in start
  super(ZMQIOLoop, self).start()
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tornado\ioloop.py", line 887, in start
  handler_func(fd_obj, events)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
  return fn(*args, **kwargs)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
  self._handle_recv()
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
  self._run_callback(callback, msg)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
  callback(*args, **kwargs)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
  return fn(*args, **kwargs)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
  return self.dispatch_shell(stream, msg)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
  handler(stream, idents, msg)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
  user_expressions, allow_stdin)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
  res = shell.run_cell(code, store_history=store_history, silent=silent)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
  return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
  interactivity=interactivity, compiler=compiler, result=result)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes
  if self.run_code(code, result):
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
 File "<ipython-input-2-69a776ba1e33>", line 3, in <module>
  x=tf.Variable([1.0,2.0])
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 233, in __init__
  constraint=constraint)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 381, in _init_from_args
  self._snapshot = array_ops.identity(self._variable, name="read")
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 131, in identity
  return gen_array_ops.identity(input, name=name)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 2656, in identity
  "Identity", input=input, name=name)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
  op_def=op_def)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3271, in create_op
  op_def=op_def)
 File "C:\Users\SKJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1650, in __init__
  self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_1
	 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_1)]]

以上这篇解决tensorflow由于未初始化变量而导致的错误问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之集合(set)
Sep 24 Python
用Python的线程来解决生产者消费问题的示例
Apr 02 Python
Python中使用items()方法返回字典元素对的教程
May 21 Python
学习python类方法与对象方法
Mar 15 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
Jun 27 Python
用python实现的线程池实例代码
Jan 06 Python
python实现一个简单的ping工具方法
Jan 31 Python
pymongo中聚合查询的使用方法
Mar 22 Python
Pandas之Dropna滤除缺失数据的实现方法
Jun 25 Python
python爬虫之爬取百度音乐的实现方法
Aug 24 Python
使用python向MongoDB插入时间字段的操作
May 18 Python
Python中requests库的用法详解
Jun 05 Python
Python3 全自动更新已安装的模块实现
Jan 06 #Python
tensorflow 只恢复部分模型参数的实例
Jan 06 #Python
春节到了 教你使用python来抢票回家
Jan 06 #Python
Tensorflow 自定义loss的情况下初始化部分变量方式
Jan 06 #Python
在flask中使用python-dotenv+flask-cli自定义命令(推荐)
Jan 05 #Python
通过 Python 和 OpenCV 实现目标数量监控
Jan 05 #Python
python中count函数简单用法
Jan 05 #Python
You might like
php通过正则表达式记取数据来读取xml的方法
2015/03/09 PHP
Laravel框架路由和控制器的绑定操作方法
2018/06/12 PHP
如何利用PHP实现上传图片功能详解
2020/09/24 PHP
关于javascript中的parseInt使用技巧
2009/09/03 Javascript
基于jquery实现的服务器验证控件的启用和禁用代码
2010/04/27 Javascript
jquery右下角弹出提示框示例代码
2013/10/08 Javascript
理解javascript回调函数
2014/12/28 Javascript
javascript实现dom元素可拖动
2016/03/21 Javascript
Javascript基础_标记文字的实现方法
2016/06/14 Javascript
微信小程序 底部导航栏目开发资料
2016/12/05 Javascript
谈谈第三方App接入微信登录 解读
2016/12/27 Javascript
SpringMVC+bootstrap table实例详解
2017/06/02 Javascript
详解vue项目构建与实战
2017/06/27 Javascript
JavaScript实现滑动门效果
2020/01/18 Javascript
[02:58]魔廷新尊——痛苦女王至宝语音台词节选
2020/06/14 DOTA
python抓取某汽车网数据解析html存入excel示例
2013/12/04 Python
python中__call__方法示例分析
2014/10/11 Python
Python实现遍历windows所有窗口并输出窗口标题的方法
2015/03/13 Python
Python中用于转换字母为小写的lower()方法使用简介
2015/05/19 Python
python实现爬取千万淘宝商品的方法
2015/06/30 Python
Python中防止sql注入的方法详解
2017/02/25 Python
Python实现合并两个列表的方法分析
2018/05/28 Python
Python HTML解析模块HTMLParser用法分析【爬虫工具】
2019/04/05 Python
python django model联合主键的例子
2019/08/06 Python
python tkinter基本属性详解
2019/09/16 Python
Python 中pandas索引切片读取数据缺失数据处理问题
2019/10/09 Python
娇韵诗加拿大官网:Clarins加拿大
2017/11/20 全球购物
香港唯港荟酒店预订:Hotel ICON
2018/03/27 全球购物
会计电算一体化个人简历的自我评价
2013/10/15 职场文书
社区工作者演讲稿
2014/05/23 职场文书
篮球赛口号
2014/06/18 职场文书
乡镇党建工作汇报材料
2014/10/27 职场文书
干部年终考核评语
2015/01/04 职场文书
小学班主任自我评价
2015/03/11 职场文书
MySQL系列之二 多实例配置
2021/07/02 MySQL
《我的美好婚事》动画化决定纪念插画与先导PV公开
2022/04/06 日漫