Skip to content
class

Buffer

extendsTensor
Buffer(data: Tensor | None = None, persistent: bool = True)

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

dataTensor= None
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= True
Whether the buffer is part of the module's state_dict. Default: True.

Attributes

persistentbool
The 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']

Used by 2

Constructors

1
dunder

__init__

→None
__init__(data: Tensor | None = None, persistent: bool = True)

Initialise the instance. See the class docstring for parameter semantics.

Dunder methods

1
dunder

__repr__

→str
__repr__()

Return a developer-facing string representation of the instance.