Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: DropoutWrapper leads to different output?

I build a LSTM like:

lstm_cell = tf.nn.rnn_cell.LSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True, activation=tf.nn.tanh)
lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=0.5)
lstm_cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * 3, state_is_tuple=True)

Then i train the model, and save variables. The next time i load saved variables and skip training, it gives me a different prediction.

If i change the output_keep_prob to 1, this model can always show me the same prediction, but if the output_keep_prob is less than 1, like 0.5, this model shows me different prediction every time.

So i guess if the DropoutWrapper leads to different output? If so, how can i solve this problem?

Thanks

like image 990
nickzxd Avatar asked Oct 30 '22 15:10

nickzxd


2 Answers

Try using the seed keyword argument to DropoutWrapper(...):

lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=0.5, seed=42)

See the docs here for DropoutWrapper.__init__

like image 87
kempy Avatar answered Nov 15 '22 05:11

kempy


Dropout will randomly activate a subset of your net, and is used during training for regularization. Because you've hardcoded dropout as 0.5, it means every time you run the net half your nodes will be randomly silenced, thus producing a different and random result.

You can sanity check this is what's happening by setting a seed, so that the same nodes will be 'randomly' silenced by dropout each time. However, what you probably want to do is make dropout a placeholder so that you can set it to 1.0 (ie keep all the nodes) during test time, which will produce the same output for each input deterministically.

like image 42
Dwight Crow Avatar answered Nov 15 '22 06:11

Dwight Crow