How to feed a 4D tenstor to LSTM model?

username: Adrien88
cooked: <p>I want to use a LSTM model to predict the future sales.</p>
<p>The data is like the table below.</p>
<div class="md-table">
<table>
<thead>
<tr>
<th>date</th>
<th style="text-align:center">store</th>
<th>family</th>
<th style="text-align:right">sales</th>
</tr>
</thead>
<tbody>
<tr>
<td>01/01/2013</td>
<td style="text-align:center">1</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>01/01/2013</td>
<td style="text-align:center">1</td>
<td>BABY CARE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>01/01/2013</td>
<td style="text-align:center">1</td>
<td>BEAUTY</td>
<td style="text-align:right">1</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>01/01/2013</td>
<td style="text-align:center">2</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>01/01/2013</td>
<td style="text-align:center">2</td>
<td>BABY CARE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>01/01/2013</td>
<td style="text-align:center">50</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>01/02/2013</td>
<td style="text-align:center">1</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>01/02/2013</td>
<td style="text-align:center">1</td>
<td>BABY CARE</td>
<td style="text-align:right">50</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>01/02/2013</td>
<td style="text-align:center">2</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">500</td>
</tr>
<tr>
<td>01/02/2013</td>
<td style="text-align:center">2</td>
<td>BABY CARE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>01/02/2013</td>
<td style="text-align:center">50</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>12/31/2015</td>
<td style="text-align:center">1</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>12/31/2015</td>
<td style="text-align:center">1</td>
<td>BABY CARE</td>
<td style="text-align:right">50</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>12/31/2015</td>
<td style="text-align:center">2</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">500</td>
</tr>
<tr>
<td>12/31/2015</td>
<td style="text-align:center">2</td>
<td>BABY CARE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
<tr>
<td>12/31/2015</td>
<td style="text-align:center">50</td>
<td>AUTOMOTIVE</td>
<td style="text-align:right">0</td>
</tr>
<tr>
<td>…</td>
<td style="text-align:center">.</td>
<td>…</td>
<td style="text-align:right">.</td>
</tr>
</tbody>
</table>
</div><blockquote>
<p>For each day, it has 50 stores.<br>
For each store, it has different type of family (product). (They are all in perfect order, thank God).<br>
Last, for each type of family, it has its sales.</p>
</blockquote>
<p>Here is the problem.</p>
<p>The dimension of input of LSTM model is (Batch_Size, Sequence_Length, Input_Dimension). It is a 3D tensor.</p>
<p>However, in my case, my Input_Dimension is 2D, which is (<b>rows</b> x <b>columns</b>)<br>
<b>rows</b>: number of rows in one day, which is 1782<br>
<b>columns</b>: number of features, which is 2 (store and family)</p>
<p>Is there a good way to make my data into a shape that can be fed into a LSTM model?</p>
<p>Thanks a lot!</p>

username: ptrblck
cooked: <p>I’m sure I understand the issue completely, as you are explaining to work with 2D data but also with 4D tensors. In the former case:</p>
<aside class="quote no-group" data-username="Adrien88" data-post="1" data-topic="139009">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adrien88/48/44782_2.png" class="avatar"> Adrien88:</div>
<blockquote>
<p>However, in my case, my Input_Dimension is 2D, which is (<strong>rows</strong> x <strong>columns</strong>)<br>
<strong>rows</strong>: number of rows in one day, which is 1782<br>
<strong>columns</strong>: number of features, which is 2 (store and family)</p>
</blockquote>
</aside>
<p>it seems that the batch dimension might be missing and you should be able to add it into dim0.</p>

username: googlebot
cooked: <p>He wants to push a hierarchical dataset into a RNN, like it is a magic black box.</p>
<p>Technically, data has to be converted to wide dense format (num of features = num stores * num families). But I doubt that the resulting model will be good… well maybe with big capacity and poisson loss it will output values in reasonable ranges, but that’s about it.</p>