Input a list of tensors to a model without the need to manually transfer each item to cuda

Hi,

I’d like to input a list of tensors to my model. However, pytorch doesn’t automatically transfer each item in this list to cuda and I need to manually do that each time by .cuda() . Is there a better more elegant way of dealing with this?

Hi,

I think this can help:

list_of_tensors = [torch.randn(1, 2, 2), torch.randn(1, 2, 2), torch.randn(1, 2, 2)]
b = torch.Tensor(len(list_of_tensors), 2, 2)
b = torch.cat(list_of_tensors, dim=0)
print(b.shape)
b = b.cuda()
print(b.device)

Please see this thread:

Bests