albanD:

Note that for simplicity, when you do mod.name = something. If something is an nn.Parameter, register_parameter() will be called automatically. Similarly, if something is a Tensor, register_buffer will be called automatically. Otherwise, mod.name is just assigned as for any other object.


@albanD, thank you for that explanation.
I verified this to work with not using register_parameter and assigning nn.Parameter instead.
But it doesn’t seem to be the case with torch.Tensor (bolded in the quote above) automatically triggering a call to register_buffer.
e.g. inside:
class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.something = torch.zeros(1)

doesn’t add something to self._buffers
It’s empty if I dump the keys, say during forward.
print("keys", self._buffers.keys())

if I use:
self.register_buffer('something', torch.zeros(1))
it does end up in self._buffers.
tested with pytorch 1.0.0.dev20190405
Also is there a magic repr way to get pytorch to show all the registered buffers in a given or all layer? (Other than doing it manually).
Thanks.