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

我写的这个程序

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由于未初始化变量而导致的错误问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 浅谈Tensorflow由于版本问题出现的几种错误及解决方法

    1.AttributeError: 'module' object has no attribute 'rnn_cell' S:将tf.nn.rnn_cell替换为tf.contrib.rnn 2.TypeError: Expected int32, got list containing Tensors of type '_Message' instead. S:由于tf.concat的问题,将tf.concat(1, [conv1, conv2]) 的格式替换为tf.concat( [con

  • 关于tensorflow的几种参数初始化方法小结

    在tensorflow中,经常会遇到参数初始化问题,比如在训练自己的词向量时,需要对原始的embeddigs矩阵进行初始化,更一般的,在全连接神经网络中,每层的权值w也需要进行初始化. tensorlfow中应该有一下几种初始化方法 1. tf.constant_initializer() 常数初始化 2. tf.ones_initializer() 全1初始化 3. tf.zeros_initializer() 全0初始化 4. tf.random_uniform_initializer()

  • Tensorflow 自定义loss的情况下初始化部分变量方式

    一般情况下,tensorflow里面变量初始化过程为: #variables ........... #..................... init = tf.initialize_all_variables() sess.run(init) 这里 tf.initialize_all_variables() 会初始化所有的变量. 实际过程中,假设有a, b, c三个变量,其中a已经被初始化了,只想单独初始化b,c,那么: #variables ... ... init = tf.vari

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

    我写的这个程序 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就相当于

  • 解决tensorflow 释放图,删除变量问题

    问题,在一个程序内构建好了一个图,运行完之后想重新使用这个图进行计算,或者想同时在train完的时候做test,就会提示***变量已存在. 解决办法: 待一个图运行完之后加上tf.reset_default_graph来重置默认的图. 补充知识:tensorflow 张量插入或者删除的办法 我就废话不多说了,大家还是直接看代码吧~ part1 = new_enc_in[:, 0:3 ] part2 = new_enc_in[:, 3:] ###part_new 要插入的张量 new_enc_in

  • tensorflow 初始化未初始化的变量实例

    今日在Stack Overflow上看到一个问如何只初始化未初始化的变量,有人提供了一个函数,特地粘贴过来共大家品鉴: import tensorflow as tf def initialize_uninitialized(sess): global_vars = tf.global_variables() is_not_initialized = sess.run([tf.is_variable_initialized(var) for var in global_vars]) not_in

  • 解决tensorflow模型参数保存和加载的问题

    终于找到bug原因!记一下:还是不熟悉平台的原因造成的! Q:为什么会出现两个模型对象在同一个文件中一起运行,当直接读取他们分开运行时训练出来的模型会出错,而且总是有一个正确,一个读取错误? 而 直接在同一个文件又训练又重新加载模型预测不出错,而且更诡异的是此时用分文件里的对象加载模型不会出错? model.py,里面含有 ModelV 和 ModelP,另外还有 modelP.py 和 modelV.py 分别只含有 ModelP 和 ModeV 这两个对象,先使用 modelP.py 和 m

  • 解决Tensorflow 内存泄露问题

    使用tensorflow进行编程时,经常遇到操作不当,带来的内存泄露问题,这里有一个可以帮助debug问题所在方法: https://stackoverflow.com/questions/51175837/tensorflow-runs-out-of-memory-while-computing-how-to-find-memory-leaks/51183870#51183870 使用tf.Graph.finalize()把运算图变成只读的,从而对图的修改都会报错,从而找到内存泄露的定点. 目

  • 解决TensorFlow训练内存不断增长,进程被杀死问题

    TensorFlow训练时,遇到内存不断增长,最终导致内存不足,进程被杀死. 在这里我不准备对造成这一现象的所有原因进行探讨,只是记录一下我在项目中遇到的这一问题,下面将对我遇到的内存不断增长的原因进行分析. 在TensorFlow中构造图,是将一些op作为节点加入图中,在run之前,是需要构造好一个图的,所以在run的时候,如果run图中不存在的节点,TensorFlow就会将节点加入图中,随着不断的迭代,造成内存不断的增长,从而导致内存不足. 下面举一个很容易发现这个问题的例子: impor

  • 解决Python3.7.0 SSL低版本导致Pip无法使用问题

    终于下决心把python从2.7升到了3.7.懒人安装当然使用Anaconda. 安装成功,编译成功.但是用pip 安装包的时候提示: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 原因是python3.7为了安全性考虑,要求使用openssl 1.0.2之后的版本.但是自带的openssl,版本是1.0.1. 解决办法: 重装一下

  • Java未赋值变量的初始值解析(默认值)

    目录 Java未赋值变量的初始值(默认值) 初始值(默认值) 实例变量(非静态字段) 类变量(静态字段) 本地变量 参数 解决java未赋值变量的默认值问题 如下所示 Java未赋值变量的初始值(默认值) 初始值(默认值) 参考:官方文档 Java程序中,任何变量必须初始化后才能使用. 以下为不同数据类型的默认值. 数据类型 初始值 byte 0 short 0 int 0 long 0L char 'u0000' float 0.0f double 0 boolean false 所有引用类型

  • tensorflow 加载部分变量的实例讲解

    tensorflow模型保存为saver = tf.train.Saver()函数,saver.save()保存模型,代码如下: import tensorflow as tf v1= tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="v1") v2= tf.Variable(tf.zeros([200]), name="v2") saver = tf.train.Saver() with tf

  • golang利用unsafe操作未导出变量-Pointer使用详解

    前言 unsafe.Pointer其实就是类似C的void *,在golang中是用于各种指针相互转换的桥梁.uintptr是golang的内置类型,是能存储指针的整型,uintptr的底层类型是int,它和unsafe.Pointer可相互转换.uintptr和unsafe.Pointer的区别就是:unsafe.Pointer只是单纯的通用指针类型,用于转换不同类型指针,它不可以参与指针运算:而uintptr是用于指针运算的,GC 不把 uintptr 当指针,也就是说 uintptr 无法

随机推荐