Skip to content
Advertisement

Reshape original X from LSTM with predictions

I have a tensor with my LSTM inputs X (in PyTorch) as well as the matching predictions Y_hat

I want to add the Y_hat as a column to the original X.

The problem is that LSTM a sliding window with seq_length. In case seq. length is 3 and I have 6 variables in X, and 2 variables in Y_hat, I have something like this:

First entry of Tensor:

JavaScript

and so on.

Is there an easy pythonesk command to reshape the X with Y so I can quickly get:

JavaScript

Advertisement

Answer

Having defined X1, X2, Y_hat1, and Y_hat2:

JavaScript

As well as stacks of inputs (X) and targets (Y):

JavaScript

You can the desired operation using concatenation and stacking operators:

JavaScript

Which expanded corresponds to:

JavaScript

In a more vectorized fashion you can do:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement