为什么添加model.eval后测试集精度波动且较低?
模型中有一个batchnorm层。在添加model.eval之前,准确率稳步上升至89%。添加model.eval后,测试集的准确率和损失变得非常不稳定,最高也只有60多。 这是代码: 另一个版本与上面的不同之处只是没有写 model.eval()
def train(epoch):
running_loss = 0.0
correct = 0
total = 0
print("---第 {} 轮训练---".format(epoch + 1))
for batch_idx, data in enumerate(train_loader, 1):
model.train()
inputs, target = data
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
predicted = outputs.data.argmax(1)
total += target.size(0)
correct = correct + (predicted == target).sum().item()
optimizer.step()
running_loss += loss.item()
if batch_idx % 50== 0:
print("训练次数:{},训练loss:{}".format(batch_idx,running_loss/50))
running_loss = 0.0
print("训练准确率为:{} %".format(100*correct/total))
def test():
correct = 0
total = 0
test_loss = 0.0
model.eval()
with torch.no_grad():
for batch_idx,data in enumerate(test_loader,1):
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs,labels)
predicted = outputs.data.argmax(1)
test_loss = test_loss + loss.item()
# print(predicted)
# print(labels)
total += labels.size(0)
correct += (predicted == labels).sum().item()
if batch_idx % 12 == 0:
print("测试{}次,测试集loss为:{}".format(batch_idx,test_loss/12))
test_loss = 0.0
print("test accurancy:{} %".format(100*correct/total))
return correct / total
There is a batchnorm layer in the model. Before model.eval is added, the accuracy rate rises steadily to 89%. After adding model.eval, the accuracy and loss of the test set become very volatile, and the highest is only more than 60.
here's the code:
The other version is different from the above just in that model.eval() is not written
def train(epoch):
running_loss = 0.0
correct = 0
total = 0
print("---第 {} 轮训练---".format(epoch + 1))
for batch_idx, data in enumerate(train_loader, 1):
model.train()
inputs, target = data
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
predicted = outputs.data.argmax(1)
total += target.size(0)
correct = correct + (predicted == target).sum().item()
optimizer.step()
running_loss += loss.item()
if batch_idx % 50== 0:
print("训练次数:{},训练loss:{}".format(batch_idx,running_loss/50))
running_loss = 0.0
print("训练准确率为:{} %".format(100*correct/total))
def test():
correct = 0
total = 0
test_loss = 0.0
model.eval()
with torch.no_grad():
for batch_idx,data in enumerate(test_loader,1):
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs,labels)
predicted = outputs.data.argmax(1)
test_loss = test_loss + loss.item()
# print(predicted)
# print(labels)
total += labels.size(0)
correct += (predicted == labels).sum().item()
if batch_idx % 12 == 0:
print("测试{}次,测试集loss为:{}".format(batch_idx,test_loss/12))
test_loss = 0.0
print("test accurancy:{} %".format(100*correct/total))
return correct / total
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论