本文整理匯總了Python中torch.nn.PoissonNLLLoss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.PoissonNLLLoss方法的具體用法?Python nn.PoissonNLLLoss怎麽用?Python nn.PoissonNLLLoss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.PoissonNLLLoss方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: loglikelihood
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import PoissonNLLLoss [as 別名]
def loglikelihood(self, reduction):
"""
Return the log-likelihood
"""
if self._distr == 'poisson':
if reduction == 'none':
return self.poisson_cross_entropy
return nn.PoissonNLLLoss(reduction=reduction)
elif self._distr == 'bernoulli':
return nn.BCELoss(reduction=reduction)
else:
raise ValueError('{} is not a valid distribution'.format(self._distr))
示例2: _get_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import PoissonNLLLoss [as 別名]
def _get_loss(self, loss_spec):
if not isinstance(self.loss_spec, str):
return self.loss_spec
elif loss_spec == 'mse':
return nn.MSELoss(reduction='mean')
elif loss_spec == 'sse':
return nn.MSELoss(reduction='sum')
elif loss_spec == 'crossentropy':
# Cross entropy loss is used for multiclass categorization and needs inputs in shape
# ((# minibatch_size, C), targets) where C is a 1-d vector of probabilities for each potential category
# and where target is a 1d vector of type long specifying the index to the target category. This
# formatting is different from most other loss functions available to autodiff compositions,
# and therefore requires a wrapper function to properly package inputs.
cross_entropy_loss = nn.CrossEntropyLoss()
return lambda x, y: cross_entropy_loss(
x.unsqueeze(0),
y.type(torch.LongTensor)
)
elif loss_spec == 'l1':
return nn.L1Loss(reduction='sum')
elif loss_spec == 'nll':
return nn.NLLLoss(reduction='sum')
elif loss_spec == 'poissonnll':
return nn.PoissonNLLLoss(reduction='sum')
elif loss_spec == 'kldiv':
return nn.KLDivLoss(reduction='sum')
else:
raise AutodiffCompositionError("Loss type {} not recognized. Loss argument must be a string or function. "
"Currently, the recognized loss types are Mean Squared Error, Cross Entropy,"
" L1 loss, Negative Log Likelihood loss, Poisson Negative Log Likelihood, "
"and KL Divergence. These are specified as 'mse', 'crossentropy', 'l1', "
"'nll', 'poissonnll', and 'kldiv' respectively.".format(loss_spec))
# performs learning/training on all input-target pairs it recieves for given number of epochs