-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3529c2
commit b7c424d
Showing
4 changed files
with
60 additions
and
64 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import pytest | ||
import torch | ||
|
||
from lightly.loss.regularizer import CO2Regularizer | ||
|
||
|
||
class TestCO2Regularizer: | ||
@pytest.mark.parametrize("bsz", range(1, 20)) | ||
def test_forward_pass_no_memory_bank(self, bsz: int) -> None: | ||
reg = CO2Regularizer(memory_bank_size=0) | ||
batch_1 = torch.randn((bsz, 32)) | ||
batch_2 = torch.randn((bsz, 32)) | ||
|
||
# symmetry | ||
l1 = reg(batch_1, batch_2) | ||
l2 = reg(batch_2, batch_1) | ||
assert l1 == pytest.approx(l2) | ||
|
||
@pytest.mark.parametrize("bsz", range(1, 20)) | ||
def test_forward_pass_memory_bank(self, bsz: int) -> None: | ||
reg = CO2Regularizer(memory_bank_size=(4096, 32)) | ||
batch_1 = torch.randn((bsz, 32)) | ||
batch_2 = torch.randn((bsz, 32)) | ||
|
||
l1 = reg(batch_1, batch_2) | ||
assert l1 > 0 | ||
|
||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="No cuda") | ||
@pytest.mark.parametrize("bsz", range(1, 20)) | ||
def test_forward_pass_cuda_no_memory_bank(self, bsz: int) -> None: | ||
reg = CO2Regularizer(memory_bank_size=0) | ||
batch_1 = torch.randn((bsz, 32)).cuda() | ||
batch_2 = torch.randn((bsz, 32)).cuda() | ||
|
||
# symmetry | ||
l1 = reg(batch_1, batch_2) | ||
l2 = reg(batch_2, batch_1) | ||
assert l1 == pytest.approx(l2) | ||
|
||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="No cuda") | ||
@pytest.mark.parametrize("bsz", range(1, 20)) | ||
def test_forward_pass_cuda_memory_bank(self, bsz: int) -> None: | ||
reg = CO2Regularizer(memory_bank_size=(4096, 32)) | ||
batch_1 = torch.randn((bsz, 32)).cuda() | ||
batch_2 = torch.randn((bsz, 32)).cuda() | ||
|
||
# symmetry | ||
l1 = reg(batch_1, batch_2) | ||
assert l1 > 0 |