Пример #1
0
def testContextManager1():
    """Test that the context manager activates extensions."""

    class Test1ExtensionNode(mdp.ExtensionNode):
        extension_name = "__test1"
        def _testtest(self):
            pass
    class Test2ExtensionNode(mdp.ExtensionNode):
        extension_name = "__test2"
        def _testtest(self):
            pass

    assert mdp.get_active_extensions() == []
    with mdp.extension('__test1'):
        assert mdp.get_active_extensions() == ['__test1']
    assert mdp.get_active_extensions() == []
    # with multiple extensions
    with mdp.extension(['__test1', '__test2']):
        active = mdp.get_active_extensions()
        assert '__test1' in active
        assert '__test2' in active
    assert mdp.get_active_extensions() == []
    mdp.activate_extension("__test1")
    # Test that only activated extensions are deactiveted.
    with mdp.extension(['__test1', '__test2']):
        active = mdp.get_active_extensions()
        assert '__test1' in active
        assert '__test2' in active
    assert mdp.get_active_extensions() == ["__test1"]
def test_simple_extension():

    class TestExtensionNode(mdp.ExtensionNode, mdp.nodes.IdentityNode):
        extension_name = "__test"
        def execute(self, x):
            self.foo = 42
            return self._non_extension_execute(x)

    class Dummy(mdp.nodes.IdentityNode):
        def _execute(self, x):
            return 42

    node = mdp.nodes.IdentityNode()
    assert mdp.numx.all(node.execute(X) == X)
    assert not hasattr(node,'foo')

    with mdp.extension("__test"):
        assert mdp.numx.all(node.execute(X) == X)
        assert hasattr(node,'foo')

    node = Dummy()
    assert not hasattr(node,'foo')
    assert node.execute(X) == 42

    with mdp.extension("__test"):
        assert node.execute(X) == 42
        assert hasattr(node,'foo')
Пример #3
0
def testContextManager1():
    """Test that the context manager activates extensions."""
    class Test1ExtensionNode(mdp.ExtensionNode):
        extension_name = "__test1"

        def _testtest(self):
            pass

    class Test2ExtensionNode(mdp.ExtensionNode):
        extension_name = "__test2"

        def _testtest(self):
            pass

    assert mdp.get_active_extensions() == []
    with mdp.extension('__test1'):
        assert mdp.get_active_extensions() == ['__test1']
    assert mdp.get_active_extensions() == []
    # with multiple extensions
    with mdp.extension(['__test1', '__test2']):
        active = mdp.get_active_extensions()
        assert '__test1' in active
        assert '__test2' in active
    assert mdp.get_active_extensions() == []
    mdp.activate_extension("__test1")
    # Test that only activated extensions are deactiveted.
    with mdp.extension(['__test1', '__test2']):
        active = mdp.get_active_extensions()
        assert '__test1' in active
        assert '__test2' in active
    assert mdp.get_active_extensions() == ["__test1"]
Пример #4
0
 def test_switchboard_gradient2(self):
     """Test gradient for a larger switchboard."""
     dim = 100
     connections = [int(i) for i in numx.random.random((dim,)) * (dim-1)]
     sboard = mdp.hinet.Switchboard(input_dim=dim, connections=connections)
     x = numx.random.random((10, dim))
     # assume a 5-dimensional gradient at this stage
     grad = numx.random.random((10, dim, 5))
     # original reference implementation
     def _switchboard_grad(self, x):
         grad = numx.zeros((self.output_dim, self.input_dim))
         grad[range(self.output_dim), self.connections] = 1
         return numx.tile(grad, (len(x), 1, 1))
     with mdp.extension("gradient"):
         result = sboard._gradient(x, grad)
         ext_grad = result[1]["grad"]
         tmp_grad = _switchboard_grad(sboard, x)
         ref_grad = numx.asarray([numx.dot(tmp_grad[i], grad[i])
                                  for i in range(len(tmp_grad))])
     assert numx.all(ext_grad == ref_grad)
Пример #5
0
    def _newton(self, y_goal, n_iterations, x_start, msg):
        """Try to reach the given y value with gradient descent.

        The Newton method is used to calculate the next point.
        """
        # can't use function decorator, since this is a coroutine
        with mdp.extension("gradient"):
            # get the y value for the output
            msg = {self.node_id + "->method": "newton", "method": "gradient"}
            y, grad, x, _ = yield x_start, msg.copy(), self.sender_id
            for _ in xrange(n_iterations):
                # use Newton's method to get the new data point
                error = np.sum((y - y_goal) ** 2, axis=1)
                error_grad = np.sum(2 * (y - y_goal)[:,:,np.newaxis] * grad,
                                    axis=1)
                err_grad_norm = np.sqrt(np.sum(error_grad**2, axis=1))
                unit_error_grad = error_grad / err_grad_norm[:,np.newaxis]
                # x_{n+1} = x_n - f(x_n) / f'(x_n)
                x = x - (error / err_grad_norm)[:,np.newaxis] * unit_error_grad
                y, grad, x, _ = yield x, msg.copy(), self.sender_id
            raise StopIteration(x, None, "exit")
Пример #6
0
    def test_switchboard_gradient2(self):
        """Test gradient for a larger switchboard."""
        dim = 100
        connections = [int(i) for i in numx.random.random((dim, )) * (dim - 1)]
        sboard = mdp.hinet.Switchboard(input_dim=dim, connections=connections)
        x = numx.random.random((10, dim))
        # assume a 5-dimensional gradient at this stage
        grad = numx.random.random((10, dim, 5))

        # original reference implementation
        def _switchboard_grad(self, x):
            grad = numx.zeros((self.output_dim, self.input_dim))
            grad[range(self.output_dim), self.connections] = 1
            return numx.tile(grad, (len(x), 1, 1))

        with mdp.extension("gradient"):
            result = sboard._gradient(x, grad)
            ext_grad = result[1]["grad"]
            tmp_grad = _switchboard_grad(sboard, x)
            ref_grad = numx.asarray(
                [numx.dot(tmp_grad[i], grad[i]) for i in range(len(tmp_grad))])
        assert numx.all(ext_grad == ref_grad)
# -*- coding: utf-8 -*-
# Generated by codesnippet sphinx extension on 2020-11-22

import mdp
import numpy as np
np.random.seed(0)
mdp.activate_extension("parallel")
mdp.deactivate_extension("parallel")

with mdp.extension("parallel"):
    pass


@mdp.with_extension("parallel")
def f():
    pass


class ParallelSFANode(mdp.parallel.ParallelExtensionNode, mdp.nodes.SFANode):
    def _fork(self):
        # implement the forking for SFANode
        return ...

    def _join(self):
        # implement the joining for SFANode
        return ...


@mdp.extension_method("parallel", mdp.nodes.SFANode)
def _fork(self):
    return ...