0

I currently have a tensor of torch.Size([1, 3, 256, 224]) but I need it to be input shape [32, 3, 256, 224]. I am capturing data in real-time so dataloader doesn't seem to be a good option. Is there any easy way to take 32 of size torch.Size([1, 3, 256, 224]) and combine them to create 1 tensor of size [32, 3, 256, 224]?

2
  • 1
    torch.cat on a list of 32 tensors Commented Jul 19, 2020 at 15:35
  • Yes, thank you. I was doing torch.concatenate but thats a numpy thing.
    – Normandy
    Commented Jul 19, 2020 at 15:48

1 Answer 1

1

You are probable using jit model, and the batch size must be exact like the one the model was trained on.

t = torch.rand(1, 3, 256, 224)
t.size() # torch.Size([1, 3, 256, 224])
t2= t.expand(32, -1,-1,-1)
t2.size() # torch.Size([32, 3, 256, 224])

Expanding a tensor does not allocate new memory, but only creates a new view on the existing tensor, and you get what you need. Only the tensor stride was changed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.