def apply_softmax_grad(self, labels, target = None):
        """
        Apply softmax derivative, where labels are the correct labels.
        """
        if not target:
            target = self

        assert labels.shape == (1, self.shape[1])
        assert target.shape == self.shape
        if isinstance(labels, CUDAMatrix):
            err_code = _cudamat.apply_softmax_grad(self.p_mat, labels.p_mat, target.p_mat)
        else:
            raise ValueError, "labels must be of type CUDAMatrix."

        if err_code:
            raise generate_exception(err_code)

        return target

        Apply softmax derivative, where labels are the correct labels.
        
cudamat/cudamat.py