本文整理匯總了Python中torch.nn.CrossEntropyLoss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.CrossEntropyLoss方法的具體用法?Python nn.CrossEntropyLoss怎麽用?Python nn.CrossEntropyLoss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.CrossEntropyLoss方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: forward
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def forward(self, input_ids, token_type_ids=None, attention_mask=None, start_positions=None, end_positions=None):
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
logits = self.qa_outputs(sequence_output)
start_logits, end_logits = logits.split(1, dim=-1)
start_logits = start_logits.squeeze(-1)
end_logits = end_logits.squeeze(-1)
if start_positions is not None and end_positions is not None:
# If we are on multi-GPU, split add a dimension
if len(start_positions.size()) > 1:
start_positions = start_positions.squeeze(-1)
if len(end_positions.size()) > 1:
end_positions = end_positions.squeeze(-1)
# sometimes the start/end positions are outside our model inputs, we ignore these terms
ignored_index = start_logits.size(1)
start_positions.clamp_(0, ignored_index)
end_positions.clamp_(0, ignored_index)
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
start_loss = loss_fct(start_logits, start_positions)
end_loss = loss_fct(end_logits, end_positions)
total_loss = (start_loss + end_loss) / 2
return total_loss
else:
return start_logits, end_logits
示例2: forward
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def forward(self, input_ids, token_type_ids=None, attention_mask=None, answer_mask=None,positions=None):
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
answer_mask = answer_mask.to(dtype=next(self.parameters()).dtype)
logits = self.qa_outputs(sequence_output).squeeze(-1)
#logits = logits*answer_mask_
logits = logits + (1-answer_mask) * -10000.0
if positions is not None:
# If we are on multi-GPU, split add a dimension
if len(positions.size()) > 1:
positions = positions.squeeze(-1)
# sometimes the positions are outside our model inputs, we ignore these terms
ignored_index = logits.size(1)
positions.clamp_(0, ignored_index)
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
total_loss = loss_fct(logits, positions)
return total_loss
else:
return logits
示例3: _loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def _loss(self, learning_algorithm, label_paddingId, use_crf=False):
"""
:param learning_algorithm:
:param label_paddingId:
:param use_crf:
:return:
"""
if use_crf:
loss_function = self.model.crf_layer.neg_log_likelihood_loss
return loss_function
elif learning_algorithm == "SGD":
loss_function = nn.CrossEntropyLoss(ignore_index=label_paddingId, reduction="sum")
return loss_function
else:
loss_function = nn.CrossEntropyLoss(ignore_index=label_paddingId, reduction="mean")
return loss_function
示例4: train_cnn
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def train_cnn(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs):
net = net.to(device)
print('training on', device)
loss = nn.CrossEntropyLoss()
batch_count = 0
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()
for X, y in train_iter:
X = X.to(device)
y = y.to(device)
y_hat = net(X)
l = loss(y_hat, y)
optimizer.zero_grad()
l.backward()
optimizer.step()
train_l_sum += l.cpu().item()
train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
n += y.shape[0]
batch_count += 1
test_acc = evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec' %
(epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc, time.time() - start))
示例5: train
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def train(encoder, decoder, dataset, lr, batch_size, num_epochs):
enc_optimizer = torch.optim.Adam(encoder.parameters(), lr=lr)
dec_optimizer = torch.optim.Adam(decoder.parameters(), lr=lr)
loss = nn.CrossEntropyLoss(reduction='none')
data_iter = Data.DataLoader(dataset, batch_size, shuffle=True)
for epoch in range(num_epochs):
l_sum = 0.0
start = time.time()
for X, Y in data_iter:
enc_optimizer.zero_grad()
dec_optimizer.zero_grad()
l = batch_loss(encoder, decoder, X, Y, loss)
l.backward()
enc_optimizer.step()
dec_optimizer.step()
l_sum += l.item()
if (epoch + 1) % 10 == 0:
print("epoch %d, loss %.3f, time: %.1f sec" % (epoch + 1, l_sum / len(data_iter), time.time() - start))
示例6: test
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def test(model, data_tar, e):
total_loss_test = 0
correct = 0
criterion = nn.CrossEntropyLoss()
with torch.no_grad():
for batch_id, (data, target) in enumerate(data_tar):
data, target = data.view(-1,28 * 28).to(DEVICE),target.to(DEVICE)
model.eval()
ypred, _, _ = model(data, data)
loss = criterion(ypred, target)
pred = ypred.data.max(1)[1] # get the index of the max log-probability
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
total_loss_test += loss.data
accuracy = correct * 100. / len(data_tar.dataset)
res = 'Test: total loss: {:.6f}, correct: [{}/{}], testing accuracy: {:.4f}%'.format(
total_loss_test, correct, len(data_tar.dataset), accuracy
)
tqdm.write(res)
RESULT_TEST.append([e, total_loss_test, accuracy])
log_test.write(res + '\n')
示例7: FocalLoss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def FocalLoss(self, logit, target, gamma=2, alpha=0.5):
n, c, h, w = logit.size()
criterion = nn.CrossEntropyLoss(weight=self.weight, ignore_index=self.ignore_index,
size_average=self.size_average)
if self.cuda:
criterion = criterion.cuda()
logpt = -criterion(logit, target.long())
pt = torch.exp(logpt)
if alpha is not None:
logpt *= alpha
loss = -((1 - pt) ** gamma) * logpt
if self.batch_average:
loss /= n
return loss
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def __init__(self, args, model):
"""Initialize an instance of the wrapper
:param args: arguments containing training and structure parameters
:type args: ModelArguments
:param model: A classifier module, ex. BERT or RNN classifier module
:type model: BertForSequenceClassification or ClassifierModule
"""
self.args = args
self.model = model
self.opt = None
self.num_epochs = args.num_pretrain_epochs
self.epochs_since_improv = 0
self.best_test_acc = 0
self.avg_accuracy = 0
self.test_accs = []
self.train_accs = []
self.loss_func = nn.CrossEntropyLoss(reduction="none")
示例9: forward
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def forward(self, input_ids, token_type_ids=None, attention_mask=None,
labels=None, input_mask=None):
last_bert_layer, pooled_output = self.bert(input_ids, token_type_ids, attention_mask, \
output_all_encoded_layers=False)
last_bert_layer = last_bert_layer.view(-1, self.hidden_size)
last_bert_layer = self.dropout(last_bert_layer)
logits = self.classifier(last_bert_layer)
if labels is not None:
loss_fct = CrossEntropyLoss()
if input_mask is not None:
masked_logits = torch.masked_select(logits, input_mask)
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
else:
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
return loss
else:
return logits
示例10: forward
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def forward(self, input_ids, token_type_ids=None, attention_mask=None,
start_positions=None, end_positions=None):
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
sequence_output = sequence_output.view(-1, self.hidden_size)
start_logits = self.start_outputs(sequence_output)
end_logits = self.end_outputs(sequence_output)
if start_positions is not None and end_positions is not None:
loss_fct = CrossEntropyLoss()
start_loss = loss_fct(start_logits.view(-1, 2), start_positions.view(-1))
end_loss = loss_fct(end_logits.view(-1, 2), end_positions.view(-1))
total_loss = start_loss + end_loss + span_loss
# total_loss = (start_loss + end_loss) / 2
return total_loss
else:
return start_logits, end_logits
示例11: main
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def main():
best_acc = 0
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('==> Preparing data..')
transforms_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
dataset_train = CIFAR10(root='../data', train=True, download=True,
transform=transforms_train)
train_loader = DataLoader(dataset_train, batch_size=args.batch_size,
shuffle=True, num_workers=args.num_worker)
# there are 10 classes so the dataset name is cifar-10
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
print('==> Making model..')
net = pyramidnet()
net = nn.DataParallel(net)
net = net.to(device)
num_params = sum(p.numel() for p in net.parameters() if p.requires_grad)
print('The number of parameters of model is', num_params)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=args.lr)
# optimizer = optim.SGD(net.parameters(), lr=args.lr,
# momentum=0.9, weight_decay=1e-4)
train(net, criterion, optimizer, train_loader, device)
示例12: get_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def get_loss(args):
if args.loss == 'svm':
loss_fn = MultiClassHingeLoss()
elif args.loss == 'ce':
loss_fn = nn.CrossEntropyLoss()
else:
raise ValueError
print('L2 regularization: \t {}'.format(args.l2))
print('\nLoss function:')
print(loss_fn)
if args.cuda:
loss_fn = loss_fn.cuda()
return loss_fn
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def __init__(self, scene_classes, semantic_classes=151):
super(SemBranch, self).__init__()
# Semantic Branch
self.in_block_sem = nn.Sequential(
nn.Conv2d(semantic_classes + 1, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
)
self.in_block_sem_1 = BasicBlockSem(64, 128, kernel_size=3, stride=2, padding=1)
self.in_block_sem_2 = BasicBlockSem(128, 256, kernel_size=3, stride=2, padding=1)
self.in_block_sem_3 = BasicBlockSem(256, 512, kernel_size=3, stride=2, padding=1)
# Semantic Scene Classification Layers
self.dropout = nn.Dropout(0.3)
self.avgpool = nn.AvgPool2d(7, stride=1)
self.fc_SEM = nn.Linear(512, scene_classes)
# Loss
self.criterion = nn.CrossEntropyLoss()
示例14: cross_entropy_loss_weighted
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def cross_entropy_loss_weighted(output, labels):
temp = labels.data.cpu().numpy()
freqCount = scipystats.itemfreq(temp)
total = freqCount[0][1]+freqCount[1][1]
perc_1 = freqCount[1][1]/total
perc_0 = freqCount[0][1]/total
weight_array = [perc_1, perc_0]
if torch.cuda.is_available():
weight_tensor = torch.FloatTensor(weight_array).cuda()
else:
weight_tensor = torch.FloatTensor(weight_array)
ce_loss = nn.CrossEntropyLoss(weight=weight_tensor)
images, channels, height, width = output.data.shape
loss = ce_loss(output, labels.long().view(images, height, width))
return loss
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import CrossEntropyLoss [as 別名]
def __init__(self, ignore_label, reduction='elementwise_mean', thresh=0.6, min_kept=256,
down_ratio=1, use_weight=False):
super(OhemCrossEntropy2dTensor, self).__init__()
self.ignore_label = ignore_label
self.thresh = float(thresh)
self.min_kept = int(min_kept)
self.down_ratio = down_ratio
if use_weight:
weight = torch.FloatTensor(
[0.8373, 0.918, 0.866, 1.0345, 1.0166, 0.9969, 0.9754, 1.0489,
0.8786, 1.0023, 0.9539, 0.9843, 1.1116, 0.9037, 1.0865, 1.0955,
1.0865, 1.1529, 1.0507])
self.criterion = torch.nn.CrossEntropyLoss(reduction=reduction,
weight=weight,
ignore_index=ignore_label)
else:
self.criterion = torch.nn.CrossEntropyLoss(reduction=reduction,
ignore_index=ignore_label)