fn

parameters_to_vector

Tensor
parameters_to_vector(parameters: Iterable[Parameter])
source

Flatten an iterable of parameters into a single 1-D Tensor.

Every parameter is detached, reshaped to 1-D, and concatenated in iteration order. Useful when an algorithm needs to view the whole parameter set as one vector — most commonly second-order optimisers (L-BFGS, conjugate-gradient, trust-region methods), hyper-gradient solvers, and natural-gradient pre-conditioners.

Parameters

parametersiterable of Parameter
Parameters to flatten. Each must be a lucid._tensor.tensor.Tensor (or subclass). All entries must share device and dtype; a mismatch raises ValueError.

Returns

Tensor

1-D tensor of length knumel(pk)\sum_k \text{numel}(p_k) on the same device / dtype as the first parameter. requires_grad is False — the result is a detached snapshot, not a graph node.

Notes

Given parameters p1,p2,,pKp_1, p_2, \dots, p_K with sizes nkn_k, the returned vector is

v  =  [vec(p1);vec(p2);;vec(pK)]Rknk.v \;=\; \bigl[\,\operatorname{vec}(p_1);\, \operatorname{vec}(p_2);\, \dots;\, \operatorname{vec}(p_K)\,\bigr] \in \mathbb{R}^{\sum_k n_k}.

The inverse operation is vector_to_parameters, which copies a 1-D tensor of the same length back into the original parameter shapes.

Examples

>>> import lucid
>>> from lucid.nn.utils import parameters_to_vector
>>> vec = parameters_to_vector(model.parameters())
>>> vec.shape
(n_params,)