---
title: neural nets in cobol & other creative ways to k*ll yourself
canonical: https://mll.sh/neural-networks-cobol/
published: 2025-12-29
modified: 2026-11-16
tags: [ai, humor, cobol, python]
author: miguel llamazares
---

# neural nets in cobol & other creative ways to k*ll yourself

> [!NOTE] disclaimer
> This is a tribute to the [Cruelty Squad](https://www.youtube.com/watch?v=CHm2d3wf8EU) video game. Just to showcase how COBOL can still be used in bizarre ways to maximize shareholder value. ✨

---

Yesterday I opened my inbox expecting the usual sprint cosplay and jira fan fiction. Instead, I got a message from a guy that predates agile, devops, and most human rights:

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Listen, employee. Your next assignment involves critical bio-surveillance operations: we have intercepted subversive penguins attempting to infiltrate corporate aquaculture infrastructure.</p>
    <p>Your task is to classify these traitorous birds based on flipper length and other obscene morphological features. </p>
    <p>BTW, you need to do it in COBOL (hope's is not a problem).</p>
    <p>Do not disappoint me. The shareholders already do.</p>
  </div>
</div>

Naturally, I asked if I could at least sketch the thing in python first. I got this reply:

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Fine. Babysit your little scripting language. But in the end, you return to the mainframe.</p> 
    <p><i>You always return to the mainframe.</i></p>
    <p>Now classify those penguins before they unionize.</p>
  </div>
</div>

Alright. Let's do this.


## housekeeping

Before we dive in, I want to vehemently recommend the [A Programmer's Introduction to Mathematics](https://pimbook.org/) book. It's a great resource for anyone who wants to understand the math and speak symbols like the grown ups if you have a coding background like myself.

I also suggest taking a look at the [3Blue1Brown series on neural networks](https://www.youtube.com/watch?v=aircAruvnKk&list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi). They are very visual and build from the ground up.

We can download the [Palmer penguin dataset from Kaggle](https://www.kaggle.com/datasets/ashkhagan/palmer-penguins-datasetalternative-iris-dataset). 

## neural what?

A neural network is just a function with knobs.

$$
f_\theta(x) = y
$$

You give it numbers. It outputs numbers.

The only thing you control is the parameters $$\theta$$. Training means adjusting those parameters so future outputs are less wrong than past ones.

There is no intelligence here. No understanding. The network does not know what it is doing. It only knows how to reduce a number called loss.

We'll use the simplest non-trivial setup: a feedforward network with one hidden layer. 

### data as numbers

The model never sees _penguins_. It sees _vectors_.

A vector is just a list of numbers arranged in a specific order. Think of it as coordinates in space, except instead of `(x, y, z)` you might have `(bill_length, bill_depth, flipper_length, body_mass)`.

Your input is a fixed-length vector of measurements:

$$
x =
\begin{bmatrix}
\text{bill length} \\
\text{bill depth} \\
\text{flipper length} \\
\text{body mass}
\end{bmatrix}
\in \mathbb{R}^4
$$

This is 4-dimensional space. Each component is a real number. 

Your target label is the species encoded as an integer (`0` for _Adelie_, `1` for _Chinstrap_, `2` for _Gentoo_):

$$
y \in \{0, 1, 2\}
$$

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>So we're reducing living creatures to indices now? How delightfully reductive.</p>
  </div>
</div>

Yes. That is the point.

Before training, we normalize:

$$
x' = \frac{x - \mu}{\sigma}
$$

This subtracts the mean and divides by the standard deviation for each feature.

Why this matters: gradient descent assumes each dimension contributes on a similar scale. If one feature ranges in thousands and another in decimals, the optimizer zigzags and wastes steps. 

### the architecture

A neural network layer is two operations:

1. linear combination
2. nonlinear distortion

If you stack only linear layers, the whole network collapses into one linear transformation. Depth adds nothing. This is why nonlinearity is mandatory.

Hidden layer:

$$
h = \text{ReLU}(xW_1 + b_1)
$$

ReLU is the simplest useful nonlinearity:

$$
\text{ReLU}(z) = \max(0, z)
$$

It zeroes negative values and leaves positive ones unchanged. Without this, the network is just linear regression wearing a trench coat.

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>A trench coat? Is this neural network going to flash people at the park?</p>
  </div>
</div>

Different kind of exposure.

Output layer:

$$
z_2 = hW_2 + b_2
$$

These are raw scores called logits. They can be any real number.

Final step:

$$
\hat{y} = \text{softmax}(z_2)
$$

Softmax converts arbitrary scores into a probability distribution that sums to 1:

$$
\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_j e^{z_j}}
$$

Shapes:

* $$W_1 \in \mathbb{R}^{4 \times k}$$ maps 4 inputs to k hidden units
* $$b_1 \in \mathbb{R}^{k}$$ offsets each hidden unit
* $$W_2 \in \mathbb{R}^{k \times 3}$$ maps hidden units to 3 classes
* $$b_2 \in \mathbb{R}^{3}$$ offsets class scores

### turning wrong into a number

The model outputs probabilities. We need a single scalar that measures how bad the prediction is.

For classification, use cross entropy:

$$
\mathcal{L}(y, \hat{y}) = -\sum_{i=1}^{3} y_i \log(\hat{y}_i)
$$

Here $$y$$ must be converted from an integer index to a one-hot vector. This is a 3-dimensional vector where the true class gets a 1 and all others get 0. If the true class is 2 (Gentoo):

$$
y = [0, 0, 1]
$$

Why three dimensions? Because we have _three species_. The vector aligns with the three output probabilities $$\hat{y} = [\hat{y}_1, \hat{y}_2, \hat{y}_3]$$.

The loss reduces to:

$$
-\log(\hat{y}_{\text{true}})
$$

If the model assigns low probability to the correct class, the loss is large. If it is confidently wrong, the loss spikes. This is intentional. Wrong certainty should hurt more than uncertainty.

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>We're teaching the machine to feel pain through logarithms. This is either genius or the beginning of a very dark timeline.</p>
  </div>
</div>

Loss is the only feedback signal the network ever gets.

### learning = nudging numbers

Training means changing parameters to reduce loss.

The rule is gradient descent:

$$
\theta \leftarrow \theta - \eta \nabla_\theta \mathcal{L}
$$

The gradient points toward steeper loss. You step in the opposite direction.

$$\eta$$ is the learning rate. It controls step size.

Too small: training crawls.
Too large: loss oscillates or explodes.

There is no universal value. You pick it empirically.

### backpropagation

Backpropagation is the chain rule applied to the network graph.

The loss depends on the output. The output depends on the last layer. That depends on the hidden layer. That depends on the input layer.

Backprop computes gradients in reverse order. The notation looks scary but it is just derivatives:

$$
\frac{\partial \mathcal{L}}{\partial W_2},
\frac{\partial \mathcal{L}}{\partial b_2},
\frac{\partial \mathcal{L}}{\partial W_1},
\frac{\partial \mathcal{L}}{\partial b_1}
$$

Read $$\frac{\partial \mathcal{L}}{\partial W_2}$$ as _"how much does loss change when I nudge $$W_2$$"_. That curly $$\partial$$ symbol just means partial derivative, which is calculus for _"change in this one thing while holding everything else constant"_.

Nothing flows backward except these derivatives. They tell you which direction to adjust each parameter.

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>So backpropagation is just playing hot-and-cold with gradients until the loss stops screaming at you?</p>
  </div>
</div>

Essentially yep.

For softmax combined with cross entropy, the gradient simplifies beautifully:

$$
\frac{\partial \mathcal{L}}{\partial z_2} = \hat{y} - y
$$

### full training loop

Training is boring and repetitive:

1. take a batch of inputs
2. compute predictions
3. compute loss
4. compute gradients
5. update parameters
6. repeat

If loss goes down, you are doing something right.
If it does not, assume your setup is broken.

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Six steps to artificial intelligence. What a time to be alive. I bet the researchers who spent decades on this love how you've reduced their life's work to "repeat until it works."</p>
  </div>
</div>

They should. It is accurate.

Let's see the flow in context:

<figure class="diagram" data-animated markdown="0">
<div class="diagram-frame">
<svg viewBox="0 0 864 630" role="img" aria-labelledby="nn-title nn-desc">
<title id="nn-title">One training step in a one-hidden-layer network</title>
<desc id="nn-desc">The left lane is the forward pass, flowing down from the input to the cross-entropy loss. The loss crosses to the right lane, where backprop climbs from the loss through the softmax shortcut, the weight and bias gradients, and the ReLU mask back to the first layer. The optimizer at the top applies the gradients and starts the next epoch.</desc>
<defs>
<marker id="nn-arr" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto-start-reverse"><path d="M1 1.5 9 5 1 8.5Z" fill="var(--fg-muted)"/></marker>
</defs>

<rect class="d-lane" x="16" y="96" width="390" height="514" rx="10" style="--d-accent: var(--d-blue)"/>
<rect class="d-lane" x="458" y="96" width="390" height="514" rx="10" style="--d-accent: var(--d-orange)"/>
<text class="d-lane-title" x="32" y="118">forward pass · making a prediction</text>
<text class="d-lane-title" x="474" y="118">backward pass · assigning blame</text>

<g class="d-card is-accent is-soft" style="--d-accent: var(--d-green)">
<rect class="d-card-bg" x="262" y="14" width="340" height="66" rx="8"/>
<text class="d-card-title is-lg" x="432" y="38" text-anchor="middle">update the knobs</text>
<text class="d-card-code is-lg" x="432" y="59" text-anchor="middle">W ← W − η·∂L/∂W  ·  b ← b − η·∂L/∂b</text>
<rect class="d-glow" x="262" y="14" width="340" height="66" rx="8" fill="none" stroke="var(--d-green)" stroke-width="1.7" style="--d-delay:3.3s"/>
</g>

<g class="d-card is-accent" style="--d-accent: var(--d-blue)">
<rect class="d-card-bg" x="32" y="136" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="211" y="161" text-anchor="middle">input x</text>
<text class="d-card-sub is-lg" x="211" y="180" text-anchor="middle">4 penguin features</text>
<rect class="d-glow" x="32" y="136" width="358" height="58" rx="8" fill="none" stroke="var(--d-blue)" stroke-width="1.7" style="--d-delay:.2s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-blue)">
<rect class="d-card-bg" x="32" y="216" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="211" y="241" text-anchor="middle">z1 = xW1 + b1</text>
<text class="d-card-sub is-lg" x="211" y="260" text-anchor="middle">weighted sum</text>
<rect class="d-glow" x="32" y="216" width="358" height="58" rx="8" fill="none" stroke="var(--d-blue)" stroke-width="1.7" style="--d-delay:.45s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-cyan)">
<rect class="d-card-bg" x="32" y="296" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="211" y="321" text-anchor="middle">h = relu(z1)</text>
<text class="d-card-sub is-lg" x="211" y="340" text-anchor="middle">keep positives</text>
<rect class="d-glow" x="32" y="296" width="358" height="58" rx="8" fill="none" stroke="var(--d-cyan)" stroke-width="1.7" style="--d-delay:.7s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-blue)">
<rect class="d-card-bg" x="32" y="376" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="211" y="401" text-anchor="middle">z2 = hW2 + b2</text>
<text class="d-card-sub is-lg" x="211" y="420" text-anchor="middle">weighted sum again</text>
<rect class="d-glow" x="32" y="376" width="358" height="58" rx="8" fill="none" stroke="var(--d-blue)" stroke-width="1.7" style="--d-delay:.95s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-cyan)">
<rect class="d-card-bg" x="32" y="456" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="211" y="481" text-anchor="middle">p = softmax(z2)</text>
<text class="d-card-sub is-lg" x="211" y="500" text-anchor="middle">all sum to 1</text>
<rect class="d-glow" x="32" y="456" width="358" height="58" rx="8" fill="none" stroke="var(--d-cyan)" stroke-width="1.7" style="--d-delay:1.2s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="32" y="536" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="211" y="561" text-anchor="middle">L = cross-entropy</text>
<text class="d-card-sub is-lg" x="211" y="580" text-anchor="middle">how wrong?</text>
<rect class="d-glow" x="32" y="536" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:1.45s"/>
</g>

<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="474" y="136" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="653" y="161" text-anchor="middle">∂L/∂W1 = xᵀ∂L/∂z1</text>
<text class="d-card-sub is-lg" x="653" y="180" text-anchor="middle">∂L/∂b1 = ∂L/∂z1</text>
<rect class="d-glow" x="474" y="136" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:3s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="474" y="216" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="653" y="241" text-anchor="middle">∂L/∂z1 = ∂L/∂h ⊙ relu′(z1)</text>
<text class="d-card-sub is-lg" x="653" y="260" text-anchor="middle">mask the dead units</text>
<rect class="d-glow" x="474" y="216" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:2.75s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="474" y="296" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="653" y="321" text-anchor="middle">∂L/∂h = W2ᵀ∂L/∂z2</text>
<text class="d-card-sub is-lg" x="653" y="340" text-anchor="middle">blame the hidden layer</text>
<rect class="d-glow" x="474" y="296" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:2.5s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="474" y="376" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="653" y="401" text-anchor="middle">∂L/∂W2 = hᵀ∂L/∂z2</text>
<text class="d-card-sub is-lg" x="653" y="420" text-anchor="middle">∂L/∂b2 = ∂L/∂z2</text>
<rect class="d-glow" x="474" y="376" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:2.25s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="474" y="456" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="653" y="481" text-anchor="middle">∂L/∂z2 = p − y</text>
<text class="d-card-sub is-lg" x="653" y="500" text-anchor="middle">softmax shortcut</text>
<rect class="d-glow" x="474" y="456" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:2s"/>
</g>
<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="474" y="536" width="358" height="58" rx="8"/>
<text class="d-card-title is-lg" x="653" y="561" text-anchor="middle">L</text>
<text class="d-card-sub is-lg" x="653" y="580" text-anchor="middle">scalar loss</text>
<rect class="d-glow" x="474" y="536" width="358" height="58" rx="8" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:1.75s"/>
</g>

<path class="d-arrow" d="M211 196 V213" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M211 196 V213" style="--d-accent: var(--d-blue); --d-delay:.35s"/>
<path class="d-arrow" d="M211 276 V293" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M211 276 V293" style="--d-accent: var(--d-blue); --d-delay:.6s"/>
<path class="d-arrow" d="M211 356 V373" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M211 356 V373" style="--d-accent: var(--d-cyan); --d-delay:.85s"/>
<path class="d-arrow" d="M211 436 V453" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M211 436 V453" style="--d-accent: var(--d-blue); --d-delay:1.1s"/>
<path class="d-arrow" d="M211 516 V533" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M211 516 V533" style="--d-accent: var(--d-cyan); --d-delay:1.35s"/>

<path class="d-arrow" d="M392 565 H470" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M392 565 H470" style="--d-accent: var(--d-orange); --d-delay:1.6s"/>

<path class="d-arrow" d="M653 534 V517" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M653 534 V517" style="--d-accent: var(--d-orange); --d-delay:1.85s"/>
<path class="d-arrow" d="M653 454 V437" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M653 454 V437" style="--d-accent: var(--d-orange); --d-delay:2.1s"/>
<path class="d-arrow" d="M653 374 V357" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M653 374 V357" style="--d-accent: var(--d-orange); --d-delay:2.35s"/>
<path class="d-arrow" d="M653 294 V277" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M653 294 V277" style="--d-accent: var(--d-orange); --d-delay:2.6s"/>
<path class="d-arrow" d="M653 214 V197" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M653 214 V197" style="--d-accent: var(--d-orange); --d-delay:2.85s"/>

<path class="d-arrow" d="M800 136 V47 H604" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M800 136 V47 H604" style="--d-accent: var(--d-green); --d-delay:3.15s"/>
<path class="d-arrow" d="M262 47 H8 V165 H30" marker-end="url(#nn-arr)"/>
<path class="d-run" pathLength="1" d="M262 47 H8 V165 H30" style="--d-accent: var(--d-green); --d-delay:3.55s"/>
<text class="d-label" x="150" y="39" text-anchor="middle">next epoch ↻</text>
</svg>
</div>
<figcaption><strong>One training step.</strong> The forward pass runs down the left lane: input, weighted sums, ReLU, softmax, loss. The loss crosses to the right lane and backprop climbs it in reverse, assigning blame to every weight and bias. At the top, the optimizer nudges each parameter by its gradient, and the next epoch drops back into the input.</figcaption>
</figure>

## from english to python

Every operation becomes explicit code.

Linear transformation:

```python
def linear(x, W, b):
    return x @ W + b
```

The `@` symbol is matrix multiplication. It computes weighted sums of inputs. When you write `x @ W`, each row of `x` gets multiplied by each column of `W` and summed up. It is the same as writing nested loops, but readable.

ReLU and its derivative:

```python
def relu(z):
    return np.maximum(0, z)

def relu_grad(z):
    return (z > 0).astype(float)
```

Softmax with numerical stability:

```python
def softmax(z):
    exp = np.exp(z - np.max(z, axis=1, keepdims=True))
    return exp / np.sum(exp, axis=1, keepdims=True)
```

One-hot encoding for the target:

```python
def one_hot(y, num_classes):
    out = np.zeros((len(y), num_classes))
    out[np.arange(len(y)), y] = 1
    return out
```

Cross entropy loss:

```python
def cross_entropy(probs, y):
    return -np.mean(np.log(probs[np.arange(len(y)), y]))
```

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Five functions. That's it? I've written longer code to format a date string.</p>
  </div>
</div>

Correct. The rest is just calling these repeatedly.

### full working code

```python
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# data
df = sns.load_dataset("penguins").dropna()
X = df[["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"]].values
y = df["species"].astype("category").cat.codes.values

scaler = StandardScaler()
X = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# helpers
def relu(z):
    return np.maximum(0, z)

def relu_grad(z):
    return (z > 0).astype(float)

def softmax(z):
    exp = np.exp(z - np.max(z, axis=1, keepdims=True))
    return exp / np.sum(exp, axis=1, keepdims=True)

def one_hot(y, k):
    out = np.zeros((len(y), k))
    out[np.arange(len(y)), y] = 1
    return out

def cross_entropy(probs, y):
    return -np.mean(np.log(probs[np.arange(len(y)), y]))

# init
np.random.seed(0)
D = X.shape[1]
H = 16
C = len(np.unique(y))

W1 = np.random.randn(D, H) * 0.01
b1 = np.zeros(H)
W2 = np.random.randn(H, C) * 0.01
b2 = np.zeros(C)

lr = 0.1

# training
for epoch in range(150):
    # forward
    z1 = X_train @ W1 + b1
    h = relu(z1)
    z2 = h @ W2 + b2
    probs = softmax(z2)

    loss = cross_entropy(probs, y_train)

    # backward
    y_oh = one_hot(y_train, C)
    dz2 = probs - y_oh
    dW2 = h.T @ dz2 / len(X_train)
    db2 = dz2.mean(axis=0)

    dh = dz2 @ W2.T
    dz1 = dh * relu_grad(z1)
    dW1 = X_train.T @ dz1 / len(X_train)
    db1 = dz1.mean(axis=0)

    W1 -= lr * dW1
    b1 -= lr * db1
    W2 -= lr * dW2
    b2 -= lr * db2

    if epoch % 50 == 0:
        print(f"epoch {epoch}, loss {loss:.4f}")

# evaluation
z1 = X_test @ W1 + b1
h = relu(z1)
z2 = h @ W2 + b2
preds = np.argmax(softmax(z2), axis=1)

accuracy = (preds == y_test).mean()
print("test accuracy:", accuracy)
```

This outputs:

```shell
$ python pengu_nn.py 
epoch 0, loss 1.0987
epoch 50, loss 0.9576
epoch 100, loss 0.4045
test accuracy: 0.9701492537313433
```

Naturally, the Handler hates this.

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Enough with your cheerful toy language. The mainframe thirsts.</p>
  </div>
</div>

Great.

## the cobol nightmare

You want neural networks in COBOL?
Enjoy the pain. 

Well, I guess I have to dust off my ancient COBOL books[^1]:

![IMS COBOL books](/assets/img/neural-networks-cobol/01_cobol-books.jpeg)

COBOL was built for accountants, not gradient descent. You get fixed-width fields, no arrays the way you want them, no dynamic memory, and arithmetic that feels like chiseling numbers into wet clay.

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Finally. A language that understands suffering. Python users have it too easy with their "readable syntax" and "helpful error messages."</p>
  </div>
</div>

I will show only the parts that correspond to neural network operations. For the full implementation including data loading, CSV parsing, train-test split, and all the `WORKING-STORAGE` boilerplate, see [the complete source code](https://github.com/mllamazares/neural-networks-in-cobol).

### data loading and preprocessing

COBOL loads CSV files line by line, validates against missing values, and encodes species as integers:

```cobol
*    ENCODE TARGET SPECIES AS INTEGER LABELS (0-2).
EVALUATE WS-SPECIES-STR
    WHEN "Adelie"    MOVE 0 TO D-Y(WS-VALID-ROWS)
    WHEN "Chinstrap" MOVE 1 TO D-Y(WS-VALID-ROWS)
    WHEN "Gentoo"    MOVE 2 TO D-Y(WS-VALID-ROWS)
END-EVALUATE
```

Normalization is done with the same z-score formula, but spelled out explicitly:

```cobol
*    STEP 5: APPLY Z-SCORE TRANSFORMATION TO ALL SAMPLES.
PERFORM VARYING IDX-ROW FROM 1 BY 1 
        UNTIL IDX-ROW > WS-VALID-ROWS
    COMPUTE D-X1(IDX-ROW) = (D-X1(IDX-ROW) - 
             WS-MEAN-X1) / WS-STD-X1
    COMPUTE D-X2(IDX-ROW) = (D-X2(IDX-ROW) - 
             WS-MEAN-X2) / WS-STD-X2
    COMPUTE D-X3(IDX-ROW) = (D-X3(IDX-ROW) - 
             WS-MEAN-X3) / WS-STD-X3
    COMPUTE D-X4(IDX-ROW) = (D-X4(IDX-ROW) - 
             WS-MEAN-X4) / WS-STD-X4
END-PERFORM
```

### weight initialization

Python uses `np.random.randn()`. COBOL implements Gaussian sampling with the [Box-Muller transform](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform):

```cobol
*    GAUSSIAN WEIGHT INITIALIZATION USING BOX-MULLER TRANSFORM.
*    G(X, Y) = SQRT(-2LN(U1)) * COS(2PI * U2).
PERFORM VARYING IDX-I FROM 1 BY 1 UNTIL IDX-I > 4
    PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 16
        COMPUTE WS-RAND-U1 = FUNCTION RANDOM
        COMPUTE WS-RAND-U2 = FUNCTION RANDOM
        COMPUTE WS-GAUSSIAN = 
            FUNCTION SQRT(-2 * FUNCTION LOG(WS-RAND-U1)) *
            FUNCTION COS(2 * WS-PI * WS-RAND-U2)
*       SCALE WEIGHTS DOWN (0.01) TO PREVENT GRADIENT EXPLOSION.
        COMPUTE W1-VAL(IDX-I, IDX-J) = WS-GAUSSIAN * 0.01
    END-PERFORM
END-PERFORM
```

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Box-Muller transform. In COBOL. You're not just implementing a neural network, you're performing an exorcism.</p>
  </div>
</div>

### forward pass: hidden layer

Python: `z1 = X @ W1 + b1`

COBOL: explicit nested loops for matrix multiplication.

```cobol
*    HIDDEN LAYER COMPUTATION: Z1 = X * W1 + B1.
PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 16
    MOVE B1-VAL(IDX-J) TO Z1-VAL(IDX-I, IDX-J)
    COMPUTE Z1-VAL(IDX-I, IDX-J) = 
            Z1-VAL(IDX-I, IDX-J) +
            (D-X1(IDX-I) * W1-VAL(1, IDX-J))
    COMPUTE Z1-VAL(IDX-I, IDX-J) = 
            Z1-VAL(IDX-I, IDX-J) +
            (D-X2(IDX-I) * W1-VAL(2, IDX-J))
    COMPUTE Z1-VAL(IDX-I, IDX-J) = 
            Z1-VAL(IDX-I, IDX-J) +
            (D-X3(IDX-I) * W1-VAL(3, IDX-J))
    COMPUTE Z1-VAL(IDX-I, IDX-J) = 
            Z1-VAL(IDX-I, IDX-J) +
            (D-X4(IDX-I) * W1-VAL(4, IDX-J))
```

This is matrix multiplication, expressed as stubbornness.

### forward pass: ReLU

Python: `h = np.maximum(0, z1)`

COBOL: an IF statement in a loop.

```cobol
*    NON-LINEAR ACTIVATION: RELU(Z) = MAX(0, Z).
IF Z1-VAL(IDX-I, IDX-J) > 0
    MOVE Z1-VAL(IDX-I, IDX-J) TO H-VAL(IDX-I, IDX-J)
ELSE
    MOVE 0 TO H-VAL(IDX-I, IDX-J)
END-IF
```

### forward pass: output layer

Python: `z2 = h @ W2 + b2`

COBOL: same pattern, different dimensions.

```cobol
*    OUTPUT LAYER COMPUTATION: Z2 = H * W2 + B2.
PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 3
    MOVE B2-VAL(IDX-J) TO Z2-VAL(IDX-I, IDX-J)
    PERFORM VARYING IDX-K FROM 1 BY 1 UNTIL IDX-K > 16
        COMPUTE Z2-VAL(IDX-I, IDX-J) = 
                Z2-VAL(IDX-I, IDX-J) + 
                (H-VAL(IDX-I, IDX-K) * 
                 W2-VAL(IDX-K, IDX-J))
    END-PERFORM
END-PERFORM
```

### forward pass: softmax

Python: vectorized exponentials and division.

COBOL: two-pass algorithm with explicit accumulation.

```cobol
*    PROBABILITY ESTIMATION: SOFTMAX(Z2).
*    P_i = EXP(Z_i) / SUM(EXP(Z_j)).
COMPUTE P-VAL(IDX-I, 1) = FUNCTION EXP(Z2-VAL(IDX-I, 1))
COMPUTE P-VAL(IDX-I, 2) = FUNCTION EXP(Z2-VAL(IDX-I, 2))
COMPUTE P-VAL(IDX-I, 3) = FUNCTION EXP(Z2-VAL(IDX-I, 3))
MOVE 0 TO WS-TEMP-MATH
ADD P-VAL(IDX-I, 1) P-VAL(IDX-I, 2) P-VAL(IDX-I, 3) 
  TO WS-TEMP-MATH
COMPUTE P-VAL(IDX-I, 1) = P-VAL(IDX-I, 1) / WS-TEMP-MATH
COMPUTE P-VAL(IDX-I, 2) = P-VAL(IDX-I, 2) / WS-TEMP-MATH
COMPUTE P-VAL(IDX-I, 3) = P-VAL(IDX-I, 3) / WS-TEMP-MATH
```

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Look at that. Three separate COMPUTE statements to normalize three probabilities. Inefficient? Yes. Beautiful? Also yes.</p>
  </div>
</div>

### loss calculation

Python: `loss = -np.mean(np.log(probs[range(n), y]))`

COBOL: loop over samples, accumulate negative log probabilities.

```cobol
*    CROSS-ENTROPY LOSS: L = -SUM(Y_TRUE * LOG(P_PRED)).
MOVE 0 TO WS-LOSS
PERFORM VARYING IDX-S FROM 1 BY 1 
        UNTIL IDX-S > WS-TRAIN-ROWS
    COMPUTE IDX-I = WS-IDX(IDX-S)
    COMPUTE IDX-J = D-Y(IDX-I) + 1
    COMPUTE WS-LOSS = WS-LOSS - 
                      FUNCTION LOG(P-VAL(IDX-I, IDX-J))
END-PERFORM
COMPUTE WS-LOSS = WS-LOSS / WS-TRAIN-ROWS
```

### backpropagation: output gradient

Python: `dz2 = probs - y_onehot`

COBOL: copy probabilities, then subtract 1 from the true class.

```cobol
*    DERIVATIVE OF SOFTMAX CW CROSS-ENTROPY: DZ2 = P - Y_TRUE.
PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 3
    MOVE P-VAL(IDX-I, IDX-J) TO BP-DZ2(IDX-J)
END-PERFORM
COMPUTE IDX-K = D-Y(IDX-I) + 1
SUBTRACT 1 FROM BP-DZ2(IDX-K)
```

### backpropagation: W2 and b2 gradients

Python: `dW2 = h.T @ dz2 / n` and `db2 = dz2.mean(axis=0)`

COBOL: accumulate gradients across all samples, then average during update.

```cobol
*    ACCUMULATE DW2 = H^T * DZ2 | DB2 = DZ2.
PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 3
    COMPUTE DB2-VAL(IDX-J) = DB2-VAL(IDX-J) + 
                             BP-DZ2(IDX-J)
    PERFORM VARYING IDX-K FROM 1 BY 1 UNTIL IDX-K > 16
        COMPUTE DW2-VAL(IDX-K, IDX-J) = 
                DW2-VAL(IDX-K, IDX-J) + 
                (H-VAL(IDX-I, IDX-K) * BP-DZ2(IDX-J))
    END-PERFORM
END-PERFORM
```

### backpropagation: hidden layer gradient

Python: `dh = dz2 @ W2.T`

COBOL: explicit matrix-vector product.

```cobol
*    BACKPROP TO HIDDEN LAYER: DH = DZ2 * W2^T.
PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 16
    MOVE 0 TO BP-DH(IDX-J)
    PERFORM VARYING IDX-K FROM 1 BY 1 UNTIL IDX-K > 3
        COMPUTE BP-DH(IDX-J) = BP-DH(IDX-J) + 
                (BP-DZ2(IDX-K) * W2-VAL(IDX-J, IDX-K))
    END-PERFORM
```

### backpropagation: ReLU gradient

Python: `dz1 = dh * (z1 > 0)`

COBOL: IF statement as a gate.

```cobol
*    DERIVATIVE OF RELU: DZ1 = DH IF Z1 > 0 ELSE 0.
IF Z1-VAL(IDX-I, IDX-J) > 0
    MOVE BP-DH(IDX-J) TO BP-DZ1(IDX-J)
ELSE
    MOVE 0 TO BP-DZ1(IDX-J)
END-IF
```

### backpropagation: W1 and b1 gradients

Python: `dW1 = X.T @ dz1 / n`

COBOL: accumulate outer products.

```cobol
*    ACCUMULATE DW1 = X^T * DZ1 | DB1 = DZ1.
COMPUTE DB1-VAL(IDX-J) = DB1-VAL(IDX-J) + 
                         BP-DZ1(IDX-J)
IF BP-DZ1(IDX-J) NOT = 0
    COMPUTE DW1-VAL(1, IDX-J) = 
      DW1-VAL(1, IDX-J) + 
      (D-X1(IDX-I) * BP-DZ1(IDX-J))
    COMPUTE DW1-VAL(2, IDX-J) = 
      DW1-VAL(2, IDX-J) + 
      (D-X2(IDX-I) * BP-DZ1(IDX-J))
    COMPUTE DW1-VAL(3, IDX-J) = 
      DW1-VAL(3, IDX-J) + 
      (D-X3(IDX-I) * BP-DZ1(IDX-J))
    COMPUTE DW1-VAL(4, IDX-J) = 
      DW1-VAL(4, IDX-J) + 
      (D-X4(IDX-I) * BP-DZ1(IDX-J))
END-IF
```

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Four separate accumulation statements. One for each input feature. Because COBOL doesn't believe in shortcuts or happiness, kek.</p>
  </div>
</div>

### parameter update

Python: `W -= lr * dW`

COBOL: compute scaled learning rate once, then apply to all parameters.

```cobol
*    PERFORM PARAMETER UPDATES: PARAM = PARAM - LR * GRADIENT.
COMPUTE WS-TEMP-MATH = WS-LR / WS-TRAIN-ROWS
PERFORM VARYING IDX-I FROM 1 BY 1 UNTIL IDX-I > 16
    PERFORM VARYING IDX-J FROM 1 BY 1 UNTIL IDX-J > 3
        COMPUTE W2-VAL(IDX-I, IDX-J) = 
                W2-VAL(IDX-I, IDX-J) - 
                (WS-TEMP-MATH * DW2-VAL(IDX-I, IDX-J))
    END-PERFORM
    COMPUTE B1-VAL(IDX-I) = B1-VAL(IDX-I) - 
                            (WS-TEMP-MATH * DB1-VAL(IDX-I))
END-PERFORM
```

### evaluation

Inference is the same forward pass without gradients. Prediction is argmax:

```cobol
*    PREDICATE SELECTION: ARGMAX PROBABILITY.
EVALUATE TRUE
    WHEN P-VAL(IDX-I, 1) >= P-VAL(IDX-I, 2) AND 
         P-VAL(IDX-I, 1) >= P-VAL(IDX-I, 3)
        MOVE 0 TO WS-PRED-CLASS
    WHEN P-VAL(IDX-I, 2) >= P-VAL(IDX-I, 1) AND 
         P-VAL(IDX-I, 2) >= P-VAL(IDX-I, 3)
        MOVE 1 TO WS-PRED-CLASS
    WHEN OTHER
        MOVE 2 TO WS-PRED-CLASS
END-EVALUATE
```

## grand finale

To compile these 578 lines of pure madness, just _keep calm and use [gnucobol](https://gnucobol.sourceforge.io/)_:

```shell
$ cobc -x -o pengu_nn pengu_nn.cob && ./pengu_nn
LOADED 0333 VALID ROWS.
DATA HOUSEKEEPING COMPLETED.
EPOCH 0000 LOSS: +000000001.098705953
EPOCH 0050 LOSS: +000000000.948385870
EPOCH 0100 LOSS: +000000000.404952448
EPOCH 0150 LOSS: +000000000.254336047
EPOCH 0200 LOSS: +000000000.157729394
EPOCH 0250 LOSS: +000000000.097222933
EPOCH 0300 LOSS: +000000000.067910843
EPOCH 0350 LOSS: +000000000.052804836
EPOCH 0400 LOSS: +000000000.043876194
EPOCH 0450 LOSS: +000000000.037968264
EPOCH 0500 LOSS: +000000000.033755593
TEST ACCURACY: +000000001.000000000
TRAIN ACCURACY: +000000000.988721804
```

Does it work? Yes. Painfully. Slowly. Correctly.

I know what you are thinking: why on earth I put 500 epochs for such a small dataset? _Coz' we can_. 😎

<div class="handler-chat">
  <div class="handler-avatar" aria-label="the handler"></div>
  <div class="handler-message">
    <p>Perfect accuracy on the test set. Either you've achieved machine learning nirvana or you've overfitted so hard the penguins are filing a restraining order.</p>
    <p>Anyway. Fair enough. The penguin insurgency will be contained... for now.</p>
  </div>
</div>

The penguins get classified. The loss goes down. The mainframe hums in approval. Somewhere, a finance department nods without understanding why.

---

You can find the full working code, including all the ugly declarations I spared you from, [in this github repo](https://github.com/mllamazares/neural-networks-in-cobol).


[^1]: yep, I know we don't need IMS here. I just wanted to flex with that since I don't get many chances, lol.


<style>
@import url('https://fonts.googleapis.com/css2?family=Barriecito&display=swap');

html {
    cursor: url('/assets/img/neural-networks-cobol/02_cursor.webp'), auto;
}

a:hover {
    cursor: url('/assets/img/neural-networks-cobol/03_hand-1.webp'), auto;
    animation: cursor 250ms linear infinite;
}

.handler-chat {
    display: flex;
    align-items: flex-start;
    margin: 1.5rem 0;
    gap: 1rem;
    background: black;
    padding: 1rem;
    border: 3px solid #00ff00;
    color: #00ff00;
    font-family: 'Barriecito', monospace;
    font-size: 1.2em;
    box-shadow: 0 8px 6px -6px black;
    min-height: 110px;
}

.handler-avatar {
    width: 100px;
    height: 100px;
    background-size: cover;
    background-position: center;
    background-image: url("/assets/img/neural-networks-cobol/04_avatar-1.webp");
    flex-shrink: 0;
    border: 2px solid #00ff00;
    animation: handler 1000ms linear infinite;
}

@keyframes cursor {
    0% {
        cursor: url("/assets/img/neural-networks-cobol/03_hand-1.webp"), pointer;
    }
    50% {
        cursor: url("/assets/img/neural-networks-cobol/05_hand-2.webp"), pointer;
    }
    100% {
        cursor: url("/assets/img/neural-networks-cobol/06_hand-3.webp"), pointer;
    }
}


@keyframes handler {
    0% {
        background-image: url("/assets/img/neural-networks-cobol/04_avatar-1.webp");
    }
    25% {
        background-image: url("/assets/img/neural-networks-cobol/07_avatar-2.webp");
    }
    75% {
        background-image: url("/assets/img/neural-networks-cobol/08_avatar-3.webp");
    }
    100% {
        background-image: url("/assets/img/neural-networks-cobol/09_avatar-4.webp");
    }
}

.handler-message {
    flex-grow: 1;
}

.handler-message p {
    margin: 0 0 0.8rem 0;
}

.handler-message p:last-child {
    margin: 0;
}

.handler-name {
    font-weight: bold;
    margin-bottom: 0.2rem;
    display: block;
}

@media (max-width: 768px) {
    .handler-chat {
        flex-direction: column;
    }

    .handler-avatar {
       margin: 0;
    }
    
    .handler-avatar {
        margin-bottom: 1rem;
    }
}

.cursor {
    display: inline;
    color: #00ff00;
    animation: blink 1s step-end infinite;
    margin-left: 2px;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

.handler-play {
    background: transparent;
    border: 2px solid #00ff00;
    color: #00ff00;
    font-family: 'Barriecito', monospace;
    font-size: 1.1em;
    padding: 0.5rem 1.2rem;
    cursor: pointer;
    transition: transform 0.15s, background 0.15s, color 0.15s;
}

.handler-play:hover {
    background: #00ff00;
    color: black;
    transform: scale(1.15);
    cursor: url('/assets/img/neural-networks-cobol/03_hand-1.webp'), auto;
    animation: cursor 250ms linear infinite;
}

.handler-chat.unplayed {
    align-items: center;
    justify-content: center;
}

.handler-chat.unplayed .handler-avatar {
    display: none;
}

.handler-chat.unplayed .handler-message {
    flex-grow: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>

<script>
document.addEventListener("DOMContentLoaded", () => {
    document.querySelectorAll('.handler-message').forEach(msg => {
        // Lock height before clearing to prevent layout shift
        msg.style.minHeight = msg.offsetHeight + 'px';

        const chat = msg.closest('.handler-chat');
        if (chat) chat.classList.add('unplayed');

        const nodesToType = Array.from(msg.childNodes).map(n => n.cloneNode(true));
        msg.innerHTML = '';

        const btn = document.createElement('button');
        btn.className = 'handler-play';
        btn.type = 'button';
        btn.textContent = '▶ play';
        btn.addEventListener('click', () => {
            btn.remove();
            if (chat) chat.classList.remove('unplayed');
            const cursor = document.createElement('span');
            cursor.className = 'cursor';
            cursor.textContent = '█';
            typeNodes(msg, nodesToType, cursor).then(() => cursor.remove());
        });
        msg.appendChild(btn);
    });

    async function typeNodes(parent, nodes, cursor) {
        for (const node of nodes) {
            if (node.nodeType === Node.TEXT_NODE) {
                const text = node.textContent;
                // Skip whitespace-only text nodes (markup indentation between blocks)
                if (text.trim() === '') continue;
                const textNode = document.createTextNode('');
                parent.appendChild(textNode);
                parent.appendChild(cursor);
                for (const char of text) {
                    textNode.textContent += char;
                    await new Promise(r => setTimeout(r, Math.random() * 30 + 20));
                }
            } else if (node.nodeType === Node.ELEMENT_NODE) {
                const newEl = node.cloneNode(false);
                parent.appendChild(newEl);
                parent.appendChild(cursor);
                await typeNodes(newEl, Array.from(node.childNodes), cursor);
            }
        }
    }
});
</script>
