コード例 #1
0
 def setUpMoreDims(self):
     self._metric = Metric('test')
     self._metric.process = Mock()
     self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                         torch.FloatTensor([[0.1, 0.2, 0.3], [1.1, 1.2, 1.3]]),
                                         torch.FloatTensor([[0.4, 0.5, 0.6], [1.4, 1.5, 1.6]]),
                                         torch.FloatTensor([[0.7, 0.8, 0.9], [1.7, 1.8, 1.9]]),
                                         torch.ones(torch.Size([]))]
     self._mean = Mean('test')
     self._mean.reset({})
     self._target = 0.95
コード例 #2
0
    def setUp(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([0.1, 0.2, 0.3]),
                                            torch.FloatTensor([0.4, 0.5, 0.6]),
                                            torch.FloatTensor([0.7, 0.8, 0.9]),
                                            torch.ones(torch.Size([]))]

        self._mean = Mean('test')
        self._mean.reset({})
        self._target = 0.5
コード例 #3
0
def mean(clazz):
    """The :func:`mean` decorator is used to add a :class:`.Mean` to the :class:`.MetricTree` which will will output a
    mean value at the end of each epoch. At build time, if the inner class is not a :class:`.MetricTree`, one will be
    created. The :class:`.Mean` will also be wrapped in a :class:`.ToDict` for simplicity.

    Example: ::

        >>> import torch
        >>> from torchbearer import metrics

        >>> @metrics.mean
        ... @metrics.lambda_metric('my_metric')
        ... def metric(y_pred, y_true):
        ...     return y_pred + y_true
        ...
        >>> metric.reset({})
        >>> metric.process({'y_pred':torch.Tensor([2]), 'y_true':torch.Tensor([2])}) # 4
        {}
        >>> metric.process({'y_pred':torch.Tensor([3]), 'y_true':torch.Tensor([3])}) # 6
        {}
        >>> metric.process({'y_pred':torch.Tensor([4]), 'y_true':torch.Tensor([4])}) # 8
        {}
        >>> metric.process_final()
        {'my_metric': 6.0}

    :param clazz: The class to *decorate*
    :return: A :class:`.MetricTree` with a :class:`.Mean` appended or a wrapper class that extends :class:`.MetricTree`
    """
    return _wrap_and_add_to_tree(clazz,
                                 lambda metric: ToDict(Mean(metric.name)))
コード例 #4
0
ファイル: decorators.py プロジェクト: zfatgxuestc/torchbearer
        def build(self):
            if isinstance(self.inner, MetricFactory):
                inner = self.inner.build()
            else:
                inner = self.inner

            if not isinstance(inner, MetricTree):
                inner = MetricTree(inner)
            inner.add_child(ToDict(Mean(inner.name)))
            return inner
コード例 #5
0
    def test_mean_dim(self):
        mean = Mean('test', dim=0)
        mean.process(torch.Tensor([[1., 2.], [3., 4.]]))
        mean.process(torch.Tensor([[4., 3.], [2., 1.]]))
        mean.process(torch.Tensor([[1., 1.], [1., 1.]]))

        res = mean.process_final()
        self.assertTrue(len(res) == 2)
        for m in res:
            self.assertTrue(abs(m - 2.0) < 0.0001)
コード例 #6
0
class TestMean(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([0.1, 0.2, 0.3]),
                                            torch.FloatTensor([0.4, 0.5, 0.6]),
                                            torch.FloatTensor([0.7, 0.8, 0.9]),
                                            torch.ones(torch.Size([]))]

        self._mean = Mean('test')
        self._mean.reset({})
        self._target = 0.5

    def test_train_dict(self):
        self._mean.train()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result)

    def test_validate_dict(self):
        self._mean.eval()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result)
コード例 #7
0
class TestMean(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [
            torch.zeros(torch.Size([])),
            torch.FloatTensor([0.1, 0.2, 0.3]),
            torch.FloatTensor([0.4, 0.5, 0.6]),
            torch.FloatTensor([0.7, 0.8, 0.9]),
            torch.ones(torch.Size([]))
        ]

        self._mean = Mean('test')
        self._mean.reset({})
        self._target = 0.5

    def test_train_dict(self):
        self.setUp()
        self._mean.train()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def test_validate_dict(self):
        self.setUp()
        self._mean.eval()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def setUpMoreDims(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [
            torch.zeros(torch.Size([])),
            torch.FloatTensor([[0.1, 0.2, 0.3], [1.1, 1.2, 1.3]]),
            torch.FloatTensor([[0.4, 0.5, 0.6], [1.4, 1.5, 1.6]]),
            torch.FloatTensor([[0.7, 0.8, 0.9], [1.7, 1.8, 1.9]]),
            torch.ones(torch.Size([]))
        ]
        self._mean = Mean('test')
        self._mean.reset({})
        self._target = 0.95

    def test_more_dims(self):
        self.setUpMoreDims()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def test_mean_dim(self):
        mean = Mean('test', dim=0)
        mean.process(torch.Tensor([[1., 2.], [3., 4.]]))
        mean.process(torch.Tensor([[4., 3.], [2., 1.]]))
        mean.process(torch.Tensor([[1., 1.], [1., 1.]]))

        res = mean.process_final()
        self.assertTrue(len(res) == 2)
        for m in res:
            self.assertTrue(abs(m - 2.0) < 0.0001)