Ejemplo n.º 1
0
    def _do_sweep(self, covs, Q, prev_contrast):
        """
        Perform a single sweep.
        
        :param covs: The covariance matrices.
        :param Q: he rotation matrix.
        :param prev_contrast: The previous contrast.
        :return: The maximum improvement in contrast, rotated matrices, the current
            contrast.
        """
        # perform a single sweep

        # initialize maximal improvement in a single sweep
        max_increase = -1
        # shuffle rotation order
        numx_rand.shuffle(self.rot_axis)
        # sweep through all axes combinations
        for (i, j) in self.rot_axis:
            # get the angle that minimizes the contrast
            # and the contrast value
            angle, contrast = self._givens_angle(i, j, covs)
            if contrast == 0:
                # we hit numerical precision in case when b_sfa == 0
                # we can only break things from here on, better quit!
                max_increase = -1
                break

            # relative improvement in the contrast function
            relative_diff = old_div((prev_contrast - contrast),
                                    abs(prev_contrast))
            if relative_diff < 0:
                # if rate of change is negative we hit numerical precision
                # or we already sit on the optimum for this pair of axis.
                # don't rotate anymore and go to the next pair
                continue

            # update the rotation matrix
            rotate(Q, angle, [i, j])
            # rotate the covariance matrices
            covs.rotate(angle, [i, j])

            # store maximum and previous rate of change
            max_increase = max(max_increase, relative_diff)
            prev_contrast = contrast

        return max_increase, covs, Q, contrast
Ejemplo n.º 2
0
    def _do_sweep(self, covs, Q, prev_contrast):
        """
        Perform a single sweep.
        
        :param covs: The covariance matrices.
        :param Q: he rotation matrix.
        :param prev_contrast: The previous contrast.
        :return: The maximum improvement in contrast, rotated matrices, the current
            contrast.
        """
        # perform a single sweep

        # initialize maximal improvement in a single sweep
        max_increase = -1
        # shuffle rotation order
        numx_rand.shuffle(self.rot_axis)
        # sweep through all axes combinations
        for (i, j) in self.rot_axis:
            # get the angle that minimizes the contrast
            # and the contrast value
            angle, contrast = self._givens_angle(i, j, covs)
            if contrast == 0:
                # we hit numerical precision in case when b_sfa == 0
                # we can only break things from here on, better quit!
                max_increase = -1
                break

            # relative improvement in the contrast function
            relative_diff = old_div((prev_contrast-contrast),abs(prev_contrast))
            if relative_diff < 0:
                # if rate of change is negative we hit numerical precision
                # or we already sit on the optimum for this pair of axis.
                # don't rotate anymore and go to the next pair
                continue

            # update the rotation matrix
            rotate(Q, angle, [i, j])
            # rotate the covariance matrices
            covs.rotate(angle, [i, j])

            # store maximum and previous rate of change
            max_increase = max(max_increase, relative_diff)
            prev_contrast = contrast

        return max_increase, covs, Q, contrast
Ejemplo n.º 3
0
    def _label_one(self, pattern, threshold):
        pattern = mdp.utils.bool_to_sign(pattern)

        has_converged = False
        while not has_converged:
            has_converged = True
            iter_order = range(len(self._weight_matrix))
            if self._shuffled_update:
                numx_rand.shuffle(iter_order)
            for row in iter_order:
                w_row = self._weight_matrix[row]

                thresh_row = threshold[row]
                new_pattern_row = numx.sign(numx.dot(w_row, pattern) - thresh_row)

                if new_pattern_row == 0:
                    # Following McKay, Neural Networks, we do nothing
                    # when the new pattern is zero
                    pass
                elif pattern[row] != new_pattern_row:
                    has_converged = False
                    pattern[row] = new_pattern_row
        return mdp.utils.sign_to_bool(pattern)
Ejemplo n.º 4
0
    def _label_one(self, pattern, threshold):
        pattern = mdp.utils.bool_to_sign(pattern)

        has_converged = False
        while not has_converged:
            has_converged = True
            iter_order = range(len(self._weight_matrix))
            if self._shuffled_update:
                numx_rand.shuffle(iter_order)
            for row in iter_order:
                w_row = self._weight_matrix[row]

                thresh_row = threshold[row]
                new_pattern_row = numx.sign(
                    numx.dot(w_row, pattern) - thresh_row)

                if new_pattern_row == 0:
                    # Following McKay, Neural Networks, we do nothing
                    # when the new pattern is zero
                    pass
                elif pattern[row] != new_pattern_row:
                    has_converged = False
                    pattern[row] = new_pattern_row
        return mdp.utils.sign_to_bool(pattern)