Problem with pytorch stack function with the labels of a set of images
I am trying to do run the following code https://github.com/kamenbliznashki/generative_models/blob/master/ssvae.py#L174
. Unfortunately I am encoutering a few problems (line 315,316).
More specifically I have a list of tensor images for example:
[test_dataloader.dataset[i][0] for i in [0,1,2]]

That I want to stack
torch.stack([test_dataloader.dataset[i][0] for i in [0,1,2]], dim=0)
#tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]],

Everything works perfectly fine in this case. But I also have a label with those images and the stack function is not working anymore. I guess the reason is that test_dataloader.dataset[i][1] for i in [0,1,2] returns a int for every images but pytorch doesn't seem to like it. I think there was an update in pytorch since the guy pushed his code, it used to work in december 2018.
torch.stack([test_dataloader.dataset[i][1] for i in [0,1,2]], dim=0).to(args.device)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-63-ac227397e18c&gt; in &lt;module&gt;
----&gt; 1 torch.stack([test_dataloader.dataset[i][1] for i in [0,1,2]], dim=0).to(args.device)

TypeError: expected Tensor as element 0 in argument 0, but got int

I have tried
torch.stack(torch.FloatTensor([test_dataloader.dataset[i][1] for i in [0,1,2]]), dim=0)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-43-7c4b1dd41c0f&gt; in &lt;module&gt;
----&gt; 1 torch.stack([torch.FloatTensor(test_dataloader.dataset[i][1]) for i in [0,1,2]], dim=0)

RuntimeError: stack expects each tensor to be equal size, but got [7] at entry 0 and [2] at entry 1

I have also tried, where my end goal is to one_hot those values:
def one_hot(x, label_size):
    out = torch.zeros(len(x), label_size).to(x.device)
    out[torch.arange(len(x)), x.squeeze()] = 1
    return out

y = torch.FloatTensor([test_dataloader.dataset[i][1] for i in [0,1,2]])
# &gt; tensor([7., 2., 1.])

y = one_hot(y, args.y_dim)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
 &lt;ipython-input-60-2357ef4d4795&gt; in &lt;module&gt;
 ----&gt; 1 y = one_hot(y, args.y_dim)

&lt;ipython-input-16-cf3479f01a91&gt; in one_hot(x, label_size)
 67 def one_hot(x, label_size):
 68     out = torch.zeros(len(x), label_size).to(x.device)
---&gt; 69     out[torch.arange(len(x)), x.squeeze()] = 1
 70     return out

IndexError: tensors used as indices must be long, byte or bool tensors


The problem with the last attempt is easy to solve. Change from torch.FloatTensor to torch.LongTensor:
y = torch.tensor([test_dataloader.dataset[i][1] for i in [0,1,2]], dtype=torch.long)

