Python torch.nn.LogSoftmax() Examples

The following are 30 code examples of torch.nn.LogSoftmax(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch.nn , or try the search function .
Example #1
Source File: neural_networks.py    From pase with MIT License 7 votes vote down vote up
def act_fun(act_type):

 if act_type=="relu":
    return nn.ReLU()
            
 if act_type=="tanh":
    return nn.Tanh()
            
 if act_type=="sigmoid":
    return nn.Sigmoid()
           
 if act_type=="leaky_relu":
    return nn.LeakyReLU(0.2)
            
 if act_type=="elu":
    return nn.ELU()
                     
 if act_type=="softmax":
    return nn.LogSoftmax(dim=1)
        
 if act_type=="linear":
     return nn.LeakyReLU(1) # initializzed like this, but not used in forward! 
Example #2
Source File: neural_networks.py    From pase with MIT License 7 votes vote down vote up
def act_fun(act_type):

 if act_type=="relu":
    return nn.ReLU()
            
 if act_type=="tanh":
    return nn.Tanh()
            
 if act_type=="sigmoid":
    return nn.Sigmoid()
           
 if act_type=="leaky_relu":
    return nn.LeakyReLU(0.2)
            
 if act_type=="elu":
    return nn.ELU()
                     
 if act_type=="softmax":
    return nn.LogSoftmax(dim=1)
        
 if act_type=="linear":
     return nn.LeakyReLU(1) # initializzed like this, but not used in forward! 
Example #3
Source File: neural_networks.py    From pase with MIT License 7 votes vote down vote up
def act_fun(act_type):

 if act_type=="relu":
    return nn.ReLU()
            
 if act_type=="tanh":
    return nn.Tanh()
            
 if act_type=="sigmoid":
    return nn.Sigmoid()
           
 if act_type=="leaky_relu":
    return nn.LeakyReLU(0.2)
            
 if act_type=="elu":
    return nn.ELU()
                     
 if act_type=="softmax":
    return nn.LogSoftmax(dim=1)
        
 if act_type=="linear":
     return nn.LeakyReLU(1) # initializzed like this, but not used in forward! 
Example #4
Source File: pspnet.py    From Single-Human-Parsing-LIP with MIT License 7 votes vote down vote up
def __init__(self, n_classes=20, sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024, backend='resnet34',
                 pretrained=True):
        super().__init__()
        self.feats = getattr(extractors, backend)(pretrained)
        self.psp = PSPModule(psp_size, 1024, sizes)
        self.drop_1 = nn.Dropout2d(p=0.3)

        self.up_1 = PSPUpsample(1024, 256)
        self.up_2 = PSPUpsample(256, 64)
        self.up_3 = PSPUpsample(64, 64)

        self.drop_2 = nn.Dropout2d(p=0.15)
        self.final = nn.Sequential(
            nn.Conv2d(64, n_classes, kernel_size=1),
            nn.LogSoftmax(dim=1)
        )

        self.classifier = nn.Sequential(
            nn.Linear(deep_features_size, 256),
            nn.ReLU(),
            nn.Linear(256, n_classes)
        ) 
Example #5
Source File: mfcc_baseline.py    From pase with MIT License 6 votes vote down vote up
def __init__(self, num_inputs=None,
                 num_spks=None, 
                 hidden_size=2048,
                 z_bnorm=False,
                 name='MLP'):
        super().__init__(name=name, max_ckpts=1000)
        if num_spks is None:
            raise ValueError('Please specify a number of spks.')
        if z_bnorm:
            # apply z-norm to the input
            self.z_bnorm = nn.BatchNorm1d(frontend.emb_dim, affine=False)
        self.model = nn.Sequential(
            nn.Conv1d(num_inputs, hidden_size, 1),
            nn.LeakyReLU(),
            nn.BatchNorm1d(hidden_size),
            nn.Conv1d(hidden_size, num_spks, 1),
            nn.LogSoftmax(dim=1)
        ) 
Example #6
Source File: gen.py    From visdial-challenge-starter-pytorch with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, config, vocabulary):
        super().__init__()
        self.config = config

        self.word_embed = nn.Embedding(
            len(vocabulary),
            config["word_embedding_size"],
            padding_idx=vocabulary.PAD_INDEX,
        )
        self.answer_rnn = nn.LSTM(
            config["word_embedding_size"],
            config["lstm_hidden_size"],
            config["lstm_num_layers"],
            batch_first=True,
            dropout=config["dropout"],
        )

        self.lstm_to_words = nn.Linear(
            self.config["lstm_hidden_size"], len(vocabulary)
        )

        self.dropout = nn.Dropout(p=config["dropout"])
        self.logsoftmax = nn.LogSoftmax(dim=-1) 
