Esempio n. 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._std = Std('test')
     self._std.reset({})
     self._target = 0.57662804083742
    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._std = Std('test')
        self._std.reset({})
        self._target = 0.31622776601684
Esempio n. 3
0
def std(clazz):
    """The :func:`std` decorator is used to add a :class:`.Std` to the :class:`.MetricTree` which will will output a
    population standard deviation 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:`.Std` will also be wrapped in a :class:`.ToDict` (with '_std'
    appended) for simplicity.

    Example: ::

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

        >>> @metrics.std
        ... @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
        {}
        >>> '%.4f' % metric.process_final()['my_metric_std']
        '1.6330'

    :param clazz: The class to *decorate*
    :return: A :class:`.MetricTree` with a :class:`.Std` appended or a wrapper class that extends :class:`.MetricTree`
    """
    return _wrap_and_add_to_tree(
        clazz, lambda metric: ToDict(Std(metric.name + '_std')))
Esempio n. 4
0
        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(Std(inner.name + '_std')))
            return inner
    def test_std_dim(self):
        std = Std('test', dim=0)
        std.process(torch.Tensor([[1., 2.], [3., 4.]]))
        std.process(torch.Tensor([[4., 3.], [2., 1.]]))
        std.process(torch.Tensor([[1., 1.], [1., 1.]]))

        res = std.process_final()
        self.assertTrue(len(res) == 2)
        for m in res:
            self.assertTrue(abs(m - 1.2649) < 0.0001)
Esempio n. 6
0
def std(clazz=None, unbiased=True, dim=None):
    """The :func:`std` decorator is used to add a :class:`.Std` to the :class:`.MetricTree` which will will output a
    sample standard deviation 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:`.Std` will also be wrapped in a :class:`.ToDict` (with '_std'
    appended) for simplicity.

    Example: ::

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

        >>> @metrics.std
        ... @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
        {}
        >>> '%.4f' % metric.process_final()['my_metric_std']
        '2.0000'

    Args:
        clazz: The class to *decorate*
        unbiased (bool): See :class:`.Std`
        dim (int, tuple): See :class:`.Std`

    Returns:
        A :class:`.MetricTree` with a :class:`.Std` appended or a wrapper class that extends :class:`.MetricTree`
    """
    if clazz is None:

        def decorator(clazz):
            return std(clazz, unbiased=unbiased, dim=dim)

        return decorator

    return _wrap_and_add_to_tree(
        clazz, lambda metric: ToDict(
            Std(metric.name + '_std', unbiased=unbiased, dim=dim)))
class TestStd(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._std = Std('test')
        self._std.reset({})
        self._target = 0.31622776601684

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

    def test_validate(self):
        self._std.eval()
        for i in range(5):
            self._std.process(self._metric.process())
        result = self._std.process_final({})
        self.assertAlmostEqual(self._target, result)
class TestStd(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._std = Std('test', unbiased=False)
        self._std.reset({})
        self._target = 0.31622776601684

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

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

    def test_precision_error(self):
        self.setUp()
        self._std.train()
        val = torch.tensor([0.55])
        for i in range(2):
            self._std.process(val)

        result = self._std.process_final({})
        self.assertEqual(0, result)

    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._std = Std('test', unbiased=False)
        self._std.reset({})
        self._target = 0.57662804083742

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

    def test_std_dim(self):
        std = Std('test', dim=0)
        std.process(torch.Tensor([[1., 2.], [3., 4.]]))
        std.process(torch.Tensor([[4., 3.], [2., 1.]]))
        std.process(torch.Tensor([[1., 1.], [1., 1.]]))

        res = std.process_final()
        self.assertTrue(len(res) == 2)
        for m in res:
            self.assertTrue(abs(m - 1.2649) < 0.0001)
Esempio n. 9
0
class TestStd(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._std = Std('test')
        self._std.reset({})
        self._target = 0.31622776601684

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

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

    def test_precision_error(self):
        self.setUp()
        self._std.train()
        val = torch.tensor([0.55])
        for i in range(2):
            self._std.process(val)

        result = self._std.process_final({})
        self.assertEqual(0, result)

    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._std = Std('test')
        self._std.reset({})
        self._target = 0.57662804083742

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