Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is averaging of the training loss in seq2seq correct? #18

Closed
smolendawid opened this issue Feb 7, 2018 · 1 comment
Closed

Is averaging of the training loss in seq2seq correct? #18

smolendawid opened this issue Feb 7, 2018 · 1 comment

Comments

@smolendawid
Copy link

I don't understand the way the training loss is averaged.

The losses are summed for each minibatch, because of the argument size_average=False in cross_entropy function. Then, there is a line loss_val = loss_val / batch_size that could average over all the batches, except that in one batch, there are many letters to decode, so the loss is calculated over more than batch_size letters. The correct number would be y.shape[0] (all the predictions from all the batches are concatenated to one-dimensional vector).
According to that, the line n. 66 in seq2seq.py should be

loss_val = loss_val / y.shape[0]

Am I right, or I'm missing something?

@awni
Copy link
Owner

awni commented Feb 13, 2018

You're correct, right now the total loss is divided by the batch size. The reasoning behind this is that if we instead divide by the length of the examples we would be down weighting the gradient of longer utterances which isn't quite right semantically since every character in the output counts essentially the same amount toward the overall error rate.

Your proposal though would likely work fine and to be honest I haven't done a careful comparison of which is a better approach. There are likely trade-offs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants