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

ComplementCrossEntropyLoss(torch.nn.Module) #3

Open
LongChenCV opened this issue Mar 26, 2019 · 0 comments
Open

ComplementCrossEntropyLoss(torch.nn.Module) #3

LongChenCV opened this issue Mar 26, 2019 · 0 comments

Comments

@LongChenCV
Copy link

LongChenCV commented Mar 26, 2019

Thanks for sharing your code, my question are:

  1. I'm confused of the function of ComplementCrossEntropyLoss? what's the difference between ComplementCrossEntropyLoss and nn.CrossEntropyLoss()
  2. In ComplementCrossEntropyLoss.py, when call for torch.nn.functional.nll_loss, why input is torch.log(1. - torch.nn.functional.softmax(input) + 1e-4)? what does 1- means?

class ComplementCrossEntropyLoss(torch.nn.Module):

Note: This is the cross entropy of the sum of all probabilities of other indices, except for the

This is used in Bayesian GAN semi-supervised learning

def init(self, except_index=None, weight=None, ignore_index=-100, size_average=True, reduce=True):
super(ComplementCrossEntropyLoss, self).init()
self.except_index = except_index
self.weight = weight
self.ignore_index = ignore_index
self.size_average = size_average
self.reduce = reduce

def forward(self, input, target=None):
# Use target if not None, else use self.except_index
if target is not None:
assert_no_grad(target)
else:
assert self.except_index is not None
target = torch.autograd.Variable(torch.LongTensor(input.data.shape[0]).fill
(self.except_index).cuda())
result = torch.nn.functional.nll_loss(
torch.log(1. - torch.nn.functional.softmax(input) + 1e-4),
target, weight=self.weight,
size_average=self.size_average,
ignore_index=self.ignore_index)
return result

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

1 participant