-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use sklearn's logistic regression for linear probing (#169)
* use binary logistic regression to initialize the linear layer * plot integrated gradients from a binary classifier * add cmap to 'visual' requirements * move model assembling to lca * rename init argument * disable feature scaling * update test and evaluation scripts to use new API * add docstrings to LCA
- Loading branch information
Showing
5 changed files
with
202 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
import numpy as np | ||
import torch | ||
from sklearn.linear_model import LogisticRegression | ||
|
||
from viscy.representation.lca import Trainer, train_and_test_linear_classifier | ||
from viscy.representation.lca import linear_from_binary_logistic_regression | ||
|
||
|
||
def test_train_and_test_linear_classifier(caplog): | ||
"""Test ``train_and_test_linear_classifier``.""" | ||
embeddings = np.random.rand(10, 8) | ||
labels = np.random.randint(0, 2, 10) | ||
with caplog.at_level("INFO"): | ||
train_and_test_linear_classifier( | ||
embeddings, | ||
labels, | ||
num_classes=3, | ||
trainer=Trainer(fast_dev_run=True), | ||
batch_size=4, | ||
def test_linear_from_logistic_regression(): | ||
""" | ||
Test ``linear_from_logistic_regression``. | ||
Check that the logits from the logistic regression | ||
and the linear model are almost equal. | ||
""" | ||
rand_data = np.random.rand(100, 8) | ||
rand_labels = np.random.randint(0, 2, size=(100)) | ||
logistic_regression = LogisticRegression().fit(rand_data, rand_labels) | ||
linear_model = linear_from_binary_logistic_regression(logistic_regression) | ||
logistic_logits = logistic_regression.decision_function(rand_data) | ||
with torch.inference_mode(): | ||
torch_logits = ( | ||
linear_model(torch.from_numpy(rand_data).float()).squeeze().numpy() | ||
) | ||
assert "accuracy_macro" in caplog.text | ||
assert "f1_weighted" in caplog.text | ||
np.testing.assert_allclose(logistic_logits, torch_logits, rtol=1e-3) |
Oops, something went wrong.