Python FailedPremissionError:尝试在Tensorflow中使用未初始化

Python FailedPremissionError:尝试在Tensorflow中使用未初始化,python,pandas,classification,tensorflow,Python,Pandas,Classification,Tensorflow,我正在使用一种“怪异”的格式上传数据。我想对数据使用NumPy或pandas格式,以便与scikit学习结果进行比较 我从Kaggle获取数字识别数据: 下面是TensorFlow教程中的代码(效果很好): 在这里,我读取数据,去掉目标变量,并将数据拆分为测试和训练数据集(这一切都很好): 以下是全部错误: --------------------------------------------------------------------------- FailedPreconditionE

我正在使用一种“怪异”的格式上传数据。我想对数据使用NumPy或pandas格式,以便与scikit学习结果进行比较

我从Kaggle获取数字识别数据:

下面是TensorFlow教程中的代码(效果很好):

在这里,我读取数据,去掉目标变量,并将数据拆分为测试和训练数据集(这一切都很好):

以下是全部错误:

---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-82-967faab7d494> in <module>()
      4     Target_batch = np.array(OHTarget[i*50:i*50+50].values)
      5     Target_batch = np.multiply(Target_batch, 1.0 / 255.0)
----> 6     train_step.run(feed_dict={x: batch, y_: Target_batch})

/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in run(self, feed_dict, session)
   1265         none, the default session will be used.
   1266     """
-> 1267     _run_using_default_session(self, feed_dict, self.graph, session)
   1268
   1269

/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _run_using_default_session(operation, feed_dict, graph, session)
   2761                        "the operation's graph is different from the session's "
   2762                        "graph.")
-> 2763   session.run(operation, feed_dict)
   2764
   2765

/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict)
    343
    344     # Run request and get response.
--> 345     results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
    346
    347     # User may have fetched the same tensor multiple times, but we

/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, target_list, fetch_list, feed_dict)
    417         # pylint: disable=protected-access
    418         raise errors._make_specific_exception(node_def, op, e.error_message,
--> 419                                               e.code)
    420         # pylint: enable=protected-access
    421       raise e_type, e_value, e_traceback

FailedPreconditionError: Attempting to use uninitialized value Variable_1
     [[Node: gradients/add_grad/Shape_1 = Shape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_1)]]
Caused by op u'gradients/add_grad/Shape_1', defined at:
  File "/Users/user32/anaconda/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    ...........

...which was originally created as op u'add', defined at:
  File "/Users/user32/anaconda/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
[elided 17 identical lines from previous traceback]
  File "/Users/user32/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-45-59183d86e462>", line 1, in <module>
    y = tf.nn.softmax(tf.matmul(x,W) + b)
  File "/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 403, in binary_op_wrapper
    return func(x, y, name=name)
  File "/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 44, in add
    return _op_def_lib.apply_op("Add", x=x, y=y, name=name)
  File "/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op
    op_def=op_def)
  File "/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1710, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 988, in __init__
    self._traceback = _extract_stack()
---------------------------------------------------------------------------
FailedPremissionError回溯(最近一次调用上次)
在()
4 Target_batch=np.array(OHTarget[i*50:i*50+50].值)
5目标批次=np.乘法(目标批次,1.0/255.0)
---->6训练步骤运行(feed_dict={x:batch,y_:Target_batch})
/运行中的Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc(self,feed_dict,session)
1265无,将使用默认会话。
1266     """
->1267使用默认会话(self、feed、dict、self.graph、session)运行
1268
1269
/Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc在使用默认会话运行时(操作、提要、图表、会话)
2761“操作的图形与会话的图形不同”
2762“图形”。)
->2763会话。运行(操作,提要)
2764
2765
/运行中的Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc(self、fetches、feed_dict)
343
344#运行请求并获取响应。
-->345 results=self.\u do\u run(目标列表、唯一获取目标、提要目录字符串)
346
347#用户可能多次获取相同的张量,但我们
/运行中的Users/user32/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.pyc(self、target、fetch、dict)
417#pylint:disable=受保护访问
418引发错误。生成特定异常(节点定义、操作、e.错误消息、,
-->419(电子代码)
420#pylint:enable=受保护访问
421提高e_类型、e_值、e_回溯
FailedPremissionError:尝试使用未初始化的值变量_1
[[Node:gradients/add_grad/Shape_1=Shape[T=DT_FLOAT,_device=“/job:localhost/replica:0/task:0/cpu:0”](变量_1)]]
由op u“梯度/添加梯度/形状1”引起,定义于:
文件“/Users/user32/anaconda/lib/python2.7/runpy.py”,第162行,在运行模块中
...........
…最初创建为op u“add”,定义为:
文件“/Users/user32/anaconda/lib/python2.7/runpy.py”,第162行,在运行模块中
“\uuuuu main\uuuuuuuuuuuuuuuuuuuuuuuuu”,fname,loader,pkg\u name)
[从以前的回溯中删除了17条相同的行]
文件“/Users/user32/anaconda/lib/python2.7/site packages/IPython/core/interactiveshell.py”,第3066行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
y=tf.nn.softmax(tf.matmul(x,W)+b)
文件“/Users/user32/anaconda/lib/python2.7/site packages/tensorflow/python/ops/math_ops.py”,第403行,二进制_op_包装
return func(x,y,name=name)
文件“/Users/user32/anaconda/lib/python2.7/site packages/tensorflow/python/ops/gen_math_ops.py”,第44行,添加
返回_op_def_lib.apply_op(“Add”,x=x,y=y,name=name)
文件“/Users/user32/anaconda/lib/python2.7/site packages/tensorflow/python/ops/op_def_library.py”,第633行,在apply_op
op_def=op_def)
文件“/Users/user32/anaconda/lib/python2.7/site packages/tensorflow/python/framework/ops.py”,第1710行,在create_op中
初始值=自身值。\默认值\初始值,初始值=初始值)
文件“/Users/user32/anaconda/lib/python2.7/site packages/tensorflow/python/framework/ops.py”,第988行,在__
self.\u traceback=\u extract\u stack()

