pymlkit
Guides

Custom loops

Using forward and backward directly.

fit covers most needs, but forward and backward are exposed for inspection and experiments.

python
out = model.forward(sample)        # populate activations
model.backward(target)             # accumulate gradients for this sample
The C library has nn_apply_gradients to apply accumulated gradients, but the Python wrapper does not expose it yet. From Python, use fit to update weights. Applying gradients manually is available from the C API.

Inference loops

forward is the right tool for evaluation, for example scoring a grid of points to visualise a decision boundary:

python
SIZE = 20
for gy in range(SIZE):
    row = ""
    for gx in range(SIZE):
        px = gx / (SIZE - 1) * 2 - 1
        py = gy / (SIZE - 1) * 2 - 1
        row += "██" if model.forward([px, py])[0] > 0.5 else "  "
    print(row)