PyTorch LSTM Data Structure

Hi, I currently have a dataset with multiple features, where each row is a time-series and each column is a time step. For example:

feature1_time1 feature1_time2 feature1_time3 feature2_time1 feature2_time2 feature2_time3 target
1 4 7 10 2 1 0
2 5 8 1 4 4 1
3 6 9 4 6 5 0

How should I re-shape the data so that I can properly represent the sequential information when I use a pytorch LSTM?

Currently I’ve left it the way it is, transformed the features into tensors and wrapped it inside a variable and reshaped it using this code:

X_train_tensors = Variable(torch.Tensor(X_train), requires_grad=True)
X_test_tensors = Variable(torch.Tensor(X_test), requires_grad=True)

y_train_tensors = Variable(torch.Tensor(y_train), requires_grad=True)
y_test_tensors = Variable(torch.Tensor(y_test))

Final Shape Looks like:

torch.Size([num_rows, 1, num_features])

The LSTM runs fine, however, I’m worried that I’ve not captured the sequential nature of the dataset by keeping it at this orientation? Should I have made every row a time-sequence and the columns a time-series? And in that case what would the final shape look like and how could I transform that using pytorch tools?

Packing them as is in a variable is not a good idea: it means that feature2_time1 came after feature1_time3 for example, which is simply not the case. Your model did good because LSTMs can just memorise any data you give them, whether the patterns actually exist or not.

Also, does feature1_time1 has multiple values (1, 2, 3, etc) ? I don’t know what the feature represents, but you can try reducing the dimension.

Hi thanks for your response! I will remove the variable, how would you suggest then that I order the data? And no feature1_time1 doesn’t have multiple values, those are different instances. So each row represents an individual user or person. @astle_dsa

The way you can arrange is probably this: (num_features, num_users, num_time steps) or (num_users, num_time steps, num_features) and pack them into a third-order tensor. So the labels or target would be a matrix. You can try out this preprocessing and test you results.