Example #1
0
 def test_normal_flow(self):
     """Test a BiFlow with normal nodes."""
     flow = BiFlow([mdp.nodes.SFANode(output_dim=5),
                    mdp.nodes.PolynomialExpansionNode(degree=3),
                    mdp.nodes.SFANode(output_dim=20)])
     data_iterables = [[np.random.random((20,10)) for _ in range(6)],
                       None,
                       [np.random.random((20,10)) for _ in range(6)]]
     flow.train(data_iterables)
     x = np.random.random([100,10])
     flow.execute(x)
Example #2
0
 def test_exit_target(self):
     """Test that the magic exit target works."""
     tracelog = []
     node1 = TraceJumpBiNode(tracelog=tracelog,
                             execute_results=[(None, None, EXIT_TARGET)],
                             verbose=False)
     node2 = IdNode()
     biflow = BiFlow([node1, node2])
     biflow.execute(None, {"a": 1})
     # bimdp.show_execution(biflow, x=None, msg={"a": 1}, debug=True)
     reference = [(None, 'bi_reset'), (None, 'execute'), (None, 'bi_reset')]
     assert tracelog == reference
Example #3
0
 def test_normal_flow(self):
     """Test a BiFlow with normal nodes."""
     flow = BiFlow([
         mdp.nodes.SFANode(output_dim=5),
         mdp.nodes.PolynomialExpansionNode(degree=3),
         mdp.nodes.SFANode(output_dim=20)
     ])
     data_iterables = [[np.random.random((20, 10)) for _ in range(6)], None,
                       [np.random.random((20, 10)) for _ in range(6)]]
     flow.train(data_iterables)
     x = np.random.random([100, 10])
     flow.execute(x)
Example #4
0
 def test_exit_target(self):
     """Test that the magic exit target works."""
     tracelog = []
     node1 = TraceJumpBiNode(
                 tracelog=tracelog,
                 execute_results=[(None, None, EXIT_TARGET)],
                 verbose=False)
     node2 = IdNode()
     biflow = BiFlow([node1, node2])
     biflow.execute(None, {"a": 1})
     # bimdp.show_execution(biflow, x=None, msg={"a": 1}, debug=True)
     reference = [
        (None, 'bi_reset'), (None, 'execute'), (None, 'bi_reset')
     ]
     assert tracelog == reference
Example #5
0
 def test_msg_normal_node(self):
     """Test that the msg is passed over a normal node."""
     node = IdNode()
     biflow = BiFlow([node])
     msg = {"a": 1}
     result = biflow.execute(np.random.random((1,1)), msg)
     assert msg == result[1]
Example #6
0
    def test_codecorator(self):
        """Test basic codecorator functionality."""

        class CoroutineBiNode(BiNode):

            @staticmethod
            def is_trainable():
                return False

            @binode_coroutine(["alpha", "beta"])
            def _execute(self, x, alpha):
                """Blabla."""
                x, alpha, beta = yield (x, {"alpha": alpha, "beta": 2},
                                        self.node_id)
                x, alpha, beta = yield (x, {"alpha": alpha+1, "beta": beta+2},
                                        self.node_id)
                yield x, {"alpha": alpha, "beta": beta}

        node = CoroutineBiNode(node_id="conode")
        flow = BiFlow([node])
        x = n.random.random((3,2))
        y, msg = flow.execute(x, {"alpha": 3})
        assert msg["alpha"] == 4
        assert msg["beta"] == 4
        assert node.execute.__doc__ == """Blabla."""
Example #7
0
    def test_codecorator(self):
        """Test basic codecorator functionality."""

        class CoroutineBiNode(BiNode):

            @staticmethod
            def is_trainable():
                return False

            @binode_coroutine(["alpha", "beta"])
            def _execute(self, x, alpha):
                """Blabla."""
                x, alpha, beta = yield (x, {"alpha": alpha, "beta": 2},
                                        self.node_id)
                x, alpha, beta = yield (x, {"alpha": alpha+1, "beta": beta+2},
                                        self.node_id)
                yield x, {"alpha": alpha, "beta": beta}

        node = CoroutineBiNode(node_id="conode")
        flow = BiFlow([node])
        x = n.random.random((3,2))
        y, msg = flow.execute(x, {"alpha": 3})
        assert msg["alpha"] == 4
        assert msg["beta"] == 4
        assert node.execute.__doc__ == """Blabla."""
Example #8
0
 def test_msg_normal_node(self):
     """Test that the msg is passed over a normal node."""
     node = IdNode()
     biflow = BiFlow([node])
     msg = {"a": 1}
     result = biflow.execute(np.random.random((1, 1)), msg)
     assert msg == result[1]
Example #9
0
    def test_codecorator2(self):
        """Test codecorator functionality with StopIteration."""
        class CoroutineBiNode(BiNode):
            @staticmethod
            def is_trainable():
                return False

            @binode_coroutine(["alpha", "beta"])
            def _execute(self, x, alpha):
                x, alpha, beta = yield (x, {
                    "alpha": alpha,
                    "beta": 2
                }, self.node_id)
                x, alpha, beta = yield (x, {
                    "alpha": alpha + 1,
                    "beta": beta + 2
                }, self.node_id)
                raise StopIteration(x, {"alpha": alpha, "beta": beta})

        node = CoroutineBiNode(node_id="conode")
        flow = BiFlow([node])
        x = n.random.random((3, 2))
        y, msg = flow.execute(x, {"alpha": 3})
        assert msg["alpha"] == 4
        assert msg["beta"] == 4
