dennybritz / rnn-tutorial-rnnlm

Recurrent Neural Network Tutorial, Part 2 - Implementing a RNN in Python and Theano
Apache License 2.0
895 stars 469 forks source link

I think there is something wrong with code of the function of calculate_total_loss #16

Open GoingMyWay opened 7 years ago

GoingMyWay commented 7 years ago

Hi, since the loss function formula is

$ \begin{aligned} L(y,o) = - \frac{1}{N} \sum{n \in N} y{n} \log o_{n} \end{aligned} $

And the code in RNNLM.ipynb is

def calculate_total_loss(self, x, y):
    L = 0
    # For each sentence...
    for i in np.arange(len(y)):
        o, s = self.forward_propagation(x[i])
        # We only care about our prediction of the "correct" words
        correct_word_predictions = o[np.arange(len(y[i])), y[i]]
        # Add to the loss based on how off we were
        L += -1 * np.sum(np.log(correct_word_predictions))
    return L

L += -1 * np.sum(np.log(correct_word_predictions)) didn't take $y_n$ that is the true labels into consideration. I'm not sure I am right, maybe $y_n$ is just 1 with one-hot encoding. Am I right? Thank you!

DanielTakeshi commented 7 years ago

It uses one-hot encoding. The true labels are accounted for with the indexing of o .