ColossalAI/colossalai/nn/metric/accuracy_2d.py

29 lines
785 B
Python
Raw Normal View History

import torch
from colossalai.nn.layer.parallel_2d import reduce_by_batch_2d, split_tensor_2d
from torch import nn
from ._utils import calc_acc
class Accuracy2D(nn.Module):
2022-01-21 02:44:30 +00:00
"""Accuracy for 2D parallelism
"""
def __init__(self):
super().__init__()
def forward(self, logits, targets):
2022-01-21 02:44:30 +00:00
"""Calculate the accuracy of predicted labels.
2022-03-25 05:02:39 +00:00
Args:
logits (:class:`torch.tensor`): Predicted labels.
targets (:class:`torch.tensor`): True labels from data.
Returns:
float: the accuracy of prediction.
2022-01-21 02:44:30 +00:00
"""
with torch.no_grad():
targets = split_tensor_2d(targets)
correct = calc_acc(logits, targets)
correct = reduce_by_batch_2d(correct)
return correct