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
5 changes: 2 additions & 3 deletions .github/workflows/ci_test-acts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ jobs:
- name: Install requirements
run: |
pip --version
pip install -q -r .actions/requires.txt
pip install -q "pytest==6.*" coverage jupytext
pip install -q -r .actions/requires.txt -r _requirements/test.txt
# this is needed to be able to run package version parsing test
pip install -q matplotlib -r _requirements/default.txt --find-links https://download.pytorch.org/whl/cpu/torch_stable.html
pip install -q -r _requirements/default.txt --find-links https://download.pytorch.org/whl/cpu/torch_stable.html

- name: Prepare dummy inputs
run: |
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ repos:
- mdformat-black
- mdformat_frontmatter

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.259
hooks:
- id: flake8
- id: ruff
args: ["--fix"]
1 change: 1 addition & 0 deletions _requirements/default.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
setuptools==67.4.0
matplotlib>=3.0.0, <3.4.0
ipython[notebook]>=8.0.0, <8.12.0
torch>=1.8.1, <1.14.0
pytorch-lightning>=1.4, <2.0.0
Expand Down
5 changes: 5 additions & 0 deletions _requirements/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage>=5.0
codecov>=2.1
pytest>=6.0
pytest-cov
jupytext
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,11 @@ def forward(self, x):

class XORDataset(data.Dataset):
def __init__(self, size, std=0.1):
"""
Inputs:
size - Number of data points we want to generate
std - Standard deviation of the noise (see generate_continuous_xor function)
"""XORDataset.

Args:
size: Number of data points we want to generate
std: Standard deviation of the noise (see generate_continuous_xor function)
"""
super().__init__()
self.size = size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def get_grads(act_fn, x):
Args:
act_fn: An object of the class "ActivationFunction" with an implemented forward pass.
x: 1D input tensor.

Returns:
A tensor with the same size of x containing the gradients of act_fn at x.
"""
Expand Down Expand Up @@ -277,7 +278,8 @@ def vis_act_fn(act_fn, ax, x):
# %%
class BaseNetwork(nn.Module):
def __init__(self, act_fn, input_size=784, num_classes=10, hidden_sizes=[512, 256, 256, 128]):
"""
"""Base Network.

Args:
act_fn: Object of the activation function that should be used as non-linearity in the network.
input_size: Size of the input images in pixels
Expand Down Expand Up @@ -429,7 +431,8 @@ def save_model(model, model_path, model_name):

# %%
def visualize_gradients(net, color="C0"):
"""
"""Visualize gradients.

