ML Katas

Creating a Label Mapping with `scatter` for Domain Adaptation

easy (<30 mins) scatter indexing
this month by E

In domain adaptation, you might need to map labels from a source domain to a target domain. Imagine you have a set of labels and a mapping that specifies how each old label corresponds to a new label. Your exercise is to create a new tensor of labels based on this mapping. You will be given a 1D tensor of source_labels and a 1D mapping tensor where mapping[i] is the new label for the old label i. Use torch.scatter to perform this mapping efficiently. [26]

Example: source_labels = torch.tensor([0, 1, 2, 0, 3]) mapping = torch.tensor([10, 11, 12, 13]) The output should be tensor([10, 11, 12, 10, 13]).

Verification: - The output tensor should have the same shape as source_labels. - Each element in the output tensor should be the result of mapping the corresponding element in source_labels through the mapping tensor.