2017-12-04 19 views
3

私は深いリカレントニューラルネットワークを形成するためにいくつかのLSTM層を使用しています。トレーニング中に各LSTMレイヤの重みを監視したいと思います。しかし、TensorBoardにLSTMレイヤウェイトのサマリーを添付する方法を見つけることができませんでした。テンソルボード - LSTMの重みを視覚化する

どのようにこれを行うことができますか?次のように

コードは次のとおりです。

  cells = [] 

      with tf.name_scope("cell_1"): 
       cell1 = tf.contrib.rnn.LSTMCell(self.embd_size, state_is_tuple=True, initializer=self.initializer) 
       cell1 = tf.contrib.rnn.DropoutWrapper(cell1, 
          input_keep_prob=self.input_dropout, 
          output_keep_prob=self.output_dropout, 
          state_keep_prob=self.recurrent_dropout) 
       cells.append(cell1) 

      with tf.name_scope("cell_2"): 
       cell2 = tf.contrib.rnn.LSTMCell(self.n_hidden, state_is_tuple=True, initializer=self.initializer) 
       cell2 = tf.contrib.rnn.DropoutWrapper(cell2, 
          output_keep_prob=self.output_dropout, 
          state_keep_prob=self.recurrent_dropout) 
       cells.append(cell2) 

      with tf.name_scope("cell_3"): 
       cell3 = tf.contrib.rnn.LSTMCell(self.embd_size, state_is_tuple=True, initializer=self.initializer) 
       # cell has no input dropout since previous cell already has output dropout 
       cell3 = tf.contrib.rnn.DropoutWrapper(cell3, 
          output_keep_prob=self.output_dropout, 
          state_keep_prob=self.recurrent_dropout) 
       cells.append(cell3) 

      cell = tf.contrib.rnn.MultiRNNCell(
       cells, state_is_tuple=True) 

      output, self.final_state = tf.nn.dynamic_rnn(
       cell, 
       inputs=self.inputs, 
       initial_state=self.init_state) 

答えて

5

tf.contrib.rnn.LSTMCellオブジェクトは、このために働くvariablesと呼ばれるpropertyを持っています。ただ一つのトリックがあります:あなたのセルがtf.nn.dynamic_rnnを通過するまで、このプロパティは空のリストを返します。少なくとも、これは単一のLSTMCellを使用する場合です。私はMultiRNNCellのために話すことができません。だから私はこれが機能するだろうと期待しており:

output, self.final_state = tf.nn.dynamic_rnn(...) 
for one_lstm_cell in cells: 
    one_kernel, one_bias = one_lstm_cell.variables 
    # I think TensorBoard handles summaries with the same name fine. 
    tf.summary.histogram("Kernel", one_kernel) 
    tf.summary.histogram("Bias", one_bias) 

そして、あなたはおそらく、そこからそれを行う方法を知っているが、

summary_op = tf.summary.merge_all() 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    train_writer = tf.summary.FileWriter(
     "my/preferred/logdir/train", graph=tf.get_default_graph()) 
    for step in range(1, training_steps+1): 
     ... 
     _, step_summary = sess.run([train_op, summary_op]) 
     train_writer.add_summary(step_summary) 

は、私は上記のリンクTensorFlowのドキュメントを見てみると、weights性もあります。違いがあるかどうか分からない。また、variablesの返品の順序は文書化されていません。結果リストを印刷して変数名を調べることで、わかりました。

さて、MultiRNNCellはそのdocに応じて同じvariables性質を持っており、それがすべて層変数を返すと言います。私は正直なところ、MultiRNNCellの仕組みが分からないので、これらが変数MultiRNNCellに属しているかどうか、またはそれに入るセルの変数が含まれているかどうかはわかりません。いずれにしても、財産が存在することを知ることはすばらしいヒントになるはずです!お役に立てれば。 variablesものの


はほとんど(すべて?)RNNクラスのために、それはDropoutWrapperのために壊すん文書化されています。 r1.2以降のproperty has been documentedでは、プロパティにアクセスすると1.2と1.4で例外が発生します(1.3のように見えますが、テストされていません)。具体的には、

from tensorflow.contrib import rnn 
... 
lstm_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0) 
wrapped_cell = rnn.DropoutWrapper(lstm_cell) 
outputs, states = rnn.static_rnn(wrapped_cell, x, dtype=tf.float32) 
print("LSTM vars!", lstm_cell.variables) 
print("Wrapped vars!", wrapped_cell.variables) 

AttributeError: 'DropoutWrapper' object has no attribute 'trainable'となります。トレースバック(またはDropoutWrapper sourceの長い目玉)から、variablesDropoutWrapper's super RNNCellのスーパーLayerに実装されていることがわかりました。めまいはまだですか?実際には、文書番号variablesがここに記載されています。文書化されたweightsプロパティを返します。 weightsプロパティは、(文書化された)self.trainable_weights + self.non_trainable_weightsプロパティを返します。そして最後に、問題の根本:

@property 
def trainable_weights(self): 
    return self._trainable_weights if self.trainable else [] 

@property 
def non_trainable_weights(self): 
    if self.trainable: 
     return self._non_trainable_weights 
    else: 
     return self._trainable_weights + self._non_trainable_weights 

variablesDropoutWrapperインスタンスでは動作しません、です。 self.trainableは定義されていないため、trainable_weightsまたはnon_trainable_weightsとなりません。

さらに深く、Layer.__init__のデフォルトはself.trainableからTrueですが、DropoutWrapperは決して呼び出しません。例えばので、Github

DropoutWrapper does not have variables because it does not itself store any. It wraps a cell that may have variables; but it's not clear what the semantics should be if you access the DropoutWrapper.variables . For example, all keras layers only report back the variables that they own; and so only one layer ever owns any variable. That said, this should probably return [] , and the reason it doesn't is that DropoutWrapper never calls super().__init__ in its constructor. That should be an easy fix; PRs welcome.

をTensorFlowの貢献を引用すると、上記の例でLSTM変数にアクセスするには、lstm_cell.variablesは十分です。


編集:私の知る限りでは、マイク・ハーンのPRは1.5に組み込まれています。さて、ドロップアウト層の変数プロパティは空のリストを返します。

+0

ありがとうございました! 'MultiRNNCell.variables'は、LSTMユニットのすべての重みと偏りを持つリストを返します。あなたのコードに概説されているように使用することができます。 – Lemon

+0

喜んで助けてください!私も何かを学んだ –

+0

同じことをしようとしていますが、エラーが発生しています: 'DropoutWrapper'オブジェクトには 'trainable'属性がありません.Tensorflowのバージョンに問題があり、1.2.1を使用しています。何か案は? –