Exemple #1
0
    def _sssrm(self, data_align, data_sup, labels):
        """Block-Coordinate Descent algorithm for fitting SS-SRM.

        Parameters
        ----------

        data_align : list of 2D arrays, element i has shape=[voxels_i, n_align]
            Each element in the list contains the fMRI data for alignment of
            one subject. There are n_align samples for each subject.

        data_sup : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in data_sup.


        Returns
        -------

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        s : array, shape=[features, samples]
            The shared response.
        """
        classes = self.classes_.size

        # Initialization:
        self.random_state_ = np.random.RandomState(self.rand_seed)
        random_states = [
            np.random.RandomState(self.random_state_.randint(2**32))
            for i in range(len(data_align))
        ]
        # Set Wi's to a random orthogonal voxels by TRs
        w, _ = srm._init_w_transforms(data_align, self.features, random_states)

        # Initialize the shared response S
        s = SSSRM._compute_shared_response(data_align, w)

        # Initialize theta and bias
        theta, bias = self._update_classifier(data_sup, labels, w, classes)

        # calculate and print the objective function
        if logger.isEnabledFor(logging.INFO):
            objective = self._objective_function(data_align, data_sup, labels,
                                                 w, s, theta, bias)
            logger.info('Objective function %f' % objective)

        # Main loop:
        for iteration in range(self.n_iter):
            logger.info('Iteration %d' % (iteration + 1))

            # Update the mappings Wi
            w = self._update_w(data_align, data_sup, labels, w, s, theta, bias)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating Wi  %f' %
                            objective)

            # Update the shared response S
            s = SSSRM._compute_shared_response(data_align, w)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating S   %f' %
                            objective)

            # Update the MLR classifier, theta and bias
            theta, bias = self._update_classifier(data_sup, labels, w, classes)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating MLR %f' %
                            objective)

        return w, s, theta, bias
Exemple #2
0
    def _sssrm(self, data_align, data_sup, labels):
        """Block-Coordinate Descent algorithm for fitting SS-SRM.

        Parameters
        ----------

        data_align : list of 2D arrays, element i has shape=[voxels_i, n_align]
            Each element in the list contains the fMRI data for alignment of
            one subject. There are n_align samples for each subject.

        data_sup : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in data_sup.


        Returns
        -------

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        s : array, shape=[features, samples]
            The shared response.
        """
        classes = self.classes_.size

        # Initialization:
        np.random.seed(self.rand_seed)
        # Set Wi's to a random orthogonal voxels by TRs
        w, _ = srm._init_w_transforms(data_align, self.features)

        # Initialize the shared response S
        s = SSSRM._compute_shared_response(data_align, w)

        # Initialize theta and bias
        theta, bias = self._update_classifier(data_sup, labels, w, classes)

        # calculate and print the objective function
        if logger.isEnabledFor(logging.INFO):
            objective = self._objective_function(data_align, data_sup, labels,
                                                 w, s, theta, bias)
            logger.info('Objective function %f' % objective)

        # Main loop:
        for iteration in range(self.n_iter):
            logger.info('Iteration %d' % (iteration + 1))

            # Update the mappings Wi
            w = self._update_w(data_align, data_sup, labels, w, s, theta, bias)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating Wi  %f'
                            % objective)

            # Update the shared response S
            s = SSSRM._compute_shared_response(data_align, w)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating S   %f'
                            % objective)

            # Update the MLR classifier, theta and bias
            theta, bias = self._update_classifier(data_sup, labels, w, classes)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating MLR %f'
                            % objective)

        return w, s, theta, bias