torch_openreml.covariance.KnownMatrix

class torch_openreml.covariance.KnownMatrix(matrix)[source]

Bases: Matrix

Fixed covariance matrix backed by a known tensor.

\[\symbf{V} = \symbf{K}\]

The matrix has no free parameters. Its values are the tensor supplied at construction; __call__() follows the dtype and device of the parameter tensor when one is provided. For a square, invertible matrix the inverse is precomputed at construction and returned by manual_inverse().

Parameters:

matrix (torch.Tensor) – The known covariance matrix, a two-dimensional tensor.

Raises:
  • TypeError – If matrix is not a Torch tensor.

  • ValueError – If matrix is not two-dimensional.

Example:

import torch
from torch_openreml.covariance import KnownMatrix

k = torch.tensor([[2.0, 0.5], [0.5, 1.0]])
mat = KnownMatrix(k)
mat()
tensor([[2.0000, 0.5000],
        [0.5000, 1.0000]])

Initialize a covariance matrix with parameter specifications.

Parameters:
  • shape (tuple or None) – Expected output dimensions of the constructed matrix. Used for validation; the actual shape may be set by subclasses.

  • param_specs (dict) – Parameter specifications. Keys should be non-empty strings representing parameter names, which must not contain "/". Values should be dictionaries containing the specification for each parameter. Each specification dictionary should contain the keys "fixed", "default", and "trans", representing whether the parameter is fixed or free (bool), the default value (1D torch.Tensor), and the transform (Transform), respectively.

Raises:
  • TypeError – If param_specs does not follow any of the requirements listed in the argument description, or if shape is not a tuple or torch.Size.

  • ValueError – If shape values are non-negative, or if a parameter name is empty or contains "/".

Methods

__call__([free_params])

Return the known covariance matrix.

auto_grad([free_params])

Compute the Jacobian of build() with respect to free parameters using automatic differentiation.

build_params([free_params, include_fixed, ...])

Construct the full parameter tensor from free parameters.

disable_cache()

Disable caching of intermediate computation results.

enable_cache()

Enable caching of intermediate computation results.

get_default_dtype_device()

Get the dtype and the device of the parameter defaults.

get_intermediates(params)

Retrieve cached intermediate computation results if still valid.

grad([free_params])

Compute the Jacobian of __call__() with respect to trainable parameters.

manual_grad([free_params])

Compute the Jacobian of __call__() with respect to free parameters using a closed-form analytic expression.

manual_inverse([free_params])

Return the inverse of the known matrix.

reset_intermediates()

Clear the intermediate computation cache.

resolve_dim(n)

Resolve a matrix dimension from an integer or a vector of levels.

set_intermediates(params, intermediates)

Cache intermediate computation results keyed by the parameter values.

set_param_specs(key, *[, fixed, default, trans])

Update the fields of one or more parameter specifications in place.

trans_grad([free_params])

Compute the element-wise derivative of the free parameter transforms.

Attributes

fixed_param_defaults

Fixed parameter defaults.

fixed_param_index

Index of fixed parameters.

fixed_param_names

Fixed parameter names.

fixed_param_trans

Transforms for fixed parameters.

free_param_defaults

Free parameter defaults.

free_param_index

Index of free parameters.

free_param_names

Free parameter names.

free_param_trans

Transforms for free parameters.

matrix

The known covariance matrix.

num_fixed_params

Total number of fixed parameters.

num_free_params

Total number of free parameters.

num_params

Total number of parameters.

param_defaults

Parameter defaults.

param_names

Parameter names.

param_specs

Parameter specifications, returned as stored rather than copied.

param_trans

Parameter transforms.

repr_dict

Key-value pairs used to build the string representation.

shape

Output matrix shape.

grad_mode

Gradient computation mode.

jacobian_method

Jacobian method used by auto_grad(), one of "jacrev", "jacfwd", or "jacobian".

jacobian_chunk_size

Number of outputs held per batch when auto_grad() differentiates with "jacrev", or None to compute the Jacobian in a single batch.

cache

Whether intermediate computation results are cached, toggled by enable_cache() and disable_cache().

property matrix

The known covariance matrix.

Type:

torch.Tensor

__call__(free_params=None)[source]

Return the known covariance matrix.

Parameters:

free_params (torch.Tensor or dict, optional) – Empty parameter tensor or dict. A tensor controls the dtype and device of the returned matrix. If omitted, the PyTorch default dtype and device are used. Default: None.

Returns:

A copy of the known covariance matrix on the resolved dtype and device.

Return type:

torch.Tensor

Raises:
  • TypeError – If free_params is not a Torch tensor or a dict.

  • ValueError – If free_params is not a 1D tensor of length zero, or is a dict with any entries, since the matrix has no free parameters.

manual_inverse(free_params=None)[source]

Return the inverse of the known matrix.

The inverse is computed once at construction when the matrix is square and invertible; this method only casts the stored inverse to the dtype and device resolved from free_params.

Parameters:

free_params (torch.Tensor or dict, optional) – Empty parameter tensor or dict. A tensor controls the dtype and device of the returned inverse. If omitted, the PyTorch default dtype and device are used. Default: None.

Returns:

The inverse of the known matrix, of shape shape.

Return type:

torch.Tensor

Raises:

NotImplementedError – If the matrix is not square or is singular, so no stored inverse exists.