Example #7
Source File: modules.py    From segmentation_models.pytorch with MIT License 6 votes vote down vote up
def __init__(self, name, **params):

        super().__init__()

        if name is None or name == 'identity':
            self.activation = nn.Identity(**params)
        elif name == 'sigmoid':
            self.activation = nn.Sigmoid()
        elif name == 'softmax2d':
            self.activation = nn.Softmax(dim=1, **params)
        elif name == 'softmax':
            self.activation = nn.Softmax(**params)
        elif name == 'logsoftmax':
            self.activation = nn.LogSoftmax(**params)
        elif name == 'argmax':
            self.activation = ArgMax(**params)
        elif name == 'argmax2d':
            self.activation = ArgMax(dim=1, **params)
        elif callable(name):
            self.activation = name(**params)
        else:
            raise ValueError('Activation should be callable/sigmoid/softmax/logsoftmax/None; got {}'.format(name)) 
Example #8
Source File: MLPBin.py    From Pytorch_Quantize_impls with MIT License 6 votes vote down vote up
def __init__(self, in_features, out_features, num_units=2048):

        super(BinMNIST, self).__init__()
        
        self.linear1 = LinearBin(in_features, num_units)
        self.norm1   =nn.BatchNorm1d(num_units, eps=1e-4, momentum=0.15)

        self.linear2 = LinearBin(num_units, num_units)
        self.norm2  = nn.BatchNorm1d(num_units, eps=1e-4, momentum=0.15)

        self.linear3 = LinearBin(num_units, num_units)
        self.norm3   =nn.BatchNorm1d(num_units, eps=1e-4, momentum=0.15)

        self.linear4 = LinearBin(num_units, out_features)
        self.norm4   =nn.BatchNorm1d(out_features, eps=1e-4, momentum=0.15)
        
        self.activation     = nn.ReLU()
        self.act_end = nn.LogSoftmax() 
Example #9
Source File: pspnet.py    From DenseFusion with MIT License 6 votes vote down vote up
def __init__(self, n_classes=21, sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024, backend='resnet18',
                 pretrained=False):
        super(PSPNet, self).__init__()
        self.feats = getattr(extractors, backend)(pretrained)
        self.psp = PSPModule(psp_size, 1024, sizes)
        self.drop_1 = nn.Dropout2d(p=0.3)

        self.up_1 = PSPUpsample(1024, 256)
        self.up_2 = PSPUpsample(256, 64)
        self.up_3 = PSPUpsample(64, 64)

        self.drop_2 = nn.Dropout2d(p=0.15)
        self.final = nn.Sequential(
            nn.Conv2d(64, 32, kernel_size=1),
            nn.LogSoftmax()
        )

        self.classifier = nn.Sequential(
            nn.Linear(deep_features_size, 256),
            nn.ReLU(),
            nn.Linear(256, n_classes)
        ) 
