解决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之重回函数
Oct 10 Python
Python实现对excel文件列表值进行统计的方法
Jul 25 Python
Python3 操作符重载方法示例
Nov 23 Python
Python跨文件全局变量的实现方法示例
Dec 10 Python
Python操作mongodb的9个步骤
Jun 04 Python
Python初学者常见错误详解
Jul 02 Python
python多进程重复加载的解决方式
Dec 13 Python
Spring @Enable模块驱动原理及使用实例
Jun 23 Python
使用PyWeChatSpy自动回复微信拍一拍功能的实现代码
Jul 02 Python
深入浅析Python代码规范性检测
Jul 31 Python
Python脚本打包成可执行文件过程解析
Oct 20 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 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代码收集表单内容并写入文件的代码
2012/01/29 PHP
PHP读取文件并可支持远程文件的代码分享
2012/10/03 PHP
php表单文件iframe异步上传实例讲解
2017/07/26 PHP
Yii框架学习笔记之应用组件操作示例
2019/11/13 PHP
说明你的Javascript技术很烂的五个原因
2011/04/26 Javascript
js控制的回到页面顶端goTop的代码实现
2013/03/20 Javascript
原生javascript图片自动或手动切换示例附演示源码
2013/09/04 Javascript
浅谈JavaScript Date日期和时间对象
2014/12/29 Javascript
jQuery实现仿百度首页滑动伸缩展开的添加服务效果代码
2015/09/09 Javascript
js 判断各种数据类型的简单方法(推荐)
2016/08/29 Javascript
vue2.0开发实践总结之入门篇
2016/12/06 Javascript
Bootstrap基本组件学习笔记之按钮组(8)
2016/12/07 Javascript
TableSort.js表格排序插件使用方法详解
2017/02/10 Javascript
vue.js模仿京东省市区三级联动的选择组件实例代码
2017/11/22 Javascript
超出JavaScript安全整数限制的数字计算BigInt详解
2018/06/24 Javascript
JavaScript数据结构之栈实例用法
2019/01/18 Javascript
JS字符串与二进制的相互转化实例代码详解
2019/06/28 Javascript
基于jsbarcode 生成条形码并将生成的条码保存至本地+源码
2020/04/27 Javascript
UEditor 自定义图片视频尺寸校验功能的实现代码
2020/10/20 Javascript
Python使用scrapy采集数据过程中放回下载过大页面的方法
2015/04/08 Python
python基于pygame实现响应游戏中事件的方法(附源码)
2015/11/11 Python
详解Python发送邮件实例
2016/01/10 Python
Python中属性和描述符的正确使用
2016/08/23 Python
python下os模块强大的重命名方法renames详解
2017/03/07 Python
Python给你的头像加上圣诞帽
2018/01/04 Python
Python面向对象总结及类与正则表达式详解
2019/04/18 Python
python定时检测无响应进程并重启的实例代码
2019/04/22 Python
python ctypes库2_指定参数类型和返回类型详解
2019/11/19 Python
vscode写python时的代码错误提醒和自动格式化的方法
2020/05/07 Python
Python 如何展开嵌套的序列
2020/08/01 Python
Python 实现RSA加解密文本文件
2020/12/30 Python
贝玲妃英国官网:Benefit英国
2018/02/03 全球购物
伯克斯奥特莱斯:Burkes Outlet
2019/03/30 全球购物
英国在线玫瑰专家:InterRose
2019/12/01 全球购物
2015年审计人员工作总结
2015/05/26 职场文书
python实现简单聊天功能
2021/07/07 Python