2016-12-23 53 views
0

我試圖運行TensorFlow會議,找出變量W.下面的值是我的代碼:TensorFlow:輸入一個會話來計算一個變量? FailedPreconditionError:試圖使用未初始化值WEIGHT_2

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(或類似的東西)的會議運行,但不能確定究竟需要什麼......究竟需要提供什麼,所以會話可以計算值的變量?謝謝!

回答

1

函數名稱initialize_all_variables有點誤導(並已在0.12中進行了更改)。它返回一個你需要運行的操作。在撥打sess.run(init)之前,W將不會被初始化。

W.initial_value表明它將與初始化值,從我的文檔閱讀(重點煤礦):

tf.Variable.initial_value

Returns the Tensor used as the initial value for the variable.

Note that this is different from initialized_value() which runs the op that initializes the variable before returning its value. This method returns the tensor that is used by the op that initializes the variable.

+0

感謝。但是爲什麼仍然會打印W.initial_value:張量(「weight_2/initial_value:0」,shape =(),dtype = float32),即使我沒有調用initialize_all_variables?謝謝! – Edamame

+0

我更新了我的答案,以解釋'initial_value'是什麼。 –

相關問題