WUT_Computer_Science/Programming/EARIN/lab5/code/example.py
Krzysztof kuhy Rudnicki cc06bc0cd8 EARIN: zero findings, verified against byte-identical program output
749 findings across six labs and two projects. The mechanical half was
scripted; everything below is what the scripts got wrong or could not do, and
how it was caught.

Three regressions I introduced and then found by RUNNING the code, not by
reading it:

- Converting print() to logger.info() made the programs SILENT. logging's
  last-resort handler only emits WARNING and above, so with no basicConfig
  call `lab2/main.py -t 1 1` printed nothing at all. Fifteen files across the
  repo now configure logging where they log.
- The same conversion dropped `end=""`. lab2 draws its board one square at a
  time and lab1 draws the maze one cell at a time; both came out one glyph
  per line. Those thirteen call sites are sys.stdout.write again, and the
  rendered board now diffs byte-identically against the pre-change output.
- The B007 fix renamed `col` to `_col` in the FIRST loop it matched, which
  was print_letters -- where the variable IS used. That would have been a
  NameError on the first call.

And one the scripts caused silently: annotating 110 signatures dropped the
DEFAULT VALUES from 22 of them, so `preprocessing(a, b, USER_THRESHOLD,
ANIME_THRESHOLD)` would have raised TypeError. Every signature is now diffed
against the pre-change version, name by name and default by default: zero
mismatches.

Real defects the lint surfaced:

- `alpha_beta` ended with an unreachable `return None` that violated its own
  return type. Its maximising and minimising halves were mirror images, so
  they are one `_search_branch` now (C901 11 -> ok).
- `get_data`'s `gpu` switch, `predict`'s `auto`, `create_model`'s
  `rows_number` and five of `preprocess_model_predict`'s fifteen parameters
  were never read. Gone, with their call sites.
- np.random.* -> Generator throughout (NPY002), `.values` -> `.to_numpy()`,
  `datetime.now()` with a tzinfo, `inplace=True` removed, `except:` given a
  type, `random.*` -> `SystemRandom` (S311).
- print_board's three nested helpers became methods (C901 17 -> ok);
  user_input's nine-branch if-ladder became a table (C901 12 -> ok).

Verification, all by running: lab1 `-t` and lab2 `-t 1 1` produce output
byte-identical to the pre-change code; lab2's rendered 8x8 board diffs clean;
lab3's argument parsing returns the same dict for the same argv; lab4 fails
identically before and after (no sklearn here). Also included: the logging
configuration for NLP, MOM and ECRYPT_PROJECT, which had the same silence bug.

Repo-wide: 2,775 findings -> 1,883.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
2026-08-28 20:39:53 +02:00

