legendre_decomp#

Submodules#

Functions#

xp_get(val)

default_B(shape, order[, xp])

Vectorized implementation of the default B tensor.

kl(P, Q[, xp])

Kullback-Leibler divergence.

get_eta(Q, D[, xp])

Eta tensor.

get_h(theta, D[, xp])

H tensor.

LD(X[, B, order, n_iter, lr, eps, error_tol, ngd, ...])

Compute many-body tensor approximation.

Package Contents#

legendre_decomp.xp_get(val)#
legendre_decomp.default_B(shape, order, xp=np)#

Vectorized implementation of the default B tensor.

Parameters:
  • shape (Sequence[int]) – Shape of the corresponding X tensor.

  • order (int) – Order of the B tensor.

  • xp (ModuleType) – Array module, either numpy (CPU) or cupy (CUDA/GPU)

Returns:

Default B tensor of specified order.

Return type:

array-like

legendre_decomp.kl(P, Q, xp=cp)#

Kullback-Leibler divergence.

Parameters:
  • P (numpy.typing.NDArray[numpy.float64]) – P tensor

  • Q (numpy.typing.NDArray[numpy.float64]) – Q tensor

  • xp (ModuleType) – Array module, either numpy (CPU) or cupy

Returns:

KL divergence.

Return type:

numpy.float64

legendre_decomp.get_eta(Q, D, xp=cp)#

Eta tensor.

Parameters:
  • Q (numpy.typing.NDArray[numpy.float64]) – Q tensor

  • D (int) – Dimensionality

  • xp (ModuleType) – Array module, either numpy (CPU) or cupy

Returns:

Eta tensor.

Return type:

numpy.typing.NDArray[numpy.float64]

legendre_decomp.get_h(theta, D, xp=cp)#

H tensor.

Parameters:
  • theta (numpy.typing.NDArray[numpy.float64]) – Theta tensor

  • D (int) – Dimensionality

  • xp (ModuleType) – Array module, either numpy (CPU) or cupy

Returns:

Updated theta.

Return type:

numpy.typing.NDArray[numpy.float64]

legendre_decomp.LD(X, B=None, order=2, n_iter=10, lr=1.0, eps=1e-05, error_tol=1e-05, ngd=True, ngd_lstsq=False, verbose=True, gpu=True, exit_abs=False, dtype=None)#

Compute many-body tensor approximation.

Parameters:
  • X (numpy.typing.NDArray[numpy.float64]) – Input tensor.

  • B (numpy.typing.NDArray[numpy.intp] | list[tuple[int, Ellipsis]] | None) – B tensor.

  • order (int) – Order of default tensor B, if not provided.

  • n_iter (int) – Maximum number of iteration.

  • lr (float) – Learning rate.

  • eps (float) – (see paper).

  • error_tol (float) – KL divergence tolerance for the iteration.

  • ngd (bool) – Use natural gradient.

  • ngd_lstsq (bool) – Use natural gradient conputed by lstsq to avoid singular matrix.

  • verbose (bool) – Print debug messages.

  • gpu (bool) – Use GPU (CUDA or ROCm depending on the installed CuPy version).

  • exit_abs (bool) – Previous implementation (wrongly?) uses kl- kl_prev as iteration exit criterion. Use abs(kl - kl_prev) instead.

  • dtype (numpy.dtype | None) – By default, the data-type is inferred from the input data.

Returns:

KL divergence history. scaleX: Scaled X tensor. Q: Q tensor. theta: Theta.

Return type:

all_history_kl