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

use torch.gather instead of direct indexing #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

afshinrahimi
Copy link

Instead of this line:

log_liklihoods.append(output[:, target])

have this line:

log_liklihoods.append(torch.gather(output, dim=1, index=target.unsqueeze(-1)))

Why?

Assume our output is 100x4 which means batch size is 100 and we have 4 classes. Target is a (100,) vector of classes, by indexing output[:, target] we will create a 100x100 matrix, instead of gathering the loglikelihoods 100x1 that we desire.

The torch.gather function does this propoerly.

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

Successfully merging this pull request may close these issues.

1 participant