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 all commits
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
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand All @@ -23,7 +23,7 @@ repos:
- id: detect-private-key

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies: [tomli]
Expand All @@ -37,7 +37,7 @@ repos:
args: ["--in-place"]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
rev: v4.0.0-alpha.8
hooks:
- id: prettier
files: \.(json|yml|yaml|toml)
Expand All @@ -54,7 +54,7 @@ repos:
- mdformat_frontmatter

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
rev: v0.5.0
hooks:
# try to fix what is possible
- id: ruff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down Expand Up @@ -796,7 +796,7 @@ def _create_model(self):
num_heads=self.hparams.num_heads,
dropout=self.hparams.dropout,
)
# Output classifier per sequence lement
# Output classifier per sequence element
self.output_net = nn.Sequential(
nn.Linear(self.hparams.model_dim, self.hparams.model_dim),
nn.LayerNorm(self.hparams.model_dim),
Expand Down Expand Up @@ -948,8 +948,8 @@ def _calculate_loss(self, batch, mode="train"):
acc = (preds.argmax(dim=-1) == labels).float().mean()

# Logging
self.log("%s_loss" % mode, loss)
self.log("%s_acc" % mode, acc)
self.log(f"{mode}_loss", loss)
self.log(f"{mode}_acc", acc)
return loss, acc

def training_step(self, batch, batch_idx):
Expand Down Expand Up @@ -1419,8 +1419,8 @@ def _calculate_loss(self, batch, mode="train"):
preds = preds.squeeze(dim=-1) # Shape: [Batch_size, set_size]
loss = F.cross_entropy(preds, labels) # Softmax/CE over set dimension
acc = (preds.argmax(dim=-1) == labels).float().mean()
self.log("%s_loss" % mode, loss)
self.log("%s_acc" % mode, acc, on_step=False, on_epoch=True)
self.log(f"{mode}_loss", loss)
self.log(f"{mode}_acc", acc, on_step=False, on_epoch=True)
return loss, acc

def training_step(self, batch, batch_idx):
Expand Down
10 changes: 5 additions & 5 deletions course_UvA-DL/06-graph-neural-networks/GNN_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down Expand Up @@ -616,7 +616,7 @@ def forward(self, data, mode="train"):
elif mode == "test":
mask = data.test_mask
else:
assert False, "Unknown forward mode: %s" % mode
assert False, f"Unknown forward mode: {mode}"

loss = self.loss_module(x[mask], data.y[mask])
acc = (x[mask].argmax(dim=-1) == data.y[mask]).sum().float() / mask.sum()
Expand Down Expand Up @@ -671,7 +671,7 @@ def train_node_classifier(model_name, dataset, **model_kwargs):
trainer.logger._default_hp_metric = None # Optional logging argument that we don't need

# Check whether pretrained model exists. If yes, load it and skip training
pretrained_filename = os.path.join(CHECKPOINT_PATH, "NodeLevel%s.ckpt" % model_name)
pretrained_filename = os.path.join(CHECKPOINT_PATH, f"NodeLevel{model_name}.ckpt")
if os.path.isfile(pretrained_filename):
print("Found pretrained model, loading...")
model = NodeLevelGNN.load_from_checkpoint(pretrained_filename)
Expand Down Expand Up @@ -790,7 +790,7 @@ def print_results(result_dict):
# %%
print("Data object:", tu_dataset.data)
print("Length:", len(tu_dataset))
print("Average label: %4.2f" % (tu_dataset.data.y.float().mean().item()))
print(f"Average label: {tu_dataset.data.y.float().mean().item():4.2f}")

# %% [markdown]
# The first line shows how the dataset stores different graphs.
Expand Down Expand Up @@ -957,7 +957,7 @@ def train_graph_classifier(model_name, **model_kwargs):
trainer.logger._default_hp_metric = None

# Check whether pretrained model exists. If yes, load it and skip training
pretrained_filename = os.path.join(CHECKPOINT_PATH, "GraphLevel%s.ckpt" % model_name)
pretrained_filename = os.path.join(CHECKPOINT_PATH, f"GraphLevel{model_name}.ckpt")
if os.path.isfile(pretrained_filename):
print("Found pretrained model, loading...")
model = GraphLevelGNN.load_from_checkpoint(pretrained_filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down Expand Up @@ -770,7 +770,7 @@ def train_model(**kwargs):
rand_imgs = torch.rand((128,) + model.hparams.img_shape).to(model.device)
rand_imgs = rand_imgs * 2 - 1.0
rand_out = model.cnn(rand_imgs).mean()
print("Average score for random images: %4.2f" % (rand_out.item()))
print(f"Average score for random images: {rand_out.item():4.2f}")

# %% [markdown]
# As we hoped, the model assigns very low probability to those noisy images.
Expand All @@ -781,7 +781,7 @@ def train_model(**kwargs):
train_imgs, _ = next(iter(train_loader))
train_imgs = train_imgs.to(model.device)
train_out = model.cnn(train_imgs).mean()
print("Average score for training images: %4.2f" % (train_out.item()))
print(f"Average score for training images: {train_out.item():4.2f}")

# %% [markdown]
# The scores are close to 0 because of the regularization objective that was added to the training.
Expand All @@ -803,8 +803,8 @@ def compare_images(img1, img2):
plt.xticks([(img1.shape[2] + 2) * (0.5 + j) for j in range(2)], labels=["Original image", "Transformed image"])
plt.yticks([])
plt.show()
print("Score original image: %4.2f" % score1)
print("Score transformed image: %4.2f" % score2)
print(f"Score original image: {score1:4.2f}")
print(f"Score transformed image: {score2:4.2f}")


# %% [markdown]
Expand Down
2 changes: 1 addition & 1 deletion course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
file_path = os.path.join(CHECKPOINT_PATH, file_name)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down
8 changes: 4 additions & 4 deletions course_UvA-DL/09-normalizing-flows/NF_image_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
file_path = os.path.join(CHECKPOINT_PATH, file_name)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down Expand Up @@ -518,7 +518,7 @@ def visualize_dequantization(quants, prior=None):
plt.plot([inp[indices[0][-1]]] * 2, [0, prob[indices[0][-1]]], color=color)
x_ticks.append(inp[indices[0][0]])
x_ticks.append(inp.max())
plt.xticks(x_ticks, ["%.1f" % x for x in x_ticks])
plt.xticks(x_ticks, [f"{x:.1f}" for x in x_ticks])
plt.plot(inp, prob, color=(0.0, 0.0, 0.0))
# Set final plot properties
plt.ylim(0, prob.max() * 1.1)
Expand Down Expand Up @@ -1199,8 +1199,8 @@ def print_num_params(model):
table = [
[
key,
"%4.3f bpd" % flow_dict[key]["result"]["val"][0]["test_bpd"],
"%4.3f bpd" % flow_dict[key]["result"]["test"][0]["test_bpd"],
"{:4.3f} bpd".format(flow_dict[key]["result"]["val"][0]["test_bpd"]),
"{:4.3f} bpd".format(flow_dict[key]["result"]["test"][0]["test_bpd"]),
"%2.0f ms" % (1000 * flow_dict[key]["result"]["time"]),
"%2.0f ms" % (1000 * flow_dict[key]["result"].get("samp_time", 0)),
"{:,}".format(sum(np.prod(p.shape) for p in flow_dict[key]["model"].parameters())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
file_path = os.path.join(CHECKPOINT_PATH, file_name)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down
8 changes: 4 additions & 4 deletions course_UvA-DL/11-vision-transformer/Vision_Transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down Expand Up @@ -353,8 +353,8 @@ def _calculate_loss(self, batch, mode="train"):
loss = F.cross_entropy(preds, labels)
acc = (preds.argmax(dim=-1) == labels).float().mean()

self.log("%s_loss" % mode, loss)
self.log("%s_acc" % mode, acc)
self.log(f"{mode}_loss", loss)
self.log(f"{mode}_acc", acc)
return loss

def training_step(self, batch, batch_idx):
Expand Down Expand Up @@ -396,7 +396,7 @@ def train_model(**kwargs):
# Check whether pretrained model exists. If yes, load it and skip training
pretrained_filename = os.path.join(CHECKPOINT_PATH, "ViT.ckpt")
if os.path.isfile(pretrained_filename):
print("Found pretrained model at %s, loading..." % pretrained_filename)
print(f"Found pretrained model at {pretrained_filename}, loading...")
# Automatically loads the model with the saved hyperparameters
model = ViT.load_from_checkpoint(pretrained_filename)
else:
Expand Down
12 changes: 6 additions & 6 deletions course_UvA-DL/12-meta-learning/Meta_Learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)
if not os.path.isfile(file_path):
file_url = base_url + file_name
print("Downloading %s..." % file_url)
print(f"Downloading {file_url}...")
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:
Expand Down Expand Up @@ -525,8 +525,8 @@ def calculate_loss(self, batch, mode):
preds, labels, acc = self.classify_feats(prototypes, classes, query_feats, query_targets)
loss = F.cross_entropy(preds, labels)

self.log("%s_loss" % mode, loss)
self.log("%s_acc" % mode, acc)
self.log(f"{mode}_loss", loss)
self.log(f"{mode}_acc", acc)
return loss

def training_step(self, batch, batch_idx):
Expand Down Expand Up @@ -573,7 +573,7 @@ def train_model(model_class, train_loader, val_loader, **kwargs):
# Check whether pretrained model exists. If yes, load it and skip training
pretrained_filename = os.path.join(CHECKPOINT_PATH, model_class.__name__ + ".ckpt")
if os.path.isfile(pretrained_filename):
print("Found pretrained model at %s, loading..." % pretrained_filename)
print(f"Found pretrained model at {pretrained_filename}, loading...")
# Automatically loads the model with the saved hyperparameters
model = model_class.load_from_checkpoint(pretrained_filename)
else:
Expand Down Expand Up @@ -947,8 +947,8 @@ def outer_loop(self, batch, mode="train"):
opt.step()
opt.zero_grad()

self.log("%s_loss" % mode, sum(losses) / len(losses))
self.log("%s_acc" % mode, sum(accuracies) / len(accuracies))
self.log(f"{mode}_loss", sum(losses) / len(losses))
self.log(f"{mode}_acc", sum(accuracies) / len(accuracies))

def training_step(self, batch, batch_idx):
self.outer_loop(batch, mode="train")
Expand Down
2 changes: 1 addition & 1 deletion course_UvA-DL/13-contrastive-learning/SimCLR.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ def train_resnet(batch_size, max_epochs=100, **kwargs):
# Check whether pretrained model exists. If yes, load it and skip training
pretrained_filename = os.path.join(CHECKPOINT_PATH, "ResNet.ckpt")
if os.path.isfile(pretrained_filename):
print("Found pretrained model at %s, loading..." % pretrained_filename)
print(f"Found pretrained model at {pretrained_filename}, loading...")
model = ResNet.load_from_checkpoint(pretrained_filename)
else:
L.seed_everything(42) # To be reproducible
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# %% [markdown]
# In this tutorial, we'll go over the basics of lightning Flash by finetuning/predictin with an ImageClassifier on [Hymenoptera Dataset](https://www.kaggle.com/ajayrana/hymenoptera-data) containing ants and bees images.
# In this tutorial, we'll go over the basics of lightning Flash by finetuning/prediction with an ImageClassifier on [Hymenoptera Dataset](https://www.kaggle.com/ajayrana/hymenoptera-data) containing ants and bees images.
#
# # Finetuning
#
Expand Down