A Tensor that a Module keeps as a buffer, not a parameter.
Module state that is not trained — BatchNorm's running_mean, a
positional table, a mask — is a buffer: it follows .to() and
.half(), appears in Module.buffers and, if persistent, in
state_dict. Assigning a bare Tensor as a module attribute does
none of that; assigning a Buffer does, as if
Module.register_buffer had been called with its persistent
flag.
Parameters
The buffer's value. Aliased, not copied: the buffer shares the
tensor's storage, detached from its graph and keeping its
requires_grad. None (default) is an empty float tensor.persistentbool= TrueWhether the buffer is part of the module's
state_dict.
Default: True.Attributes
persistentboolThe flag given at construction.
Notes
The module stores this object itself, so module.name is buffer.
Operations on it return plain tensors, as for Parameter.
Examples
>>> import lucid
>>> from lucid.nn import Buffer, Module
>>> class Counter(Module):
... def __init__(self):
... super().__init__()
... self.steps = Buffer(lucid.zeros(1))
... self.scratch = Buffer(lucid.zeros(1), persistent=False)
>>> m = Counter()
>>> [name for name, _ in m.named_buffers()]
['steps', 'scratch']
>>> list(m.state_dict())
['steps']