Passing multiple arguments in module.apply

I have the following issue where I need to pass multiple args in the module.apply callable but for some reason it keeps throwing an error.

ValueError: unsupported format character 'b' (0x62) at index 1

Any ideas?

Here’s a MWE:

dummy_params = []

def init_buffers(module, params, flag=True):
    for name in list(module._parameters.keys()):
        if module._parameters[name] is None:
        data = module._parameters[name].data
        module.register_buffer("%alpha" % name, torch.zeros(data.size()))
        module.register_buffer("%beta" % name, torch.zeros(data.size()))

        if flag is False:
            module.register_buffer("%delta" % name, torch.zeros(0, data.numel()))

        params.append((module, name))

class Mynet(torch.nn.Module):
    def __init__(self):
        super(Mynet, self).__init__()
        self.fc1 = torch.nn.Linear(2,2)
        self.fc2 = torch.nn.Linear(2,2)
    def forward(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        return x
net = Mynet()
net.apply(lambda module: init_buffers(module=module, params=params))


The problem is that "%beta" % name is not a valid string replacement in python I think?
At least this is what the error message indicates.

Thanks again for saving the day @albanD.

If you don’t mind me asking a couple follow ups just to clarify my understanding?

1.) Is this the default way to pass multiple args into .apply(), or is there a better way?

net.apply(lambda module: init_buffers(module=module, params=params))

If you notice in the example there’s dummy_params = [] list which holds values of module and name but because I’ve popped module params and register different params and names before adding them to dummy_list, now if I print(dummy_list) it throws an error of param bias not existing in module.

The only way to get values from dummy_params is to iterate over and use module.__getattr__('%s_alpha' % name), which leads me to following question.

2.) Is module.__getattr__ the default way to retrieve values from module.register_buffer, even if I had them stored in dummy_list?


I am not sure how to use apply. I would refer you to the doc :slight_smile:
ANd you can do mod.your_buffer after registering it yes.

That’s great thank you!