-
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.
Refactor transforms, add tests for ContrastiveMLP
Fix prediction_writer loop
- Loading branch information
Showing
9 changed files
with
125 additions
and
48 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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright Contributors to the Cellarium project. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
|
||
import torch | ||
from torch import nn | ||
|
||
|
||
class Randomize(nn.Module): | ||
""" | ||
Randomly selects between the augmented and original data | ||
for each sample according to probability p_apply. | ||
Args: | ||
p_apply: | ||
Probability of selecting augmentation for each sample. | ||
""" | ||
|
||
def __init__(self, p_apply): | ||
super().__init__() | ||
|
||
self.p_apply = p_apply | ||
|
||
def forward(self, x_aug: torch.Tensor, x_ng: torch.Tensor) -> torch.Tensor: | ||
""" | ||
Args: | ||
x_aug: Augmented gene counts. | ||
x_ng: Gene counts. | ||
Returns: | ||
Randomized augmented gene counts. | ||
""" | ||
p_apply_n1 = torch.Tensor([self.p_apply]).expand(x_ng.shape[0], 1).type_as(x_ng) | ||
apply_mask_n1 = torch.bernoulli(p_apply_n1).bool() | ||
|
||
x_ng = torch.where(apply_mask_n1, x_aug, x_ng) | ||
return x_ng |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright Contributors to the Cellarium project. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import math | ||
import os | ||
from pathlib import Path | ||
|
||
import lightning.pytorch as pl | ||
import numpy as np | ||
import torch | ||
|
||
from cellarium.ml import CellariumModule | ||
from cellarium.ml.models import ContrastiveMLP | ||
from cellarium.ml.transforms import Duplicate | ||
from cellarium.ml.utilities.data import collate_fn | ||
from tests.common import BoringDataset | ||
|
||
|
||
def test_load_from_checkpoint_multi_device(tmp_path: Path): | ||
n, g = 4, 3 | ||
devices = int(os.environ.get("TEST_DEVICES", "1")) | ||
# dataloader | ||
train_loader = torch.utils.data.DataLoader( | ||
BoringDataset(np.arange(n * g).reshape(n, g).astype("float32")), | ||
collate_fn=collate_fn, | ||
) | ||
# model | ||
model = ContrastiveMLP( | ||
n_obs=3, | ||
embed_dim=2, | ||
hidden_size=[2], | ||
temperature=1.0, | ||
) | ||
module = CellariumModule( | ||
transforms=[Duplicate()], | ||
model=model, | ||
optim_fn=torch.optim.Adam, | ||
optim_kwargs={"lr": 1e-3}, | ||
) | ||
# trainer | ||
trainer = pl.Trainer( | ||
accelerator="cpu", | ||
devices=devices, | ||
max_epochs=1, | ||
default_root_dir=tmp_path, | ||
) | ||
# fit | ||
trainer.fit(module, train_dataloaders=train_loader) | ||
|
||
# run tests only for rank 0 | ||
if trainer.global_rank != 0: | ||
return | ||
|
||
# load model from checkpoint | ||
ckpt_path = tmp_path / f"lightning_logs/version_0/checkpoints/epoch=0-step={math.ceil(n / devices)}.ckpt" | ||
assert ckpt_path.is_file() | ||
loaded_model = CellariumModule.load_from_checkpoint(ckpt_path).model | ||
assert isinstance(loaded_model, ContrastiveMLP) |