generated from ashleve/lightning-hydra-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from lamalab-org/hydra
Run model from `hydra` setup
- Loading branch information
Showing
11 changed files
with
179 additions
and
168 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
_target_: molbind.data.dataloaders.load_combined_loader | ||
central_modality: "smiles" | ||
modalities: | ||
- "selfies" | ||
train_frac : 0.8 | ||
val_frac : 0.2 | ||
valid_frac : 0.2 | ||
seed: 42 | ||
fraction_data: 1.0 | ||
dataset_path: "subset.csv" | ||
dataset_path: "${paths.data_dir}/subset.csv" | ||
batch_size: 64 |
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,7 +1,4 @@ | ||
# https://wandb.ai | ||
|
||
wandb: | ||
_target_: lightning.pytorch.loggers.wandb.WandbLogger | ||
offline: False | ||
project: "molbind" | ||
entity: "adrianmirza" | ||
entity: "wandb_username" |
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,29 @@ | ||
# model architecture | ||
encoders: | ||
smiles: | ||
pretrained: True | ||
freeze_encoder: False | ||
|
||
selfies: | ||
pretrained: True | ||
freeze_encoder: False | ||
|
||
projection_heads: | ||
smiles: | ||
dims: [768, 256, 128] | ||
activation: LeakyReLU | ||
batch_norm: False | ||
selfies: | ||
dims: [768, 256, 128] | ||
activation: LeakyReLU | ||
batch_norm: False | ||
|
||
optimizer: | ||
lr: 0.0001 | ||
weight_decay: 0.0001 | ||
|
||
loss: | ||
temperature: 0.1 | ||
|
||
# compile model for faster training with pytorch 2.0 | ||
compile: false |
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
File renamed without changes.
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,33 +1,95 @@ | ||
from molbind.models.lightning_module import train_molbind | ||
import hydra | ||
import pytorch_lightning as L | ||
import polars as pl | ||
from molbind.data.dataloaders import load_combined_loader | ||
from molbind.models.lightning_module import MolBindModule | ||
from omegaconf import DictConfig | ||
import torch | ||
import rootutils | ||
|
||
rootutils.setup_root(__file__, indicator=".project-root", pythonpath=True) | ||
|
||
if __name__ == "__main__": | ||
config = { | ||
"wandb": {"entity": "adrianmirza", "project_name": "embedbind"}, | ||
"model": { | ||
"projection_heads": { | ||
"selfies": {"dims": [256, 128], "activation": "leakyrelu", "batch_norm": False}, | ||
"smiles": {"dims": [256, 128], "activation": "leakyrelu", "batch_norm": False}, | ||
}, | ||
"encoders": { | ||
"smiles": {"pretrained": True, "freeze_encoder": False}, | ||
"selfies": {"pretrained": True, "freeze_encoder": False}, | ||
}, | ||
"optimizer": {"lr": 1e-4, "weight_decay": 1e-4}, | ||
}, | ||
"loss": {"temperature": 0.1}, | ||
"data": { | ||
"central_modality": "smiles", | ||
"modalities": ["selfies"], | ||
"dataset_path": "subset.csv", | ||
"train_frac": 0.8, | ||
"valid_frac": 0.2, | ||
"seed": 42, | ||
"fraction_data": 1, | ||
"batch_size": 64, | ||
}, | ||
} | ||
|
||
config = DictConfig(config) | ||
|
||
def train_molbind(config: DictConfig): | ||
wandb_logger = L.loggers.WandbLogger(**config.logger.wandb) | ||
|
||
device_count = torch.cuda.device_count() | ||
trainer = L.Trainer( | ||
max_epochs=100, | ||
accelerator="cuda", | ||
log_every_n_steps=10, | ||
logger=wandb_logger, | ||
devices=device_count if device_count > 1 else "auto", | ||
strategy="ddp" if device_count > 1 else "auto", | ||
) | ||
|
||
train_modality_data = {} | ||
valid_modality_data = {} | ||
|
||
# Example SMILES - SELFIES modality pair: | ||
data = pl.read_csv(config.data.dataset_path) | ||
shuffled_data = data.sample( | ||
fraction=config.data.fraction_data, shuffle=True, seed=config.data.seed | ||
) | ||
dataset_length = len(shuffled_data) | ||
valid_shuffled_data = shuffled_data.tail( | ||
int(config.data.valid_frac * dataset_length) | ||
) | ||
train_shuffled_data = shuffled_data.head( | ||
int(config.data.train_frac * dataset_length) | ||
) | ||
|
||
columns = shuffled_data.columns | ||
# extract non-central modalities | ||
non_central_modalities = config.data.modalities | ||
central_modality = config.data.central_modality | ||
|
||
for column in columns: | ||
if column in non_central_modalities: | ||
# drop nan for specific pair | ||
train_modality_pair = train_shuffled_data[ | ||
[central_modality, column] | ||
].drop_nulls() | ||
valid_modality_pair = valid_shuffled_data[ | ||
[central_modality, column] | ||
].drop_nulls() | ||
|
||
train_modality_data[column] = [ | ||
train_modality_pair[central_modality].to_list(), | ||
train_modality_pair[column].to_list(), | ||
] | ||
valid_modality_data[column] = [ | ||
valid_modality_pair[central_modality].to_list(), | ||
valid_modality_pair[column].to_list(), | ||
] | ||
|
||
combined_loader = load_combined_loader( | ||
central_modality=config.data.central_modality, | ||
data_modalities=train_modality_data, | ||
batch_size=config.data.batch_size, | ||
shuffle=True, | ||
num_workers=1, | ||
) | ||
|
||
valid_dataloader = load_combined_loader( | ||
central_modality=config.data.central_modality, | ||
data_modalities=valid_modality_data, | ||
batch_size=config.data.batch_size, | ||
shuffle=False, | ||
num_workers=1, | ||
) | ||
|
||
trainer.fit( | ||
MolBindModule(config), | ||
train_dataloaders=combined_loader, | ||
val_dataloaders=valid_dataloader, | ||
) | ||
|
||
|
||
@hydra.main(version_base="1.3", config_path="../configs", config_name="train.yaml") | ||
def main(config: DictConfig): | ||
train_molbind(config) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
Oops, something went wrong.