ICCS Summer School 2025
These slides can be viewed at:
The html and source can be found on GitHub. Follow this link:
Based on the workshop developed by Jack Atkinson and Jim Denholm:
V1.0 released and JOSE paper accepted:
The key learning objective from this workshop could be simply summarised as: Provide the ability to develop ML models in PyTorch.
Specifically:
With regards to specific ML content, we cover:
\[y = mx + c\]
\[\frac{dy}{dx} = m\]
\[y = x\]
\[\frac{dy}{dx} = 1\]
\[-\frac{dy}{dx}\]
\[L_{\text{MSE}} = \frac{1}{n}\sum_{i=1}^{n}\left(y_{i} - f(x_{i})\right)^{2}\]
Model: \(f(x) = mx + c\)
Data: \(\{x_{i}, y_{i}\}\)
Loss: \(\frac{1}{n}\sum_{i=1}^{n}(y_{i} - x_{i})^{2}\)
\[ \frac{\partial L}{\partial m} \;=\; \frac{1}{n}\sum_{i=1}^{n} 2\bigl(m\,x_{i}+c-y_{i}\bigr)\,x_{i}. \]
\[ \frac{\partial L}{\partial c} \;=\; \frac{1}{n}\sum_{i=1}^{n} 2\bigl(m\,x_{i}+c-y_{i}\bigr). \]
\[m_{n + 1} = m_{n} - \frac{dL}{dm} \cdot l_{r}\]
\[c_{n + 1} = c_{n} - \frac{dL}{dc} \cdot l_{r}\]
To fit a model we need:
\[a_{l+1} = \sigma \left( W_{l}a_{l} + b_{l} \right)\]
Image source: 3Blue1Brown
pandas
data frame).nn.Module
Dataset
class doesDataset
class MyDataset(torch.utils.data.Dataset):
def __init__(self, root_dir, split="train", transform=None):
# 1️ load or download files / labels
self.paths, self.labels = load_index_file(root_dir, split)
self.transform = transform # 2️ save transforms
The constructor is where you gather file paths, download archives, read CSVs, etc.
__len__
& __getitem__
def __len__(self):
return len(self.paths) # total #samples
def __getitem__(self, idx):
img = PIL.Image.open(self.paths[idx]).convert("RGB")
if self.transform: # 3️ apply transforms
img = self.transform(img)
label = self.labels[idx]
return img, label # 4️ single example
With these two methods PyTorch knows how big the dataset is and how to fetch one record.
Dataset
in an iterablenum_workers
) to pre‑fetch data in parallel(batch, labels)
tuples ready for the GPUnn.Sequential
mlp = torch.nn.Sequential(
torch.nn.Linear(784, 256), torch.nn.ReLU(),
torch.nn.Linear(256, 64), torch.nn.ReLU(),
torch.nn.Linear(64, 10)
)
out = mlp(torch.rand(32, 784)) # 32‑sample batch
Great for simple feed‑forward stacks when no branching logic is needed.
nn.Module
overview__init__(self)
: declare layersforward(self, x)
: define the forward pass__init__
class MyCNN(torch.nn.Module):
def __init__(self, num_classes=2):
super().__init__()
self.features = torch.nn.Sequential(
torch.nn.Conv2d(3, 32, 3, padding=1), torch.nn.ReLU(),
torch.nn.MaxPool2d(2),
torch.nn.Conv2d(32, 64, 3, padding=1), torch.nn.ReLU(),
torch.nn.MaxPool2d(2)
)
self.classifier = torch.nn.Linear(64*56*56, num_classes)
forward
pass def forward(self, x):
x = self.features(x) # conv stack
x = x.flatten(1) # N,…
x = self.classifier(x) # logits
return x
Only forward is needed – back‑prop is handled automatically.
forward
model = MyCNN()
logits1 = model(images) # preferred ✔
logits2 = model.forward(images) # works, but avoid
model(input)
internally routes to model.forward(input)
via __call__
.
nn.Module
= reusable building block; override __init__
& forward
model(x)
is the idiomatic way to run a forward passnn.Sequential
for quick layer chainsImage source: Palmer Penguins by Alison Horst
Advantages over simple ANNs:
Image source: Machine Learning Mastery
Some other points:
See the torch.nn.Conv1d
docs
torch.nn.Conv2d
docs.torch.nn.AdaptiveAvgPool2d
torch.nn.AdaptiveMaxPool2d
torchvision.models
docs.Image source: npmjs.com
In this exercise, we’ll train a CNN to estimate the centre \((x_{\text{c}}, y_{\text{c}})\) and the \(x\) and \(y\) radii of an ellipse defined by \[ \frac{(x - x_{\text{c}})^{2}}{r_{x}^{2}} + \frac{(y - y_{\text{c}})^{2}}{r_{y}^{2}} = 1 \]
The ellipse, and its background, will have random colours chosen uniformly on \(\left[0,\ 255\right]^{3}\).
In short, the model must learn to estimate \(x_{\text{c}}\), \(y_{\text{c}}\), \(r_{x}\) and \(r_{y}\).
For more information we can be reached at:
Jack Atkinson
You can also contact the ICCS, make a resource allocation request, or visit us at the Summer School RSE Helpdesk.