Loading...
Create a Lambda transform that converts integer labels to one-hot encoded tensors.
Lambda transforms let you wrap any custom function as a torchvision transform. One-hot encoding is a common technique that converts a class label into a binary vector — useful for classification tasks.
Write a function create_onehot_transform(num_classes) that returns a Lambda transform. When applied to an integer label, the transform should produce a dictionary with the one-hot tensor and its sum.
The transform should return a dictionary with keys: "tensor" (one-hot list), "sum" (float, should be 1.0).
num_classes=5, then apply to label=2
{"tensor": [0.0, 0.0, 1.0, 0.0, 0.0], "sum": 1.0}Lambda transform creates a zero tensor and sets index 2 to 1.0