关于如何解决这个问题,有什么想法吗?

出现
FailedPremissionError
是因为程序试图在初始化变量之前读取变量(名为
“variable_1”
)。在TensorFlow中,所有变量都必须通过运行其“初始值设定项”来显式初始化“行动。为了方便起见,可以在训练循环之前执行以下语句,在当前会话中运行所有变量初始值设定项:

tf.initialize_all_variables().run()
请注意,此答案假设,与问题中一样,您使用的是
tf.InteractiveSession
,它允许您在不指定会话的情况下运行操作。对于非交互式使用,更常见的是使用
tf.Session
,并按如下方式初始化:

init_op = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init_op)

tf.initialize\u all\u variables()
已弃用。而是使用以下命令初始化tensorflow变量:

tf.global_variables_initializer()
一个常见的示例用法是:

with tf.Session() as sess:
     sess.run(tf.global_variables_initializer())

根据官方文件

此异常通常在运行以下操作时引发: 在初始化tf.变量之前读取该变量

在您的情况下,该错误甚至解释了哪些变量未初始化:
尝试使用未初始化的值变量_1
。其中一个TF教程解释了很多关于变量及其

基本上,要初始化变量,您有3个选项:

  • 使用初始化所有全局变量
  • 初始化您关心的变量。请注意,您可以使用此函数模拟全局变量初始值设定项:
    tf.variable\u初始值设定项(tf.global\u variables())
  • 仅使用
    var\u名称初始化一个变量。初始值设定项
我几乎总是使用第一种方法。请记住,您应该将其放在会话运行中。所以你会得到这样的结果:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

如果您对变量的更多信息感到好奇,请阅读了解如何报告未初始化变量,并检查
是否已初始化
tf.global_variables_initializer()
with tf.Session() as sess:
     sess.run(tf.global_variables_initializer())
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
with sess.as_default():
    result = compute_fn([seed_input,1])
FailedPreconditionError: Attempting to use uninitialized value block1_conv2/bias
     [[Node: block1_conv2/bias/read = Identity[T=DT_FLOAT, _class=["loc:@block1_conv2/bias"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](block1_conv2/bias)]]
     [[Node: predictions/Softmax/_7 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_168_predictions/Softmax", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
init_op = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init_op)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
var_ab = tf.variables_initializer([a, b], name="a_and_b")
with tf.Session() as sess:
    sess.run(var_ab)
# create variable W as 784 x 10 tensor, filled with zeros
W = tf.Variable(tf.zeros([784,10])) with tf.Session() as sess:
    sess.run(W.initializer)
sess = tf.Session()
sess.run(tf.local_variables_initializer())
tf.compat.v1.global_variables_initializer
x1 = tf.Variable(5)
y1 = tf.Variable(3)

z1 = x1 + y1

init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    init.run()
    print(sess.run(z1))