title: pytorch DataLoader: `Tensors must have same number of dimensions`
body: <p>I am trying to fit an LSTM model in Pytorch. My data is too big to be read into memory and so I want to create mini-batches of data using the <code>DataLoader</code> function from Pytorch.</p>
  
  <p>I have two features as input (<code>X1</code>, <code>X2</code>). I have one output feature (y). I am using 365 timesteps of <code>X1</code> &amp; <code>X2</code> as features used to predict <code>y</code>. </p>
  
  <p>The dimensions of my training array is:</p>
  
  <p><code>(n_observations, n_timesteps, n_features)</code> == <code>(9498, 365, 2)</code></p>
  
  <p>I don't understand why the code below isn't working because I have seen other examples where the X, y pairs have different numbers of dimensions (<a href="https://github.com/kratzert/pangeo_lstm_example/blob/master/LSTM_for_rainfall_runoff_modelling.ipynb" rel="nofollow noreferrer">LSTM for runoff modelling</a>, <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#iterating-through-the-dataset" rel="nofollow noreferrer">Pytorch's own docs</a> )</p>
  
  <h1>Minimum Reproducible Example</h1>
  
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import torch
  from torch.utils.data import DataLoader
  
  train_x = torch.Tensor(np.random.random((9498, 365, 2)))
  train_y = torch.Tensor(np.random.random((9498, 1)))
  val_x = torch.Tensor(np.random.random((1097, 365, 2)))
  val_y = torch.Tensor(np.random.random((1097, 1)))
  test_x = torch.Tensor(np.random.random((639, 365, 2)))
  test_y = torch.Tensor(np.random.random((639, 1)))
  
  train_dataset = (train_x, train_y)
  test_dataset = (test_x, test_y)
  val_dataset = (val_x, val_y)
  
  train_dataloader = DataLoader(train_dataset, batch_size=256)
  
  iterator = train_dataloader.__iter__()
  iterator.next()
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-47-2a0b28b53c8f&gt; in &lt;module&gt;
       13 
       14 iterator = train_dataloader.__iter__()
  ---&gt; 15 iterator.next()
  
  /opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
      344     def __next__(self):
      345         index = self._next_index()  # may raise StopIteration
  --&gt; 346         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
      347         if self._pin_memory:
      348             data = _utils.pin_memory.pin_memory(data)
  
  /opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
       45         else:
       46             data = self.dataset[possibly_batched_index]
  ---&gt; 47         return self.collate_fn(data)
  
  /opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
       53             storage = elem.storage()._new_shared(numel)
       54             out = elem.new(storage)
  ---&gt; 55         return torch.stack(batch, 0, out=out)
       56     elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
       57             and elem_type.__name__ != 'string_':
  
  RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 4 and 3 at /tmp/pip-req-build-4baxydiv/aten/src/TH/generic/THTensor.cpp:680
  </code></pre>