Tensorboard - 可视化LSTM的权重

Lem*_*mon 8 tensorflow tensorboard

我正在使用几个LSTM层来形成深度递归神经网络.我想在训练期间监控每个LSTM层的权重.但是,我无法找到如何将LSTM图层权重的摘要附加到TensorBoard.

有什么建议可以做到这一点?

代码如下:

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)
Run Code Online (Sandbox Code Playgroud)

Dyl*_*n F 10

tf.contrib.rnn.LSTMCell对象有一个称为适用于此的属性variables.只有一个技巧:该属性返回一个空列表,直到您的单元格通过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)
Run Code Online (Sandbox Code Playgroud)

然后你可能知道如何从那里做到,但是

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)
Run Code Online (Sandbox Code Playgroud)

看看我上面链接的TensorFlow文档,还有一个weights属性.我不知道区别,如果有的话.并且,variables没有记录退货的顺序.我通过打印结果列表并查看变量名称来计算出来.

现在,根据其文档MultiRNNCell具有相同的variables属性,它表示它返回所有图层变量.老实说,我不知道它是如何工作的,所以我无法告诉你这些变量是否属于专有的变量,或者它是否包含进入它的单元格中的变量.无论哪种方式,知道财产存在应该是一个很好的提示!希望这可以帮助.MultiRNNCellMultiRNNCell


尽管variables有大多数(所有?)RNN类的文档记录,但它确实有用DropoutWrapper.该属性自r1.2起已被记录,但访问该属性会导致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)
Run Code Online (Sandbox Code Playgroud)

会扔AttributeError: 'DropoutWrapper' object has no attribute 'trainable'.从回溯(或长的盯着DropoutWrapper源),我注意到,variables在实施DropoutWrapper的超级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
Run Code Online (Sandbox Code Playgroud)

也就是说,variables对一个DropoutWrapper实例不起作用.无论是将trainable_weightsnon_trainable_weights因为self.trainable没有定义.

更深入一步,Layer.__init__默认self.trainableTrue,但从DropoutWrapper不调用它.在Github上引用TensorFlow贡献者,

DropoutWrapper没有变量,因为它本身不存储任何变量.它包含一个可能有变量的单元格; 但是如果你访问它,它不清楚语义应该是什么DropoutWrapper.variables.例如,所有keras层仅报告它们拥有的变量; 所以只有一层拥有任何变量.也就是说,这可能应该返回[],而不是它的原因是DropoutWrapper从不super().__init__在其构造函数中调用.这应该是一个简单的解决方案; PR欢迎.

因此,例如,要访问上例中的LSTM变量,lstm_cell.variables就足够了.


编辑:据我所知,Mike Khan的PR已被纳入1.5.现在,dropout图层的variables属性返回一个空列表.