torch_openreml.covariance.CompoundSymmetricMatrix¶
- class torch_openreml.covariance.CompoundSymmetricMatrix(n, param_specs=None)[source]¶
Bases:
MatrixCompound symmetric covariance matrix with shared variance and correlation.
\[\symbf{V} = \sigma^2 \left[(1 - \rho)\symbf{I}_n + \rho \symbf{J}_n \right]\]where \(\symbf{I}_n\) is the identity matrix and \(\symbf{J}_n\) is the matrix of ones. All diagonal entries equal \(\sigma^2\) and all off-diagonal entries equal \(\sigma^2 \rho\).
For \(\symbf{V}\) to be positive definite, the correlation parameter must satisfy \(\rho > -1/(n-1)\). The default transform enforces this by mapping an unconstrained scalar through a sigmoid scaled to \((-1/(n-1),\, 1)\).
Initialize a compound symmetric covariance matrix of size
n x n.- Parameters:
n (int) – Matrix dimension.
param_specs (dict) – Parameter specifications. Keys should be strings representing parameter names. 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.
Example:
import torch from torch_openreml.covariance import CompoundSymmetricMatrix mat = CompoundSymmetricMatrix(3) mat
CompoundSymmetricMatrix(shape=(3, 3), param_specs={'sigma^2': {'fixed': False, 'default': tensor([0.]), 'trans': TransformExpPow2()}, 'rho': {'fixed': False, 'default': tensor([0.]), 'trans': TransformChain([TransformSigmoid(), TransformScaleShift(a=1.5, b=-0.5)])}})free_params = torch.tensor([0.5, 0.0]) mat(free_params)
tensor([[2.7183, 0.6796, 0.6796], [0.6796, 2.7183, 0.6796], [0.6796, 0.6796, 2.7183]])mat.grad(free_params)
(tensor([[[5.4366, 1.3591, 1.3591], [1.3591, 5.4366, 1.3591], [1.3591, 1.3591, 5.4366]], [[0.0000, 1.0194, 1.0194], [1.0194, 0.0000, 1.0194], [1.0194, 1.0194, 0.0000]]]), ['sigma^2', 'rho'])Methods
__call__([free_params])Construct the matrix from a flat parameter tensor.
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.
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 trainable parameters using a closed-form analytic expression.map_theta_to_dv(theta)An interface compatible with
torch_openreml.REMLthat maps parameters to the matrix Jacobian.map_theta_to_v(theta)An interface compatible with
torch_openreml.REMLthat maps parameters to a matrix.reset_intermediates()Clear the intermediate computation cache.
set_intermediates(params, intermediates)Cache intermediate computation results keyed by parameter hash.
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.
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.
param_transParameter transforms.
repr_dictKey-value pairs used to build the string representation.
shapeOutput matrix shape.
- __call__(free_params=None)[source]¶
Construct the matrix from a flat parameter tensor.
Must be implemented by subclasses. Implementations should convert
free_paramsviabuild_params()to validate, include fixed parameters, and apply transforms before any computation.- Parameters:
free_params (torch.Tensor or dict) – Flat 1D parameter tensor or parameter dictionary. If omitted, default values are used. Default:
None.- Returns:
Constructed matrix of shape
shape.- Return type:
torch.Tensor
- manual_grad(free_params=None)[source]¶
Compute the Jacobian of
__call__()with respect to trainable parameters using a closed-form analytic expression.- Parameters:
free_params (torch.Tensor or dict) – Flat 1D parameter tensor or parameter dictionary. If omitted, default values are used. Default:
None.- Returns:
(grad, grad_names), wheregradis a 3D tensor of shape(num_free_params, *shape)andgrad_namesis a list of the corresponding parameter names. Returns(None, [])if all parameters are fixed.- Return type:
tuple