Complete worked example
python
from bootstrap_lab import torch_setup
torch = torch_setup()
def centered(rows):
x = torch.tensor(rows, dtype=torch.float32)
if x.ndim != 2 or x.shape[0] == 0 or x.shape[1] == 0:
raise ValueError('nonempty matrix required')
means = x.mean(dim=0, keepdim=True)
result = x - means
return {'means': means.tolist(), 'centered': result.tolist()}- Reasoning
- The helper computes feature means with dim=0 and keepdim=True, retaining shape one by features. Subtracting that result from the batch uses an explicit broadcast.