torch stack producing wrong dimensions
I am quite new to pytorch, and I am trying to create data within a dataloader and my code looks like so:

a=[]
.. within a for loop
self.a.append(torch.stack([b[ith_idx][j], \
b[ith_idx][rnd_dist], \
b[rnd_cls_idx][rnd_dist_rnd_cls]]\
))
self.c.append([1,0])


where, b is a python list of tensors. For example, the first element of b has shape torch.Size([46, 3, 512, 512]) (3 channels 512 x 512).

        self.a = torch.stack(self.a)
        self.c = torch.tensor(self.c)


and I notice I have shapes of [500,3,3,512,512] and [500,2] for a and b, while I was expecting 500,3,3,512,512 and 500 as tensor shapes.

Any pointers as to why this is happening would be helpful.

The shape of c is [500, 2] because you are appending a list of two values, [1,0] each time to c inside the loop. If you append either 1 or 0 iteratively then you will have a list of 500 elements.

For instance, you can append 1 or 0 randomly like follows:

self.c.append(np.random.randint(2))  # import numpy as np


or

self.c.append(torch.randint(0,2,(1,)))