Args:
net: Object of class BaseNetwork
color: Color in which we want to visualize the histogram (for easier separation of activation functions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@
# %%
class BaseNetwork(nn.Module):
def __init__(self, act_fn, input_size=784, num_classes=10, hidden_sizes=[512, 256, 256, 128]):
"""
"""Base Network.

Args:
act_fn: Object of the activation function that should be used as non-linearity in the network.
input_size: Size of the input images in pixels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,13 @@
# %%
class CIFARModule(L.LightningModule):
def __init__(self, model_name, model_hparams, optimizer_name, optimizer_hparams):
"""
Inputs:
model_name - Name of the model/CNN to run. Used for creating the model (see function below)
model_hparams - Hyperparameters for the model, as dictionary.
optimizer_name - Name of the optimizer to use. Currently supported: Adam, SGD
optimizer_hparams - Hyperparameters for the optimizer, as dictionary. This includes learning rate, weight decay, etc.
"""CIFARModule.

Args:
model_name: Name of the model/CNN to run. Used for creating the model (see function below)
model_hparams: Hyperparameters for the model, as dictionary.
optimizer_name: Name of the optimizer to use. Currently supported: Adam, SGD
optimizer_hparams: Hyperparameters for the optimizer, as dictionary. This includes learning rate, weight decay, etc.
"""
super().__init__()
# Exports the hyperparameters to a YAML file, and create "self.hparams" namespace
Expand Down Expand Up @@ -337,10 +338,11 @@ def create_model(model_name, model_hparams):

# %%
def train_model(model_name, save_name=None, **kwargs):
"""
Inputs:
model_name - Name of the model you want to run. Is used to look up the class in "model_dict"
save_name (optional) - If specified, this name will be used for creating the checkpoint and logging directory.
"""Train model.

Args:
model_name: Name of the model you want to run. Is used to look up the class in "model_dict"
save_name (optional): If specified, this name will be used for creating the checkpoint and logging directory.
"""
if save_name is None:
save_name = model_name
Expand Down Expand Up @@ -417,12 +419,13 @@ def train_model(model_name, save_name=None, **kwargs):
# %%
class InceptionBlock(nn.Module):
def __init__(self, c_in, c_red: dict, c_out: dict, act_fn):
"""
Inputs:
c_in - Number of input feature maps from the previous layers
c_red - Dictionary with keys "3x3" and "5x5" specifying the output of the dimensionality reducing 1x1 convolutions
c_out - Dictionary with keys "1x1", "3x3", "5x5", and "max"
act_fn - Activation class constructor (e.g. nn.ReLU)
"""InceptionBlock.

Args:
c_in: Number of input feature maps from the previous layers
c_red: Dictionary with keys "3x3" and "5x5" specifying the output of the dimensionality reducing 1x1 convolutions
c_out: Dictionary with keys "1x1", "3x3", "5x5", and "max"
act_fn: Activation class constructor (e.g. nn.ReLU)
"""
super().__init__()

Expand Down Expand Up @@ -666,10 +669,11 @@ def forward(self, x):

class ResNetBlock(nn.Module):
def __init__(self, c_in, act_fn, subsample=False, c_out=-1):
"""
Inputs:
c_in - Number of input features
act_fn - Activation class constructor (e.g. nn.ReLU)
"""ResNetBlock.

Args:
c_in: Number of input features
act_fn: Activation class constructor (e.g. nn.ReLU)
subsample - If True, we want to apply a stride inside the block and reduce the output shape by 2 in height and width
c_out - Number of output features. Note that this is only relevant if subsample is True, as otherwise, c_out = c_in
"""
Expand Down Expand Up @@ -711,8 +715,9 @@ def forward(self, x):
# %%
class PreActResNetBlock(nn.Module):
def __init__(self, c_in, act_fn, subsample=False, c_out=-1):
"""
Inputs:
"""PreAct ResNet Block.

Args:
c_in - Number of input features
act_fn - Activation class constructor (e.g. nn.ReLU)
subsample - If True, we want to apply a stride inside the block and reduce the output shape by 2 in height and width
Expand Down Expand Up @@ -780,8 +785,9 @@ def __init__(
block_name="ResNetBlock",
**kwargs,
):
"""
Inputs:
"""ResNet.

Args:
num_classes - Number of classification outputs (10 for CIFAR10)
num_blocks - List with the number of ResNet blocks to use. The first block of each group uses downsampling, except the first.
c_hidden - List with the hidden dimensionalities in the different blocks. Usually multiplied by 2 the deeper we go.
Expand Down Expand Up @@ -949,8 +955,9 @@ def forward(self, x):
# %%
class DenseLayer(nn.Module):
def __init__(self, c_in, bn_size, growth_rate, act_fn):
"""
Inputs:
"""DenseLayer.

Args:
c_in - Number of input channels
bn_size - Bottleneck size (factor of growth rate) for the output of the 1x1 convolution. Typically between 2 and 4.
growth_rate - Number of output channels of the 3x3 convolution
Expand Down Expand Up @@ -980,8 +987,9 @@ def forward(self, x):
# %%
class DenseBlock(nn.Module):
def __init__(self, c_in, num_layers, bn_size, growth_rate, act_fn):
"""
Inputs:
"""Dense Block.

Args:
c_in - Number of input channels
num_layers - Number of dense layers to apply in the block
bn_size - Bottleneck size to use in the dense layers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ def forward(self, x, mask=None, return_attention=False):
# %%
class EncoderBlock(nn.Module):
def __init__(self, input_dim, num_heads, dim_feedforward, dropout=0.0):
"""
"""EncoderBlock.

Args:
input_dim: Dimensionality of the input
num_heads: Number of heads to use in the attention block
Expand Down Expand Up @@ -572,8 +573,9 @@ def get_attention_maps(self, x, mask=None):
# %%
class PositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=5000):
"""
Args
"""Positional Encoding.

