2016-12-05 6 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 

エラー私は得た:

私は初期化されていない変数をプリントアウトしたときに、私は tf.global_variables_initializer.run()init.run()を交換した場合210
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)]

を持って、それが働きました。

なぜ、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()'を使う代わりに、これらの内部変数を明示的に初期化することはできますか?私の埋め込みは実際にはサンプルコードのものより少し複雑です。まず、訓練された単語の埋め込みを復元し、[0,0 ...、0]の前に 'embeddings'変数を作成します。ですから、 'global_variables_initializer()'を使うことはできません。これは復元された埋め込みを上書きします。ありがとう。 – Harrison

関連する問題