TL; DR
細胞の質問重みとバイアスからのコードに適切に再利用されるようです。マルチセルlstm1
とlstm2
は同じ動作をし、MultiRNNCell内部のセルは独立した重みとバイアスを持ちます。私。 擬似コードに:
lstm1._cells[0].weights == lstm2._cells[0].weights
lstm1._cells[1].weights == lstm2._cells[1].weights
長いバージョン
これは、これまで決定的な答えはありませんが、それは私がこれまでの研究の成果です。
これはハックのようですが、get_variable
メソッドをオーバーライドして、どの変数にアクセスしているか確認できます。たとえば、次のように:出力は(私は繰り返し行を削除)次のようになります
def create_lstm_multicell(name):
def lstm_cell(i, s):
print('creating cell %i in %s' % (i, s))
return rnn.LSTMCell(nstates, reuse=tf.get_variable_scope().reuse)
lstm_multi_cell = rnn.MultiRNNCell([lstm_cell(i, name) for i in range(n_layers)])
return lstm_multi_cell
with tf.variable_scope('lstm') as scope:
lstm1 = create_lstm_multicell('lstm1')
layer1, _ = tf.nn.dynamic_rnn(lstm1, x, dtype=tf.float32)
val_1 = tf.reduce_sum(layer1)
with tf.variable_scope('lstm') as scope:
scope.reuse_variables()
lstm2 = create_lstm_multicell('lstm2')
layer2, _ = tf.nn.dynamic_rnn(lstm2, x, dtype=tf.float32)
val_2 = tf.reduce_sum(layer2)
:
creating cell 0 in lstm1
creating cell 1 in lstm1
get variable: lstm/rnn/multi_rnn_cell/cell_0/lstm_cell/weights
get variable: lstm/rnn/multi_rnn_cell/cell_0/lstm_cell/biases
get variable: lstm/rnn/multi_rnn_cell/cell_1/lstm_cell/weights
get variable: lstm/rnn/multi_rnn_cell/cell_1/lstm_cell/biases
creating cell 0 in lstm2
creating cell 1 in lstm2
get variable: lstm/rnn/multi_rnn_cell/cell_0/lstm_cell/weights
get variable: lstm/rnn/multi_rnn_cell/cell_0/lstm_cell/biases
get variable: lstm/rnn/multi_rnn_cell/cell_1/lstm_cell/weights
get variable: lstm/rnn/multi_rnn_cell/cell_1/lstm_cell/biases
この出力
from tensorflow.python.ops import variable_scope as vs
def verbose(original_function):
# make a new function that prints a message when original_function starts and finishes
def new_function(*args, **kwargs):
print('get variable:', '/'.join((tf.get_variable_scope().name, args[0])))
result = original_function(*args, **kwargs)
return result
return new_function
vs.get_variable = verbose(vs.get_variable)
今、私たちは、以下の変更されたコードを実行することができますlstm1
とlstm2
のセルが同じ重みの&のバイアスを使用することを示しています。両方とも、最初のセルには離散ウェイト&のバイアスがありますMultiRNNCell内の2番目のセル。
さらに、lstm1
とlstm2
の出力であるval_1
とval_2
は、最適化時には同じです。
MultiRNNCellは、内部にcell_0
、cell_1
などの名前空間を作成すると思います。したがって、lstm1
とlstm2
の間の重みが再利用されます。
あなたの体重を再利用することはどういう意味ですか?あなたはステートフルなlstmを作りたいですか? – dv3
@ dv3いいえ、私はstatefu LSTMは必要ありません。 lstm1とlstm2が同じように動作するようにします。つまり、マルチセルのすべてのセルの重みは、lstm1とlstm2の間で同じにする必要があります。 –