Exemple #1
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."""
Exemple #2
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
    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."""
 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]
Exemple #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]
 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)
Exemple #7
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
 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
Exemple #9
0
 def test_index_with_node_ids(self):
     """Test a BiFlow indexed by keys."""
     pca_node = nodes.PCABiNode(node_id="pca")
     biflow = BiFlow([pca_node])
     x = biflow["pca"]
     assert x is pca_node
     assert pca_node in biflow
     assert 'pca' in biflow
    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)
Exemple #11
0
 def test_append_node_copy(self):
     """Test that appending a node does not perform a deept copy."""
     node1 = nodes.IdentityBiNode()
     node2 = nodes.IdentityBiNode()
     flow = BiFlow([node1])
     flow += node2
     assert flow[0] is node1
     assert type(flow) is BiFlow
Exemple #12
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
Exemple #13
0
    def trace_training(self, path, flow, x, msg=None, stop_msg=None,
                       trace_name="training", debug=False, **kwargs):
        """Trace a single training phase and the stop_training.

        Return a tuple containing a list of the training slide filenames, the
        training node ids and the same for stop_training.

        path -- Path were the inspection files will be stored.
        trace_name -- Name prefix for this inspection (default is training).
        **kwargs -- Additional arguments for flow.train can be specified
            as keyword arguments.
        """
        self._reset()
        self._trace_path = path
        # train and stop filenames must be different
        self._trace_name = trace_name + "_t"
        self._flow = flow
        self._tracing_decorator.decorate_flow(flow)
        biflownode = BiFlowNode(BiFlow(flow.flow))
        try:
            biflownode.train(x=x, msg=msg, **kwargs)
            # reset is important for the following stop_training
            biflownode.bi_reset()
        # Note: this also catches legacy string exceptions (which are still
        #    used in numpy, e.g. np.core.multiarray.error)
        except:
            if debug:
                # insert the error slide and encapsulate the exception
                traceback.print_exc()
                self._write_error_frame()
                result = (self._slide_filenames, self._slide_node_ids,
                          None, None)
                raise TraceDebugException(result=result)
            else:
                raise
        train_filenames = self._slide_filenames
        train_node_ids = self._slide_node_ids
        self._reset()
        self._trace_name = trace_name + "_s"
        try:
            biflownode.stop_training(stop_msg)
        except:
            if debug:
                # insert the error slide and encapsulate the exception
                traceback.print_exc()
                self._write_error_frame()
                result = (train_filenames, train_node_ids,
                          self._slide_filenames, self._slide_node_ids)
                raise TraceDebugException(result=result)
            else:
                raise
        stop_filenames = self._slide_filenames
        stop_node_ids = self._slide_node_ids
        # restore undecorated flow
        self._tracing_decorator.decorate_flow(flow, undecorate_mode=True)
        return train_filenames, train_node_ids, stop_filenames, stop_node_ids
Exemple #14
0
 def test_two_nodes2(self):
     """Test a TestBiFlowNode with two normal nodes using a normal Flow."""
     sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
     sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
     flownode = BiFlowNode(BiFlow([sfa_node, sfa2_node]))
     flow = mdp.Flow([flownode])
     data_iterables = [[n.random.random((30,10)) for _ in range(6)]]
     flow.train(data_iterables)
     x = n.random.random([100,10])
     flow.execute(x)
    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
Exemple #16
0
 def test_two_nodes1(self):
     """Test a TestBiFlowNode with two normal nodes."""
     sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
     sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
     flownode = BiFlowNode(BiFlow([sfa_node, sfa2_node]))
     for _ in range(2):
         for _ in range(6):
             flownode.train(n.random.random((30,10)))
         flownode.stop_training()
     x = n.random.random([100,10])
     flownode.execute(x)
    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
Exemple #18
0
 def test_pretrained_nodes(self):
     """Test a TestBiFlowNode with two normal pretrained nodes."""
     sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
     sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
     flownode = BiFlowNode(BiFlow([sfa_node, sfa2_node]))
     flow = mdp.Flow([flownode])
     data_iterables = [[n.random.random((30,10)) for _ in range(6)]]
     flow.train(data_iterables)
     pretrained_flow = flow[0]._flow
     biflownode = BiFlowNode(pretrained_flow)
     x = n.random.random([100,10])
     biflownode.execute(x)
 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
