Tensorflow bidirectional_dynamic_rnn()FailedPreconditionError:尝试使用未初始化的值BiRNN / FW / LSTMCell / B

时间:2017-02-21 08:00:02

标签: tensorflow

尝试拨打tf.nn.bidirectional_dynamic_rnn()时出现上述错误。我打电话给tf.global_variables_initializer()。起初我以为是因为我没有将sequence_length传递给tf.nn.bidirectional_dynamic_rnn()。然而,即使在我这样做之后,它仍然显示相同的错误。

有什么想法吗?

堆栈跟踪:

 Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1580, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 964, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/Keven/Documents/stanford local/cs224n project/224n-project/bi_lstm_encoder.py", line 49, in <module>
    test_bilstm()
  File "/Users/Keven/Documents/stanford local/cs224n project/224n-project/bi_lstm_encoder.py", line 43, in test_bilstm
    out = session.run(pred, feed_dict={input_placeholder: doc, sequence_placeholder: sequence_length})
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 766, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 964, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1014, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1034, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value BiRNN/FW/LSTMCell/B
     [[Node: BiRNN/FW/LSTMCell/B/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](BiRNN/FW/LSTMCell/B)]]

Caused by op u'BiRNN/FW/LSTMCell/B/read', defined at:
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1580, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 964, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/Keven/Documents/stanford local/cs224n project/224n-project/bi_lstm_encoder.py", line 49, in <module>
    test_bilstm()
  File "/Users/Keven/Documents/stanford local/cs224n project/224n-project/bi_lstm_encoder.py", line 42, in test_bilstm
    pred = BidirectionalLSTMEncoder().add_prediction_op(input_placeholder, sequence_placeholder, 6)
  File "/Users/Keven/Documents/stanford local/cs224n project/224n-project/bi_lstm_encoder.py", line 20, in add_prediction_op
    preds, _ = tf.nn.bidirectional_dynamic_rnn(cell_forward, cell_backward, inputs, sequence_length=sequence_length, dtype=tf.float32)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 652, in bidirectional_dynamic_rnn
    time_major=time_major, scope=fw_scope)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 845, in dynamic_rnn
    dtype=dtype)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 1012, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2636, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2469, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2419, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 995, in _time_step
    skip_conditionals=True)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 403, in _rnn_step
    new_output, new_state = call_cell()
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 983, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell.py", line 500, in __call__
    initializer=init_ops.zeros_initializer, dtype=dtype)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1024, in get_variable
    custom_getter=custom_getter)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 850, in get_variable
    custom_getter=custom_getter)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 346, in get_variable
    validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 331, in _true_getter
    caching_device=caching_device, validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 677, in _get_single_variable
    expected_shape=shape)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 224, in __init__
    expected_shape=expected_shape)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 367, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1424, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value BiRNN/FW/LSTMCell/B
     [[Node: BiRNN/FW/LSTMCell/B/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](BiRNN/FW/LSTMCell/B)]]

代码:

import tensorflow as tf
import numpy as np
from SubModel import SubModel


# input:
# shape=(?, max_timestep_doc2, 3 * word_vector_size)
#
# output:
# shape=(?, max_timestep_doc2, 2 * word_vector_size)

class BidirectionalLSTMEncoder(SubModel):

  def add_prediction_op(self, inputs, output_size=None):
    sequence_length = [5, 5]
    cell_forward = tf.nn.rnn_cell.LSTMCell(output_size, num_proj=output_size)
    cell_backward = tf.nn.rnn_cell.LSTMCell(output_size, num_proj=output_size)
    preds, _ = tf.nn.bidirectional_dynamic_rnn(cell_forward, cell_backward, inputs, sequence_length=sequence_length, dtype=tf.float32)
    return preds

  def __init__(self):
    pass


def test_bilstm():
  print('testing bidirectional lstm layer')

  with tf.variable_scope("test_bilstm_layer"):
    input_placeholder = tf.placeholder(tf.float32, shape=(None, 5, 9))
    sequence_placeholder = tf.placeholder(tf.int32, shape=(None,))

  init = tf.global_variables_initializer()

  with tf.Session() as session:
    session.run(init)
    doc = np.ones(shape=(2, 5, 9), dtype=np.float32) * 0.5

    pred = BidirectionalLSTMEncoder().add_prediction_op(input_placeholder, 6)
    out = session.run(pred, feed_dict={input_placeholder: doc})
    print("out = " + str(out))

    # assert np.allclose(CD_correct, out, atol=1e-2), "new state vector does not seem to be correct."

if __name__ == "__main__":
  test_bilstm()

1 个答案:

答案 0 :(得分:0)

没关系。事实证明我没有正确地初始化变量..

我移动了这一行:

pred = BidirectionalLSTMEncoder().add_prediction_op(input_placeholder, 6)

在这一行之上:

with tf.Session() as session:

然后一切正常。包含变量初始化的函数需要在tf.global_variables_initializer()

之前调用