Ejemplo n.º 1
0
    def test_lwr_linear(self):
        """Test LWLR on random linear models of dimensions from 1 to 20.
         It should return exact results, give of take floating point imprecisions."""

        for cpp in [True, False]:
            if cpp:
                learners.enable_fastlearners(silent_fail=False)
            else:
                learners.disable_fastlearners()

            for _ in range(20):
                n = random.randint(1, 20)
                m = random.randint(1, 5)
                f = random_linear(n, m)
                cfg = {'m_channels'  : [learners.Channel('x_{}'.format(i), (0.0, 1.0))
                                        for i in range(n)],
                       's_channels'  : [learners.Channel('y_{}'.format(i), (0.0, 1.0))
                                        for i in range(m)],
                       'm_uniformize': True,
                       'sigma'       : 1.0}

                for learner in [learners.LWLRLearner(cfg), learners.ESLWLRLearner(cfg)]:

                    for _ in range(2*n):
                        x = np.random.rand(n)
                        y = f(x)
                        learner.update(tools.to_signal(x, cfg['m_channels']),
                                       tools.to_signal(y, cfg['s_channels']))

                    for _ in range(10):
                        x = np.random.rand(n).ravel()
                        y = f(x)
                        yp = learner.predict(tools.to_signal(x, cfg['m_channels']))
                        yp = tools.to_vector(yp, cfg['s_channels'])
                        self.assertTrue(np.allclose(y, yp, rtol = 1e-5, atol = 1e-5))
Ejemplo n.º 2
0
    def test_lwr_linear(self):
        """Test LWLR on random linear models of dimensions from 1 to 20.
         It should return exact results, give of take floating point imprecisions."""

        for cpp in [True, False]:
            if cpp:
                learners.enable_fastlearners(silent_fail=False)
            else:
                learners.disable_fastlearners()

            for _ in range(20):
                n = random.randint(1, 20)
                m = random.randint(1, 5)
                f = random_linear(n, m)
                cfg = {
                    'm_channels': [
                        learners.Channel('x_{}'.format(i), (0.0, 1.0))
                        for i in range(n)
                    ],
                    's_channels': [
                        learners.Channel('y_{}'.format(i), (0.0, 1.0))
                        for i in range(m)
                    ],
                    'm_uniformize':
                    True,
                    'sigma':
                    1.0
                }

                for learner in [
                        learners.LWLRLearner(cfg),
                        learners.ESLWLRLearner(cfg)
                ]:

                    for _ in range(2 * n):
                        x = np.random.rand(n)
                        y = f(x)
                        learner.update(tools.to_signal(x, cfg['m_channels']),
                                       tools.to_signal(y, cfg['s_channels']))

                    for _ in range(10):
                        x = np.random.rand(n).ravel()
                        y = f(x)
                        yp = learner.predict(
                            tools.to_signal(x, cfg['m_channels']))
                        yp = tools.to_vector(yp, cfg['s_channels'])
                        self.assertTrue(
                            np.allclose(y, yp, rtol=1e-5, atol=1e-5))
Ejemplo n.º 3
0
def bench_lwlr_linear_online(cpp, K=1000):
    """Test LWLR on random linear models of dimensions from 1 to 20.
     It should return exact results, give of take floating point imprecisions."""
    random.seed(0)
    np.random.seed(0)

    if cpp:
        learners.enable_fastlearners(silent_fail=False)
    else:
        learners.disable_fastlearners()

    for i in range(K):
        n = random.randint(1, 20)
        m = random.randint(1, 5)
        f = random_linear(n, m)
        cfg = {'m_channels'  : [learners.Channel('x_{}'.format(i), (0.0, 1.0))
                                for i in range(n)],
               's_channels'  : [learners.Channel('y_{}'.format(i), (0.0, 1.0))
                                for i in range(m)],
               'm_uniformize': True,
               'sigma'       : 1.0}

        for learner in [learners.LWLRLearner(cfg), learners.ESLWLRLearner(cfg)]:

            for i in range(50):
                x = np.random.rand(n)
                y = f(x)
                learner.update(learners.tools.to_signal(x, cfg['m_channels']),
                               learners.tools.to_signal(y, cfg['s_channels']))

            for i in range(950):
                x = np.random.rand(n)
                y = f(x)
                learner.update(learners.tools.to_signal(x, cfg['m_channels']),
                               learners.tools.to_signal(y, cfg['s_channels']))

                x = np.random.rand(n).ravel()
                y = f(x)
                yp = learner.predict(learners.tools.to_signal(x, cfg['m_channels']))
                yp = learners.tools.to_vector(yp, cfg['s_channels'])
                assert np.allclose(y, yp, rtol = 1e-5, atol = 1e-5)