Exemple #20
0
 def test_wrong_argument_handling(self):
     """Test correct error for additional arguments in Node instance."""
     samples = mdp.numx_rand.random((100, 10))
     labels = mdp.numx.arange(100)
     # labels argument of FDANode is not supported in biflow
     flow = BiFlow([mdp.nodes.PCANode(), mdp.nodes.FDANode()])
     # the iterables are passed as if this were a normal Flow
     pytest.raises(BiFlowException, flow.train,
                   [[samples], [samples, labels]])
     # messing up the data iterables further doesn't matter, this is
     # actually interpreted as three data chunks for the FDANode training,
     # since argument iterables are not supported by BiFlow
     pytest.raises(BiFlowException, flow.train,
                   [[samples], [samples, labels, labels]])
Exemple #21
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
Exemple #22
0
 def test_use_copies_msg_flownode(self):
     """Test the correct reaction to an outgoing use_copies message."""
     stop_result = ({"clonelayer" + MSG_ID_SEP + "use_copies": True},
                    EXIT_TARGET)
     stop_sfa_node = SFABiNode(stop_result=stop_result,
                               input_dim=10, output_dim=3)
     biflownode = BiFlowNode(BiFlow([stop_sfa_node]))
     clonelayer = CloneBiLayer(node=biflownode,
                               n_nodes=3,
                               use_copies=False,
                               node_id="clonelayer")
     biflow = clonelayer + IdentityBiNode()
     x = n.random.random((100,30))
     biflow.train(x)
     assert clonelayer.use_copies is True
 def test_use_copies_msg(self):
     """Test the correct reaction to an outgoing use_copies message."""
     stop_result = ({
         "clonelayer" + MSG_ID_SEP + "use_copies": True
     }, EXIT_TARGET)
     stop_sfa_node = SFABiNode(stop_result=stop_result,
                               input_dim=10,
                               output_dim=3)
     biflownode = BiFlowNode(BiFlow([stop_sfa_node]))
     clonelayer = ParallelCloneBiLayer(node=biflownode,
                                       n_nodes=3,
                                       use_copies=False,
                                       node_id="clonelayer")
     data = [[n.random.random((100, 30)) for _ in range(5)]]
     biflow = ParallelBiFlow([clonelayer])
     biflow.train(data, scheduler=mdp.parallel.Scheduler())
     assert clonelayer.use_copies is True
Exemple #24
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)
Exemple #25
0
    def setup_parallel_training(self,
                                data_iterables,
                                msg_iterables=None,
                                stop_messages=None,
                                train_callable_class=BiFlowTrainCallable):
        """Prepare the flow for handing out tasks to do the training.

        After calling setup_parallel_training one has to pick up the
        tasks with get_task, run them and finally return the results via
        use_results. tasks are available as long as task_available is
        True. Training may require multiple phases, which are each closed by
        calling use_results.

        data_iterables -- A list of iterables, one for each node in the flow.
            The iterators returned by the iterables must
            return data arrays that are then used for the node training.
            See Flow.train for more details.
            If a custom train_callable_class is used to preprocess the data
            then other data types can be used as well.
        msg_iterables - A list of iterables for the messages. Can also be
            a single message if data_iterables is a single array.
        stop_messages -- Sequence of messages for stop_training.
        train_callable_class -- Class used to create training callables for the
            scheduler. By specifying your own class you can implement data
            transformations before the data is actually fed into the flow
            (e.g. from 8 bit image to 64 bit double precision).
            Note that the train_callable_class is only used if a scheduler was
            provided. If a scheduler is provided the default class used is
            NodeResultContainer.
        """
        self._bi_reset()  # normally not required, just for safety
        if self.is_parallel_training:
            err = "Parallel training is already underway."
            raise ParallelBiFlowException(err)
        self._train_callable_class = train_callable_class
        data_iterables, msg_iterables = self._sanitize_training_iterables(
            data_iterables=data_iterables, msg_iterables=msg_iterables)
        self._train_data_iterables = data_iterables
        self._train_msg_iterables = msg_iterables
        if stop_messages is None:
            stop_messages = [None] * len(data_iterables)
        self._stop_messages = stop_messages
        self._flownode = BiFlowNode(BiFlow(self.flow))
        self._i_train_node = 0
        self._next_train_phase()
