How to put tensors in tensor?

Hi, it’s a possibly the funniest question here but i stumbled.
I have such tensors:
image = torch.randn(1, 3, 224, 224)
coords = torch.randn(1, 2)

and want to get mapping in the form of a tensor:
mapping = torch.tensor((image, coords))
That through mapping[0] get a picture and mapping[1] get a coords but obviously i have ValueError: only one element tensors can be converted to Python scalars.

Is there correct way to do this?

Hi @MikeMACintosh,

I don’t think this is possible because they have different sizes, if they were both the same size you could use or torch.stack to represent them as a single tensor. Can you share more about your use case?

Hi @AlphaBetaGamma96
Thanks for reply, yes i can’t use cat/stack because tensors have a different shape.
I have many images and corresponding coordinate.
I create a tensor from image and feed them to DINO model to get image embedding.
Next i do some image manipulation and i want to be able to get the coordinates for the corresponding image tensor.
Moreover, I want to be able to save embeddings to disk via‘image_embedding.pth’) and later do torch.load(‘image_embedding.pth’) to continue working with them.

Right now I only save image embeddings, but I want to have an image-to-coordinate mapping and be able to save it to disk and load later.

If you’re wanting to save batches of images/coorindates, you could use torch.stack for all your images and all your coordinates separately. Then save the pair of tensors in a tuple via

1 Like