I am using several LSTM layers to form a deep recurrent neural network. I would like to monitor the weights of each LSTM layer during training. However, I couldn't find out how to attach summaries of the LSTM layer weights to TensorBoard.
Any suggestions how this can be done?
The code is as follows:
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)
tf.contrib.rnn.LSTMCell
objects have a property calledvariables
that works for this. There's just one trick: The property returns an empty list until your cell goes throughtf.nn.dynamic_rnn
. At least this is the case when using a single LSTMCell. I can't speak forMultiRNNCell
. So I expect this would work:And then you probably know how to do it from there, but
Looking at the TensorFlow documentation I linked above, there's also a
weights
property. I don't know the difference, if there is any. And, the order of thevariables
return isn't documented. I figured it out by printing the resulting list and looking at the variable names.Now,
MultiRNNCell
has the samevariables
property according to its doc and it says it returns all layer variables. I honestly don't know howMultiRNNCell
works, so I cannot tell you whether these are variables belonging exclusively toMultiRNNCell
or if it includes variables from the cells that go into it. Either way, knowing the property exists should be a nice tip! Hope this helps.Although
variables
is documented for most (all?) RNN classes, it does break forDropoutWrapper
. The property has been documented since r1.2, but accessing the property causes an exception in 1.2 and 1.4 (and looks like 1.3, but untested). Specifically,will throw
AttributeError: 'DropoutWrapper' object has no attribute 'trainable'
. From the traceback (or a long stare at the DropoutWrapper source), I noticed thatvariables
is implemented in DropoutWrapper's superRNNCell
's superLayer
. Dizzy yet? Indeed, we find the documentedvariables
property here. It returns the (documented)weights
property. Theweights
property returns the (documented)self.trainable_weights + self.non_trainable_weights
properties. And finally the root of the problem:That is,
variables
does not work for aDropoutWrapper
instance. Neither willtrainable_weights
ornon_trainable_weights
sinceself.trainable
is not defined.One step deeper,
Layer.__init__
defaultsself.trainable
toTrue
, butDropoutWrapper
never calls it. To quote a TensorFlow contributor on Github,So for instance, to access the LSTM variables in the above example,
lstm_cell.variables
suffices.Edit: To the best of my knowledge, Mike Khan's PR has been incorporated into 1.5. Now, the variables property of the dropout layer returns an empty list.