Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some problem about metrics.py #15

Closed
MJ-Zeng opened this issue Jul 22, 2022 · 3 comments
Closed

some problem about metrics.py #15

MJ-Zeng opened this issue Jul 22, 2022 · 3 comments

Comments

@MJ-Zeng
Copy link

MJ-Zeng commented Jul 22, 2022

I introduced L1Metric class ,I get a error:

AttributeError: 'L1Metric' object has no attribute 'abs_record'

class L1Metric(AbsMetric):
    r"""Calculate the Mean Absolute Error (MAE).
    """
    def __init__(self):
        super(L1Metric, self).__init__()
        
    def update_fun(self, pred, gt):
        r"""
        """
        abs_err = torch.abs(pred - gt)
        self.record.append(abs_err)
        self.bs.append(pred.size()[0])
        
    def score_fun(self):
        r"""
        """
        records = np.array(self.abs_record)
        batch_size = np.array(self.bs)
        return [(records*batch_size).sum()/(sum(batch_size))]

L1Metric class inherit AbsMetric class,but AbsMetric class has no attribute 'abs_record',So I guess there are maybe some problems,of course,this property may also come from other places

class AbsMetric(object):
    r"""An abstract class for the performance metrics of a task. 

    Attributes:
        record (list): A list of the metric scores in every iteration.
        bs (list): A list of the number of data in every iteration.
    """
    def __init__(self):
        self.record = []
        self.bs = []
    
    @property
    def update_fun(self, pred, gt):
        r"""Calculate the metric scores in every iteration and update :attr:`record`.

        Args:
            pred (torch.Tensor): The prediction tensor.
            gt (torch.Tensor): The ground-truth tensor.
        """
        pass
    
    @property
    def score_fun(self):
        r"""Calculate the final score (when an epoch ends).

        Return:
            list: A list of metric scores.
        """
        pass
    
    def reinit(self):
        r"""Reset :attr:`record` and :attr:`bs` (when an epoch ends).
        """
        self.record = []
        self.bs = []
Baijiong-Lin added a commit that referenced this issue Jul 22, 2022
@Baijiong-Lin
Copy link
Collaborator

We have fixed the problem. Thank you.

@MJ-Zeng
Copy link
Author

MJ-Zeng commented Jul 22, 2022

ok, there are an another problem,
During the training of the model, the tensor format cannot be directly converted to the numpy format

        records = np.array(self.record)
        batch_size = np.array(self.bs)

Baijiong-Lin added a commit that referenced this issue Jul 22, 2022
@Baijiong-Lin
Copy link
Collaborator

We have fixed this problem. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants