52  Practical Implementation: PyTorch and JAX

Author

Diego Villalba

Published

October 16, 2026

This chapter translates neural network concepts into production frameworks:

1 Deep Learning Framework Ecosystem

Framework Paradigm Primary Use Case
scikit-learn Object-Oriented High-Level Small tabular data baselines
PyTorch Imperative / Dynamic Graph General deep learning & research standard
JAX Pure Functional / XLA JIT High-order differentiation & TPU acceleration
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_moons

X, y = make_moons(n_samples=400, noise=0.2, random_state=42)
clf = MLPClassifier(hidden_layer_sizes=(16, 16), max_iter=500, random_state=42)
clf.fit(X, y)
print(f"Accuracy: {clf.score(X, y):.3f}")

2 PyTorch Fundamentals

2.1 Tensors and Autograd

\[ \frac{\partial \mathcal{L}}{\partial \theta} \quad \forall \theta \text{ with } \texttt{requires\_grad=True} \tag{1}\]

loss.backward() computes gradients automatically via chain rule backpropagation across dynamic computation graphs.

model.train() — Activates training mode for Dropout/BatchNorm.

Step 1 / 5

Numerically stable Binary Cross Entropy with Logits Loss:

\[ \mathcal{L} = \frac{1}{N} \sum_{i=1}^{N} \left[ \max(z_i, 0) - z_i y_i + \log\left(1 + e^{-|z_i|}\right) \right] \tag{2}\]

3 JAX Fundamentals

JAX relies on pure functions and stateless pytrees.

Mutable object-oriented model encapsulation holding parameters internally.

Single-instance scalar Binary Cross Entropy in JAX:

\[ \mathcal{L}(w) = -\left[ y \log \sigma(wx) + (1-y) \log(1 - \sigma(wx)) \right] \tag{3}\]

import jax
import jax.numpy as jnp
import optax

def loss_fn(params, x, y):
    logits = forward(params, x)
    return optax.sigmoid_binary_cross_entropy(logits, y).mean()

@jax.jit
def train_step(params, opt_state, x, y):
    loss, grads = jax.value_and_grad(loss_fn)(params, x, y)
    updates, new_opt_state = optimizer.update(grads, opt_state, params)
    new_params = optax.apply_updates(params, updates)
    return new_params, new_opt_state, loss

4 Summary

PyTorch provides an intuitive, object-oriented imperative workflow ideal for rapid prototyping and debugging. JAX enforces pure functional programming, enabling high-order automatic differentiation and JIT compilation via XLA (Paszke et al. 2019; Bradbury et al. 2018).

References