Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOA CapsuleNetwork by Luminosity-e

import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F

class CapsuleAI:
def init(self, input_size, num_classes, activation_fn):
self.input_size = input_size
self.num_classes = num_classes
self.activation_fn = activation_fn
self.capsule_net = self._create_capsule_network()
self.optimizer = self._create_optimizer()
self.loss_function = SymphonicLoss()
self.pipeline = nn.Sequential(
CapsuleRegularization(),
DynamicRouting()
)

python
Copy code
def _create_capsule_network(self):
return CapsuleNetwork(self.input_size, self.num_classes, self.activation_fn)

def _create_optimizer(self):
return optim.AdamW(self.capsule_net.parameters(), lr=1e-3)

def train(self, train_loader, num_epochs):
self.capsule_net.train()

for epoch in range(num_epochs):
for inputs, targets in train_loader:
self.optimizer.zero_grad()

outputs, _ = self.capsule_net(inputs)

outputs = self.pipeline(outputs) # Apply the pipeline transformations

loss = self.loss_function(outputs, targets)

loss.backward()
self.optimizer.step()

def predict(self, test_loader):
self.capsule_net.eval()

predictions = []

with torch.no_grad():
for inputs, _ in test_loader:
outputs, _ = self.capsule_net(inputs)

outputs = self.pipeline(outputs) # Apply the pipeline transformations

_, predicted_labels = torch.max(outputs, dim=1)
predictions.append(predicted_labels)

return torch.cat(predictions)
class CapsuleLayer(nn.Module):
def init(self, num_capsules, num_route_nodes, in_channels, out_channels):
super().init()
self.num_capsules = num_capsules
self.num_route_nodes = num_route_nodes
self.in_channels = in_channels
self.out_channels = out_channels

python
Copy code
self.route_weights = nn.Parameter(torch.randn(num_capsules, num_route_nodes, in_channels, out_channels))

def forward(self, x):
batch_size = x.size(0)
x = x.unsqueeze(2).unsqueeze(4) # Add dimensions

x = self._tile_input(x)
u_hat = self._compute_u_hat(x)
b = self._initialize_routing(batch_size, x.device)
v = self._perform_dynamic_routing(u_hat, b)

return v.squeeze()

def _tile_input(self, x):
return x.repeat(1, 1, self.num_capsules, 1, 1)

def _compute_u_hat(self, x):
u_hat = torch.matmul(x, self.route_weights)
u_hat = u_hat.view(u_hat.size(0), self.num_route_nodes, self.num_capsules, self.out_channels, -1)
return u_hat

def _initialize_routing(self, batch_size, device):
return torch.zeros(batch_size, self.num_route_nodes, self.num_capsules, 1, 1, device=device)

def _perform_dynamic_routing(self, u_hat, b):
num_iterations = 3
for _ in range(num_iterations):
c = F.softmax(b, dim=2)
s = (c * u_hat).sum(dim=1, keepdim=True)
v = self._squash(s)

return v

def _squash(self, s):
squared_norm = (s ** 2).sum(dim=-1, keepdim=True)
scale = squared_norm / (1 + squared_norm)
v = scale * s / torch.sqrt(squared)







This post first appeared on A Day Dream Lived., please read the originial post: here

Share the post

SOA CapsuleNetwork by Luminosity-e

×

Subscribe to A Day Dream Lived.

Get updates delivered right to your inbox!

Thank you for your subscription

×