Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat a tuple/list of Tensors as a single Tensor

Tags:

pytorch

I'm using Pytorch for some robotics Reinforcement Learning tasks. I'd like to use both images and information about the state as observations for this task. The implementation I'm using does not directly support this so I'm making some amendments. Expected observations are either state, as a 1 dimensional Tensor, or images as a 3 dimensional Tensor (channels, width, height). In my task I would like the observation to be a tuple of Tensors.

In many places in my codebase, the observation is of course expected to be a single Tensor, not a tuple of Tensors. Is there an easy way to treat a tuple of Tensors as a single Tensor?

For example, I would like:

observation.to(device)

to work as normal when observation is a single Tensor, and call .to(device) on each Tensor when observation is a tuple of Tensors.

It should be simple enough to create a data type that can support this, but I'm wondering does such a data type already exist? I haven't found anything so far.

like image 366
Harry Uglow Avatar asked May 28 '19 14:05

Harry Uglow


People also ask

How do you turn a tuple into a tensor?

The best way to convert from a tuple to a tensor is to use the torch. stack or torch.cat . Although as previously mentioned in this thread, all the tensors must be of 'equal size'. So, it's something you'll have to implement on a case-by-case basis.

How do you concatenate tensors PyTorch?

We can join tensors in PyTorch using torch.cat() and torch. stack() functions. Both the function help us to join the tensors but torch.cat() is basically used to concatenate the given sequence of tensors in the given dimension.

How do you add two tensors together?

Two tensors of the same size can be added together by using the + operator or the add function to get an output tensor of the same shape.


1 Answers

If your tensors are all of the same size, you can use torch.stack to concatenate them into one tensor with one more dimension.

Example:

>>> import torch
>>> a=torch.randn(2,1)
>>> b=torch.randn(2,1)
>>> c=torch.randn(2,1)
>>> a
tensor([[ 0.7691],
        [-0.0297]])
>>> b
tensor([[ 0.4844],
        [-0.9142]])
>>> c
tensor([[ 0.0210],
        [-1.1543]])
>>> torch.stack((a,b,c))
tensor([[[ 0.7691],
         [-0.0297]],

        [[ 0.4844],
         [-0.9142]],

        [[ 0.0210],
         [-1.1543]]])

You can then use torch.unbind to go the other direction.

like image 118
Brennan Vincent Avatar answered Oct 02 '22 22:10

Brennan Vincent