File size: 1,918 Bytes
c33f5fa | 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 | from __future__ import annotations
import torch
from torch import nn
def squash(vectors: torch.Tensor) -> torch.Tensor:
squared_norm = vectors.square().sum(dim=-1, keepdim=True)
scale = squared_norm / (1 + squared_norm)
return scale * vectors / torch.sqrt(squared_norm + 1e-8)
class DynamicRoutingCapsuleNet(nn.Module):
def __init__(self, routing_iterations: int = 3) -> None:
super().__init__()
self.routing_iterations = routing_iterations
self.primary = nn.Linear(64, 28)
self.transforms = nn.Parameter(torch.randn(7, 10, 4, 8) * 0.08)
def forward(self, pixels: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
primary = squash(torch.tanh(self.primary(pixels)).reshape(-1, 7, 4))
votes = torch.einsum("bpd,pcde->bpce", primary, self.transforms)
routing_logits = torch.zeros(
len(pixels),
7,
10,
device=pixels.device,
)
digit_capsules = None
for iteration in range(self.routing_iterations):
coupling = torch.softmax(routing_logits, dim=2)
digit_capsules = squash((coupling[..., None] * votes).sum(dim=1))
if iteration + 1 < self.routing_iterations:
agreement = (votes * digit_capsules[:, None]).sum(dim=-1)
routing_logits = routing_logits + agreement
assert digit_capsules is not None
return digit_capsules, digit_capsules.norm(dim=-1)
class MatchedMLP(nn.Module):
def __init__(self) -> None:
super().__init__()
self.network = nn.Sequential(
nn.Linear(64, 54),
nn.GELU(),
nn.Linear(54, 10),
)
def forward(self, pixels: torch.Tensor) -> torch.Tensor:
return self.network(pixels)
def parameter_count(model: nn.Module) -> int:
return sum(parameter.numel() for parameter in model.parameters())
|