2016-12-05 1 views
0

Meine sieht aus wie diesestf.nn.dynamic_rnn angehoben nicht initialisierten Wert Fehler zu verwenden

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 
     ) 

Und mein 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 

Funktion 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 

Der Fehler Ich habe:

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)]] 

Allerdings, wenn ich die nicht initialisierten Variablen ausgedruckt, ich erhielt [array([], dtype=object)]

Wenn ich init.run() mit tf.global_variables_initializer.run() ersetzt, es funktionierte.

Eine Idee, warum init.run() nicht funktioniert?

Antwort

0

Sie definiert init wie folgt:

init = tf.variables_initializer([embeddings]) 

Diese Definition bedeutet, dass init nur die embeddings Variable initialisiert. Der Aufruf der tf.nn.dynamic_rnn()-Funktion erstellt mehr Variablen, die die verschiedenen internen Gewichtungen im LSTM darstellen, und diese werden nicht von init initialisiert.

Im Gegensatz dazu gibt tf.global_variables_initializer() eine Operation zurück, die beim Ausführen alle (globalen) Variablen in Ihrem Modell initialisiert, einschließlich der für die LSTM erstellten Variablen.

+0

Ist es möglich, diese internen Variablen explizit zu initialisieren, anstatt 'global_variables_initializer()' zu verwenden? Meine 'embeddings' sind eigentlich etwas komplizierter als die im Beispielcode. Zuerst stelle ich eine eingelernte Worteinbettung wieder her und führe [0,0 ..., 0] dazu, um die Variable 'embeddings' zu erzeugen. Daher kann ich 'global_variables_initializer()' nicht verwenden, wodurch die wiederhergestellte Einbettung überschrieben wird. Vielen Dank. – Harrison

Verwandte Themen