Exemple #26
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)
Exemple #27
0
 def test_training_targets(self):
     """Test targeting during training and stop_training."""
     tracelog = []
     verbose = False
     node1 = TraceJumpBiNode(output_dim=1,
                             tracelog=tracelog,
                             node_id="node_1",
                             train_results=[[None]],
                             stop_train_results=[None],
                             execute_results=[
                                 None,
                                 (None, {
                                     "b": 2
                                 }, "node_3"),
                                 (None, {
                                     "b": 2
                                 }, EXIT_TARGET),
                             ],
                             verbose=verbose)
     node2 = TraceJumpBiNode(output_dim=1,
                             tracelog=tracelog,
                             node_id="node_2",
                             train_results=[[None]],
                             stop_train_results=[(None, {
                                 "b": 2
                             }, "node_1")],
                             execute_results=[
                                 None, (None, None, "node_1"),
                                 (None, None, "node_1")
                             ],
                             verbose=verbose)
     node3 = TraceJumpBiNode(output_dim=1,
                             tracelog=tracelog,
                             node_id="node_3",
                             train_results=[[(None, {
                                 "a": 1
                             }, "node_2"), None]],
                             stop_train_results=[(None, {
                                 "a": 1
                             }, "node_2")],
                             execute_results=[(None, {
                                 "b": 2
                             }, EXIT_TARGET)],
                             verbose=verbose)
     biflow = BiFlow([node1, node2, node3])
     data_iterables = [[np.random.random((1, 1)) for _ in range(2)],
                       [np.random.random((1, 1)) for _ in range(2)],
                       [np.random.random((1, 1)) for _ in range(2)]]
     biflow.train(data_iterables)
     # print ",\n".join(str(log) for log in tracelog)
     # tracelog reference
     reference = [('node_1', 'bi_reset'), ('node_2', 'bi_reset'),
                  ('node_3', 'bi_reset'), ('node_1', 'train'),
                  ('node_1', 'bi_reset'), ('node_2', 'bi_reset'),
                  ('node_3', 'bi_reset'), ('node_1', 'train'),
                  ('node_1', 'bi_reset'), ('node_2', 'bi_reset'),
                  ('node_3', 'bi_reset'), ('node_1', 'stop_training'),
                  ('node_1', 'bi_reset'), ('node_2', 'bi_reset'),
                  ('node_3', 'bi_reset'), ('node_1', 'execute'),
                  ('node_2', 'train'), ('node_1', 'bi_reset'),
                  ('node_2', 'bi_reset'), ('node_3', 'bi_reset'),
                  ('node_1', 'execute'), ('node_2', 'train'),
                  ('node_1', 'bi_reset'), ('node_2', 'bi_reset'),
                  ('node_3', 'bi_reset'), ('node_2', 'stop_training'),
                  ('node_1', 'execute'), ('node_2', 'execute'),
                  ('node_3', 'execute'), ('node_1', 'bi_reset'),
                  ('node_2', 'bi_reset'), ('node_3', 'bi_reset'),
                  ('node_1', 'execute'), ('node_2', 'execute'),
                  ('node_3', 'train'), ('node_2', 'execute'),
                  ('node_1', 'execute'), ('node_3', 'train'),
                  ('node_1', 'bi_reset'), ('node_2', 'bi_reset'),
                  ('node_3', 'bi_reset'), ('node_1', 'execute'),
                  ('node_2', 'execute'), ('node_3', 'train'),
                  ('node_2', 'execute'), ('node_1', 'execute'),
                  ('node_3', 'train'), ('node_1', 'bi_reset'),
                  ('node_2', 'bi_reset'), ('node_3', 'bi_reset'),
                  ('node_3', 'stop_training'), ('node_2', 'execute'),
                  ('node_3', 'execute'), ('node_1', 'bi_reset'),
                  ('node_2', 'bi_reset'), ('node_3', 'bi_reset')]
     assert tracelog == reference
