Convert a LSTM model from Keras to PyTorch

I have a model developed in Keras that I wish to port over to PyTorch. The model is

from keras.models import Sequential

from keras.layers import Dropout, Dense, LSTM, Bidirectional,Embedding, GlobalMaxPool1D

vocabulary_size = vocab_size

seq_len = 40

embed_len=20

model = Sequential()

model.add(Embedding(vocabulary_size+1, embed_len, input_length=seq_len))

model.add(LSTM(units = 80, return_sequences=True))

model.add(Dropout(0.5))

model.add(LSTM(units = 40))

model.add(Dropout(0.5))

model.add(Dense(units=20, activation='softmax'))

model.summary()

In my experience, the best approach is to do it bit by bit and compare the intermediate results when you put in the same inputs. Typically, you have to rearrange axes a bit to make things work.

1 Like