Example #10
Source File: mnist.py    From dgl with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 n_kernels,
                 in_feats,
                 hiddens,
                 out_feats):
        super(MoNet, self).__init__()
        self.pool = nn.MaxPool1d(2)
        self.layers = nn.ModuleList()
        self.readout = MaxPooling()

        # Input layer
        self.layers.append(
            GMMConv(in_feats, hiddens[0], 2, n_kernels))

        # Hidden layer
        for i in range(1, len(hiddens)):
            self.layers.append(GMMConv(hiddens[i - 1], hiddens[i], 2, n_kernels))

        self.cls = nn.Sequential(
            nn.Linear(hiddens[-1], out_feats),
            nn.LogSoftmax()
        ) 
Example #11
Source File: mnist.py    From dgl with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 k,
                 in_feats,
                 hiddens,
                 out_feats):
        super(ChebNet, self).__init__()
        self.pool = nn.MaxPool1d(2)
        self.layers = nn.ModuleList()
        self.readout = MaxPooling()

        # Input layer
        self.layers.append(
            ChebConv(in_feats, hiddens[0], k))

        for i in range(1, len(hiddens)):
            self.layers.append(
                ChebConv(hiddens[i - 1], hiddens[i], k))

        self.cls = nn.Sequential(
            nn.Linear(hiddens[-1], out_feats),
            nn.LogSoftmax()
        ) 
Example #12
Source File: demo_graph.py    From tensorboardX with MIT License 6 votes vote down vote up
def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.i2h = nn.Linear(
            n_categories +
            input_size +
            hidden_size,
            hidden_size)
        self.i2o = nn.Linear(
            n_categories +
            input_size +
            hidden_size,
            output_size)
        self.o2o = nn.Linear(hidden_size + output_size, output_size)
        self.dropout = nn.Dropout(0.1)
        self.softmax = nn.LogSoftmax(dim=1) 
Example #13
Source File: adaptive.py    From RAdam with Apache License 2.0 6 votes vote down vote up
def log_prob(self, w_in):
        lsm = nn.LogSoftmax(dim=1).cuda()

        head_out = self.head(w_in)

        batch_size = head_out.size(0)
        prob = torch.zeros(batch_size, self.cutoff[-1]).cuda()

        lsm_head = lsm(head_out) 
        prob.narrow(1, 0, self.output_size).add_(lsm_head.narrow(1, 0, self.output_size).data)

        for i in range(len(self.tail)):
            pos = self.cutoff[i]
            i_size = self.cutoff[i + 1] - pos
            buffer = lsm_head.narrow(1, self.cutoff[0] + i, 1)
            buffer = buffer.expand(batch_size, i_size)
            lsm_tail = lsm(self.tail[i](w_in)) 
            prob.narrow(1, pos, i_size).copy_(buffer.data).add_(lsm_tail.data)

        return prob 
Example #14
Source File: pspnet.py    From 6-PACK with MIT License 5 votes vote down vote up
def __init__(self, n_classes=6, sizes=(1, 2, 3, 6), psp_size=2048, deep_features_size=1024, backend='resnet18',
                 pretrained=False):
        super(PSPNet, self).__init__()
        self.feats = getattr(extractors, backend)(pretrained)
        self.psp = PSPModule(psp_size, 1024, sizes)

        self.drop_1 = nn.Dropout2d(p=0.3)
        self.drop_2 = nn.Dropout2d(p=0.15)

        self.up_1 = PSPUpsample(1024, 256)
        self.up_2 = PSPUpsample(256, 64)
        self.up_3 = PSPUpsample(64, 64)

        self.up_1_seg = PSPUpsample(1024, 256)
        self.up_2_seg = PSPUpsample(256, 64)
        self.up_3_seg = PSPUpsample(64, 64)

        self.final = nn.Sequential(
            nn.Conv2d(64, 32, kernel_size=1),
            nn.LogSoftmax()
        )

        self.final_seg = nn.Sequential(
            nn.Conv2d(64, 2, kernel_size=1)
        )

        self.classifier = nn.Sequential(
            nn.Linear(deep_features_size, 256),
            nn.ReLU(),
            nn.Linear(256, n_classes)
        ) 