Exemple #28
0
    def use_results(self, results):
        """Use the result from the scheduler.

        During parallel training this will start the next training phase.
        For parallel execution this will return the result, like a normal
        execute would. In addition it will join any forked nodes.

        results -- Iterable containing the results, normally the return value
            of scheduler.ResultContainer.get_results().
            The individual results can be the return values of the tasks.
        """
        if self.is_parallel_training:
            for result in results:
                self._flownode.join(result)
            # perform local stop_training with result check
            self._stop_training_hook()
            result = self._flownode.stop_training(
                self._stop_messages[self._i_train_node])
            self._post_stop_training_hook()
            if (result is not None):
                target = result[2]
                # values of +1, -1 and EXIT_TARGET are tolerated
                if target not in [1, -1, EXIT_TARGET]:
                    err = ("Target node not found in flow during " +
                           "stop_training phase, last result: " + str(result))
                    raise BiFlowException(err)
            self._flownode.bi_reset()
            if self.verbose:
                print("finished parallel training phase of node no. " +
                      "%d in parallel flow" % (self._i_train_node + 1))
            if not self.flow[self._i_train_node].is_training():
                self._i_train_node += 1
            self._next_train_phase()
        elif self.is_parallel_executing:
            self._exec_data_iterator = None
            self._exec_msg_iterator = None
            self._exec_target_iterator = None
            y_results = []
            msg_results = MessageResultContainer()
            # use internal flownode to join all biflownodes
            self._flownode = BiFlowNode(BiFlow(self.flow))
            for result_tuple in results:
                result, forked_biflownode = result_tuple
                # consolidate results
                if isinstance(result, tuple) and (len(result) == 2):
                    y, msg = result
                    msg_results.add_message(msg)
                else:
                    y = result
                if y is not None:
                    try:
                        y_results.append(y)
                    except:
                        err = "Some but not all y return values were None."
                        raise BiFlowException(err)
                else:
                    y_results = None
                # join biflownode
                if forked_biflownode is not None:
                    self._flownode.join(forked_biflownode)
            # return results
            if y_results is not None:
                y_results = n.concatenate(y_results)
            return (y_results, msg_results.get_message())
        else:
            err = "It seems that there are no results to retrieve."
            raise BiFlowException(err)
Exemple #29
0
 def test_fda_binode(self):
     """Test using the FDABiNode in a BiFlow."""
     samples = mdp.numx_rand.random((100, 10))
     labels = mdp.numx.arange(100)
     flow = BiFlow([mdp.nodes.PCANode(), nodes.FDABiNode()])
     flow.train([[samples], [samples]], [None, [{"labels": labels}]])
