I'm in the process of rebuilding a network using PyTorch. The Keras implementation uses a LSTM module with the parameter go_backwards=true:

model = Sequential()
model.add(LSTM(output_dim=z, go_backwards=True))

According to the Tensorflow Doc this

process[es] the input sequence backwards and return[s] the reversed sequence.

Sadly, I'm not sure how to reimplement this behavior in PyTorch, as there is no similar functionality implemented (as far as I know). I tried looking up the keras implementation but got lost somewhere in the keras backend.py ... Would it be sufficient to reverse the input passed into the LSTM regarding the relevant dimension and do the same to the output? Do you have another idea?

class Model(nn.Module):
    def __init__(self):
        self.lstm = nn.LSTM(input_size=y,hidden_size=z,batch_first=True)

    def forward(self, x):
        x = torch.flip(x, [2])
        x, _ = self.lstm(x)
        x = torch.flip(x, [2])

Please ask if you need more information on what I'm trying to accomplish. Thank you in advance!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.