Example #15
Source File: train.py    From pytorch-deeplab-resnet with MIT License 5 votes vote down vote up
def loss_calc(out, label,gpu0):
    """
    This function returns cross entropy loss for semantic segmentation
    """
    # out shape batch_size x channels x h x w -> batch_size x channels x h x w
    # label shape h x w x 1 x batch_size  -> batch_size x 1 x h x w
    label = label[:,:,0,:].transpose(2,0,1)
    label = torch.from_numpy(label).long()
    label = Variable(label).cuda(gpu0)
    m = nn.LogSoftmax()
    criterion = nn.NLLLoss2d()
    out = m(out)
    
    return criterion(out,label) 
Example #16
Source File: model.py    From deepWordBug with Apache License 2.0 5 votes vote down vote up
def __init__(self, classes=4, bidirection = False, layernum=1, length=20000,embedding_size =100, hiddensize = 100):
        super(smallRNN, self).__init__()
        self.embd = nn.Embedding(length, embedding_size)
        # self.lstm = nn.LSTMCell(hiddensize, hiddensize)
        self.lstm = nn.LSTM(embedding_size, hiddensize, layernum, bidirectional = bidirection)
        self.hiddensize = hiddensize
        numdirections = 1 + bidirection
        self.hsize = numdirections * layernum
        self.linear = nn.Linear(hiddensize * numdirections, classes)
        self.log_softmax = nn.LogSoftmax() 
Example #17
Source File: model.py    From deepWordBug with Apache License 2.0 5 votes vote down vote up
def __init__(self, classes=4, bidirection = False, layernum=1, char_size = 69, hiddensize = 100):
        super(smallcharRNN, self).__init__()
        # self.embd = nn.Embedding(length, embedding_size)
        # self.lstm = nn.LSTMCell(hiddensize, hiddensize)
        self.lstm = nn.LSTM(char_size, hiddensize, layernum, bidirectional = bidirection)
        self.hiddensize = hiddensize
        numdirections = 1 + bidirection
        self.hsize = numdirections * layernum
        self.linear = nn.Linear(hiddensize * numdirections, classes)
        self.log_softmax = nn.LogSoftmax() 
