{"action":"created","issue":{"url":"https://api.github.com/repos/pytorch/pytorch/issues/563","repository_url":"https://api.github.com/repos/pytorch/pytorch","labels_url":"https://api.github.com/repos/pytorch/pytorch/issues/563/labels{/name}","comments_url":"https://api.github.com/repos/pytorch/pytorch/issues/563/comments","events_url":"https://api.github.com/repos/pytorch/pytorch/issues/563/events","html_url":"https://github.com/pytorch/pytorch/issues/563","id":202638938,"number":563,"title":"CrossEntropyLoss masking","user":{"login":"alrojo","id":12167999,"avatar_url":"https://avatars2.githubusercontent.com/u/12167999?v=3","gravatar_id":"","url":"https://api.github.com/users/alrojo","html_url":"https://github.com/alrojo","followers_url":"https://api.github.com/users/alrojo/followers","following_url":"https://api.github.com/users/alrojo/following{/other_user}","gists_url":"https://api.github.com/users/alrojo/gists{/gist_id}","starred_url":"https://api.github.com/users/alrojo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alrojo/subscriptions","organizations_url":"https://api.github.com/users/alrojo/orgs","repos_url":"https://api.github.com/users/alrojo/repos","events_url":"https://api.github.com/users/alrojo/events{/privacy}","received_events_url":"https://api.github.com/users/alrojo/received_events","type":"User","site_admin":false},"labels":[{"id":424131849,"url":"https://api.github.com/repos/pytorch/pytorch/labels/enhancement","name":"enhancement","color":"84b6eb","default":true},{"id":466131885,"url":"https://api.github.com/repos/pytorch/pytorch/labels/needs%20discussion","name":"needs discussion","color":"cc317c","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2017-01-23T20:15:19Z","updated_at":"2017-05-31T10:53:25Z","closed_at":null,"body":"The [CrossEntropyLoss](http://pytorch.org/docs/nn.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss) class and function uses inputs (unscaled probabilities), targets and class weights to calculate the loss.\r\nThe reason for using class weights is to help with imbalanced datasets.\r\nHowever, with this setup you are not allowed to handle masking, which is a core issue in time-series (RNN, NLP) training with imbalanced sequence length.\r\n\r\nI propose two alternative approaches that would solve this problem, while still allowing weighting class imbalances.\r\n\r\n**1.** have the weights as a function argument at every execution. Such as [TensorFlow](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/seq2seq/python/ops/loss.py) is doing. Such that the weights is the same size as the target tensor, with a weighting on each of the samples. This would still allow the user to supply increased weight sizes at the indices of their imbalanced classes, while masking would be putting ones at wanted samples and zeros everywhere else.\r\n\r\n**2.** A perhaps more elegant solution would be to have the CrossEntropyLoss exactly the same as tensorflows [cross entropy loss function](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn_ops.py#L1637), which seems to be the same as PyTorch's, but without averaging the loss of every sample. This would allow the user to average how they see fit and produce similar functions to the one in proposal (1)."},"comment":{"url":"https://api.github.com/repos/pytorch/pytorch/issues/comments/305156008","html_url":"https://github.com/pytorch/pytorch/issues/563#issuecomment-305156008","issue_url":"https://api.github.com/repos/pytorch/pytorch/issues/563","id":305156008,"user":{"login":"kvrd18","id":10166968,"avatar_url":"https://avatars0.githubusercontent.com/u/10166968?v=3","gravatar_id":"","url":"https://api.github.com/users/kvrd18","html_url":"https://github.com/kvrd18","followers_url":"https://api.github.com/users/kvrd18/followers","following_url":"https://api.github.com/users/kvrd18/following{/other_user}","gists_url":"https://api.github.com/users/kvrd18/gists{/gist_id}","starred_url":"https://api.github.com/users/kvrd18/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kvrd18/subscriptions","organizations_url":"https://api.github.com/users/kvrd18/orgs","repos_url":"https://api.github.com/users/kvrd18/repos","events_url":"https://api.github.com/users/kvrd18/events{/privacy}","received_events_url":"https://api.github.com/users/kvrd18/received_events","type":"User","site_admin":false},"created_at":"2017-05-31T10:53:25Z","updated_at":"2017-05-31T10:53:25Z","body":"@fmassa Thank you. I'll let you know once I get a working prototype."}}
{"actor":{"display_login":"kvrd18"}}