pdft.training.train_basis_steps#
- pdft.training.train_basis_steps(basis, *, dataset, objective, optimizer, steps, rate, batch_size=2, view=ParameterView(name='tensors'), frozen_indices=None, frame=None, seed=0, callback=None)[source]#
Train
basisforstepssteps, each on a fresh batch under a fresh mask.- Parameters:
dataset (Sequence) – Real images of the basis’s
image_size, in the image’s own frame; or colour images as stacks of channel planes,(C, H, W)(pdft.color), all with the sameC.objective (Callable[[object, Array, Array], Array]) –
(basis, images, masks) -> scalar, traceable;imagesandmasksare stacked along a leading axis, the masks(B, H, W)whatever the images’ channels. For completion,pdft.tasks.completion_loss. One that ignores the masks trains on the batches alone.optimizer (RiemannianAdam) – A
RiemannianAdam; its learning rate is used as it is, at every step.rate (float) – The probability that a pixel is observed, in all its channels at once.
view (ParameterView) – What is trained. The default is the tensors, each on its manifold.
cp_phases_viewandcp_diagonals_viewtrain the controlled-phase angles as free numbers, on which the update is plain Adam; every other tensor is then left as it is.frozen_indices (list[int] | None) – Indices into the view’s parameters (for the default view, into
basis.tensors) that are not updated.frame (Callable[[Array], Array] | None) – Applied to each batch and to its masks once they are drawn, to take them from the image’s frame to the basis’s:
pdft.circuit.bit_reversefor a basis with the QFT topology.callback (Callable[[int, object, float], None] | None) –
callback(step, basis, loss)after every step: the basis as that step left it, and the loss the step started from.steps (int)
batch_size (int)
seed (int)
- Returns:
TrainingResult – Its
loss_historyholds, per step, the loss of that step’s batch before its update.- Raises:
FloatingPointError – On a loss that is not finite.
- Return type:
Notes
The draw, per step and from one
np.random.default_rng(seed), is the batch (choiceover the dataset, without replacement within a batch) and then its masks (random(batch.shape) < rate, one mask per image over its pixels; the channels of a colour image share it). That order is the completion paper’s, so a seed names the same batches and masks as it does there.Under the default view the tensors come back as complex128 whatever they went in as, as
RiemannianAdamreturns them everywhere (a frozen tensor comes back as it went in). A flat view trains its angles in double precision and writes them into tensors of the precision the basis had.Single-precision tensors on a GPU cannot be trained under the default view: the manifold of a tensor is read off its values, and there that test runs in reduced precision and takes unitary gates for phase tensors, so the run fails or leaves the manifold. It is the same defect as in the other trainers. Train in double precision, or through a flat view, which names its manifold and is not affected.