I learned Keras and now i will learn PyTorch, I am a beginner. I tried to use a LSTM (both in keras and PyTorch), and the one of PyTorch doesn’t train. I know approximately how the loss and the accuracy must be with Keras, and here, they doesn’t change during the epoch. So i did the assumption that my PyTorch code is not good. I juste want to use one LSTM layer with 256 filters and one linear layer.
This is my PyTorch code :
I want to do some classifications, I have 3 classes, and the data are 1D time series with 17908 values. In the train, there are 14001 time series. The shape of the matrix is 14001x17908.
as well as the usage of batch_first=True in the nn.LSTM module the input data should have a shape of [batch_size=14001, seq_len=1, nb_features=17908].
If that’s correct, note that you would only be using a single time step so that the nn.LSTM module might not be really useful.
Could you explain the approach you’ve used in Keras/TF, i.e. which shapes (in particular sequence lengths) were used there?
PS: unrelated to this issue, but it seems you would like to transform the numpy arrays to tensors here:
X_train, X_valid = [torch.tensor(arr, dtype=torch.float32) for arr in (X_train, X_valid)]
y_train, y_valid = [torch.tensor(arr, dtype=torch.long) for arr in (target_train, target_valid)]
so it seems that the feature dimension is set to one and the temporal dimension is large.
If I’m not misunderstanding the Keras docs or the posted Keras code, I guess this would be the main difference between both codes.
Refer the docs of torch LSTM how the input data should be arranged. It is an excellent piece of documentation. Kind of felt like the input data is not arranged properly in the code. Definitely use the batch_first = True option in the lstm. Makes life a lot easier.