title: How to combine/stack tensors and combine dimensions in PyTorch?
body: <p>I need to combine 4 tensors, representing greyscale images, of size [1,84,84], into a stack of shape [4,84,84], representing four greyscale images with each image represented as a "channel" in tensor style CxWxH.</p>
  
  <p>I am using PyTorch.</p>
  
  <p>I've tried using torch.stack and torch.cat but if one of these is the solution, I am not having luck figuring out the correct prep/methodology to get my results.</p>
  
  <p>Thank you for any help.</p>
  
  <pre><code>import torchvision.transforms as T
  
  class ReplayBuffer:
      def __init__(self, buffersize, batchsize, framestack, device, nS):
          self.buffer = deque(maxlen=buffersize)
          self.phi = deque(maxlen=framestack)
          self.batchsize = batchsize
          self.device = device
  
          self._initialize_stack(nS)
  
      def get_stack(self):
          #t =  torch.cat(tuple(self.phi),dim=0)
          t =  torch.stack(tuple(self.phi),dim=0)
          return t
  
      def _initialize_stack(self, nS):
          while len(self.phi) &lt; self.phi.maxlen:
              self.phi.append(torch.tensor([1,nS[1], nS[2]]))
  
  a = ReplayBuffer(buffersize=50000, batchsize=64, framestack=4, device='cuda', nS=[1,84,84])
  print(a.phi)
  s = a.get_stack()
  print(s, s.shape)
  </code></pre>
  
  <p>The above code returns:</p>
  
  <pre><code>print(a.phi)
  
  deque([tensor([ 1, 84, 84]), tensor([ 1, 84, 84]), tensor([ 1, 84, 84]), tensor([ 1, 84, 84])], maxlen=4)
  
  print(s, s.shape)
  
  tensor([[ 1, 84, 84],
          [ 1, 84, 84],
          [ 1, 84, 84],
          [ 1, 84, 84]]) torch.Size([4, 3])
  </code></pre>
  
  <p>But what I would like is the return to simply be [4, 84, 84]. I suspect this is quite simple but it's escaping me.</p>