本文整理汇总了Python中torch.nn.functional.log_softmax方法的典型用法代码示例。如果您正苦于以下问题:Python functional.log_softmax方法的具体用法?Python functional.log_softmax怎么用?Python functional.log_softmax使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.log_softmax方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hier_topk
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def hier_topk(cls_scores, icls_scores, vocab, topk):
batch_size = len(cls_scores)
cls_scores = F.log_softmax(cls_scores, dim=-1)
cls_scores_topk, cls_topk = cls_scores.topk(topk, dim=-1)
final_topk = []
for i in range(topk):
clab = cls_topk[:, i]
mask = vocab.get_mask(clab)
masked_icls_scores = F.log_softmax(icls_scores + mask, dim=-1)
icls_scores_topk, icls_topk = masked_icls_scores.topk(topk, dim=-1)
topk_scores = cls_scores_topk[:, i].unsqueeze(-1) + icls_scores_topk
final_topk.append( (topk_scores, clab.unsqueeze(-1).expand(-1, topk), icls_topk) )
topk_scores, cls_topk, icls_topk = zip(*final_topk)
topk_scores = torch.cat(topk_scores, dim=-1)
cls_topk = torch.cat(cls_topk, dim=-1)
icls_topk = torch.cat(icls_topk, dim=-1)
topk_scores, topk_index = topk_scores.topk(topk, dim=-1)
batch_index = cls_topk.new_tensor([[i] * topk for i in range(batch_size)])
cls_topk = cls_topk[batch_index, topk_index]
icls_topk = icls_topk[batch_index, topk_index]
return topk_scores, cls_topk.tolist(), icls_topk.tolist()
示例2: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, input, target): # input = Variable(logits), target = labels
loss = Variable(torch.zeros(1).cuda()) if self.gpu else Variable(torch.zeros(1))
# novel loss
if self.loo > 0.:
target_novel = self.labels_relevant[target]
for i, rel in enumerate(self.relevant):
if target_novel[:,i].any():
relevant_loc = target_novel[:,i].nonzero().view(-1)
loss += -F.log_softmax(input[relevant_loc][:, rel], dim=1)[:,0].mean() * self.class_weight[i]
loss *= self.loo
# known loss
log_probs = F.log_softmax(input, dim=1)
loss += F.nll_loss(log_probs, Variable(target))
# regularization
if self.label_smooth > 0.:
loss -= (log_probs.mean() + self.kld_u_const) * self.label_smooth
return loss
示例3: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, input, hidden, encoder_outputs):
embedded = self.embedding(input).view(1, 1, -1)
embedded = self.dropout(embedded)
attn_weights = F.softmax(
self.attn(torch.cat((embedded[0], hidden[0]), 1)), dim=1)
attn_applied = torch.bmm(attn_weights.unsqueeze(0),
encoder_outputs.unsqueeze(0))
output = torch.cat((embedded[0], attn_applied[0]), 1)
output = self.attn_combine(output).unsqueeze(0)
output = F.relu(output)
output, hidden = self.gru(output, hidden)
output = F.log_softmax(self.out(output[0]), dim=1)
return output, hidden, attn_weights
示例4: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
# x: (batch, sentence_length)
x = self.embed(x)
# x: (batch, sentence_length, embed_dim)
# TODO init embed matrix with pre-trained
x = x.unsqueeze(1)
# x: (batch, 1, sentence_length, embed_dim)
x1 = self.conv_and_pool(x, self.conv11) # (batch, kernel_num)
x2 = self.conv_and_pool(x, self.conv12) # (batch, kernel_num)
x3 = self.conv_and_pool(x, self.conv13) # (batch, kernel_num)
x = torch.cat((x1, x2, x3), 1) # (batch, 3 * kernel_num)
x = self.dropout(x)
logit = F.log_softmax(self.fc1(x), dim=1)
# logit = F.softmax(self.fc1(x), dim=1)
# logit = self.fc1(x)
return logit
示例5: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, input, target):
if input.dim()>2:
input = input.view(input.size(0),input.size(1),-1) # N,C,H,W => N,C,H*W
input = input.transpose(1,2) # N,C,H*W => N,H*W,C
input = input.contiguous().view(-1,input.size(2)) # N,H*W,C => N*H*W,C
target = target.view(-1,1)
logpt = F.log_softmax(input)
logpt = logpt.gather(1,target)
logpt = logpt.view(-1)
pt = Variable(logpt.data.exp())
if self.alpha is not None:
if self.alpha.type()!=input.data.type():
self.alpha = self.alpha.type_as(input.data)
at = self.alpha.gather(0,target.data.view(-1))
logpt = logpt * Variable(at)
loss = -1 * (1-pt)**self.gamma * logpt
if self.size_average: return loss.mean()
else: return loss.sum()
示例6: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
if self.adj_dropout > 0:
edge_index, edge_type = dropout_adj(
edge_index, edge_type, p=self.adj_dropout,
force_undirected=self.force_undirected, num_nodes=len(x),
training=self.training
)
concat_states = []
for conv in self.convs:
x = torch.tanh(conv(x, edge_index))
concat_states.append(x)
concat_states = torch.cat(concat_states, 1)
x = global_add_pool(concat_states, batch)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
if self.regression:
return x[:, 0]
else:
return F.log_softmax(x, dim=-1)
示例7: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, inputs):
"""
:param inputs: batch_size x input_size
:return:
"""
if self.is_lstm:
h, c= inputs
if h.dim() == 3:
h = h.squeeze(0)
c = c.squeeze(0)
logits = self.p_h(h) + self.p_c(c)
else:
logits = self.p_h(inputs)
logits = logits.view(-1, self.k_size)
log_qy = F.log_softmax(logits, dim=1)
return logits, log_qy
示例8: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
return F.log_softmax(self.proj(x), dim=-1)
示例9: modify_output_for_loss_fn
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def modify_output_for_loss_fn(loss_fn, output, dim):
if loss_fn == "ce":
return output
if loss_fn == "mse":
return F.softmax(output, dim=dim)
if loss_fn == "nll":
return F.log_softmax(output, dim=dim)
if loss_fn in ["bce", "wbce", "wbce1"]:
return torch.sigmoid(output)
示例10: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
# x = self.conv3(x)
x = x.view(x.size(0),-1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return (x, F.log_softmax(x))
示例11: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)
示例12: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
out = self.conv1(x)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool2d(out, 4)
out = out.view(out.size(0), -1)
out = self.linear(out)
return (out,F.log_softmax(out))
示例13: PreActResNet18
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def PreActResNet18():
op = PreActResNet(PreActBlock, [2,2,2,2])
return op
#return (op,F.log_softmax(op))
示例14: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool2d(out, 4)
out = out.view(out.size(0), -1)
out = self.linear(out)
return (out,F.log_softmax(out))
示例15: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import log_softmax [as 别名]
def forward(self, x):
out = self.conv1(x)
out = self.trans1(self.dense1(out))
out = self.trans2(self.dense2(out))
out = self.trans3(self.dense3(out))
out = self.dense4(out)
out = F.avg_pool2d(F.relu(self.bn(out)), 4)
out = out.view(out.size(0), -1)
out = self.linear(out)
return (out,F.log_softmax(out))