Skip to content

Commit

Permalink
Merge pull request #33 from simon-hirsch/check_rank_inv_gram
Browse files Browse the repository at this point in the history
Add check for inverse gramian
  • Loading branch information
simon-hirsch authored Dec 10, 2024
2 parents 38bd9ce + 2340353 commit db9180a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/rolch/gram.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ def init_inverted_gram(X: np.ndarray, w: np.ndarray, forget: float = 0) -> np.nd
Returns:
np.ndarray: Gramian Matrix.
Raises:
ValueError: If the matrix is not invertible (if rank(X.T @ X) < X.shape[0]).
"""
inv_gram = np.linalg.inv(init_gram(X=X, w=w, forget=forget))
return inv_gram
gram = init_gram(X=X, w=w, forget=forget)
rank = np.linalg.matrix_rank(gram)
if rank == gram.shape[0]:
inv_gram = np.linalg.inv(gram)
return inv_gram
else:
raise ValueError("Matrix is not invertible.")


# TODO (SH): For some reason we switched the syntax in C++ here
Expand Down
14 changes: 14 additions & 0 deletions tests/test_gram.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ def make_x_y_w(N, D, random_weights=True):
FORGET = [0, 0.0001, 0.001, 0.01, 0.1]
BATCH_SIZE = [10, 25]

@pytest.mark.parametrize("N", N)
@pytest.mark.parametrize("D", D)
@pytest.mark.parametrize("random_weights", RANDOM_WEIGHTS)
@pytest.mark.parametrize("forget", FORGET)
def test_inverse_rank_deficit(
N, D, random_weights, forget
):
X, _, w = make_x_y_w(N, D, random_weights=random_weights)
for d in range(1, D+1):
choice = np.random.choice(np.arange(D), d)
XX = np.hstack((X, X[:, choice]))
with pytest.raises(ValueError):
gram_start = init_inverted_gram(XX[:-1], w[:-1], forget)


@pytest.mark.parametrize("N", N)
@pytest.mark.parametrize("D", D)
Expand Down

0 comments on commit db9180a

Please sign in to comment.