158 lines
6.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Minimal PyTorch training example shipped with lab 5."""
import logging
import numpy as np
import torch
from torch import nn, optim
# numpy for loading dataset
# The output layer is a sigmoid, so 0.5 is the class boundary.
DECISION_THRESHOLD = 0.5
# pytorch for deep learning models
# nn like neural network
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(message)s", level=logging.INFO)
# Pima indians describes patient medical data and whether they had diabetes for last 5
# years
# It is binary classification (they could either have diabetes 1 or not 0)
# load the file as a matrix of numbers,
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
input_columns = 8
# split into input (X) -> in this case everything beside info whether patient had
# diabetes or not is input
# We are splitting data into two subsets by using NumPy slice operator : and choose
# first 8 columns using 0:8 slice
X = dataset[:, 0:input_columns]
# and output (y) variables -> in this case we are only interested whether patient had
# diabetes or not as an output
# you can simplify that y = f(X)
# We are splitting the data by using slice operator : and choosing last column
y = dataset[:, input_columns]
# we need to convert this data to pytorch tensors
# Pytorch usually operates on 32-bit floating point and NumPy by default uses 64 bit
# floating point
X = torch.tensor(X, dtype=torch.float32)
# We can also correct the shape to fit what PyTorch would expect (here we are converting
# n vectors to n x 1 matrix)
# This simplifies handling matrix multiplication operations (which are the basis of deep
# learning models)
# reshape is converting the output variable y from a 1-dimensional NumPy array to a
# 2-dimensional PyTorch tensor with a shape of (n, 1), where n is the number of samples
# in the dataset.
y = torch.tensor(y, dtype=torch.float32).reshape(-1, 1)
# define the model
# this class is a subclass of nn.Module -> base class provided by PyTorch for building
# neural network models.
class PimaClassifier(nn.Module):
"""Two hidden layers and a sigmoid output, for the Pima dataset."""
def __init__(self) -> None:
"""Build the two-layer network used by this example."""
super().__init__()
# There are 3 (fully connected) layers in class, each with their activation
# function
# creates Linear layer, it maps input to a hidden layer of 12 neurons
# input features have a size of 8 (same number as number of features in pima
# indians diabetes dataset)
first_output_neurons = 12
self.hidden1 = nn.Linear(input_columns, first_output_neurons)
# This creates ReLU (rectified linear unit) activation function applied after
# first hidden layer
self.act1 = nn.ReLU()
# This maps the output of first layer (which was 12 neurons) to new hidden layer
# of 8 neurons
second_output_neurons = 8
self.hidden2 = nn.Linear(first_output_neurons, second_output_neurons)
# ReLU activation function applied after second hidden layer
self.act2 = nn.ReLU()
# We map output of second layer to a single output neuron -> which will
# represent the predicted
# probability of a sample having diabetes
self.output = nn.Linear(second_output_neurons, 1)
# sigmoid function forces output to be either 0 or 1
self.act_output = nn.Sigmoid()
# forward pass is computation of output based on input 'x'
def forward(self, x: object) -> object:
"""Run one forward pass over the batch."""
# Applies first hidden layer (and then ReLU activation) to input x
x = self.act1(self.hidden1(x))
# Applies second hidden layer (and then ReLU activation) to input x
x = self.act2(self.hidden2(x))
# Applies output layer (and then Sigmoid activation) to input x
return self.act_output(self.output(x))
# Create object from model class
model = PimaClassifier()
logger.info("%s", model)
# train the model
# first we need to specify what is the goal of training
# we have input X and output y and we want the model to be as close to y as possible
# Since this is binary classification problem we will use "binary cross entropy" to
# measure the distance between
# our prediction and y
loss_fn = nn.BCELoss() # binary cross entropy
# Optimizer adjust model weights to produce better output
# Its described as being able to tune itself to a lot of problems
# inputs are:
# parameters which it will optimize (from the model)
# and lr (learning rate) which is step size of each iteration
optimizer = optim.Adam(model.parameters(), lr=0.001)
# epoch is the entire training dataset passed to a model once
n_epochs = 100
# batch is one or more sample passed to model
# number of epochs and the size of a batch can be chosen experimentally by trial and
# error.
# a lot of epochs and big size of batch means more time and more memory consumption but
# more accurate results
batch_size = 10
# We split dataset into batches and pass batches one by one into a model to training
# loop
# after using all batches we finish one epoch and can start over again to refine the
# model
# we use two nested for loops for training, one is for epochs
for _epoch in range(n_epochs):
# and one for batches
for i in range(0, len(X), batch_size):
# Split X data into a batch with the size from batch_size
Xbatch = X[i : i + batch_size]
# run the model on the batch and return "batched" output
y_pred = model(Xbatch)
# Split y data into a batch with the size from batch_size
ybatch = y[i : i + batch_size]
# Compare loss
loss = loss_fn(y_pred, ybatch)
# optimize model
optimizer.zero_grad()
# calculate the inaccuracy
loss.backward()
# optimizer takes next step
optimizer.step()
# compute final accuracy
y_pred = model(X)
accuracy = (y_pred.round() == y).float().mean()
logger.info("Accuracy %s", accuracy)
# make class predictions with the model
predictions = (model(X) > DECISION_THRESHOLD).int()
for i in range(5):
logger.info("%s => %d (expected %d)", X[i].tolist(), predictions[i], y[i])