converting tensor to one hot encoded tensor of indices
I have my label tensor of shape (1,1,128,128,128) in which the values might range from 0,24. I want to convert this to one hot encoded tensor, using the nn.fucntional.one_hot function

n = 24
one_hot = torch.nn.functional.one_hot(indices, n)


but this expects a tensor of indices, honestly, I am not sure how to get those. The only tensor I have is the label tensor of the shape described above and it contains values ranging from 1-24, not the indices

How can I get a tensor of indices from my tensor? Thanks in advance.

If the error you are getting is this one:

Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
RuntimeError: one_hot is only applicable to index tensor.


Maybe you just need to convert to int64:

import torch

# random Tensor with the shape you said
indices = torch.Tensor(1, 1, 128, 128, 128).random_(1, 24)
# indices.shape =&gt; torch.Size([1, 1, 128, 128, 128])
# indices.dtype =&gt; torch.float32

n = 24
one_hot = torch.nn.functional.one_hot(indices.to(torch.int64), n)
# one_hot.shape =&gt; torch.Size([1, 1, 128, 128, 128, 24])
# one_hot.dtype =&gt; torch.int64


You can use indices.long() too.
