File size: 1,925 Bytes
bd894ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | from __future__ import annotations
import torch
from torch import nn
from torch.nn import functional as F
class LinearSplineLayer(nn.Module):
def __init__(
self,
input_dimensions: int,
output_dimensions: int,
grid_points: int,
) -> None:
super().__init__()
centers = torch.linspace(-1, 1, grid_points)
self.register_buffer("centers", centers)
self.spacing = float(centers[1] - centers[0])
self.coefficients = nn.Parameter(
torch.randn(output_dimensions, input_dimensions, grid_points) * 0.05
)
self.base_weight = nn.Parameter(
torch.randn(output_dimensions, input_dimensions) * 0.1
)
self.bias = nn.Parameter(torch.zeros(output_dimensions))
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
distance = torch.abs(inputs[:, :, None] - self.centers)
basis = F.relu(1 - distance / self.spacing)
spline = torch.einsum("big,oig->bo", basis, self.coefficients)
return spline + F.linear(inputs, self.base_weight, self.bias)
class SplineKAN(nn.Module):
def __init__(self) -> None:
super().__init__()
self.first = LinearSplineLayer(2, 16, 14)
self.second = LinearSplineLayer(16, 1, 8)
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
hidden = torch.tanh(self.first(inputs))
return self.second(hidden)
class MatchedMLP(nn.Module):
def __init__(self) -> None:
super().__init__()
self.network = nn.Sequential(
nn.Linear(2, 32),
nn.Tanh(),
nn.Linear(32, 16),
nn.Tanh(),
nn.Linear(16, 1),
)
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
return self.network(inputs)
def parameter_count(model: nn.Module) -> int:
return sum(parameter.numel() for parameter in model.parameters())
|