Convert int into one-hot format
Hi all. <SCODE>for batch_idx, (x, y) in enumerate(train_loader):
    y_onehot = y.numpy()
    y_onehot = (np.arange(num_labels) == y_onehot[:,None]).astype(np.float32)
    y_onehot = torch.from_numpy(y_onehot)
<ECODE> However, I notice that the it gets slower each iteration, and I doubt it’s these code which might request new memory each iteration that makes the code slower. Thanks!
Response777
moskomule
Response777
Hi, <SCODE>import torch

batch_size = 5
nb_digits = 10
# Dummy input that HAS to be 2D for the scatter (you can use view(-1,1) if needed)
y = torch.LongTensor(batch_size,1).random_() % nb_digits
# One hot encoding buffer that you create out of the loop and just keep reusing
y_onehot = torch.FloatTensor(batch_size, nb_digits)

# In your for loop
y_onehot.zero_()
y_onehot.scatter_(1, y, 1)

print(y)
print(y_onehot)
<ECODE>
albanD
Thanks, that is exactly what I need!
Response777
Nadav_Bhonker
Response777
apaszke
And also for future readers just to reiterate what user moskomule says- cross entropy and neg. log-likelihood losses in pytorch do NOT require one-hot encodings, so you can just use the normal target vector.
ncullen93
apaszke
How about overriding the default nn.Embedding weights data with torch.eye ? <SCODE>emb = nn.Embedding(10, 10) 
emb.weight.data = torch.eye(10)
<ECODE>
rajarsheem
Additionaly, zero + scatter a few ones will be  much faster than copying whole rows, of which most values are 0 anyway.
apaszke
<SCODE>batch_size=10
y = torch.LongTensor(batch_size,5,5).random_() % 3#3 classes,5x5 img
y_onehot = torch.FloatTensor(batch_size,3, 5,5)#I want the one hot going through the chans dim
y_onehot.zero_()
ones=torch.ones(y.size())

y_onehot.scatter_(1,y,ones)
<ECODE> Could you help me with this? Thanks!
rogetrullo
oh never mind, I just found that it works if I add a singleton dimension so that y and y_onehot have the same NUMBER of dimensions…
rogetrullo
zeng
albanD
Sorry, I write wrong, it is also not available for cuda.LongTensor.
zeng
albanD
Thank you, my problem. I ignored the y_onehot type is not a cuda tensor.
zeng
Thank you! I got my cvae implemented with this tip.
kakrafoon