Complete worked example
python
from bootstrap_lab import torch_setup
torch = torch_setup()
from torch import nn
def image_features(batch_size):
if type(batch_size) is not int or batch_size < 1:
raise ValueError('positive batch required')
image = torch.ones(batch_size, 1, 3, 3)
convolution = nn.Conv2d(1, 1, kernel_size=2, bias=False)
with torch.no_grad():
convolution.weight.fill_(1)
output = convolution(image)
flattened = output.flatten(start_dim=1)
return {'shape': list(output.shape), 'features': flattened.tolist()}- Reasoning
- The fixture creates owned three-by-three single-channel images of ones and a two-by-two kernel with weights one and no bias. Each output value is four, and the spatial result is two by two.