torch_openreml.covariance.IdentityMatrix

class torch_openreml.covariance.IdentityMatrix(n, dtype=None, device=None)[source]

Bases: Matrix

Fixed \(n \times n\) identity covariance matrix.

\[\symbf{V} = \symbf{I}_n\]

This matrix has no trainable parameters, so grad() always returns (None, []). It is typically used to represent independent, homoscedastic residuals.

Initialize a fixed identity matrix of size n x n.

Parameters:
  • n (int) – Matrix dimension.

  • dtype (torch.dtype, optional) – Desired dtype of the matrix. Defaults to the PyTorch default dtype.

  • device (torch.device, optional) – Desired device of the matrix. Defaults to the PyTorch default device.

Example:

import torch
from torch_openreml.covariance import IdentityMatrix

mat = IdentityMatrix(3)
mat()
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

Methods

__call__(*args, **kwargs)

Construct the matrix from a flat parameter tensor.

auto_grad(params)

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

check_params(params)

Validate a parameter tensor and return its device and dtype.

from_param_dict(param_dict)

Extract parameter tensors from a dictionary into a flat 1D tensor.

get_intermediates(params)

Retrieve cached intermediate computation results if still valid.

grad(params)

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

manual_grad(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.REML that maps parameters to the matrix Jacobian.

map_theta_to_v(theta)

An interface compatible with torch_openreml.REML that maps parameters to a matrix.

reset_intermediates()

Clear the intermediate computation cache.

set_intermediates(params, intermediates)

Cache intermediate computation results keyed by parameter hash.

set_no_grad([index, param_name])

Set the indices of parameters to exclude from gradient computation.

to_param_dict(params)

Convert a flat parameter tensor to a parameter dictionary.

trans_grad(params)

Compute the element-wise derivative of the parameter transforms.

trans_params(params)

Apply parameter transforms to a flat parameter tensor.

Attributes

no_grad_index

Indices of parameters excluded from gradient computation.

num_params

Total number of parameters.

param_names

Ordered parameter names.

repr_dict

Key-value pairs used to build the string representation.

shape

Output matrix shape.

trans

Parameter transforms.

__call__(*args, **kwargs)[source]

Construct the matrix from a flat parameter tensor.

Must be implemented by subclasses. Implementations should convert params via from_param_dict() or to_param_dict(), then call check_params() to validate and trans_params() to apply transforms before any computation.

Parameters:

params (torch.Tensor or dict) – Flat 1D parameter tensor or parameter dictionary.

Returns:

Constructed matrix of shape shape.

Return type:

torch.Tensor