Example #10
0
    def test_normal_multiphase(self):
        """Test training and execution with multiple training phases.

        The node with multiple training phases is a hinet.FlowNode.
        """
        sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
        sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
        flownode = mdp.hinet.FlowNode(mdp.Flow([sfa_node, sfa2_node]))
        flow = BiFlow([flownode,
                       mdp.nodes.PolynomialExpansionNode(degree=2),
                       mdp.nodes.SFANode(output_dim=5)])
        data_iterables = [[np.random.random((30,10)) for _ in range(6)],
                          None,
                          [np.random.random((30,10)) for _ in range(6)]]
        flow.train(data_iterables)
        x = np.random.random([100,10])
        flow.execute(x)
Example #11
0
    def test_normal_multiphase(self):
        """Test training and execution with multiple training phases.

        The node with multiple training phases is a hinet.FlowNode.
        """
        sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
        sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
        flownode = mdp.hinet.FlowNode(mdp.Flow([sfa_node, sfa2_node]))
        flow = BiFlow([
            flownode,
            mdp.nodes.PolynomialExpansionNode(degree=2),
            mdp.nodes.SFANode(output_dim=5)
        ])
        data_iterables = [[np.random.random((30, 10)) for _ in range(6)], None,
                          [np.random.random((30, 10)) for _ in range(6)]]
        flow.train(data_iterables)
        x = np.random.random([100, 10])
        flow.execute(x)
Example #12
0
 def test_execute_jump(self):
     """Test jumping around during execution."""
     tracelog = []
     verbose = False
     node1 = TraceJumpBiNode(
                 tracelog=tracelog,
                 node_id="node_1",
                 execute_results=[(None, None, "node_3"),
                                  (None, None, "node_2")],
                 verbose=verbose)
     node2 = TraceJumpBiNode(
                 tracelog=tracelog,
                 node_id="node_2",
                 execute_results=[(None, None, "node_1")],
                 verbose=verbose)
     node3 = TraceJumpBiNode(
                 tracelog=tracelog,
                 node_id="node_3",
                 execute_results=[(None, None, "node_1")],
                 verbose=verbose)
     biflow = BiFlow([node1, node2, node3])
     biflow.execute(None, {"a": 1})
     # bimdp.show_execution(biflow, x=None, msg={"a": 1}, debug=True)
     # tracelog reference
     reference = [
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'execute'),
         ('node_3', 'execute'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_3', 'execute'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
     ]
     assert tracelog == reference
Example #13
0
 def test_execute_jump(self):
     """Test jumping around during execution."""
     tracelog = []
     verbose = False
     node1 = TraceJumpBiNode(tracelog=tracelog,
                             node_id="node_1",
                             execute_results=[(None, None, "node_3"),
                                              (None, None, "node_2")],
                             verbose=verbose)
     node2 = TraceJumpBiNode(tracelog=tracelog,
                             node_id="node_2",
                             execute_results=[(None, None, "node_1")],
                             verbose=verbose)
     node3 = TraceJumpBiNode(tracelog=tracelog,
                             node_id="node_3",
                             execute_results=[(None, None, "node_1")],
                             verbose=verbose)
     biflow = BiFlow([node1, node2, node3])
     biflow.execute(None, {"a": 1})
     # bimdp.show_execution(biflow, x=None, msg={"a": 1}, debug=True)
     # tracelog reference
     reference = [
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'execute'),
         ('node_3', 'execute'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_3', 'execute'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
     ]
     assert tracelog == reference
Example #14
0
    def test_codecorator_defaults(self):
        """Test codecorator argument default values."""
        class CoroutineBiNode(BiNode):
            @staticmethod
            def is_trainable():
                return False

            @binode_coroutine(["alpha", "beta"], defaults=(7, 8))
            def _execute(self, x):
                x, alpha, beta = yield (x, None, self.node_id)
                raise StopIteration(x, {"alpha": alpha, "beta": beta})

        node = CoroutineBiNode(node_id="conode")
        flow = BiFlow([node])
        x = n.random.random((3, 2))
        y, msg = flow.execute(x)
        assert msg["alpha"] == 7
        assert msg["beta"] == 8
Example #15
0
    def test_codecorator_defaults(self):
        """Test codecorator argument default values."""

        class CoroutineBiNode(BiNode):
            @staticmethod
            def is_trainable():
                return False

            @binode_coroutine(["alpha", "beta"], defaults=(7, 8))
            def _execute(self, x):
                x, alpha, beta = yield (x, None, self.node_id)
                raise StopIteration(x, {"alpha": alpha, "beta": beta})

        node = CoroutineBiNode(node_id="conode")
        flow = BiFlow([node])
        x = n.random.random((3, 2))
        y, msg = flow.execute(x)
        assert msg["alpha"] == 7
        assert msg["beta"] == 8
Example #16
0
    def test_codecorator2(self):
        """Test codecorator functionality with StopIteration."""

        class CoroutineBiNode(BiNode):
            @staticmethod
            def is_trainable():
                return False

            @binode_coroutine(["alpha", "beta"])
            def _execute(self, x, alpha):
                x, alpha, beta = yield (x, {"alpha": alpha, "beta": 2}, self.node_id)
                x, alpha, beta = yield (x, {"alpha": alpha + 1, "beta": beta + 2}, self.node_id)
                raise StopIteration(x, {"alpha": alpha, "beta": beta})

        node = CoroutineBiNode(node_id="conode")
        flow = BiFlow([node])
        x = n.random.random((3, 2))
        y, msg = flow.execute(x, {"alpha": 3})
        assert msg["alpha"] == 4
        assert msg["beta"] == 4