Exemple #30
0
    def execute(self,
                iterable=None,
                msg_iterable=None,
                target_iterable=None,
                scheduler=None,
                execute_callable_class=None,
                overwrite_result_container=True):
        """Execute the flow and return (y, msg).

        If a scheduler is provided the execution will be done in parallel on
        the scheduler.

        iterable -- Single array or iterable.
        msg_iterable -- Single message or iterable.
        target_iterable -- Single target or iterable.
        scheduler -- Value can be either None for normal execution (default
            value) or a Scheduler instance for parallel execution with the
            scheduler.
        execute_callable_class -- Class used to create execution callables for
            the scheduler. By specifying your own class you can implement data
            transformations before the data is actually fed into the flow
            (e.g. from 8 bit image to 64 bit double precision).
            Note that the execute_callable_class is only used if a scheduler was
            provided. If a scheduler is provided the default class used is
            NodeResultContainer.
        overwrite_result_container -- If set to True (default value) then
            the result container in the scheduler will be overwritten with an
            instance of OrderedResultContainer, if it is not already an
            instance of OrderedResultContainer.
        """
        if self.is_parallel_training:
            raise ParallelBiFlowException("Parallel training is underway.")
        if scheduler is None:
            if execute_callable_class is not None:
                err = ("A execute_callable_class was specified but no "
                       "scheduler was given, so the execute_callable_class "
                       "has no effect.")
                raise ParallelBiFlowException(err)
            return super(ParallelBiFlow, self).execute(iterable, msg_iterable,
                                                       target_iterable)
        if execute_callable_class is None:
            execute_callable_class = BiFlowExecuteCallable
        # check that the scheduler is compatible
        if overwrite_result_container:
            if not isinstance(scheduler.result_container,
                              parallel.ExecuteResultContainer):
                scheduler.result_container = parallel.ExecuteResultContainer()
        # do parallel execution
        self._flownode = BiFlowNode(BiFlow(self.flow))
        try:
            self.setup_parallel_execution(
                iterable=iterable,
                msg_iterable=msg_iterable,
                target_iterable=target_iterable,
                execute_callable_class=execute_callable_class)
            while self.task_available:
                task = self.get_task()
                scheduler.add_task(*task)
            result = self.use_results(scheduler.get_results())
        finally:
            # reset remaining iterator references, which cannot be pickled
            self._exec_data_iterator = None
            self._exec_msg_iterator = None
            self._exec_target_iterator = None
        return result
 def test_training_targets(self):
     """Test targeting during training and stop_training."""
     tracelog = []
     verbose = False
     node1 = TraceJumpBiNode(
                 output_dim=1,
                 tracelog=tracelog,
                 node_id="node_1",
                 train_results=[[None]],
                 stop_train_results=[None],
                 execute_results=[None, (None, {"b": 2}, "node_3"),
                                  (None, {"b": 2}, EXIT_TARGET),],
                 verbose=verbose)
     node2 = TraceJumpBiNode(
                 output_dim=1,
                 tracelog=tracelog,
                 node_id="node_2",
                 train_results=[[None]],
                 stop_train_results=[(None, {"b": 2}, "node_1")],
                 execute_results=[None, (None, None, "node_1"),
                                  (None, None, "node_1")],
                 verbose=verbose)
     node3 = TraceJumpBiNode(
                 output_dim=1,
                 tracelog=tracelog,
                 node_id="node_3",
                 train_results=[[(None, {"a": 1}, "node_2"), None]],
                 stop_train_results=[(None, {"a": 1}, "node_2")],
                 execute_results=[(None, {"b": 2}, EXIT_TARGET)],
                 verbose=verbose)
     biflow = BiFlow([node1, node2, node3])
     data_iterables = [[np.random.random((1,1)) for _ in range(2)],
                       [np.random.random((1,1)) for _ in range(2)],
                       [np.random.random((1,1)) for _ in range(2)]]
     biflow.train(data_iterables)
     # print ",\n".join(str(log) for log in tracelog)
     # tracelog reference
     reference = [
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'train'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'train'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'stop_training'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'execute'),
         ('node_2', 'train'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'execute'),
         ('node_2', 'train'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_2', 'stop_training'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_3', 'execute'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_3', 'train'),
         ('node_2', 'execute'),
         ('node_1', 'execute'),
         ('node_3', 'train'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_1', 'execute'),
         ('node_2', 'execute'),
         ('node_3', 'train'),
         ('node_2', 'execute'),
         ('node_1', 'execute'),
         ('node_3', 'train'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset'),
         ('node_3', 'stop_training'),
         ('node_2', 'execute'),
         ('node_3', 'execute'),
         ('node_1', 'bi_reset'),
         ('node_2', 'bi_reset'),
         ('node_3', 'bi_reset')
     ]
     assert tracelog == reference
 def test_fda_binode(self):
     """Test using the FDABiNode in a BiFlow."""
     samples = mdp.numx_rand.random((100,10))
     labels = mdp.numx.arange(100)
     flow = BiFlow([mdp.nodes.PCANode(), nodes.FDABiNode()])
     flow.train([[samples],[samples]], [None,[{"labels": labels}]])