pdft.training.train_basis_exact

pdft.training.train_basis_exact#

pdft.training.train_basis_exact(basis, *, dataset, k, sweeps, passes=1, frozen_indices=None, precision='double', callback=None)[source]#

Train a separable circuit basis by exact block-coordinate descent on its sparsity.

Parameters:
  • dataset (Sequence[Array]) – Images in the basis’s frame and of its image_size. They are summed as they are; normalise them first for each to weigh the same.

  • k (int | Sequence[int]) – The coefficient budget, or several, whose objectives are averaged.

  • sweeps (int) – Rounds of the alternation: the codes, then every block’s gates.

  • passes (int) – Visits to every gate of a block per sweep, its codes held fixed.

  • frozen_indices (Sequence[int] | None) – Indices into basis.tensors that are not updated.

  • precision (str) – "double" or "single": the precision of everything the size of an image (the images, the transforms, the codes, each image’s part of M, each block’s unitary and its gradient) for every basis and whatever the tensors’ precision, including the bases whose forward_transform casts to double (CLAUDE.md 13). "single" halves the memory these take. M is summed in double precision and each update is computed in double precision from its environment either way; the tensors come back in the dtype they came in with, unitary to its rounding.

  • callback (Callable[[int, object, float], None] | None) – callback(sweep, basis, loss) after every sweep: the basis as the sweep left it, and the objective the sweep started from.

Returns:

TrainingResult – Its loss_history holds, per sweep, the objective at its start: the energy outside the top k summed over the images (the mean over the budgets). No sweep raises it in exact arithmetic; once it has converged, rounding can, by about 1e-15 of it when the working precision and the tensors are both double, 1e-7 when either is single.

Raises:
  • ValueError – If a gate couples the two registers (separable_blocks), the dataset or k is empty, an image is not (2**m, 2**n) or not finite, a budget, sweeps or passes is below 1, a frozen index is not one of the basis’s tensors, or precision is neither "double" nor "single".

  • TypeError – For a basis with no circuit of its own (a BlockedBasis), or a budget, sweeps or passes that is not an integer.

Return type:

TrainingResult

Notes

When every image and every tensor is exactly real, as for a RealRichBasis on real images, each environment is exactly real and the updates are computed in real arithmetic: the basis stays exactly real. One complex tensor makes every update complex, as under the gradient trainers.

Where the data leave a gate’s best value open (several score the same, as when the images are zero or their codes already exact), the gate takes the one of them nearest its value, and is left as it is when its environment is zero; zero here means below the rounding of the working precision, next to the block’s M.

The top k is MSELoss’s, with no tie band (CLAUDE.md 14): where coefficients tie in magnitude, as conjugate pairs of a real image do at the Fourier start, rounding decides which is kept, and the CPU and the GPU, or the two precisions, can follow different trajectories.