Args:
d_model: Hidden dimensionality of the input.
max_len: Maximum length of a sequence to expect.
"""
Expand Down Expand Up @@ -758,7 +760,8 @@ def __init__(
dropout=0.0,
input_dropout=0.0,
):
"""
"""TransformerPredictor.

Args:
input_dim: Hidden dimensionality of the input
model_dim: Hidden dimensionality to use inside the Transformer
Expand Down
24 changes: 16 additions & 8 deletions course_UvA-DL/06-graph-neural-networks/GNN_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def __init__(self, c_in, c_out):
self.projection = nn.Linear(c_in, c_out)

def forward(self, node_feats, adj_matrix):
"""
"""Forward.

Args:
node_feats: Tensor with node features of shape [batch_size, num_nodes, c_in]
adj_matrix: Batch of adjacency matrices of the graph. If there is an edge from i to j,
Expand Down Expand Up @@ -317,7 +318,8 @@ def __init__(self, c_in, c_out, num_heads=1, concat_heads=True, alpha=0.2):
nn.init.xavier_uniform_(self.a.data, gain=1.414)

def forward(self, node_feats, adj_matrix, print_attn_probs=False):
"""
"""Forward.

Args:
node_feats: Input features of the node. Shape: [batch_size, c_in]
adj_matrix: Adjacency matrix including self-connections. Shape: [batch_size, num_nodes, num_nodes]
Expand Down Expand Up @@ -497,7 +499,8 @@ def __init__(
dp_rate=0.1,
**kwargs,
):
"""
"""GNNModel.

Args:
c_in: Dimension of input features
c_hidden: Dimension of hidden features
Expand All @@ -523,7 +526,8 @@ def __init__(
self.layers = nn.ModuleList(layers)

def forward(self, x, edge_index):
"""
"""Forward.

Args:
x: Input features per node
edge_index: List of vertex index pairs representing the edges in the graph (PyTorch geometric notation)
Expand All @@ -549,7 +553,8 @@ def forward(self, x, edge_index):
# %%
class MLPModel(nn.Module):
def __init__(self, c_in, c_hidden, c_out, num_layers=2, dp_rate=0.1):
"""
"""MLPModel.

Args:
c_in: Dimension of input features
c_hidden: Dimension of hidden features
Expand All @@ -567,7 +572,8 @@ def __init__(self, c_in, c_hidden, c_out, num_layers=2, dp_rate=0.1):
self.layers = nn.Sequential(*layers)

def forward(self, x, *args, **kwargs):
"""
"""Forward.

Args:
x: Input features per node
"""
Expand Down Expand Up @@ -844,7 +850,8 @@ def print_results(result_dict):
# %%
class GraphGNNModel(nn.Module):
def __init__(self, c_in, c_hidden, c_out, dp_rate_linear=0.5, **kwargs):
"""
"""GraphGNNModel.

Args:
c_in: Dimension of input features
c_hidden: Dimension of hidden features
Expand All @@ -857,7 +864,8 @@ def __init__(self, c_in, c_hidden, c_out, dp_rate_linear=0.5, **kwargs):
self.head = nn.Sequential(nn.Dropout(dp_rate_linear), nn.Linear(c_hidden, c_out))

def forward(self, x, edge_index, batch_idx):
"""
"""Forward.

Args:
x: Input features per node
edge_index: List of vertex index pairs representing the edges in the graph (PyTorch geometric notation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ def forward(self, x):
# %%
class Sampler:
def __init__(self, model, img_shape, sample_size, max_len=8192):
"""
"""Sampler.

Args:
model: Neural network to use for modeling E_theta
img_shape: Shape of the images to model
Expand Down
8 changes: 5 additions & 3 deletions course_UvA-DL/08-deep-autoencoders/Deep_Autoencoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def get_train_images(num):
# %%
class Encoder(nn.Module):
def __init__(self, num_input_channels: int, base_channel_size: int, latent_dim: int, act_fn: object = nn.GELU):
"""
"""Encoder.

Args:
num_input_channels : Number of input channels of the image. For CIFAR, this parameter is 3
base_channel_size : Number of channels we use in the first convolutional layers. Deeper layers might use a duplicate of it.
Expand Down Expand Up @@ -190,7 +191,8 @@ def forward(self, x):
# %%
class Decoder(nn.Module):
def __init__(self, num_input_channels: int, base_channel_size: int, latent_dim: int, act_fn: object = nn.GELU):
"""
"""Decoder.

Args:
num_input_channels : Number of channels of the image to reconstruct. For CIFAR, this parameter is 3
base_channel_size : Number of channels we use in the last convolutional layers. Early layers might use a duplicate of it.
Expand Down Expand Up @@ -263,7 +265,7 @@ def forward(self, x):
return x_hat

def _get_reconstruction_loss(self, batch):
"""Given a batch of images, this function returns the reconstruction loss (MSE in our case)"""
"""Given a batch of images, this function returns the reconstruction loss (MSE in our case)."""
x, _ = batch # We do not need the labels
x_hat = self.forward(x)
loss = F.mse_loss(x, x_hat, reduction="none")
Expand Down
Loading