【问题标题】:TensorFlow: Input for a session to compute a variable? FailedPreconditionError: Attempting to use uninitialized value weight_2TensorFlow:计算变量的会话输入? FailedPreconditionError:尝试使用未初始化的值 weight_2
【发布时间】:2016-12-23 02:00:59
【问题描述】:

我正在尝试运行 TensorFlow 会话来找出变量 W 的值。下面是我的代码:

W = tf.Variable(rng.randn(), name="weight")
init = tf.initialize_all_variables()

print(W.dtype)
print(W.initial_value)
print(W.value)

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

然后我得到以下输出和错误:

<dtype: 'float32_ref'>
Tensor("weight_2/initial_value:0", shape=(), dtype=float32)
<bound method Variable.value of <tensorflow.python.ops.variables.Variable object at 0x7f031f36a470>>
---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
    971     try:
--> 972       return fn(*args)
    973     except errors.OpError as e:

/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
    953                                  feed_dict, fetch_list, target_list,
--> 954                                  status, run_metadata)
    955 

/usr/lib/python3.4/contextlib.py in __exit__(self, type, value, traceback)
     65             try:
---> 66                 next(self.gen)
     67             except StopIteration:

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/errors.py in raise_exception_on_not_ok_status()
    462           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 463           pywrap_tensorflow.TF_GetCode(status))
    464   finally:

FailedPreconditionError: Attempting to use uninitialized value weight_2
     [[Node: _send_weight_2_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=-1169476299400319384, tensor_name="weight_2:0", _device="/job:localhost/replica:0/task:0/cpu:0"](weight_2)]]

During handling of the above exception, another exception occurred:

FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-35-892407d423e3> in <module>()
      7 
      8 sess = tf.Session()
----> 9 sess.run(W)

/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    715     try:
    716       result = self._run(None, fetches, feed_dict, options_ptr,
--> 717                          run_metadata_ptr)
    718       if run_metadata:
    719         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    913     if final_fetches or final_targets:
    914       results = self._do_run(handle, final_targets, final_fetches,
--> 915                              feed_dict_string, options, run_metadata)
    916     else:
    917       results = []

/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
    963     if handle is None:
    964       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 965                            target_list, options, run_metadata)
    966     else:
    967       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
    983         except KeyError:
    984           pass
--> 985       raise type(e)(node_def, op, message)
    986 
    987   def _extend_graph(self):

FailedPreconditionError: Attempting to use uninitialized value weight_2
     [[Node: _send_weight_2_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=-1169476299400319384, tensor_name="weight_2:0", _device="/job:localhost/replica:0/task:0/cpu:0"](weight_2)]]

我猜我需要一个 W 公式(或类似的公式)来运行会话,但不确定究竟需要什么......究竟需要提供什么,以便会话可以计算变量的值?谢谢!

【问题讨论】:

    标签: python python-3.x tensorflow


    【解决方案1】:

    函数名称initialize_all_variables 有点误导(在 0.12 中已更改)。它返回您需要运行的操作。 W 在你调用 sess.run(init) 之前不会被初始化。

    W.initial_value 显示了 它将被初始化的值,来自我在文档中读到的内容(强调我的):

    tf.Variable.initial_value

    返回用作变量初始值的张量。

    请注意,这与运行 op 的 initialized_value() 不同 在返回值之前初始化变量。 这个方法 返回初始化操作的操作使用的张量 变量。

    【讨论】:

    • 谢谢。但是为什么 W.initial_value 仍然打印 : Tensor("weight_2/initial_value:0", shape=(), dtype=float32) 即使我不调用 initialize_all_variables ?谢谢!
    • 我更新了我的答案以解释initial_value 是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2019-02-13
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多