PyTorch 计算 MSE 和 MAE
PyTorch calculate MSE and MAE
我想计算下面模型的 MSE 和 MAE。该模型在每个 Epoch 之后计算 MSE。请问我需要做什么才能获得整体 MSE 值?我可以使用相同的代码来计算 MAE 吗?非常感谢
model.eval()
for images, paths in tqdm(loader_test):
images = images.to(device)
targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
targets = targets.float().to(device)
# forward pass:
output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)
# logging:
loss = torch.mean((preds - targets)**2)
count_error = torch.abs(preds - targets).mean()
mean_test_error += count_error
writer.add_scalar('test_loss', loss.item(), global_step=global_step)
writer.add_scalar('test_count_error', count_error.item(), global_step=global_step)
global_step += 1
average_accuracy = 0
mean_test_error = mean_test_error / len(loader_test)
writer.add_scalar('mean_test_error', mean_test_error.item(), global_step=global_step)
average_accuracy += mean_test_error
average_accuracy = average_accuracy /len(loader_test)
print("Average accuracy: %f" % average_accuracy)
print("Test count error: %f" % mean_test_error)
if mean_test_error < best_test_error:
best_test_error = mean_test_error
torch.save({'state_dict':model.state_dict(),
'optimizer_state_dict':optimizer.state_dict(),
'globalStep':global_step,
'train_paths':dataset_train.files,
'test_paths':dataset_test.files},checkpoint_path)
首先,为了简单起见,您希望在测试阶段将批量大小保持为 1。
这可能是特定于任务的,但是热图回归模型的 MAE 和 MSE 的计算是根据以下等式完成的:
这意味着在您的代码中,您应该更改计算 MAE 的行,如下所示
error = torch.abs(preds - targets).sum().data
squared_error = ((preds - targets)*(preds - targets)).sum().data
runnning_mae += error
runnning_mse += squared_error
之后,在纪元结束后,
mse = math.sqrt(running_mse\len(loader_test))
mae = running_mae\len(loader_test)
我想计算下面模型的 MSE 和 MAE。该模型在每个 Epoch 之后计算 MSE。请问我需要做什么才能获得整体 MSE 值?我可以使用相同的代码来计算 MAE 吗?非常感谢
model.eval()
for images, paths in tqdm(loader_test):
images = images.to(device)
targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
targets = targets.float().to(device)
# forward pass:
output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)
# logging:
loss = torch.mean((preds - targets)**2)
count_error = torch.abs(preds - targets).mean()
mean_test_error += count_error
writer.add_scalar('test_loss', loss.item(), global_step=global_step)
writer.add_scalar('test_count_error', count_error.item(), global_step=global_step)
global_step += 1
average_accuracy = 0
mean_test_error = mean_test_error / len(loader_test)
writer.add_scalar('mean_test_error', mean_test_error.item(), global_step=global_step)
average_accuracy += mean_test_error
average_accuracy = average_accuracy /len(loader_test)
print("Average accuracy: %f" % average_accuracy)
print("Test count error: %f" % mean_test_error)
if mean_test_error < best_test_error:
best_test_error = mean_test_error
torch.save({'state_dict':model.state_dict(),
'optimizer_state_dict':optimizer.state_dict(),
'globalStep':global_step,
'train_paths':dataset_train.files,
'test_paths':dataset_test.files},checkpoint_path)
首先,为了简单起见,您希望在测试阶段将批量大小保持为 1。
这可能是特定于任务的,但是热图回归模型的 MAE 和 MSE 的计算是根据以下等式完成的:
这意味着在您的代码中,您应该更改计算 MAE 的行,如下所示
error = torch.abs(preds - targets).sum().data
squared_error = ((preds - targets)*(preds - targets)).sum().data
runnning_mae += error
runnning_mse += squared_error
之后,在纪元结束后,
mse = math.sqrt(running_mse\len(loader_test))
mae = running_mae\len(loader_test)