本文整理匯總了Python中torch.nn.BCEWithLogitsLoss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.BCEWithLogitsLoss方法的具體用法?Python nn.BCEWithLogitsLoss怎麽用?Python nn.BCEWithLogitsLoss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.BCEWithLogitsLoss方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: masked_binary_cross_entropy
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def masked_binary_cross_entropy(logits, target, length):
'''
logits: (batch, max_len, num_class)
target: (batch, max_len, num_class)
'''
if USE_CUDA:
length = Variable(torch.LongTensor(length)).cuda()
else:
length = Variable(torch.LongTensor(length))
bce_criterion = nn.BCEWithLogitsLoss()
loss = 0
for bi in range(logits.size(0)):
for i in range(logits.size(1)):
if i < length[bi]:
loss += bce_criterion(logits[bi][i], target[bi][i])
loss = loss / length.float().sum()
return loss
示例2: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, gan_mode, target_real_label=1.0, target_fake_label=0.0):
""" Initialize the GANLoss class.
Parameters:
gan_mode (str) - - the type of GAN objective. It currently supports vanilla, lsgan, and wgangp.
target_real_label (bool) - - label for a real image
target_fake_label (bool) - - label of a fake image
Note: Do not use sigmoid as the last layer of Discriminator.
LSGAN needs no sigmoid. vanilla GANs will handle it with BCEWithLogitsLoss.
"""
super(GANLoss, self).__init__()
self.register_buffer('real_label', torch.tensor(target_real_label))
self.register_buffer('fake_label', torch.tensor(target_fake_label))
self.gan_mode = gan_mode
if gan_mode == 'lsgan':
self.loss = nn.MSELoss()
elif gan_mode == 'vanilla':
self.loss = nn.BCEWithLogitsLoss()
elif gan_mode in ['wgangp']:
self.loss = None
else:
raise NotImplementedError('gan mode %s not implemented' % gan_mode)
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, gan_type, real_label_val=1.0, fake_label_val=0.0):
super(GANLoss, self).__init__()
self.gan_type = gan_type.lower()
self.real_label_val = real_label_val
self.fake_label_val = fake_label_val
if self.gan_type == 'gan' or self.gan_type == 'ragan':
self.loss = nn.BCEWithLogitsLoss()
elif self.gan_type == 'lsgan':
self.loss = nn.MSELoss()
elif self.gan_type == 'wgan-gp':
def wgan_loss(input, target):
# target is boolean
return -1 * input.mean() if target else input.mean()
self.loss = wgan_loss
else:
raise NotImplementedError('GAN type [{:s}] is not found'.format(self.gan_type))
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, discriminator, d_optimizer, size_average=True,
loss='L2', batch_acum=1, device='cpu'):
super().__init__()
self.discriminator = discriminator
self.d_optimizer = d_optimizer
self.batch_acum = batch_acum
if loss == 'L2':
self.loss = nn.MSELoss(size_average)
self.labels = [1, -1, 0]
elif loss == 'BCE':
self.loss = nn.BCEWithLogitsLoss()
self.labels = [1, 0, 1]
elif loss == 'Hinge':
self.loss = None
else:
raise ValueError('Urecognized loss: {}'.format(loss))
self.device = device
示例5: forward
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def forward(self, obs_variable, actions_variable, target):
"""
Compute the cross entropy loss using the logit, this is more numerical
stable than first apply sigmoid function and then use BCELoss.
As in discriminator, we only want to discriminate the expert from
learner, thus this is a binary classification problem.
Parameters
----------
obs_variable (Variable): state wrapped in Variable
actions_variable (Variable): action wrapped in Variable
target (Variable): 1 or 0, mark the real and fake of the
samples
Returns
-------
loss (Variable):
"""
logits = self.get_logits(obs_variable, actions_variable)
loss_fn = nn.BCEWithLogitsLoss()
loss = loss_fn(logits, target)
return loss
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, gan_type, real_label_val=1.0, fake_label_val=0.0):
super(GANLoss, self).__init__()
self.gan_type = gan_type.lower()
self.real_label_val = real_label_val
self.fake_label_val = fake_label_val
if self.gan_type == 'gan' or self.gan_type == 'ragan':
self.loss = nn.BCEWithLogitsLoss()
elif self.gan_type == 'lsgan':
self.loss = nn.MSELoss()
elif self.gan_type == 'wgan-gp':
def wgan_loss(input, target):
# target is boolean
return -1 * input.mean() if target else input.mean()
self.loss = wgan_loss
else:
raise NotImplementedError('GAN type [{:s}] is not found'.format(self.gan_type))
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, opt):
super(DiscriminatorLoss, self).__init__()
self.gpu_id = opt.gpu_ids[0]
# Adversarial criteria for the predictions
if opt.dis_adv_loss_type == 'gan':
self.crit = nn.BCEWithLogitsLoss()
elif opt.dis_adv_loss_type == 'lsgan':
self.crit = nn.MSELoss()
# Targets for criteria
self.labels_real = []
self.labels_fake = []
# Iterate over discriminators to inialize labels
for size in opt.dis_output_sizes:
shape = (opt.batch_size, 1, size, size)
self.labels_real += [Variable(torch.ones(shape).cuda(self.gpu_id))]
self.labels_fake += [Variable(torch.zeros(shape).cuda(self.gpu_id))]
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, params):
configure_logger(params['output_dir'])
log('Parameters {}'.format(params))
self.params = params
self.binding = load_bindings(params['rom_file_path'])
self.max_word_length = self.binding['max_word_length']
self.sp = spm.SentencePieceProcessor()
self.sp.Load(params['spm_file'])
kg_env = KGA2CEnv(params['rom_file_path'], params['seed'], self.sp,
params['tsv_file'], step_limit=params['reset_steps'],
stuck_steps=params['stuck_steps'], gat=params['gat'])
self.vec_env = VecEnv(params['batch_size'], kg_env, params['openie_path'])
self.template_generator = TemplateActionGenerator(self.binding)
env = FrotzEnv(params['rom_file_path'])
self.vocab_act, self.vocab_act_rev = load_vocab(env)
self.model = KGA2C(params, self.template_generator.templates, self.max_word_length,
self.vocab_act, self.vocab_act_rev, len(self.sp), gat=self.params['gat']).cuda()
self.batch_size = params['batch_size']
if params['preload_weights']:
self.model = torch.load(self.params['preload_weights'])['model']
self.optimizer = optim.Adam(self.model.parameters(), lr=params['lr'])
self.loss_fn1 = nn.BCELoss()
self.loss_fn2 = nn.BCEWithLogitsLoss()
self.loss_fn3 = nn.MSELoss()
示例9: get_gan_criterion
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def get_gan_criterion(mode):
if mode == 'dcgan':
criterion = GANLoss(dis_loss=nn.BCEWithLogitsLoss(),gen_loss=nn.BCEWithLogitsLoss())
elif mode == 'lsgan':
criterion = GANLoss(dis_loss=nn.MSELoss(),gen_loss=nn.MSELoss())
elif mode == 'hinge':
def hinge_dis(pre, margin):
'''margin should not be 0'''
logict = (margin>0).float() + (-1. * (margin<0).float())
return torch.mean(F.relu((margin-pre)*logict))
def hinge_gen(pre, margin):
return -torch.mean(pre)
criterion = GANLoss(real_label=1,fake_label=-1,dis_loss=hinge_dis,gen_loss=hinge_gen)
else:
raise NotImplementedError('{} is not implementation'.format(mode))
return criterion
示例10: compute_generator_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def compute_generator_loss(netD, fake_imgs, real_labels, local_label, transf_matrices, transf_matrices_inv, gpus):
criterion = nn.BCEWithLogitsLoss()
local_label_cond = local_label[:, 0, :] + local_label[:, 1, :] + local_label[:, 2, :] + local_label[:, 3, :]
local_label_cond[local_label_cond < 0] = 0
fake_features = nn.parallel.data_parallel(netD, (fake_imgs, local_label, transf_matrices, transf_matrices_inv), gpus)
# fake pairs
inputs = (fake_features, local_label_cond)
fake_logits = nn.parallel.data_parallel(netD.get_cond_logits, inputs, gpus)
errD_fake = criterion(fake_logits, real_labels)
if netD.get_uncond_logits is not None:
fake_logits = nn.parallel.data_parallel(netD.get_uncond_logits, (fake_features), gpus)
uncond_errD_fake = criterion(fake_logits, real_labels)
errD_fake += uncond_errD_fake
return errD_fake
#############################
示例11: compute_generator_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def compute_generator_loss(netD, fake_imgs, real_labels, local_label, transf_matrices, transf_matrices_inv, gpus):
criterion = nn.BCEWithLogitsLoss()
local_label = local_label.detach()
local_label_cond = local_label[:, 0, :] + local_label[:, 1, :] + local_label[:, 2, :]
fake_features = nn.parallel.data_parallel(netD, (fake_imgs, local_label, transf_matrices, transf_matrices_inv), gpus)
# fake pairs
inputs = (fake_features, local_label_cond)
fake_logits = nn.parallel.data_parallel(netD.get_cond_logits, inputs, gpus)
errD_fake = criterion(fake_logits, real_labels)
if netD.get_uncond_logits is not None:
fake_logits = nn.parallel.data_parallel(netD.get_uncond_logits, (fake_features), gpus)
# fake_logits = torch.clamp(fake_logits, 1e-8, 1-1e-8)
uncond_errD_fake = criterion(fake_logits, real_labels)
errD_fake += uncond_errD_fake
return errD_fake
#############################
示例12: init_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def init_loss(criterion_name):
if criterion_name=='bce':
loss = nn.BCEWithLogitsLoss()
elif criterion_name=='cce':
loss = nn.CrossEntropyLoss()
elif criterion_name.startswith('arc_margin'):
loss = nn.CrossEntropyLoss()
elif 'cce' in criterion_name:
loss = nn.CrossEntropyLoss()
elif criterion_name == 'focal_loss':
loss = FocalLoss()
else:
raise Exception('This loss function is not implemented yet.')
return loss
示例13: forward
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def forward(self, output, target):
background = target == 0
foreground = target == 1
loss = nn.BCEWithLogitsLoss(size_average=self.size_average)
background_loss = loss(output[background], target[background])
foreground_loss = loss(output[foreground], target[foreground])
return background_loss + foreground_loss
示例14: segmentation_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def segmentation_loss(output, target, weight_bce=1.0, weight_dice=1.0):
bce = nn.BCEWithLogitsLoss()
dice = DiceLoss()
return weight_bce*bce(output, target) + weight_dice*dice(output, target)
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import BCEWithLogitsLoss [as 別名]
def __init__(self, q=0.5,
noisy_weight=0.5,
curated_weight=0.5):
super().__init__()
lq = LqLoss(q=q)
bce = nn.BCEWithLogitsLoss()
self.loss = NoisyCuratedLoss(lq, bce,
noisy_weight,
curated_weight)