Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One hot encoding in pytorch

Tags:

pytorch

I am really new to coding, right now I am trying to turn my label to one hot encoding. I have already done transferring the np.array to tensor as shown below

tensor([4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 3., 3., 3., 3., 3., 3.,
        3., 3., 3., 3., 3., 3., 3., 3., 3., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
        2., 2., 2.], dtype=torch.float64)

and I am using code to do one hot encoding 

aaa = F.one_hot(torch_qyh, num_classes=5)

However, there is an error showing "RuntimeError: one_hot is only applicable to index tensor." Any help would appreciated.

like image 926
o0o Avatar asked Oct 31 '25 21:10

o0o


1 Answers

You will have to convert it in long type. Can't do it with float. F.one_hot only takes LongTensor.

F.one_hot(t.long())
like image 138
Dishin H Goyani Avatar answered Nov 04 '25 20:11

Dishin H Goyani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!