GAMs - Good As Mnew
Neal Fultz
Nov 6, 2025
Anderson MBDSA
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.
GAMs = Spline + Penalty
Splines can represent arbitrary curves by
Penalty is applied to params (a la ridge) to reduce "wiggliness", enforce convexity, etc.
Technical aside h/t wikipedia
Client 1
Client 2
Tradeoffs
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
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)
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
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