Complete worked example
python
from bootstrap_lab import torch_setup
torch = torch_setup()
from torch import nn
class TinyNet(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.ModuleList([nn.Linear(2, 2), nn.Linear(2, 1)])
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
def registered(batch_size):
if type(batch_size) is not int or batch_size < 1:
raise ValueError('positive batch size required')
model = TinyNet()
output = model(torch.ones(batch_size, 2))
return {'shape': list(output.shape),
'parameters': sum(parameter.numel() for parameter in model.parameters())}- Reasoning
- TinyNet stores two linear layers in ModuleList and applies them in order. A two-feature batch becomes a one-output batch, and the helper counts every registered parameter, including biases.