Passing multiple arguments in module.apply

kirk86: <p>I have the following issue where I need to pass multiple args in the <code>module.apply</code> callable but for some reason it keeps throwing an error.</p>
<pre><code class="lang-auto">ValueError: unsupported format character 'b' (0x62) at index 1
</code></pre>
<p>Any ideas?</p>
<p>Here’s a MWE:</p>
<pre><code class="lang-auto">dummy_params = []

def init_buffers(module, params, flag=True):
    for name in list(module._parameters.keys()):
        if module._parameters[name] is None:
            continue
        data = module._parameters[name].data
        module._parameters.pop(name)
        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))
</code></pre>

albanD: <p>Hi,</p>
<p>The problem is that <code>"%beta" % name</code> is not a valid string replacement in python I think?<br>
At least this is what the error message indicates.</p>

kirk86: <p>Thanks again for saving the day <a class="mention" href="/u/alband">@albanD</a>.</p>
<p>If you don’t mind me asking a couple follow ups just to clarify my understanding?</p>
<p>1.) Is this the default way to pass multiple args into <code>.apply()</code>, or is there a better way?</p>
<pre><code class="lang-auto">net.apply(lambda module: init_buffers(module=module, params=params))
</code></pre>
<p>If you notice in the example there’s <code>dummy_params = []</code> list which holds values of <code>module</code> and <code>name</code> but because I’ve <code>popped module params</code> and register different params and names before adding them to <code>dummy_list</code>, now if I <code>print(dummy_list)</code> it throws an error of param <code>bias</code> not existing in module.</p>
<p>The only way to get values from <code>dummy_params</code> is to iterate over and use <code>module.__getattr__('%s_alpha' % name)</code>, which leads me to following question.</p>
<p>2.) Is <code>module.__getattr__</code> the default way to retrieve values from <code>module.register_buffer</code>, even if I had them stored in <code>dummy_list</code>?</p>

albanD: <p>Hi,</p>
<p>I am not sure how to use apply. I would refer you to the doc <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
ANd you can do <code>mod.your_buffer</code> after registering it yes.</p>

kirk86: <aside class="quote no-group" data-username="albanD" data-post="4" data-topic="108857">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>ANd you can do <code>mod.your_buffer</code> after registering it yes.</p>
</blockquote>
</aside>
<p>That’s great thank you!</p>