Python torch.nn 模块,L1Loss() 实例源码

我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用torch.nn.L1Loss()

项目:DeblurGAN    作者:KupynOrest    | 项目源码 | 文件源码
def init_loss(opt, tensor):
    disc_loss = None
    content_loss = None

    if opt.model == 'content_gan':
        content_loss = PerceptualLoss()
        content_loss.initialize(nn.MSELoss())
    elif opt.model == 'pix2pix':
        content_loss = ContentLoss()
        content_loss.initialize(nn.L1Loss())
    else:
        raise ValueError("Model [%s] not recognized." % opt.model)

    if opt.gan_type == 'wgan-gp':
        disc_loss = DiscLossWGANGP() 
    elif opt.gan_type == 'lsgan':
        disc_loss = DiscLossLS()
    elif opt.gan_type == 'gan':
        disc_loss = DiscLoss()
    else:
        raise ValueError("GAN [%s] not recognized." % opt.gan_type)
    disc_loss.initialize(opt, tensor)
    return disc_loss, content_loss
项目:PytorchDL    作者:FredHuangBia    | 项目源码 | 文件源码
def createCriterion(opt, model):
    "Criterion is still a legacy of pytorch."
    # criterion = nn.MultiCriterion()
    # if opt.absLoss != 0:
    #   criterion.add(nn.AbsCriterion(), weight=opt.absLoss)
    # if opt.mseLoss != 0:
    #   criterion.add(nn.MSECriterion(), weight=opt.absLoss)
    # if opt.gdlLoss != 0:
    #   criterion.add(nn.GDLCriterion(), weight=opt.absLoss)
    # if opt.customLoss != 0:
    #   criterion.add(customCriterion(), weight=opt.customLoss)

    if opt.L1Loss != 0:
        criterion = nn.L1Loss()
    elif opt.mseLoss != 0:
        criterion = nn.MSELoss()
    elif opt.gdlLoss != 0:
        criterion = nn.GDLLoss()

    return criterion
项目:NeuralFDR    作者:fxia22    | 项目源码 | 文件源码
def train_network_to_target_p(network, optimizer, x, target_p, num_it = 1000, dim = 1, cuda = False):
    target = Variable(torch.from_numpy(target_p.astype(np.float32)))
    l1loss = nn.L1Loss()
    batch_size = len(x)
    n_samples = len(x)
    loss_hist = []
    choice = range(n_samples)
    x_input = Variable(torch.from_numpy(x[choice].astype(np.float32).reshape(batch_size,dim)))


    if cuda:
        x_input = x_input.cuda()
        target = target.cuda()

    for iteration in range(num_it):
        if iteration % 100 == 0:
            print iteration

        optimizer.zero_grad()
        output = network.forward(x_input) 

        loss = l1loss(output, target)
        loss.backward()

        optimizer.step()
        loss_hist.append(loss.data[0])

    return loss_hist
项目:DeblurGAN    作者:KupynOrest    | 项目源码 | 文件源码
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()
项目:pytorch-skipthoughts    作者:kaniblu    | 项目源码 | 文件源码
def __init__(self, model, data_generator, epochs, loss):
        self.epochs = epochs
        self.model = model
        self.data_generator = data_generator
        self.loss = loss

        if loss == "smoothl1":
            self.loss_fn = F.smooth_l1_loss
        elif loss == "l1":
            self.loss_fn = nn.L1Loss()
        elif loss == "l2":
            self.loss_fn = nn.MSELoss()
        else:
            raise ValueError("Unrecognized loss type: {}".format(loss))
项目:SimGAN_pytorch    作者:AlexHex7    | 项目源码 | 文件源码
def build_network(self):
        print('=' * 50)
        print('Building network...')
        self.R = Refiner(4, cfg.img_channels, nb_features=64)
        self.D = Discriminator(input_features=cfg.img_channels)

        if cfg.cuda_use:
            self.R.cuda(cfg.cuda_num)
            self.D.cuda(cfg.cuda_num)

        self.opt_R = torch.optim.Adam(self.R.parameters(), lr=cfg.r_lr)
        self.opt_D = torch.optim.SGD(self.D.parameters(), lr=cfg.d_lr)
        self.self_regularization_loss = nn.L1Loss(size_average=False)
        self.local_adversarial_loss = nn.CrossEntropyLoss(size_average=True)
        self.delta = cfg.delta
项目:covfefe    作者:deepnn    | 项目源码 | 文件源码
def l1_loss(size_ave=True):
    return nn.L1Loss(size_average=size_ave)
项目:rarepepes    作者:kendricktan    | 项目源码 | 文件源码
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
项目:generative_models    作者:j-min    | 项目源码 | 文件源码
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)
项目:pix2pix    作者:leVirve    | 项目源码 | 文件源码
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()
项目:mlp_stock    作者:melissa135    | 项目源码 | 文件源码
def train(trainloader,testloader):

    max_epochs = 100
    moving_average = 30

    mlp = MLP()
    print mlp

    criterion = nn.L1Loss()
    optimizer = optim.Adam(mlp.parameters(),lr=0.001)

    mlp_list = []
    crt_list = []

    for epoch in range(0, max_epochs):

        current_loss = 0
        for i,data in enumerate(trainloader,0):

            input,target = data
            input,target = Variable(input),Variable(target)

            mlp.zero_grad()
            output = mlp(input.float())
            loss = criterion(output, target.float())

            loss.backward()
            optimizer.step()

            loss = loss.data[0]
            current_loss += loss

        #print ('[ %d ] loss : %.3f' % (epoch+1,current_loss))

        train_c = incorrectness(mlp,trainloader)
        test_c = incorrectness(mlp,testloader)
        print ('[ %d ] incorrectness: %.4f %.4f' % (epoch+1,train_c,test_c))

    mlp_list.append(mlp)
    crt_list.append(train_c+test_c)

    if epoch >= moving_average:
        if early_stop(crt_list,moving_average):
        print 'Early stopping.'
        index = len(mlp_list) - moving_average/2
        return mlp_list[index]

        current_loss = 0

    return mlp