2016-12-05 54 views
0

我看起來像這樣tf.nn.dynamic_rnn提出嘗試使用未初始化值誤差

with graph.as_default(): 

    train_inputs = tf.placeholder(tf.int32, shape=[None, None]) 

    with tf.device('/cpu:0'): 
     embeddings = tf.Variable(tf.zeros([vocab_size, options.embed_size])) 

     restorer = tf.train.Saver({'embeddings': embeddings}) 

     init = tf.variables_initializer([embeddings]) 

     uninit = tf.report_uninitialized_variables() 

     embed = tf.nn.embedding_lookup(embeddings, train_inputs) 

     # length() returns a [batch_szie,] tensor of true lengths of sentences (lengths before zero-padding) 
     sequence_length = length(embed) 

     lstm = tf.nn.rnn_cell.LSTMCell(options.rnn_size) 

     output, _ = tf.nn.dynamic_rnn(
      lstm, 
      embed, 
      dtype=tf.float32, 
      swequence_length=sequence_length 
     ) 

而且我session

with tf.Session(graph=graph) as session: 

    restorer.restore(session, options.restore_path) 
    # tf.global_variables_initializer.run() 
    init.run() 

    print session.run([uninit]) 

    while len(data.ids): 
     # data.generate_batch returns a list of size [batch_size, max_length], and zero-padding is used, when the sentences are shorter than max_length. For example, batch_inputs = [[1,2,3,4], [3,2,1,0], [1,2,0,0]] 
     batch_inputs, _ = data.generate_batch(options.batch_size) 
     feed_dict = {train_inputs: batch_inputs} 

     test = session.run([tf.shape(output)], feed_dict=feed_dict) 

     print test 

功能length()

def length(self, sequence): 
     length = tf.sign(sequence) 
     length = tf.reduce_sum(length, reduction_indices=1) 
     length = tf.cast(length, tf.int32) 
     return length 

錯誤我得到了:

Traceback (most recent call last): 
    File "rnn.py", line 103, in <module> 
    test = session.run([tf.shape(output)], feed_dict=feed_dict) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 766, in run 
    run_metadata_ptr) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 964, in _run 
    feed_dict_string, options, run_metadata) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1014, in _do_run 
    target_list, options, run_metadata) 
    File "/usr/local/lib/python2.7/dist-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 RNN/LSTMCell/W_0 
     [[Node: RNN/LSTMCell/W_0/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](RNN/LSTMCell/W_0)]] 

Caused by op u'RNN/LSTMCell/W_0/read', defined at: 
    File "rnn.py", line 75, in <module> 
    sequence_length=sequence_length, 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 845, in dynamic_rnn 
    dtype=dtype) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 1012, in _dynamic_rnn_loop 
    swap_memory=swap_memory) 
    File "/usr/local/lib/python2.7/dist-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/dist-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/dist-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/dist-packages/tensorflow/python/ops/rnn.py", line 995, in _time_step 
    skip_conditionals=True) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 403, in _rnn_step 
    new_output, new_state = call_cell() 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 983, in <lambda> 
    call_cell = lambda: cell(input_t, state) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py", line 496, in __call__ 
    dtype, self._num_unit_shards) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py", line 329, in _get_concat_variable 
    sharded_variable = _get_sharded_variable(name, shape, dtype, num_shards) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py", line 359, in _get_sharded_variable 
    dtype=dtype)) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1024, in get_variable 
    custom_getter=custom_getter) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 850, in get_variable 
    custom_getter=custom_getter) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 346, in get_variable 
    validate_shape=validate_shape) 
    File "/usr/local/lib/python2.7/dist-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/dist-packages/tensorflow/python/ops/variable_scope.py", line 677, in _get_single_variable 
    expected_shape=shape) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 224, in __init__ 
    expected_shape=expected_shape) 
    File "/usr/local/lib/python2.7/dist-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/dist-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/dist-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op 
    op_def=op_def) 
    File "/usr/local/lib/python2.7/dist-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/dist-packages/tensorflow/python/framework/ops.py", line 1128, in __init__ 
    self._traceback = _extract_stack() 

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

然而,當我打印出來的未初始化的變量,我得到了[array([], dtype=object)]

當我tf.global_variables_initializer.run()取代init.run(),它的工作。

任何想法爲什麼init.run()不起作用?

回答

0

您定義init如下:

init = tf.variables_initializer([embeddings]) 

該定義是指init僅初始化embeddings變量。調用tf.nn.dynamic_rnn()函數會創建更多變量,表示LSTM中的各種內部權重,並且這些變量不會被init初始化。

相比之下,tf.global_variables_initializer()會返回一個操作,該操作在運行時會初始化模型中的所有(全局)變量,包括爲LSTM創建的變量。

+0

是否可以顯式初始化這些內部變量而不是使用'global_variables_initializer()'?我的'embeddings'實際上比示例代碼中的更復雜一些。首先我恢復一個訓練詞嵌入,並在其前面加上[0,0 ...,0]來創建'embeddings'變量。所以我不能使用'global_variables_initializer()',它將覆蓋已恢復的嵌入。謝謝。 – Harrison

相關問題