PyTorch: TypeError: 'int' object is not subscriptable
I'm new to pytorch and I am creating a one hot encoding function for multi-layer perceptron but I'm having some issues. Here is the code:
def one_hot_encoding(label):
    for idx, val in enumerate(label):
        one_hot_outputs = [0]*len(label)
        idx_n = idx[val]
        one_hot_outputs[idx_n] = 1
return one_hot_outputs

I'm having a type error saying:
in one_hot_encoding(label)
  2     for idx, val in enumerate(label):
  3         one_hot_outputs = [0]*len(label)
&gt; 4         idx_n = idx[val]
  5         one_hot_outputs[idx_n] = 1
  6     return one_hot_outputs
TypeError: 'int' object is not subscriptable

Any help?

This is not a direct answer, but an alternative. PyTorch already has functionality for this: torch.nn.functional.one_hot. So, if you have a label tensor label and n classes, just call:
torch.nn.functional.one_hot(label, num_classes=n)

