Tensorflow: LSTMセルで双方向rnnを初期化するときにエラーが発生しました

作成日 2016年01月18日  ·  3コメント  ·  ソース: tensorflow/tensorflow

双方向_rnnを初期化しようとすると、LSTMセルを使用してテンソルフローを使用してbi-rnnモデルを構築したいと思います。
それは与える: ValueError: Over-sharing: Variable BiRNN_FW/RNN/BasicLSTMCell/Linear/Matrix already exists, disallowed. Did you mean to set reuse=True in VarScope?

import tensorflow as tf
from tensorflow.models.rnn import rnn, rnn_cell
from tensorflow.python.ops.constant_op import constant
import numpy as np

class Model(object):
    def __init__(self, batch_size, len_word, num_chars, dim_embed, dim_hidden):
        self.batch_size = batch_size
        self.dim_embed = dim_embed
        self.dim_hidden = dim_hidden
        self.num_chars = num_chars
        self.len_word = len_word

        with tf.device("/cpu:0"):
            self.embedding = tf.Variable(tf.random_uniform([num_chars, dim_embed], -0.1, 0.1), name='embedding')

        self.W_emb = tf.Variable(tf.random_uniform([dim_hidden*2, dim_embed], -0.1, 0.1), name='W_emb')
        self.b_emb = tf.Variable(tf.zeros([dim_embed]), name='b_emb')
        self.lstm_fw_cell = rnn_cell.BasicLSTMCell(dim_hidden)
        self.lstm_bw_cell = rnn_cell.BasicLSTMCell(dim_hidden)

    def build_model(self):
        inputs = tf.placeholder(tf.int32, [self.batch_size, self.len_word])
        input_length = tf.placeholder(tf.int64, [self.batch_size])
        lstm_state_fw = self.lstm_fw_cell.zero_state(self.batch_size, tf.float32)
        lstm_state_bw = self.lstm_bw_cell.zero_state(self.batch_size, tf.float32)

        with tf.device("/cpu:0"):
            embedded_input = tf.nn.embedding_lookup(self.embedding, tf.transpose(inputs))

        brnn_output = rnn.bidirectional_rnn(
            self.lstm_fw_cell, self.lstm_bw_cell,
            tf.unpack(embedded_input),
            sequence_length=input_length,
            initial_state_fw=lstm_state_fw,
            initial_state_bw=lstm_state_bw,
        )

        pooled_output = tf.reduce_sum( tf.pack(brnn_output), 0 )
        pooled_output = pooled_output / tf.expand_dims( tf.to_float(input_length) + 1e-6, 1)
        final_emb = tf.nn.xw_plus_b(pooled_output, self.W_emb, self.b_emb)
        final_emb = tf.nn.l2_normalize(final_emb, dim=1, epsilon=1e-7)

        return final_emb

最も参考になるコメント

LSTMセルに異なる変数スコープを指定する必要があります。

with tf.variable_scope('forward'):
    self.lstm_fw_cell = rnn_cell.BasicLSTMCell(dim_hidden)   
with tf.variable_scope('backward'):
    self.lstm_bw_cell = rnn_cell.BasicLSTMCell(dim_hidden)

そうしないと、名前の衝突が発生し(両方のセルが「BiRNN_FW / RNN / BasicLSTMCell / Linear / Matrix」名を使用しようとします)、tfはこれを、2つのセルのパラメーターを共有することであるかのように解釈します。あなたが欲しい。 2番目のスコープwith variable_scope(name, reuse=True)変数を再利用するように明示的に指示していないため、TFは例外をスローします。

上記のように変数スコープを設定すると、変数に一意の名前が作成されます。
BiRNN_FW / RNN / BasicLSTMCell /フォワード/リニア/マトリックス
BiRNN_FW / RNN / BasicLSTMCell /後方/線形/マトリックス

詳細については、共有変数ガイドをお読みください。

全てのコメント3件

LSTMセルに異なる変数スコープを指定する必要があります。

with tf.variable_scope('forward'):
    self.lstm_fw_cell = rnn_cell.BasicLSTMCell(dim_hidden)   
