Selected topic
Capsule Networks
Prefer practical output? Use related tools below while reading.
Suppose we want to recognize handwritten digits in images (MNIST dataset). We use a Capsule Network with the following architecture:
python
import torch
import torch.nn as nnclass PrimaryCaps(nn.Module):
def __init__(self, num_capsules=64):
super(PrimaryCaps, self).__init__()
self.conv = nn.Conv2d(1, 8, kernel_size=9)
self.cap = nn.Linear(144, 16) # 144 input channels (7x7 receptive field)
def forward(self, x):
x = F.relu(self.conv(x))
x = x.view(-1, 144) # Flatten
return F.softmax(self.cap(x), dim=1)
class DigitCaps(nn.Module):
def __init__(self, num_capsules=10):
super(DigitCaps, self).__init__()
self.cap = nn.Linear(16, 16 * num_capsules)
def forward(self, x):
x = F.softmax(self.cap(x), dim=1)
return x
class CapsNet(nn.Module):
def __init__(self):
super(CapsNet, self).__init__()
self.primary_caps = PrimaryCaps()
self.digit_caps = DigitCaps()
def forward(self, x):
x = self.primary_caps(x)
return self.digit_caps(x)
model = CapsNet()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
forward method implements the routing algorithm, which dynamically selects which lower-level capsules contribute to each higher-level capsule.Keep in mind that this is a simplified example, and real-world implementations may involve more complex architectures and training procedures.