Reset weights in Keras layer
<p>I'd like to reset (randomize) the weights of all layers in my Keras (deep learning) model. The reason is that I want to be able to train the model several times with different data splits without having to do the (slow) model recompilation every time.</p>
<p>Inspired by <a href="https://github.com/fchollet/keras/pull/1908" rel="noreferrer">this discussion</a>, I'm trying the following code:</p>
<pre><code># Reset weights
for layer in KModel.layers:
    if hasattr(layer,'init'):
        input_dim = layer.input_shape[1]
        new_weights = layer.init((input_dim, layer.output_dim),name='{}_W'.format(layer.name))
        layer.trainable_weights[0].set_value(new_weights.get_value())
</code></pre>
<p>However, it only partly works.</p>
<p>Partly, becuase I've inspected some layer.get_weights() values, and they seem to change. But when I restart the training, the cost values are much lower than the initial cost values on the first run. It's almost like I've succeeded resetting some of the weights, but not all of them.</p>