1 for layer in model.layers:
2 if isinstance(layer, tf.keras.Model):
3 reset_weights(layer)
4 continue
5 for k, initializer in layer.__dict__.items():
6 if "initializer" not in k:
7 continue
8 # find the corresponding variable
9 var = getattr(layer, k.replace("_initializer", ""))
10 var.assign(initializer(var.shape, var.dtype))