1

What is the proper way to reinitialize weights of some layers in TF2?

There exists a valid method for TF1: https://stackoverflow.com/a/51727616/11836236

and some workaround for TF2: https://stackoverflow.com/a/57826239/11836236 but this solution does not utilize the initializers that are specified in each layers.

1 Answers1

0

This works for me with tf 2.3.0: (edited from this answer)

for layer in model.layers:         
    if hasattr(layer, 'kernel_initializer') and \
            hasattr(layer, 'bias_initializer'):
        layer.set_weights([layer.kernel_initializer(shape=np.asarray(layer.kernel.shape)), \
            layer.bias_initializer(shape=np.asarray(layer.bias.shape))])

Hope it might help.

xldk
  • 11
  • 2
  • Thanks. This seems exactly what I was looking for. However, I don't need the solution anymore so I won't confirm it right now. – Tuukka Salmi Sep 17 '20 at 06:16