torch_openreml.covariance.KnownMatrix¶
- class torch_openreml.covariance.KnownMatrix(matrix)[source]¶
Bases:
MatrixFixed 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 bymanual_inverse().- Parameters:
matrix (torch.Tensor) – The known covariance matrix, a two-dimensional tensor.
- Raises:
TypeError – If
matrixis not a Torch tensor.ValueError – If
matrixis 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_specsdoes not follow any of the requirements listed in the argument description, or ifshapeis not a tuple or torch.Size.ValueError – If
shapevalues 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_defaultsFixed parameter defaults.
fixed_param_indexIndex of fixed parameters.
fixed_param_namesFixed parameter names.
fixed_param_transTransforms for fixed parameters.
free_param_defaultsFree parameter defaults.
free_param_indexIndex of free parameters.
free_param_namesFree parameter names.
free_param_transTransforms for free parameters.
The known covariance matrix.
num_fixed_paramsTotal number of fixed parameters.
num_free_paramsTotal number of free parameters.
num_paramsTotal number of parameters.
param_defaultsParameter defaults.
param_namesParameter names.
param_specsParameter specifications, returned as stored rather than copied.
param_transParameter transforms.
repr_dictKey-value pairs used to build the string representation.
shapeOutput matrix shape.
grad_modeGradient computation mode.
jacobian_methodJacobian method used by
auto_grad(), one of"jacrev","jacfwd", or"jacobian".jacobian_chunk_sizeNumber of outputs held per batch when
auto_grad()differentiates with"jacrev", orNoneto compute the Jacobian in a single batch.cacheWhether intermediate computation results are cached, toggled by
enable_cache()anddisable_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_paramsis not a Torch tensor or a dict.ValueError – If
free_paramsis 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.