LSTM - Problem with multidimensional forecasting

Lxnus: <p>Hey everyone,</p>
<p>I am currently working on a lstm which should predict a multidimensional output given any input.<br>
To clarify what I mean, I have the following setup:</p>
<p>Input: [batch_size, seq_len, n_features] = [32, 16, 2]</p>
<p>LSTM:</p>
<pre><code class="lang-auto">    def __init__(self, in_features: int, n_hidden: int, out_features: int, num_layers: int):
        super(LSTMModel, self).__init__()
        self.in_features = in_features
        self.n_hidden = n_hidden
        self.out_features = out_features
        self.num_layers = num_layers

        self.lstm1 = nn.LSTM(
            input_size=in_features,
            hidden_size=n_hidden,
            num_layers=num_layers,
            batch_first=True,
            bias=True,
        )
        self.linear = nn.Linear(
            in_features=self.n_hidden,
            out_features=self.out_features,
            bias=True
        )

    def forward(self, x: torch.FloatTensor) -&gt; torch.Tensor:
        batch_size = x.size(0)

        h_t = torch.zeros(self.num_layers, batch_size, self.n_hidden, device=x.device).float()
        c_t = torch.zeros(self.num_layers, batch_size, self.n_hidden, device=x.device).float()

        x, (h_t, c_t) = self.lstm1(x, (h_t, c_t))
        x = self.linear(x[:, -1, :])
        return x
</code></pre>
<p>The Output should be:<br>
[batch_size, target_seq_len, target_n_features] = [32, 4, 2]</p>
<p>The reason for this setup: I want to ‘predict target_seq_len’, where every target is a vector with ‘target_n_features’ given a history (sequence) with the setting (desc. above)</p>
<p>So I made a multidimensional forecasting… But the problem is, that the code I use for this task only gives me an output of [batch_size, target_seq_len]. What can I do to solve the problem?</p>
<p>I use standard PyTorch and pytorch-lighnting for my problem.</p>
<p>Here is also a screenshot of the shapes (console): The first line is the y_pred and the second is the y_true</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/4/f4a3acdcbc97b6fe523c4fa35efd410c0e478b3a.jpeg" alt="Screenshot 2022-07-05 164054" data-base62-sha1="yUbaSa9pZJfz2FtWwhOohmk1MAa" width="183" height="31"></p>
<p>Do you have any ideas for code-changes or solutions?</p>
<p>~ Linus</p>

ptrblck: <aside class="quote no-group" data-username="Lxnus" data-post="1" data-topic="155882">
<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/lxnus/48/51183_2.png" class="avatar"> Lxnus:</div>
<blockquote>
<p>So I made a multidimensional forecasting… But the problem is, that the code I use for this task only gives me an output of [batch_size, target_seq_len]. What can I do to solve the problem?</p>
</blockquote>
</aside>
<p>You are explicitly using the last time step of the <code>LSTM</code> output only in this line of code:</p>
<pre data-code-wrap="python"><code class="lang-python">x = self.linear(x[:, -1, :])
</code></pre>
<p>and are thus removing the temporal dimension.<br>
You can directly pass the entire tensor to the linear layer which would then apply the layer to each time step.</p>

Lxnus: <p>Thank you for your answer. That solved my problem completely! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>