How to reset parameters of layer

Hello everyone,
How to reset the parameters of layer4 in resnet18, using the Module.apply? Here my code but it doesn’t work.
Thank you.

def reset_weights(m):
    for name, layer in m.named_children():
        if name=='layer4' and hasattr(layer, 'reset_parameters'):
                print(f'Reset trainable parameters of layer = {layer}')
                layer.reset_parameters()

model.appy(reset_weight)

layer4 is an nn.Sequential module, which doesn’t have the reset_parameters method, so your conditions won’t be met.
You could iterate all modules of layer4 and check for this attribute:

def reset_weights(m):
    for name, layer in m.named_children():
        if name=='layer4':
            print(name)
            for n, l in layer.named_modules():
                print(n)
                if hasattr(l, 'reset_parameters'):
                    print(f'Reset trainable parameters of layer = {l}')
                    l.reset_parameters()


model = models.resnet18()
model.apply(reset_weights)
1 Like

It works. I owe you one.

How can I reset parameters of layers 3 and higher layers of Resnet18 so that I can train for those layers?