Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
UvA DL Tutorials: Update accelerator API to PL 1.8
  • Loading branch information
phlippe committed Jan 4, 2023
commit a6657dd9749df0c0212b58edd437a094c85051a3
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ def train_model(model_name, save_name=None, **kwargs):
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, save_name), # Where to save models
# We run on a single GPU (if possible)
gpus=1 if str(device) == "cuda:0" else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
# How many epochs to train for if no patience is set
max_epochs=180,
callbacks=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,8 @@ def train_reverse(**kwargs):
trainer = pl.Trainer(
default_root_dir=root_dir,
callbacks=[ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc")],
gpus=1 if str(device).startswith("cuda") else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=10,
gradient_clip_val=5,
enable_progress_bar=True,
Expand Down Expand Up @@ -1439,7 +1440,8 @@ def train_anomaly(**kwargs):
trainer = pl.Trainer(
default_root_dir=root_dir,
callbacks=[ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc")],
gpus=1 if str(device).startswith("cuda") else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=100,
gradient_clip_val=2,
enable_progress_bar=True,
Expand Down
6 changes: 4 additions & 2 deletions course_UvA-DL/06-graph-neural-networks/GNN_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ def train_node_classifier(model_name, dataset, **model_kwargs):
trainer = pl.Trainer(
default_root_dir=root_dir,
callbacks=[ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc")],
gpus=AVAIL_GPUS,
accelerator="gpu" if AVAIL_GPUS > 0 else "cpu",
devices=max(1, AVAIL_GPUS),
max_epochs=200,
enable_progress_bar=False,
) # 0 because epoch size is 1
Expand Down Expand Up @@ -932,7 +933,8 @@ def train_graph_classifier(model_name, **model_kwargs):
trainer = pl.Trainer(
default_root_dir=root_dir,
callbacks=[ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc")],
gpus=AVAIL_GPUS,
accelerator="gpu" if AVAIL_GPUS > 0 else "cpu",
devices=max(1, AVAIL_GPUS),
max_epochs=500,
enable_progress_bar=False,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ def train_model(**kwargs):
# Create a PyTorch Lightning trainer with the generation callback
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "MNIST"),
gpus=1 if str(device).startswith("cuda") else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=60,
gradient_clip_val=0.1,
callbacks=[
Expand Down
3 changes: 2 additions & 1 deletion course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ def train_cifar(latent_dim):
# Create a PyTorch Lightning trainer with the generation callback
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "cifar10_%i" % latent_dim),
gpus=1 if str(device).startswith("cuda") else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=500,
callbacks=[
ModelCheckpoint(save_weights_only=True),
Expand Down
3 changes: 2 additions & 1 deletion course_UvA-DL/09-normalizing-flows/NF_image_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,8 @@ def train_flow(flow, model_name="MNISTFlow"):
# Create a PyTorch Lightning trainer
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, model_name),
gpus=1 if torch.cuda.is_available() else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=200,
gradient_clip_val=1.0,
callbacks=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ def train_model(**kwargs):
# Create a PyTorch Lightning trainer with the generation callback
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "PixelCNN"),
gpus=1 if str(device).startswith("cuda") else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=150,
callbacks=[
ModelCheckpoint(save_weights_only=True, mode="min", monitor="val_bpd"),
Expand Down
3 changes: 2 additions & 1 deletion course_UvA-DL/11-vision-transformer/Vision_Transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ def test_step(self, batch, batch_idx):
def train_model(**kwargs):
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "ViT"),
gpus=1 if str(device) == "cuda:0" else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=180,
callbacks=[
ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc"),
Expand Down
3 changes: 2 additions & 1 deletion course_UvA-DL/12-meta-learning/Meta_Learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ def validation_step(self, batch, batch_idx):
def train_model(model_class, train_loader, val_loader, **kwargs):
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, model_class.__name__),
gpus=1 if str(device) == "cuda:0" else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=200,
callbacks=[
ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc"),
Expand Down
9 changes: 6 additions & 3 deletions course_UvA-DL/13-contrastive-learning/SimCLR.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ def validation_step(self, batch, batch_idx):
def train_simclr(batch_size, max_epochs=500, **kwargs):
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "SimCLR"),
gpus=1 if str(device) == "cuda:0" else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=max_epochs,
callbacks=[
ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc_top5"),
Expand Down Expand Up @@ -540,7 +541,8 @@ def prepare_data_features(model, dataset):
def train_logreg(batch_size, train_feats_data, test_feats_data, model_suffix, max_epochs=100, **kwargs):
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "LogisticRegression"),
gpus=1 if str(device) == "cuda:0" else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=max_epochs,
callbacks=[
ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc"),
Expand Down Expand Up @@ -731,7 +733,8 @@ def test_step(self, batch, batch_idx):
def train_resnet(batch_size, max_epochs=100, **kwargs):
trainer = pl.Trainer(
default_root_dir=os.path.join(CHECKPOINT_PATH, "ResNet"),
gpus=1 if str(device) == "cuda:0" else 0,
accelerator="gpu" if str(device).startswith("cuda") else "cpu",
devices=1,
max_epochs=max_epochs,
callbacks=[
ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc"),
Expand Down