1 of 19

GAMs - Good As Mnew

Neal Fultz

Nov 6, 2025

Anderson MBDSA

2 of 19

Executive Summary

Two clients in last year have used GAMs.

This is a quick review of GAMS, in theory, and how to scale them, in practice.

3 of 19

GAMs = Spline + Penalty

Splines can represent arbitrary curves by

  • interpolating
    • not just lerp, fancier basis functions
  • between knots / control points.
    • where changes happen � (related to changepoints)
    • choosing knots is a dark art,� usually just choose "enough"

Penalty is applied to params (a la ridge) to reduce "wiggliness", enforce convexity, etc.

4 of 19

Technical aside h/t wikipedia

5 of 19

Client 1

  • Sociolinguistics
    • Several audio clips with, without an AI accent filter
    • R
  • library(mgcv)
  • m1 <- gam(f0 ~ s(t, by = filename) + s(filename, bs = "re"), data=df)

6 of 19

7 of 19

Client 2

  • Ad tech
  • Causal estimates for streaming / linear video ads
  • Python shop

​

8 of 19

9 of 19

Tradeoffs

  • PyGAM
    • Precomputes basis functions
    • PIRLS
  • What spline implementation?
    • sklearn
      • same upfront memory cost
    • PyTorch
      • Designed for fixed core applications
      • Neural Nets / Universal Approximation
        • infinite width
        • ~~ spline with infinite knots
      • requires some custom code

10 of 19

11 of 19

Initial impl

class BSplineLayer(nn.Module):

def __init__(self, knots, order): # <snip>

def forward(self, x):

basis = ((x >= self.knots[:-1]) & (x < self.knots[1:])).float()

​

# Apply Cox-de Boor recursion

for k in range(1, self.order + 1):

new_basis = torch.zeros(batch_size, n + self.order - k)

for i in range(n + self.order - k):

denom1 = self.knots[i+k] - self.knots[i]

term1 = ((x - self.knots[i]) / denom1) * basis[:, i]

denom2 = self.knots[i+k+1] - self.knots[i+1]

term2 = ((self.knots[i+k+1] - x) / denom2) * basis[:, i+1]

new_basis[:, i] = term1 + term2

basis = new_basis

return basis

​

12 of 19

13 of 19

Bells & Whistles 1

class TrainableBSplineLayer(BSplineLayer):

"""

A BSplineLayer where the knots are trainable parameters.

"""

def __init__(self, knots, order):

super(TrainableBSplineLayer, self).__init__(knots, order)

self.knots = nn.Parameter(self.knots)

​

​

14 of 19

15 of 19

16 of 19

Bells and Whistles 2

class TensorProductSurfaceLayer(nn.Module):

# <snip>

def forward(self, input1, input2):

tensor_product = torch.einsum('bi, bj -> bij', input1, input2)

​

# Reshape the tensor product to (batch_size, output_dim1 * output_dim2)

batch_size, output_dim1, output_dim2 = tensor_product.shape

output = tensor_product.view(batch_size, output_dim1 * output_dim2)

​

return output

​

17 of 19

18 of 19

Bells and Whistles 3

def spline_regularization_loss(weights):

​

diff_x1 = torch.diff(weights, dim=1)

​

regularization = torch.sum(diff_x1**2) # L2 norm of the diff

​

return regularization

​

19 of 19