Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Improve numerical stability of Frobenius estimator #120

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions curvlinops/norm/hutchinson.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Hutchinson-style matrix norm estimation."""

from numpy import dot
from scipy.sparse.linalg import LinearOperator

from curvlinops.trace.hutchinson import HutchinsonTraceEstimator
from curvlinops.sampling import random_vector


class HutchinsonSquaredFrobeniusNormEstimator:
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(self, A: LinearOperator):
Args:
A: Linear operator whose squared Frobenius norm will be estimated.
"""
self._trace_estimator = HutchinsonTraceEstimator(A.T @ A)
self._A = A

def sample(self, distribution: str = "rademacher") -> float:
"""Draw a sample from the squared Frobenius norm estimator.
Expand All @@ -59,4 +60,7 @@ def sample(self, distribution: str = "rademacher") -> float:
Returns:
Sample from the squared Frobenius norm estimator.
"""
return self._trace_estimator.sample(distribution=distribution)
dim = self._A.shape[1]
v = random_vector(dim, distribution)
Av = self._A @ v
return dot(Av, Av)
Loading