损失函数实现及代码分析⽬录
幼儿园班务工作计划2021SC@SDUSC
normal_train.py中交叉熵损失函数实现
设置优化器
花雨夜
optimizer = AdamW(
[
{"params": dule.bert.parameters(), "lr": args.lrmain},
{"params": dule.linear.parameters(), "lr": args.lrlast},
反贪污
])
设置尺度criterion
criterion = nn.CrossEntropyLoss()
test_accs = []
for epoch in range(args.epochs):
train(labeled_trainloader, model, optimizer, criterion, epoch)
val_loss, val_acc = validate(
val_loader, model, criterion, epoch, mode='Valid Stats')
print("epoch {}, val acc {}, val_loss {}".format(
epoch, val_acc, val_loss))
当val_acc的值>=best_acc时,将best_acc的值进⾏更新,把val_acc的值赋给best_acc
if val_acc >= best_acc:
best_acc = val_acc
test_loss, test_acc = validate(
test_loader, model, criterion, epoch, mode='Test Stats ')
test_accs.append(test_acc)
print("epoch {}, test acc {},test loss {}".format(
epoch, test_acc, test_loss))
print('Best val_acc:')
print(best_acc)
分手祝福print('Test acc:')
print(test_accs)
对于枚举valloader中每⼀批数据的输⼊,标记和长度,进⾏loss计算
for batch_idx, (inputs, targets, length) in enumerate(valloader):
inputs, targets = inputs.cuda(), targets.cuda(non_blocking=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
计算总体样本和总损失
correct += (np.array(predicted.cpu()) ==
np.array(targets.cpu())).sum()
loss_total += loss.item() * inputs.shape[0]
total_sample += inputs.shape[0]化学物质的分类
南面百城⽤设置好的优化器训练模型
def train(labeled_trainloader, model, optimizer, criterion, epoch):
如何写好读后感for batch_idx, (inputs, targets, length) in enumerate(labeled_trainloader): inputs, targets = inputs.cuda(), targets.cuda(non_blocking=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
<_grad()
print('epoch {}, step {}, loss {}'.format(
epoch, batch_idx, loss.item()))
loss.backward()
焖锅的做法optimizer.step()