我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用torch.nn.BCELoss()。
def PseudoLabelingLoss(loss_fn=BCELoss()): def call(prediction, y, i, epoch): i = i.data i_supervised = (i <= 0).nonzero().squeeze() i_unsupervised = (i > 0).nonzero().squeeze() supervised_prediction = prediction[i_supervised] supervised_y = y[i_supervised] unsupervised_prediction = prediction[i_unsupervised] unsupervised_y = y[i_unsupervised] supervised_loss = loss_fn(supervised_prediction, supervised_y) unsupervised_loss = loss_fn(unsupervised_prediction, unsupervised_y) a = max((epoch-12), 0) / 35 loss = (supervised_loss + a * unsupervised_loss) / (1 + a) return loss return call
def __init__(self, ce_weights=None): super(CrowdCounter, self).__init__() self.CCN = CMTL() if ce_weights is not None: ce_weights = torch.Tensor(ce_weights) ce_weights = ce_weights.cuda() self.loss_mse_fn = nn.MSELoss() self.loss_bce_fn = nn.BCELoss(weight=ce_weights)
def test_bce_with_logits_gives_same_result_as_sigmoid_and_bce_loss(self): sigmoid = nn.Sigmoid() target = Variable(torch.rand(64, 4)) output = Variable(torch.rand(64, 4) - 0.5) self.assertEqual(nn.BCEWithLogitsLoss()(output, target), nn.BCELoss()(sigmoid(output), target)) weight = torch.rand(4) self.assertEqual(nn.BCEWithLogitsLoss(weight)(output, target), nn.BCELoss(weight)(sigmoid(output), target)) target = Variable(torch.FloatTensor(4, 1).fill_(0)) output = Variable(torch.FloatTensor(4, 1).fill_(-100)) self.assertEqual(nn.BCEWithLogitsLoss()(output, target), nn.BCELoss()(sigmoid(output), target)) weight = torch.FloatTensor(1).uniform_() self.assertEqual(nn.BCEWithLogitsLoss(weight)(output, target), nn.BCELoss(weight)(sigmoid(output), target))
def test_bce_loss_broadcasts_weights(self): sigmoid = nn.Sigmoid() target = Variable(torch.rand(16, 4)) output = Variable(torch.rand(16, 4) - 0.5) weight = torch.rand(4) out1 = nn.BCELoss(weight)(sigmoid(output), target) weight = weight.expand(16, 4).contiguous() out2 = nn.BCELoss(weight)(sigmoid(output), target) self.assertEqual(out1, out2) weight = torch.rand(16, 1) out1 = nn.BCELoss(weight)(sigmoid(output), target) weight = weight.expand(16, 4).contiguous() out2 = nn.BCELoss(weight)(sigmoid(output), target) self.assertEqual(out1, out2)
def train(): optimizer = optim.Adam(generator.parameters(), lr=learning_rate) loss = nn.BCELoss() num_iterations = 100 input = Variable(torch.FloatTensor(batch_size, sampler0.num_memory_channels, sampler0.num_history, sampler0.memory_size)) for iteration in range(num_iterations): optimizer.zero_grad() music = torch.FloatTensor(sampler1.sample()) input.data.resize_(music.size()).copy_(music) output_gen = generator(input) gen_loss = (output_gen - input).abs().mean() #gen_loss = loss(output_gen, input) print("[{0}]: {1}".format(iteration, gen_loss.data[0])) gen_loss.backward() optimizer.step() test()
def __init__(self, mpii, batch_size): super(HeatmapModel, self).__init__() self.heatmap_size = mpii.heatmap_size ndf = 32 self.conv0 = nn.Conv2d(mpii.image_num_components, ndf, 11, stride=2) self.conv1 = nn.Conv2d(ndf, ndf * 2, 9, stride=2) self.conv2 = nn.Conv2d(ndf * 2, ndf * 4, 7, stride=2) self.conv3 = nn.Conv2d(ndf * 4, ndf * 8, 5, stride=2) self.fc0_size = 256 * 3 * 3 self.fc0 = nn.Linear(self.fc0_size, mpii.heatmap_size * mpii.heatmap_size) self.relu = nn.ReLU(inplace=True) self.tanh = nn.Tanh() self.sigmoid = nn.Sigmoid() self.loss = nn.BCELoss().cuda() self.images = Variable(torch.FloatTensor(batch_size, mpii.image_num_components, mpii.image_size, mpii.image_size)).cuda() self.labels = Variable(torch.FloatTensor(batch_size, self.heatmap_size, self.heatmap_size)).cuda()
def loss(self,x): self.forward(x) criterion = nn.BCELoss() x_recons = self.sigmoid(self.cs[-1]) Lx = criterion(x_recons,x) * self.A * self.B Lz = 0 kl_terms = [0] * T for t in xrange(self.T): mu_2 = self.mus[t] * self.mus[t] sigma_2 = self.sigmas[t] * self.sigmas[t] logsigma = self.logsigmas[t] # Lz += (0.5 * (mu_2 + sigma_2 - 2 * logsigma)) # 11 kl_terms[t] = 0.5 * torch.sum(mu_2+sigma_2-2 * logsigma,1) - self.T * 0.5 Lz += kl_terms[t] # Lz -= self.T / 2 Lz = torch.mean(Lz) #################################################### loss = Lz + Lx # 12 return loss # correct
def __init__(self, real_label = 1.0, fake_label = 0.0, use_lsgan = True): super(GANLoss, self).__init__() self.real_label = real_label self.fake_label = fake_label self.real_target = None self.fake_target = None if use_lsgan: self.loss = nn.MSELoss() else: self.loss = nn.BCELoss()
def __init__(self, use_lsgan=True, target_real_label=1.0, target_fake_label=0.0, tensor=torch.FloatTensor): super(GANLoss, self).__init__() self.real_label = target_real_label self.fake_label = target_fake_label self.real_label_var = None self.fake_label_var = None self.Tensor = tensor if use_lsgan: self.loss = nn.MSELoss() else: self.loss = nn.BCELoss()
def __init__(self, use_l1=True, target_real_label=1.0, target_fake_label=0.0, tensor=torch.FloatTensor): super(GANLoss, self).__init__() self.real_label = target_real_label self.fake_label = target_fake_label self.real_label_var = None self.fake_label_var = None self.Tensor = tensor if use_l1: self.loss = nn.L1Loss() else: self.loss = nn.BCELoss()
def __init__(self, use_lsgan=False, target_real_label=1.0, target_fake_label=0.0, tensor=torch.FloatTensor): super(GANLoss, self).__init__() self.real_label = target_real_label self.fake_label = target_fake_label self.real_label_var = None self.fake_label_var = None self.Tensor = tensor if use_lsgan: self.loss = nn.MSELoss() else: self.loss = nn.BCELoss()
def __init__(self, weight=None, size_average=True): super(BCELoss2d, self).__init__() self.bce_loss = nn.BCELoss(weight, size_average)
def bce_loss(loss_weight=None, size_ave=True): return nn.BCELoss(weight=loss_weight,size_average=size_ave)
def __init__(self, hps: HyperParams): self.hps = hps self.net = getattr(models, hps.net)(hps) self.bce_loss = nn.BCELoss() self.mse_loss = nn.MSELoss() self.optimizer = None # type: optim.Optimizer self.tb_logger = None # type: tensorboard_logger.Logger self.logdir = None # type: Path self.on_gpu = torch.cuda.is_available() if self.on_gpu: self.net.cuda()
def __init__(self, sign=1): super(BCELoss, self).__init__() self.sign = sign self.main = nn.BCELoss()
def cuda(self, device_id=None): super(BCELoss, self).cuda(device_id) self.main.cuda()
def __init__(self, image_size, n_z, n_chan, hiddens, ngpu, loss=['KLD', 'BCE']): """ VAE object. This class is a wrapper of a VAE as explained in the paper: AUTO-ENCODING VARIATIONAL BAYES by Kingma et.al. Instance of this class initializes the parameters required for the Encoder and Decoder. Arguments: image_size = Height / width of the real images n_z = Dimensionality of the latent space n_chan = Number of channels of the real images hiddens = Number of nodes in the hidden layers of the encoder and decoder Format: hiddens = {'enc': n_enc_hidden, 'dec': n_dec_hidden } ngpu = Number of gpus to be allocated, if to be run on gpu loss = The loss function to be used. For multiple losses, add them in a list """ super(VAE, self).__init__() self.vae_net = vae(image_size, n_z, n_chan, hiddens['enc'], hiddens['dec'], ngpu) self.ngpu = ngpu self.n_z = n_z self.image_size = image_size self.n_chan = n_chan if 'BCE' in loss: self.recons_loss = nn.BCELoss(size_average=False) elif 'MSE' in loss: self.recons_loss = nn.MSELoss() self.KLD_loss = u.KLD
def __init__(self, image_size, n_z, n_chan, hiddens, depths, ngpu, loss='BCE'): """ MLPGAN object. This class is a wrapper of a generalized MLPGAN. Instance of this class initializes the Generator and the Discriminator. Arguments: image_size = Height / width of the real images n_z = Dimensionality of the latent space n_chan = Number of channels of the real images hiddens = Number of nodes in the hidden layers of the generator and discriminator Format: hiddens = {'gen': n_gen_hidden, 'dis': n_dis_hidden } depths = Number of fully connected layers in the generator and discriminator Format: depths = {'gen': n_gen_depth, 'dis': n_dis_depth } ngpu = Number of gpus to allocated, if to be run on gpu loss (opt) = The loss function to be used. Default is BCE loss """ super(MLPGAN, self).__init__() self.Gen_net = Generator(image_size, n_z, n_chan, hiddens['gen'], depths['gen'], ngpu) self.Dis_net = Discriminator(image_size, n_chan, hiddens['dis'], depths['dis'], ngpu) self.ngpu = ngpu self.n_z = n_z self.image_size = image_size self.n_chan = n_chan if loss == 'BCE': self.loss = nn.BCELoss() elif loss == 'MSE': self.loss = nn.MSELoss()
def __init__(self, nic, noc, ngf, ndf, beta=0.5, lamb=100, lr=1e-3, cuda=True, crayon=False): """ Args: nic: Number of input channel noc: Number of output channels ngf: Number of generator filters ndf: Number of discriminator filters lamb: Weight on L1 term in objective """ self.cuda = cuda self.start_epoch = 0 self.crayon = crayon if crayon: self.cc = CrayonClient(hostname="localhost", port=8889) try: self.logger = self.cc.create_experiment('pix2pix') except: self.cc.remove_experiment('pix2pix') self.logger = self.cc.create_experiment('pix2pix') self.gen = self.cudafy(Generator(nic, noc, ngf)) self.dis = self.cudafy(Discriminator(nic, noc, ndf)) # Optimizers for generators self.gen_optim = self.cudafy(optim.Adam( self.gen.parameters(), lr=lr, betas=(beta, 0.999))) # Optimizers for discriminators self.dis_optim = self.cudafy(optim.Adam( self.dis.parameters(), lr=lr, betas=(beta, 0.999))) # Loss functions self.criterion_bce = nn.BCELoss() self.criterion_mse = nn.MSELoss() self.criterion_l1 = nn.L1Loss() self.lamb = lamb
def train(): optimizer = optim.Adam(generator.parameters(), lr=learning_rate) loss = nn.BCELoss().cuda() num_iterations = int(sampler.num_memories / batch_size) num_epochs = 100 for epoch in range(num_epochs): for iteration in range(num_iterations): memories, futures_voc = sampler.sample() generator.zero_grad() voc = generator(memories) gen_loss_voc = loss(voc, futures_voc) gen_loss = gen_loss_voc gen_loss.backward() optimizer.step() sys.stdout.write("[{0}/{1}]: {2}\r".format(epoch, iteration, gen_loss_voc.data[0])) sys.stdout.flush() sys.stdout.write("\n") torch.save(generator.state_dict(), "model.pth") test()
def __init__(self, joint_index, mpii, batch_size): super(JointModel, self).__init__() self.joint_index = joint_index self.heatmap_size = mpii.heatmap_size self.max_people = mpii.max_people self.max_joints = mpii.max_joints ndf = 64 self.conv0 = nn.Conv2d(mpii.image_num_components, ndf, 11, stride=2) self.conv1 = nn.Conv2d(ndf, ndf * 2, 9, stride=2) self.conv2 = nn.Conv2d(ndf * 2, ndf * 4, 7, stride=2) self.conv3 = nn.Conv2d(ndf * 4, ndf * 8, 5, stride=2) self.fc0_size = 512 * 3 * 3 self.fc0 = nn.Linear(self.fc0_size, self.heatmap_size * self.heatmap_size) self.relu = nn.ReLU(inplace=True) self.tanh = nn.Tanh() self.softmax = nn.Softmax() self.loss = nn.BCELoss().cuda() self.images = Variable(torch.FloatTensor(batch_size, mpii.image_num_components, mpii.image_size, mpii.image_size)).cuda() self.labels = Variable(torch.FloatTensor(batch_size, self.heatmap_size, self.heatmap_size)).cuda() self.scale_heatmap = nn.UpsamplingBilinear2d(scale_factor=4)
def __init__(self, recon_loss='l2'): """VAE Loss = Reconsturction Loss + KL Divergence""" super(VAELoss, self).__init__() if recon_loss == 'bce': self.recon_loss = nn.BCELoss(size_average=False) elif recon_loss == 'l2': self.recon_loss = nn.MSELoss(size_average=False) elif recon_loss == 'l1': self.recon_loss = nn.L1Loss(size_average=False)
def __init__(self, weight): super(BCE_Loss, self).__init__() self.loss = nn.BCELoss(weight=weight)
def __init__(self, use_lsgan=True, target_real_label=1.0, target_fake_label=0.0, tensor=torch.FloatTensor, gpu_ids=[]): super(GANLoss, self).__init__() self.real_label = target_real_label self.fake_label = target_fake_label self.real_label_var = None self.fake_label_var = None self.Tensor = tensor if use_lsgan: self.loss = nn.MSELoss() else: self.loss = nn.BCELoss() self.gpu_ids = gpu_ids
def batchBCELoss(self, inp, target): """ Returns Binary Cross Entropy Loss for discriminator. Inputs: inp, target - inp: batch_size x seq_len - target: batch_size (binary 1/0) """ loss_fn = nn.BCELoss() h = self.init_hidden(inp.size()[0]) out = self.forward(inp, h) return loss_fn(out, target)
def get_loss_function(self): def loss_function(recon_x, x, mu, logvar): bce = nn.BCELoss(recon_x, x) kld_element = mu.pow(2).add_(logvar.exp()) \ .mul_(-1).add_(1).add_(logvar) kld = torch.sum(kld_element).mul_(-0.5) return bce + self.beta * kld
def setup(self): ?, ?1 = self.opt.lr, self.opt.beta1 self.? = self.opt.lamb self.criterion = nn.BCELoss() self.criterion_l1 = nn.L1Loss() self.g_optimizer = torch.optim.Adam( self.generator.parameters(), lr=?, betas=(?1, 0.999)) self.d_optimizer = torch.optim.Adam( self.discriminator.parameters(), lr=?, betas=(?1, 0.999)) if self.opt.cuda: self.generator = self.generator.cuda() self.discriminator = self.discriminator.cuda()
def __init__(self): super(AgentParams, self).__init__() if self.agent_type == "sl": if self.circuit_type == "ntm": self.criteria = nn.BCELoss() self.optim = optim.RMSprop self.steps = 100000 # max #iterations self.batch_size = 16 self.early_stop = None # max #steps per episode self.clip_grad = 50. self.lr = 1e-4 self.optim_eps = 1e-10 # NOTE: we use this setting to be equivalent w/ the default settings in tensorflow self.optim_alpha = 0.9 # NOTE: only for rmsprop, alpha is the decay in tensorflow, whose default is 0.9 self.eval_freq = 500 self.eval_steps = 50 self.prog_freq = self.eval_freq self.test_nepisodes = 5 elif self.circuit_type == "dnc": self.criteria = nn.BCELoss() self.optim = optim.RMSprop self.steps = 100000 # max #iterations self.batch_size = 16 self.early_stop = None # max #steps per episode self.clip_grad = 50. self.lr = 1e-4 self.optim_eps = 1e-10 # NOTE: we use this setting to be equivalent w/ the default settings in tensorflow self.optim_alpha = 0.9 # NOTE: only for rmsprop, alpha is the decay in tensorflow, whose default is 0.9 self.eval_freq = 500 self.eval_steps = 50 self.prog_freq = self.eval_freq self.test_nepisodes = 5 elif self.agent_type == "empty": self.criteria = nn.BCELoss() self.optim = optim.RMSprop self.steps = 100000 # max #iterations self.batch_size = 16 self.early_stop = None # max #steps per episode self.clip_grad = 50. self.lr = 1e-4 self.optim_eps = 1e-10 # NOTE: we use this setting to be equivalent w/ the default settings in tensorflow self.optim_alpha = 0.9 # NOTE: only for rmsprop, alpha is the decay in tensorflow, whose default is 0.9 self.eval_freq = 500 self.eval_steps = 50 self.prog_freq = self.eval_freq self.test_nepisodes = 5 self.env_params = EnvParams() self.circuit_params = CircuitParams()
def main(params): if not torch.cuda.is_available(): raise ValueError("Cuda is not available") print ('Set random seed to: {}'.format(params['seed'])) random.seed(params['seed']) torch.manual_seed(params['seed']) torch.cuda.manual_seed(params['seed']) print ('Get voc datasets.. ', end='', flush=True) voc_datasets = GetVocDatasets(params) print ('Done') voc_loaders = GetVocLoaders(params, voc_datasets) voc_evaluators = GetVocEvaluators(params) num_classes = voc_datasets['train'].num_classes model = GetPretrainedModel(params, num_classes) model = model.cuda() criterion = nn.BCELoss() best_model, best_epoch, best_summary, meanAP_historys =\ Train(params, model, criterion, OptimizerScheduler, voc_loaders, voc_evaluators) test_evaluation_summarys = Test(params, best_model, voc_loaders, voc_evaluators, 'test') if not os.path.isdir(params['save_dir']): print ("Directory {} doesn't exist. Create one.".format( params['save_dir'])) os.makedirs(params['save_dir']) weight_save_path = os.path.join(params['save_dir'], 'model.pt') torch.save(best_model, weight_save_path) print ("Best model is saved: {}".format(weight_save_path)) summary_save_path = os.path.join(params['save_dir'], 'summary.json') summary = { 'best_epoch': best_epoch, 'best_summary': best_summary, 'meanAP_historys': meanAP_historys, 'test_summary': test_evaluation_summarys, } json.dump(summary, open(summary_save_path, 'w')) print ("Summary json file is saved: {}".format(summary_save_path))
def train_discriminator(discriminator, dis_opt, real_data_samples, generator, oracle, d_steps, epochs): """ Training the discriminator on real_data_samples (positive) and generated samples from generator (negative). Samples are drawn d_steps times, and the discriminator is trained for epochs epochs. """ # generating a small validation set before training (using oracle and generator) pos_val = oracle.sample(100) neg_val = generator.sample(100) val_inp, val_target = helpers.prepare_discriminator_data(pos_val, neg_val, gpu=CUDA) for d_step in range(d_steps): s = helpers.batchwise_sample(generator, POS_NEG_SAMPLES, BATCH_SIZE) dis_inp, dis_target = helpers.prepare_discriminator_data(real_data_samples, s, gpu=CUDA) for epoch in range(epochs): print('d-step %d epoch %d : ' % (d_step + 1, epoch + 1), end='') sys.stdout.flush() total_loss = 0 total_acc = 0 for i in range(0, 2 * POS_NEG_SAMPLES, BATCH_SIZE): inp, target = dis_inp[i:i + BATCH_SIZE], dis_target[i:i + BATCH_SIZE] dis_opt.zero_grad() out = discriminator.batchClassify(inp) loss_fn = nn.BCELoss() loss = loss_fn(out, target) loss.backward() dis_opt.step() total_loss += loss.data[0] total_acc += torch.sum((out>0.5)==(target>0.5)).data[0] if (i / BATCH_SIZE) % ceil(ceil(2 * POS_NEG_SAMPLES / float( BATCH_SIZE)) / 10.) == 0: # roughly every 10% of an epoch print('.', end='') sys.stdout.flush() total_loss /= ceil(2 * POS_NEG_SAMPLES / float(BATCH_SIZE)) total_acc /= float(2 * POS_NEG_SAMPLES) val_pred = discriminator.batchClassify(val_inp) print(' average_loss = %.4f, train_acc = %.4f, val_acc = %.4f' % ( total_loss, total_acc, torch.sum((val_pred>0.5)==(val_target>0.5)).data[0]/200.)) # MAIN