MLP on CIFAR-10 (SMAI Assignment 4, Q1, trained on the true labels)
Fully connected network on the flattened 32x32x3 images. Architecture: 3072 -> 128 -> 64 -> 10, hidden activation relu, 402,250 parameters, loss = categorical cross-entropy on 10 logits.
Training
Plain gradient descent, learning rate 0.05, batch size 64, grad_accumulation_steps 1, 100 epochs, no early stopping, seed 1058 (torch.manual_seed(1059) before building the model, DataLoader generator seed 1060). Train on the 50,000-image train part, test on the 10,000-image test part. No validation set is used in this experiment.
Preprocessing
Flatten to 3072 numbers, divide by 255, then subtract the column mean and divide by the column standard deviation of the 50,000 training images
(preprocessing.npz, arrays mean and std).
Results (test part, one seed)
Accuracy 0.4612, loss 4.4279, mean F1 0.4634; training accuracy 0.8762, training loss 0.3740.
Files
model.npz (weights W0, W1, ... with shape (n_in, n_out), biases b0, b1, ..., plus layer_sizes, activations, loss; same format as Model.save_to of the NumPy library),
model_state_dict.pt (PyTorch state dict, torch.nn.Linear weights have shape (n_out, n_in)), preprocessing.npz, config.json.
Usage (NumPy only)
import numpy as np
pre, m = np.load("preprocessing.npz"), np.load("model.npz")
act = str(m["activations"][0]) # hidden activation: relu / tanh / sigmoid / elu
def predict(images_uint8): # (n, 32, 32, 3) uint8 CIFAR-10 images -> class numbers 0..9
x = images_uint8.reshape(len(images_uint8), -1).astype(np.float64) / 255.0
x = (x - pre["mean"]) / pre["std"] # mean / std of the 50,000 training images, per column
n_layers = len([k for k in m.files if k.startswith("W")])
for i in range(n_layers):
x = x @ m[f"W{i}"] + m[f"b{i}"] # W has shape (n_in, n_out); row-vector convention
if i < n_layers - 1:
x = {"relu": lambda z: np.maximum(0, z), "tanh": np.tanh, "sigmoid": lambda z: 1 / (1 + np.exp(-z)),
"elu": lambda z: np.where(z > 0, z, np.expm1(np.minimum(z, 0)))}[act](x)
return x.argmax(axis=1) # x are the logits
Classes: 0 airplane, 1 automobile, 2 bird, 3 cat, 4 deer, 5 dog, 6 frog, 7 horse, 8 ship, 9 truck.