def test_weight(self):
        self.summary.add(1., 0.5)
        self.summary.add(2., numpy.array(0.4))
        self.summary.add(3., torch.from_numpy(numpy.array(0.3)))

        mean = self.summary.compute_mean().array
        val = (1 * 0.5 + 2 * 0.4 + 3 * 0.3) / (0.5 + 0.4 + 0.3)
        testing.assert_allclose(mean, val)
    def test_weight(self):
        self.summary.add({'a': (1., 0.5)})
        self.summary.add({'a': (2., numpy.array(0.4))})
        self.summary.add({'a': (3., torch.from_numpy(numpy.array(0.3)))})

        mean = self.summary.compute_mean()
        val = (1 * 0.5 + 2 * 0.4 + 3 * 0.3) / (0.5 + 0.4 + 0.3)
        testing.assert_allclose(mean['a'], val)

        with self.assertRaises(ValueError):
            self.summary.add({'a': (4., numpy.array([0.5]))})

        with self.assertRaises(ValueError):
            self.summary.add({'a': (4., torch.from_numpy(numpy.array([0.5])))})
Beispiel #3
0
    def _run_trainer(self, extension, expect, optimizer=None):
        if optimizer is None:
            optimizer = self.optimizer
        extension.initialize(self.trainer)
        actual = []
        for _ in range(self.warmup_iter + 2):
            self.trainer.updater.update()
            actual.append(optimizer.param_groups[0]['x'])
            if self.trigger(self.trainer):
                extension(self.trainer)

        testing.assert_allclose(actual[0], expect[0])
        testing.assert_allclose(actual[self.warmup_iter - 1], expect[1])
        testing.assert_allclose(actual[self.warmup_iter], expect[2])
        testing.assert_allclose(actual[self.warmup_iter + 1], expect[3])
    def test_basic(self, backend_config):
        self.summary.add(backend_config.get_tensor(numpy.array(1, 'f')))
        self.summary.add(backend_config.get_tensor(numpy.array(-2, 'f')))

        mean = self.summary.compute_mean()
        testing.assert_allclose(mean, numpy.array(-0.5, 'f'))

        mean, std = self.summary.make_statistics()
        testing.assert_allclose(mean, numpy.array(-0.5, 'f'))
        testing.assert_allclose(std, numpy.array(1.5, 'f'))
    def test_float(self):
        self.summary.add(1.)
        self.summary.add(2.)
        self.summary.add(3.)

        mean = self.summary.compute_mean()
        testing.assert_allclose(mean, 2.)

        mean, std = self.summary.make_statistics()
        testing.assert_allclose(mean, 2.)
        testing.assert_allclose(std, numpy.sqrt(2. / 3.))
    def test_serialize_backward_compat(self):
        with tempfile.NamedTemporaryFile(delete=False) as f:
            # old version does not save anything
            torch.save(dict(dummy=0), f.name)
            with testing.assert_warns(UserWarning):
                self.summary.load_state_dict(torch.load(f.name))

        self.summary.add(2.)
        self.summary.add(3.)

        mean = self.summary.compute_mean()
        testing.assert_allclose(mean, 2.5)

        mean, std = self.summary.make_statistics()
        testing.assert_allclose(mean, 2.5)
        testing.assert_allclose(std, 0.5)
    def check(self, summary, data):
        mean = summary.compute_mean()
        self.assertEqual(set(mean.keys()), set(data.keys()))
        for name in data.keys():
            m = sum(data[name]) / float(len(data[name]))
            testing.assert_allclose(mean[name], m)

        stats = summary.make_statistics()
        self.assertEqual(
            set(stats.keys()),
            set(data.keys()).union(name + '.std' for name in data.keys()))
        for name in data.keys():
            m = sum(data[name]) / float(len(data[name]))
            s = numpy.sqrt(
                sum(x * x for x in data[name]) / float(len(data[name]))
                - m * m)
            testing.assert_allclose(stats[name], m)
            testing.assert_allclose(stats[name + '.std'], s)
    def check_serialize(self, value1, value2, value3):
        self.summary.add(value1)
        self.summary.add(value2)

        summary = pytorch_trainer.reporter.Summary()
        testing.save_and_load_pth(self.summary, summary)
        summary.add(value3)

        expected_mean = (value1 + value2 + value3).to(dtype=torch.float) / 3.
        expected_std = ((value1 ** 2 + value2 ** 2 + value3 ** 2)
            .to(dtype=torch.float) / 3. - expected_mean ** 2).sqrt()

        mean = summary.compute_mean()
        testing.assert_allclose(mean, expected_mean)

        mean, std = summary.make_statistics()
        testing.assert_allclose(mean, expected_mean)
        testing.assert_allclose(std, expected_std)