udacity / deep-learning-v2-pytorch

Projects and exercises for the latest Deep Learning ND program https://www.udacity.com/course/deep-learning-nanodegree--nd101
MIT License
5.31k stars 5.34k forks source link

Unnecessary steps were introduced in forward pass step in Sentiment_RNN_Solution.ipynb #298

Closed sahandilshan closed 3 years ago

sahandilshan commented 3 years ago

In the Sentiment_RNN_Solution.ipynb, the forward pass is getting all time steps output from the LSTM and pass it to the next fully connected layer. The filtering process only happens in the last step.

        lstm_out, hidden = self.lstm(embeds, hidden)

        # stack up lstm outputs
        lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)  <----------- stacking the all time steps output

        # dropout and fully-connected layer
        out = self.dropout(lstm_out)
        out = self.fc(out)
        # sigmoid function
        sig_out = self.sig(out)

        # reshape to be batch_size first
        sig_out = sig_out.view(batch_size, -1)
        sig_out = sig_out[:, -1] # get last batch of labels    <----------- Omiting all but the last time step output

unnecessary calculations happen in the middle of the forward pass.