标签:mod 而不是 ORC rect col pytorch ret strong sum
这几天关于accuracy和loss的计算有一些疑惑,原来是自己还没有弄清楚。
给出实例
def train(train_loader, model, criteon, optimizer, epoch): train_loss = 0 train_acc = 0 num_correct= 0 for step, (x,y) in enumerate(train_loader): # x: [b, 3, 224, 224], y: [b] x, y = x.to(device), y.to(device) model.train() logits = model(x) loss = criteon(logits, y) optimizer.zero_grad() loss.backward() optimizer.step() train_loss += float(loss.item()) train_losses.append(train_loss) pred = logits.argmax(dim=1) num_correct += torch.eq(pred, y).sum().float().item() logger.info("Train Epoch: {}\t Loss: {:.6f}\t Acc: {:.6f}".format(epoch,train_loss/len(train_loader),num_correct/len(train_loader.dataset))) return num_correct/len(train_loader.dataset), train_loss/len(train_loader)
那么,accuracy的计算也就是在整个train_loader的for循环中(步数),把每个mini_batch中判断正确的个数累加起来,然后除以样本总数就行了;
label,在Pytorch中,只要把这两个值输进去就能计算交叉熵,用的方法是nn.CrossEntropyLoss,这个方法其实是计算了一个minibatch的均值了,因此累加以后需要除以的步数,也就是
minibatch的个数,而不是像accuracy那样是样本个数,这一点非常重要。
标签:mod 而不是 ORC rect col pytorch ret strong sum
原文地址:https://www.cnblogs.com/yqpy/p/11497199.html