Example #18
Source File: adaptive_softmax.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
def __init__(self, vocab_size, input_dim, cutoff, dropout):
        super().__init__()

        if vocab_size > cutoff[-1]:
            cutoff = cutoff + [vocab_size]
        else:
            assert vocab_size == cutoff[
                -1], 'cannot specify cutoff smaller than vocab size'

        output_dim = cutoff[0] + len(cutoff) - 1

        self.vocab_size = vocab_size
        self.cutoff = cutoff
        self.dropout = dropout

        self.lsm = nn.LogSoftmax(dim=1)
        self.head = nn.Linear(input_dim, output_dim, bias=False)
        self.tail = nn.ModuleList()

        for i in range(len(cutoff) - 1):
            self.tail.append(
                nn.Sequential(
                    nn.Linear(input_dim, input_dim // 4 ** i, bias=False),
                    nn.Dropout(dropout),
                    nn.Linear(input_dim // 4 ** i, cutoff[i + 1] - cutoff[i], bias=False)
                )
            )

        def init_weights(m):
            if hasattr(m, 'weight'):
                nn.init.xavier_uniform_(m.weight)

        self.apply(init_weights) 
Example #19
Source File: model.py    From deepWordBug with Apache License 2.0 5 votes vote down vote up
def __init__(self, classes=4, bidirection = False, layernum=1, char_size = 69, hiddensize = 100):
        super(smallcharRNN, self).__init__()
        # self.embd = nn.Embedding(length, embedding_size)
        # self.lstm = nn.LSTMCell(hiddensize, hiddensize)
        self.lstm = nn.LSTM(char_size, hiddensize, layernum, bidirectional = bidirection)
        self.hiddensize = hiddensize
        numdirections = 1 + bidirection
        self.hsize = numdirections * layernum
        self.linear = nn.Linear(hiddensize * numdirections, classes)
        self.log_softmax = nn.LogSoftmax() 
Example #20
Source File: cross_entropy_loss.py    From ABD-Net with MIT License 5 votes vote down vote up
def __init__(self, num_classes, epsilon=0.1, use_gpu=True, label_smooth=True):
        super(CrossEntropyLoss, self).__init__()
        self.num_classes = num_classes
        self.epsilon = epsilon if label_smooth else 0
        self.use_gpu = use_gpu
        self.logsoftmax = nn.LogSoftmax(dim=1) 
Example #21
Source File: metrics.py    From unsupervised-data-augmentation with Apache License 2.0 5 votes vote down vote up
def cross_entropy_smooth(input, target, size_average=True, label_smoothing=0.1):
    y = torch.eye(10).cuda()
    lb_oh = y[target]

    target = lb_oh * (1 - label_smoothing) + 0.5 * label_smoothing

    logsoftmax = nn.LogSoftmax()
    if size_average:
        return torch.mean(torch.sum(-target * logsoftmax(input), dim=1))
    else:
        return torch.sum(torch.sum(-target * logsoftmax(input), dim=1)) 
Example #22
Source File: gazenet.py    From Where-are-they-looking-PyTorch with MIT License 5 votes vote down vote up
def __init__(self, opt):
        super(Net, self).__init__()
        self.salpath = AlexSal(opt)
        self.gazepath = AlexGaze(opt)
        self.opt = opt

        self.smax = nn.LogSoftmax(dim=1)  
        self.nolog_smax = nn.Softmax(dim=1)

        self.fc_0_0 = nn.Linear(169, 25)
        self.fc_0_m1 = nn.Linear(169, 25)
        self.fc_0_1 = nn.Linear(169, 25)
        self.fc_m1_0 = nn.Linear(169, 25)
        self.fc_1_0 = nn.Linear(169, 25) 
Example #23
Source File: Translator.py    From fastNLP with Apache License 2.0 5 votes vote down vote up
def __init__(self, opt):
        self.opt = opt
        self.device = torch.device('cuda' if opt.cuda else 'cpu')

        checkpoint = torch.load(opt.model)
        model_opt = checkpoint['settings']
        self.model_opt = model_opt

        model = Transformer(
            model_opt.src_vocab_size,
            model_opt.tgt_vocab_size,
            model_opt.max_token_seq_len,
            tgt_emb_prj_weight_sharing=model_opt.proj_share_weight,
            emb_src_tgt_weight_sharing=model_opt.embs_share_weight,
            d_k=model_opt.d_k,
            d_v=model_opt.d_v,
            d_model=model_opt.d_model,
            d_word_vec=model_opt.d_word_vec,
            d_inner=model_opt.d_inner_hid,
            n_layers=model_opt.n_layers,
            n_head=model_opt.n_head,
            dropout=model_opt.dropout)

        model.load_state_dict(checkpoint['model'])
        print('[Info] Trained model state loaded.')

        model.word_prob_prj = nn.LogSoftmax(dim=1)

        model = model.to(self.device)

        self.model = model
        self.model.eval() 
Example #24
Source File: HAN.py    From fastNLP with Apache License 2.0 5 votes vote down vote up
def __init__(self, input_size, output_size,
                 word_hidden_size, word_num_layers, word_context_size,
                 sent_hidden_size, sent_num_layers, sent_context_size):
        super(HAN, self).__init__()

        self.word_layer = AttentionNet(input_size,
                                       word_hidden_size,
                                       word_num_layers,
                                       word_context_size)
        self.sent_layer = AttentionNet(2 * word_hidden_size,
                                       sent_hidden_size,
                                       sent_num_layers,
                                       sent_context_size)
        self.output_layer = nn.Linear(2 * sent_hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1) 
Example #25
Source File: dec_attn.py    From e2e-nlg-challenge-2017 with Apache License 2.0 5 votes vote down vote up
def __init__(self, rnn_config, output_size, prev_y_dim, enc_dim, enc_num_directions):
        super(DecoderRNNAttnBahd, self).__init__()

        self.rnn = get_GRU_unit(rnn_config)

        # Setting attention
        dec_dim = rnn_config["hidden_size"]
        self.attn_module = AttnBahd(enc_dim, dec_dim, enc_num_directions)
        self.W_combine = nn.Linear(prev_y_dim + enc_dim * enc_num_directions, dec_dim)
        self.W_out = nn.Linear(dec_dim, output_size)
        self.log_softmax = nn.LogSoftmax()  # works with NLL loss 
Example #26
Source File: losses.py    From conditional-motion-propagation with MIT License 5 votes vote down vote up
def MultiChannelSoftBinaryCrossEntropy(input, target, reduction='mean'):
    '''
    input: N x 38 x H x W --> 19N x 2 x H x W
    target: N x 19 x H x W --> 19N x 1 x H x W
    '''
    input = input.view(-1, 2, input.size(2), input.size(3))
    target = target.view(-1, 1, input.size(2), input.size(3))

    logsoftmax = nn.LogSoftmax(dim=1)
    if reduction == 'mean':
        return torch.mean(torch.sum(-target * logsoftmax(input), dim=1))
    else:
        return torch.sum(torch.sum(-target * logsoftmax(input), dim=1)) 
Example #27
Source File: models.py    From AMNRE with MIT License 5 votes vote down vote up
def __init__(self):
        super(MonoRE,self).__init__()
        self.relation_emb=nn.Embedding(dimR,Encodered_dim)
        self.dropout=nn.Dropout(p=Att_dropout)
        #self.softmax=nn.Softmax()
        #self.logsoftmax=nn.LogSoftmax()
        self.M=nn.Linear(Encodered_dim,dimR) 
Example #28
Source File: models.py    From AMNRE with MIT License 5 votes vote down vote up
def __init__(self):
        super(MonoRE,self).__init__()
        self.relation_emb=nn.Embedding(dimR,Encodered_dim)
        self.dropout=nn.Dropout(p=Att_dropout)
        #self.softmax=nn.Softmax()
        #self.logsoftmax=nn.LogSoftmax()
        self.M=nn.Linear(Encodered_dim,dimR) 
Example #29
Source File: models.py    From AMNRE with MIT License 5 votes vote down vote up
def __init__(self):
        super(MultiRE,self).__init__()
        self.relation_emb=nn.Embedding(dimR,Encodered_dim)
        self.dropout=nn.Dropout(p=Att_dropout)
        #self.softmax=nn.Softmax()
        #self.logsoftmax=nn.LogSoftmax()
        self.M=nn.Linear(Encodered_dim,dimR) 
Example #30
Source File: load_and_test_model.py    From deep-learning-flower-identifier with MIT License 5 votes vote down vote up
def load_model(checkpoint_path):
    """
    Sample code for loading a saved model
    :param checkpoint_path:
    :return:
    """
    chpt = torch.load(checkpoint_path)
    pretrained_model = getattr(models, chpt['arch'])
    if callable(pretrained_model):
        model = pretrained_model(pretrained=True)
        for param in model.parameters():
            param.requires_grad = False
    else:
        print("Sorry base architecture not recognized")
    model.class_to_idx = chpt['class_to_idx']
    # Create the classifier
    classifier = nn.Sequential(OrderedDict([
        ('fc1', nn.Linear(25088, 4096)),
        ('relu', nn.ReLU()),
        ('fc2', nn.Linear(4096, 102)),
        ('output', nn.LogSoftmax(dim=1))
    ]))
    # Put the classifier on the pretrained network
    model.classifier = classifier
    model.load_state_dict(chpt['state_dict'])
    return model