Differentiable Curves in PyTorch: Small, Auditable Models with TorchCurves
Most machine learning models are hard to read: thousands of weights, no shape you can point at. A learnable curve is the opposite: a smooth function of one input, set by a dozen numbers, trained with the same gradient descent as any neural network. TorchCurves, a new open-source PyTorch library, makes such curves a drop-in layer. This post explains what they are, where they earn their place and how to start.
For developers: TorchCurves in Practice: Learnable B-spline and Legendre Curves in PyTorch has the code, from install to a monotone calibration curve, an additive model on tabular data, export to JSON and SciPy, tests, and real outputs from our runs.
What a differentiable curve is
A parametric curve maps a scalar input to a point. In one dimension that point is just a number, so the curve is a function f(x). The function is built from a fixed set of basis functions (simple building blocks) multiplied by coefficients (the numbers you learn) and added up. Change the coefficients and the shape changes.
"Differentiable" means you can compute how the output changes when any coefficient, or the input itself, changes. That is what lets PyTorch train the curve with gradient descent, either alone or as one layer inside a larger network.
Two bases matter in practice, and TorchCurves supports both [1][2]:
- B-splines. Piecewise polynomials joined smoothly at points called knots. Each basis function is a local bump, so each coefficient only affects a short stretch of the curve. That makes B-splines good at sharp features and easy to constrain.
- Legendre polynomials. Global polynomials on the interval from -1 to 1. Each coefficient affects the whole curve. They are very smooth and compact, but a local kink needs many terms.
None of this is new mathematics. Penalised B-splines ("P-splines") have been a standard smoothing tool since Eilers and Marx's 1996 paper [3], and generalised additive models (GAMs) built from them are a staple of statistics. What is new is having these curves as ordinary, batched PyTorch layers that sit inside deep learning code.
TorchCurves in brief
TorchCurves was released by Alex Shtoff and described in a paper posted to arXiv on 10 November 2026 [1]. It is Apache-2.0 licensed, installs with pip install torchcurves, and version 0.3.4 requires Python 3.12+ and PyTorch 2.9+ [2][4]. Its main pieces are:
BSplineCurveandLegendreCurve: layers that own their coefficients. Given a batch of inputs with one column per curve, they return a point on each curve.BSplineBasis: the basis without weights, for when another network predicts the coefficients.Sum: adds curve outputs, which turns a set of curves into a Kolmogorov-Arnold Network (KAN) layer [5] or an additive model.- A functional API (
bspline_curves,legendre_curves) and input maps that squash raw inputs onto the curve's interval.
A minimal, correct example (we ran it with torchcurves 0.3.4 and PyTorch 2.15 on CPU):
import torch
import torchcurves as tc
u = torch.rand(4, 3) # 4 samples, 3 curves
curve = tc.BSplineCurve(num_curves=3, dim=2, knots_config=8)
y = curve(u) # torch.Size([4, 3, 2])
y.sum().backward() # gradients reach curve.control_points
Note the argument names. The earlier version of this post showed BSplineCurve(num_control_points=8, degree=3, dim=1); that signature does not exist and raises a TypeError. The number of coefficients is set by knots_config, and num_curves is required.
The paper is candid about performance: the library uses classic algorithms written as vectorised PyTorch, favouring readability over custom GPU kernels. In the author's timings it is slower than SciPy or NumPy for small problems, "generally in the ballpark" of the EfficientKAN implementation for KAN training, and faster for larger batches and dimensions [1].
Why it matters to a business
The interesting property is not speed; it is that each curve is small enough to show to a person. That helps in three situations.
When someone has to sign the model off. A credit, pricing or risk model often has to be explained to a reviewer, a customer or an auditor. An additive model, prediction = bias + f1(x1) + f2(x2) + ..., can be reviewed one plotted curve at a time. With TorchCurves it trains in the same PyTorch pipeline as the rest of your models, and the whole model is a table of coefficients you can version and diff.
When the shape must follow a rule. Higher bids should not lower the chance of winning an auction; a larger claim should not lower a risk score. A B-spline whose coefficients increase is itself increasing [2], so you can build monotonicity into the model rather than hoping the data teaches it. The library's own example does exactly this for ad-auction win probability [2].
When a number needs a richer representation. Inside a recommender or transformer, a numeric feature such as "time since last visit" can be mapped by a curve to an embedding vector instead of being bucketed. This continuous-embedding use is the library's first listed use case [1][2] and builds on the author's earlier work on factorisation machines [6].
Where curves fit among other models
Curves are not a free lunch. An additive model cannot see interactions between features unless you add them explicitly. We trained six models on the same synthetic table (five features, known shapes, plus one hidden interaction) to make the trade-off concrete:
| Approach | Readable? | Shape constraints | Best when |
|---|---|---|---|
| Linear or logistic regression | Yes | Sign of a slope only | Effects really are straight lines |
| Spline GAM (scikit-learn, SciPy, R mgcv) | Yes, one curve per feature | With extra work | Tabular data, mature statistical tooling wanted |
| TorchCurves additive or monotone layers | Yes, one curve per feature | Monotone by construction; smoothness penalties | The curve must live inside a PyTorch model, or coefficients come from another network |
| KAN built from curves | Partly; many curves combined | Per curve | Research, scientific functions, compact nonlinear models |
| Gradient boosting, MLPs | No; needs post-hoc explanation | Limited (boosting libraries offer monotone constraints) | Accuracy first, interactions matter |
If a single curve on its own is the whole job, SciPy's make_smoothing_spline fitted our test curve more closely than gradient descent did, with the smoothing level chosen automatically [8]. TorchCurves pays off when the curve is one part of something PyTorch trains.
A playbook for your first curve model
- Pick one decision that needs explaining. A price response, a risk score, a calibration step. Write down the rules the shape must obey (increasing, flat beyond a cap, smooth).
- Build the baselines first. A linear model, a scikit-learn spline GAM [7] and your current black box. They tell you what readability costs on your data.
- Standardise inputs and choose the input map. TorchCurves squashes raw inputs onto the curve's interval; the default "rational" map puts most knots near zero. Clamp to a known range when you can.
- Start with 8 to 12 B-spline coefficients per feature and a small second-difference penalty, the P-spline idea [3]. Pick the count on a validation set.
- Add constraints in the parameterisation, such as cumulative sums of positive steps for monotone curves, not as hopeful penalties.
- Export the coefficients, knots and input map to JSON and re-evaluate them outside PyTorch. We matched SciPy to within 1e-6.
- Review the plotted curves with the people who own the decision before you look at the accuracy table again.
Checklist
- Inputs standardised, and the input map and scale chosen deliberately.
- Number of coefficients chosen on validation data, not by eye.
- Smoothness penalty on neighbouring coefficients, especially at the edges.
- Monotone or bounded shapes enforced by construction where a rule requires it.
- Curves plotted per feature and checked against domain expectations.
- Coefficients, knots, degree, input map and library version stored with the model.
- Tests: shapes, gradients (
torch.autograd.gradcheck), a reference evaluation in SciPy, and monotonicity on a dense grid.
Common mistakes
- Copying invented APIs. Check signatures against the installed version; the library renamed its input-map arguments in 0.3.0 [2].
- Feeding raw, unscaled features. Values far outside the knot range are squashed or clamped, and the curve goes flat where your data actually lives.
- Too many coefficients, no penalty. In our calibration run the last unpenalised coefficient drifted to 32.6 on the logit scale, driven by a handful of points near the edge. A penalty of 0.001 brought it to 5.1.
- Calling an additive model "explainable" and stopping there. It is readable, but it still misses interactions. Measure what that costs.
- Exporting with a fixed batch size.
torch.exportfixes the batch dimension unless you mark it dynamic; our first export refused any batch but 8.
What to do this week
- Install
torchcurvesin a throwaway environment and run the hello-world above. - Pick one model your team struggles to explain and fit a spline GAM baseline to it in scikit-learn.
- Rebuild one input of that model as a TorchCurves monotone curve and plot it next to the old behaviour.
- Write the JSON export and a SciPy re-evaluation test, so the curve can be audited without PyTorch.
- Show the plots to the person who signs the model off and note what they ask.
If you want help building interpretable models into a production MLOps pipeline, with versioned coefficients, tests and monitoring, see our MLOps setup service.
References
- A. Shtoff, "TorchCurves: Differentiable Parametric Curves", arXiv:2611.14516, November 2026
- alexshtf/torchcurves on GitHub: README, source and release notes (v0.3.4)
- P. H. C. Eilers and B. D. Marx, "Flexible smoothing with B-splines and penalties", Statistical Science 11(2), 1996
- torchcurves on PyPI
- Z. Liu et al., "KAN: Kolmogorov-Arnold Networks", ICLR 2025
- A. Shtoff, E. Abboud, R. Stram, O. Somekh, "Function Basis Encoding of Numerical Features in Factorization Machines", TMLR 2024
- scikit-learn: SplineTransformer
- SciPy: make_smoothing_spline