Skip to content
class

ParameterList

extendsModule
ParameterList(values: Iterable[Tensor] | None = None)

A list-like container that registers Parameter objects with the module.

ParameterList stores an ordered sequence of Parameter leaf tensors and registers each one via register_parameter. This means every stored parameter appears in parameters(), participates in gradient tracking, and is included in state_dict() / load_state_dict.

It is semantically equivalent to assigning a list of Parameter\s as attributes of a module, but is the correct approach when the number of parameters is data-dependent or only known at runtime.

Parameters

valuesIterable[Tensor] or None= None
Initial iterable of Parameter objects, or plain tensors -- each wrapped in a Parameter first, as every entry point that adds one does. Each is registered under its zero-based integer index formatted as a string. Pass None (default) for an empty list.

Attributes

_parametersOrderedDict[str, Parameter | None]
Internal ordered mapping from string index to Parameter, inherited from Module.

Notes

  • forward is intentionally not implemented. Access individual parameters via indexing: self.scales[i].
  • Unlike ModuleList, this container holds Parameter objects (leaf tensors with requires_grad=True by default), not arbitrary modules.

Examples

**Per-layer learnable temperature scales (e.g. knowledge distillation):**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> class ScaledDistiller(nn.Module):
...     def __init__(self, n_layers: int) -> None:
...         super().__init__()
...         self.scales = nn.ParameterList(
...             [nn.Parameter(lucid.ones(1)) for _ in range(n_layers)]
...         )
...
...     def forward(self, feats: list[lucid.Tensor]) -> list[lucid.Tensor]:
...         return [f * self.scales[i] for i, f in enumerate(feats)]
>>>
>>> distiller = ScaledDistiller(n_layers=6)
**Dynamically growing learnable biases:**
>>> bias_bank = nn.ParameterList()
>>> for _ in range(4):
...     bias_bank.append(nn.Parameter(lucid.zeros(128)))
>>> # All 4 biases visible to optimizer:
>>> n_params = sum(p.numel() for p in bias_bank.parameters())

Used by 1

Constructors

1
dunder

__init__

→None
__init__(values: Iterable[Tensor] | None = None)

Initialise the ParameterList module. See the class docstring for parameter semantics.

Instance methods

3
fn

append

→None
append(param: Tensor)

Append a module to the end of the ParameterList.

A plain tensor is wrapped in a Parameter, as the reference does for every entry point that adds one.

fn

extend

→None
extend(parameters: Iterable[Tensor])

Append each module from an iterable to the ParameterList.

fn

forward

→Tensor
forward(args: object = ())

Apply the contained modules to the input.

Parameters

None
No description.

Returns

Tensor

Output tensor produced by the contained modules.

Dunder methods

4
dunder

__getitem__

→Parameter | None
__getitem__(idx: int)

Return the parameter at the given index, as the reference does -- None if that slot was explicitly cleared by assignment, never an assertion failure for reading back what was legitimately put there (LCD-571).

dunder

__iter__

→Iterator[Parameter | None]
__iter__()

Iterate over the registered parameters, None included for a slot explicitly cleared by assignment (LCD-571).

dunder

__len__

→int
__len__()

Return the number of registered child modules.

dunder

__setitem__

→None
__setitem__(idx: int, param: Tensor)

Replace the parameter at the given index.

A plain tensor is wrapped in a Parameter, as the reference does; anything else that is not a Parameter is a TypeError from the assignment.