예제 #1
0
    def _check_train_args(self, x, labels):
        if (isinstance(labels, (list, tuple, numx.ndarray))
                and len(labels) != x.shape[0]):
            msg = ("The number of labels should be equal to the number of "
                   "datapoints (%d != %d)" % (len(labels), x.shape[0]))
            raise mdp.TrainingException(msg)

        if (not isinstance(labels, (list, tuple, numx.ndarray))):
            labels = [labels]

        if (not numx.all(map(lambda x: abs(x) == 1, labels))):
            msg = "The labels must be either -1 or 1."
            raise mdp.TrainingException(msg)
예제 #2
0
 def _check_train_args(self, x, labels):
     if isinstance(
             labels,
         (list, tuple, numx.ndarray)) and (len(labels) != x.shape[0]):
         msg = ("The number of labels should be equal to the number of "
                "datapoints (%d != %d)" % (len(labels), x.shape[0]))
         raise mdp.TrainingException(msg)
예제 #3
0
    def _gradient(self, x, grad=None):
        """Calculate the contribution to the grad for this node at point x.

        The contribution is then combined with the given gradient, to get
        the gradient for the original x.

        This is a template function, derived classes should override _get_grad.
        """
        if self.is_training():
            raise mdp.TrainingException("The training is not completed yet.")
        if grad is None:
            grad = np.zeros((len(x), self.input_dim, self.input_dim))
            diag_indices = np.arange(self.input_dim)
            grad[:, diag_indices, diag_indices] = 1.0
        new_grad = self._get_grad(x)
        # combine the gradients
        grad = np.asarray(
            [np.dot(new_grad[i], grad[i]) for i in range(len(new_grad))])
        # update the x value for the next node
        result = self._execute(x)
        if isinstance(result, tuple):
            x = result[0]
            msg = result[1]
        else:
            x = result
            msg = {}
        msg.update({"grad": grad})
        return x, msg
예제 #4
0
 def _check_train_args(self, x, *args, **kwargs):
     if self.training_type == 'batch':
         # check that we have at least 2 time samples for batch training
         if x.shape[0] < 2:
             raise mdp.TrainingException(
                 "Need at least 2 time samples for 'batch' training type (%d given)"
                 % (x.shape[0]))
예제 #5
0
 def _check_train_args(self, x, labels):
     super(ClassifierCumulator, self)._check_train_args(x, labels)
     if (isinstance(labels, (list, tuple, numx.ndarray)) and
         len(labels) != x.shape[0]):
         msg = ("The number of labels must be equal to the number of "
                "datapoints (%d != %d)" % (len(labels), x.shape[0]))
         raise mdp.TrainingException(msg)
예제 #6
0
 def _stop_training(self):
     #print "PFA stop_training "+str(self.sindex)+" of "+str(self.maxindex)+"   layer "+str(self.layerIndex)+" of "+str(self.layerMax)
     if self.endData is None:
         raise mdp.TrainingException("train was never called")
     self._prepare_start_end_cor()
     self._start_end_cor_clear_mean()
     self.calc_PFA()
예제 #7
0
    def stop_training(self, msg=None):
        """Stop training phase and start an execute phase with a target.

        The possible return types are None, y, (y, msg), (y, msg, target).
        For None nothing more happens, the training phase ends like for a
        standard MDP node.
        If a return value is given then an excute phase is started.

        This template method normally calls a _stop_training method from
        self._train_seq.

        If a stop_result was given in __init__ then it is used but can be
        overwritten by the returned _stop_training result or by the 
        msg argument provided by the BiFlow.
        """
        # basic checks
        if self.is_training() and self._train_phase_started == False:
            raise mdp.TrainingException("The node has not been trained.")
        if not self.is_training():
            err = "The training phase has already finished."
            raise mdp.TrainingFinishedException(err)
        # call stop_training
        if not msg:
            result = self._train_seq[self._train_phase][1]()
            target = None
        else:
            msg_id_keys = self._get_msg_id_keys(msg)
            target = self._extract_message_key("target", msg, msg_id_keys)
            method_name = self._extract_message_key("method", msg, msg_id_keys)
            default_method = self._train_seq[self._train_phase][1]
            method, target = self._get_method(method_name, default_method,
                                              target)
            msg, arg_dict = self._extract_method_args(method, msg, msg_id_keys)
            result = method(**arg_dict)
        # close the current phase
        self._train_phase += 1
        self._train_phase_started = False
        # check if we have some training phase left
        if self.get_remaining_train_phase() == 0:
            self._training = False
        # use stored stop message and update it with the result
        if self._stop_result:
            if self.has_multiple_training_phases():
                stored_stop_result = self._stop_result[self._train_phase - 1]
            else:
                stored_stop_result = self._stop_result
            # make sure that the original dict in stored_stop_result is not
            # modified (this could have unexpected consequences in some cases)
            stored_msg = stored_stop_result[0].copy()
            if msg:
                stored_msg.update(msg)
            msg = stored_msg
            if target is None:
                target = stored_stop_result[1]
        return self._combine_result(result, msg, target)
예제 #8
0
    def _split_x(self, x):
        # split the data dims
        _d = (self.observation_dim * 2 + self.action_dim + self.reward_dim + 1)
        if x.shape[1] < _d:
            raise mdp.TrainingException(
                "input x requires minimum %d dims, given %d." %
                (_d, x.shape[1]))
        elif x.shape[1] == _d:
            dims = [
                self.observation_dim, self.observation_dim, self.action_dim,
                self.reward_dim, 1
            ]
        else:
            dims = [
                self.observation_dim, self.observation_dim, self.action_dim,
                self.reward_dim, 1, x.shape[1] - _d
            ]

        out = []
        start_indx = 0
        for i in xrange(len(dims)):
            out.append(x[:, start_indx:start_indx + dims[i]])
            start_indx += dims[i]
        return out
예제 #9
0
 def _ignore_input_train_wrapper(x, *args, **kwargs):
     if self._stored_input is None:
         raise mdp.TrainingException(
             "No stored inputs to train on! Set using"
             "'set_stored_input' method")
     fn(self._stored_input, *args, **kwargs)