Exemplo n.º 1
0
class TestRunningMean(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._mean = RunningMean('test')
        self._cache = [
            torch.Tensor([1.0]),
            torch.Tensor([1.5]),
            torch.Tensor([2.0])
        ]
        self._target = 1.5

    def test_train(self):
        result = self._mean._process_train(torch.FloatTensor([1.0, 1.5, 2.0]))
        self.assertAlmostEqual(self._target, result, 3, 0.002)

    def test_step(self):
        result = self._mean._step(self._cache)
        self.assertEqual(self._target, result)

    def test_dims(self):
        mean = RunningMean('test', dim=0)
        cache = [
            mean._process_train(torch.Tensor([[1., 2.], [3., 4.]])),
            mean._process_train(torch.Tensor([[4., 3.], [2., 1.]])),
            mean._process_train(torch.Tensor([[1., 1.], [1., 1.]]))
        ]

        res = mean._step(cache)
        self.assertTrue(len(res) == 2)
        for m in res:
            self.assertTrue(abs(m - 2.0) < 0.0001)
Exemplo n.º 2
0
    def test_dims(self):
        mean = RunningMean('test', dim=0)
        cache = [
            mean._process_train(torch.Tensor([[1., 2.], [3., 4.]])),
            mean._process_train(torch.Tensor([[4., 3.], [2., 1.]])),
            mean._process_train(torch.Tensor([[1., 1.], [1., 1.]]))
        ]

        res = mean._step(cache)
        self.assertTrue(len(res) == 2)
        for m in res:
            self.assertTrue(abs(m - 2.0) < 0.0001)
Exemplo n.º 3
0
class TestRunningMean(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._mean = RunningMean('test')
        self._cache = [1.0, 1.5, 2.0]
        self._target = 1.5

    def test_train(self):
        result = self._mean._process_train(torch.FloatTensor([1.0, 1.5, 2.0]))
        self.assertAlmostEqual(self._target, result, 3, 0.002)

    def test_step(self):
        result = self._mean._step(self._cache)
        self.assertEqual(self._target, result)