bartorch.nlop.Parameters#
- class bartorch.nlop.Parameters(module)#
A module’s parameters as one argument of an operator.
FromTorchdifferentiates by its arguments, so a denoiser whose weights are to be trained inside a graph BART applies has to take them rather than close over them. A module keeps its parameters as several real tensors of several shapes; this is the one complex vector BART can carry, and the way back.The weights ride in the real part. BART’s operators are complex throughout, so the vector is complex64 and its imaginary half is an exact null direction: nothing reads it, and the gradient that comes back is complex with the answer in its real part – which is what an optimizer over the packed vector wants, and why the packed vector is the thing to hold as a
torch.nn.Parameter.- Parameters:
module (torch.nn.Module) – Read for the names and shapes of its parameters. It is not kept differentiably: what the operator differentiates is the vector.
Examples
>>> weights = nlop.Parameters(denoiser) >>> prior = nlop.FromTorch( ... lambda x, w: torch.func.functional_call(denoiser, weights.unpack(w), (x,)), ... [state, weights.shape], ... state, ... ) >>> trained = torch.nn.Parameter(weights.pack()) >>> torch.optim.Adam([trained], lr=1e-3)
and afterwards
weights.load(trained)puts them back in the module.- __init__(module)#
Methods
__init__(module)load(weights)Write
weightsback into the module, after training.pack()The module's parameters as they stand, as one complex vector.
unpack(weights)weightsas the mappingtorch.func.functional_calltakes.