with tf.variable_scope('backward'):
    self.lstm_bw_cell = rnn_cell.BasicLSTMCell(dim_hidden)

そうしないと、名前の衝突が発生し(両方のセルが「BiRNN_FW / RNN / BasicLSTMCell / Linear / Matrix」名を使用しようとします)、tfはこれを、2つのセルのパラメーターを共有することであるかのように解釈します。あなたが欲しい。 2番目のスコープwith variable_scope(name, reuse=True)変数を再利用するように明示的に指示していないため、TFは例外をスローします。

上記のように変数スコープを設定すると、変数に一意の名前が作成されます。
BiRNN_FW / RNN / BasicLSTMCell /フォワード/リニア/マトリックス
BiRNN_FW / RNN / BasicLSTMCell /後方/線形/マトリックス

詳細については、共有変数ガイドをお読みください。

変数は...Cell.__init__ではなく...Cell.__call__内に作成されるため、これはもう発生しないことに注意してください。したがって、セルを構築するためのスコープは必要なく、変数スコープを処理します。それ自体がbidirectional_rnn内にあるため、自分でスコープを設定する必要はありません。

私はまだ同じ問題を抱えています。 何か疑惑はありますか? 私はサロモンによって提案されたアプローチを試しましたが、同じ結果です。 結果として、Doenstはタプルを返します。
((encoder_fw_outputs、
エンコーダ_bw_outputs)、
(encoder_fw_final_state、
エンコーダー_bw_final_state))=(
tf.nn.bidirection_dynamic_rnn(cell_fw =エンコーダーセル、
cell_bw =エンコーダーセル、
入力=エンコーダー入力_埋め込み、
sequence_length =エンコーダー入力_長さ、
dtype = tf.float64、time_major = True)

ValueErrorトレースバック(最後の最後の呼び出し)
()
20入力= encoder_inputs_embedded、
21 sequence_length =エンコーダー入力_長さ、
---> 22 dtype = tf.float32、time_major = True)
23)
24

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in双方向_dynamic_rnn(cell_fw、cell_bw、inputs、sequence_length、initial_state_fw、initial_state_bw、dtype、parallel_iterations、swap_memory 、time_major、scope)
348 initial_state = initial_state_fw、dtype = dtype、
349 parallel_iterations = parallel_iterations、swap_memory = swap_memory、
-> 350 time_major = time_major、scope = fw_scope)
351
352#逆方向

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in dynamic_rnn(cell、inputs、sequence_length、initial_state、dtype、parallel_iterations、swap_memory、time_major、scope )
544 swap_memory = swap_memory、
545 sequence_length = sequence_length、
-> 546 dtype = dtype)
547
548#_dynamic_rnn_loopの出力は、常に[時間、バッチ、深さ]の形になります。

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in _dynamic_rnn_loop(cell、inputs、initial_state、parallel_iterations、swap_memory、sequence_length、dtype)
711 loop_vars =(time、output_ta、state)、
712 parallel_iterations = parallel_iterations、
-> 713 swap_memory = swap_memory)
714
715#出力タプルを使用しない場合は、最終出力を解凍します。

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in while_loop(cond、body、loop_vars、shape_invariants、parallel_iterations、back_prop、swap_memory、name)
2603コンテキスト= WhileContext(parallel_iterations、back_prop、swap_memory、name)
2604 ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT、context)
-> 2605結果= context.BuildLoop(cond、body、loop_vars、shape_invariants)
2606戻り結果
2607

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in BuildLoop(self、pred、body、loop_vars、shape_invariants)
2436 self.Enter()
2437 original_body_result、exit_vars = self._BuildLoop(
-> 2438 pred、body、original_loop_vars、loop_vars、shape_invariants)
ついに2439:
2440 self.Exit()

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in _BuildLoop(self、pred、body、original_loop_vars、loop_vars、shape_invariants)
2386構造= original_loop_vars、
2387 flat_sequence = vars_for_body_with_tensor_arrays)
-> 2388 body_result = body(* packed_vars_for_body)
nest.is_sequence(body_result)でない場合は2389:
2390 body_result = [body_result]

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in _time_step(time、output_ta_t、state)
694 call_cell = call_cell、
695 state_size = state_size、
-> 696 skip_conditionals = True)
697その他:
698(出力、new_state)= call_cell()

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in _rnn_step(time、sequence_length、min_sequence_length、max_sequence_length、zero_output、state、call_cell、state_size、skip_conditionals )
175#ステップ。 max_seq_lenが展開の数と等しい場合、これはより高速です
176#(これはdynamic_rnnで一般的です)。
-> 177 new_output、new_state = call_cell()
178 nest.assert_same_structure(state、new_state)
179 new_state = nest.flatten(new_state)

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in()
682
683 input_t = nest.pack_sequence_as(structure = input、flat_sequence = input_t)
-> 684 call_cell = lambda:cell(input_t、state)
685
sequence_lengthがNoneでない場合は686:

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.pyc in __call __(self、inputs、state、scope)
336#i = input_gate、j = new_input、f = forget_gate、o = output_gate
337 lstm_matrix = _linear([inputs、m_prev]、4 * self._num_units、bias = True、
-> 338スコープ=スコープ)
339 i、j、f、o = array_ops.split(
340値= lstm_matrix、num_or_size_splits = 4、axis = 1)

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.pyc in _linear(args、output_size、bias、bias_start、scope)
745 vs.variable_scope(scope)をouter_scopeとして使用:
746の重み= vs.get_variable(
-> 747 "weights"、[total_arg_size、output_size]、dtype = dtype)
len(args)== 1の場合は748
749 res = math_ops.matmul(args [0]、weights)

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(name、shape、dtype、initializer、regularizer、trainable、collections、caching_device、partitioner 、validate_shape、custom_getter)
986コレクション=コレクション、caching_device = cacheing_device、
987パーティショナー=パーティショナー、validate_shape = validate_shape、
-> 988 custom_getter = custom_getter)
989 get_variable_or_local_docstring =(
990 "" "%s

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self、var_store、name、shape、dtype、initializer、regularizer、trainable、collections 、caching_device、partitioner、validate_shape、custom_getter)
888コレクション=コレクション、caching_device = cacheing_device、
889パーティショナー=パーティショナー、validate_shape = validate_shape、
-> 890 custom_getter = custom_getter)
891
892 def _get_partitioned_variable(self、

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self、name、shape、dtype、initializer、regularizer、reuse、trainable、collections 、caching_device、partitioner、validate_shape、custom_getter)
346再利用=再利用、trainable = trainable、collections = collections、
347 cache_device = cacheing_device、partitioner = partitioner、
-> 348 validate_shape = validate_shape)
349
350 def _get_partitioned_variable(

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _true_getter(name、shape、dtype、initializer、regularizer、reuse、trainable、collections、caching_device 、partitioner、validate_shape)
331初期化子=初期化子、正則化=正則化、再利用=再利用、
332 trainable = trainable、collections = collections、
-> 333 cache_device = cacheing_device、validate_shape = validate_shape)
334
custom_getterがNoneでない場合は335:

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _get_single_variable(self、name、shape、dtype、initializer、regularizer、partition_info、reuse、trainable 、コレクション、caching_device、validate_shape)
637「VarScopeでreuse = Trueを設定するつもりでしたか?」
638 "元々定義されている場所:\ n \ n%s"%(
-> 639名、 "" .join(traceback.format_list(tb))))
640 found_var = self._vars [name]
シェイプでない場合は641.is_compatible_with(found_var.get_shape()):

ValueError:変数双方向_rnn / fw / lstm_cell / weightsはすでに存在し、許可されていません。 VarScopeでreuse = Trueを設定するつもりでしたか? 当初の定義:

ファイル "/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py"、行747、_linear
"重み"、[total_arg_size、output_size]、dtype = dtype)
ファイル "/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py"、行338、__ call__
scope = scope)
ファイル ""、24行目、
time_major = True

このページは役に立ちましたか?
0 / 5 - 0 評価