Exemple #1
0
    def _update_dict_slow(self, subset, D_range):
        """Update dictionary from statistic
        Parameters
        ----------
        subset: ndarray (len_subset),
            Mask used on X

        """
        D_subset = self.D_[:, subset]

        if self.projection == "full":
            norm = enet_norm(self.D_, self.l1_ratio)
        else:
            norm = enet_norm(D_subset, self.l1_ratio)
        R = self.B_[:, subset] - np.dot(D_subset.T, self.A_).T

        ger, = linalg.get_blas_funcs(("ger",), (self.A_, D_subset))
        for k in D_range:
            ger(1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R += np.dot(stat.A[:, j].reshape(n_components, 1),
            D_subset[k] = R[k] / (self.A_[k, k])
            if self.projection == "full":
                self.D_[k][subset] = D_subset[k]
                self.D_[k] = enet_projection(self.D_[k], norm[k], self.l1_ratio)
                D_subset[k] = self.D_[k][subset]
            else:
                D_subset[k] = enet_projection(D_subset[k], norm[k], self.l1_ratio)
            ger(-1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R -= np.dot(stat.A[:, j].reshape(n_components, 1),
        if self.projection == "partial":
            self.D_[:, subset] = D_subset
Exemple #2
0
    def _update_dict_slow(self, subset, D_range):
        """Update dictionary from statistic
        Parameters
        ----------
        subset: ndarray (len_subset),
            Mask used on X

        """
        D_subset = self.D_[:, subset]

        if self.projection == 'full':
            norm = enet_norm(self.D_, self.l1_ratio)
        else:
            norm = enet_norm(D_subset, self.l1_ratio)
        R = self.B_[:, subset] - np.dot(D_subset.T, self.A_).T

        ger, = linalg.get_blas_funcs(('ger', ), (self.A_, D_subset))
        for k in D_range:
            ger(1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R += np.dot(stat.A[:, j].reshape(n_components, 1),
            D_subset[k] = R[k] / (self.A_[k, k])
            if self.projection == 'full':
                self.D_[k][subset] = D_subset[k]
                self.D_[k] = enet_projection(self.D_[k], norm[k],
                                             self.l1_ratio)
                D_subset[k] = self.D_[k][subset]
            else:
                D_subset[k] = enet_projection(D_subset[k], norm[k],
                                              self.l1_ratio)
            ger(-1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R -= np.dot(stat.A[:, j].reshape(n_components, 1),
        if self.projection == 'partial':
            self.D_[:, subset] = D_subset
Exemple #3
0
    def _update_dict_slow(self, subset, D_range):
        """Update dictionary from statistic
        Parameters
        ----------
        subset: ndarray (len_subset),
            Mask used on X
        Q: ndarray (n_components, n_features):
            Dictionary to perform ridge regression
        l1_ratio: float in [0, 1]:
            Controls the sparsity of the dictionary
        stat: DictMFStats,
            Statistics kept by the algorithm, to be updated by the function
        var_red: boolean,
            Online update of the Gram matrix (Experimental)
        random_state: int or RandomState
            Pseudo number generator state used for random sampling.

        """
        D_subset = self.D_[:, subset]

        if self.projection == 'full':
            norm = enet_norm(self.D_, self.l1_ratio)
        else:
            if self.var_red != 'weight_based':
                self.G_ -= D_subset.dot(D_subset.T)
            norm = enet_norm(D_subset, self.l1_ratio)
        R = self.B_[:, subset] - np.dot(D_subset.T, self.A_).T

        ger, = linalg.get_blas_funcs(('ger',), (self.A_, D_subset))
        for k in D_range:
            ger(1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R += np.dot(stat.A[:, j].reshape(n_components, 1),
            D_subset[k] = R[k] / (self.A_[k, k])
            if self.projection == 'full':
                self.D_[k][subset] = D_subset[k]
                self.D_[k] = enet_projection(self.D_[k],
                                             norm[k],
                                             self.l1_ratio)
                D_subset[k] = self.D_[k][subset]
            else:
                D_subset[k] = enet_projection(D_subset[k], norm[k],
                                              self.l1_ratio)
            ger(-1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R -= np.dot(stat.A[:, j].reshape(n_components, 1),
        if self.projection == 'partial':
            self.D_[:, subset] = D_subset
            if self.var_red != 'weight_based':
                self.G_ += D_subset.dot(D_subset.T)
        elif self.var_red != 'weight_based':
            self.G_ = self.D_.dot(self.D_.T).T
Exemple #4
0
def test_fast_enet_l2_ball():
    random_state = check_random_state(0)
    norms = np.zeros(10)
    for i in range(10):
        a = random_state.randn(100)
        c = np.zeros(100)
        c[:] = enet_projection(a, 2, 0.0)
        norms[i] = np.sqrt(np.sum(c ** 2))
    assert_array_almost_equal(norms, np.ones(10) * sqrt(2))
    for i in range(10):
        a = random_state.randn(100)
        a /= np.sqrt(np.sum(a ** 2)) * 10
        c = enet_projection(a, 2, 0.0)
        assert_array_almost_equal(a, c)
Exemple #5
0
def test_fast_enet_l2_ball():
    random_state = check_random_state(0)
    norms = np.zeros(10)
    for i in range(10):
        a = random_state.randn(100)
        c = np.zeros(100)
        c[:] = enet_projection(a, 2, 0.0)
        norms[i] = np.sqrt(np.sum(c**2))
    assert_array_almost_equal(norms, np.ones(10) * sqrt(2))
    for i in range(10):
        a = random_state.randn(100)
        a /= np.sqrt(np.sum(a**2)) * 10
        c = enet_projection(a, 2, 0.0)
        assert_array_almost_equal(a, c)
Exemple #6
0
    def _update_dict_slow(self, subset, D_range):
        """Update dictionary from statistic
        Parameters
        ----------
        subset: ndarray (len_subset),
            Mask used on X
        Q: ndarray (n_components, n_features):
            Dictionary to perform ridge regression
        l1_ratio: float in [0, 1]:
            Controls the sparsity of the dictionary
        stat: DictMFStats,
            Statistics kept by the algorithm, to be updated by the function
        var_red: boolean,
            Online update of the Gram matrix (Experimental)
        random_state: int or RandomState
            Pseudo number generator state used for random sampling.

        """
        D_subset = self.D_[:, subset]

        if self.projection == 'full':
            norm = enet_norm(self.D_, self.l1_ratio)
        else:
            if self.var_red != 'weight_based':
                self.G_ -= D_subset.dot(D_subset.T)
            norm = enet_norm(D_subset, self.l1_ratio)
        R = self.B_[:, subset] - np.dot(D_subset.T, self.A_).T

        ger, = linalg.get_blas_funcs(('ger', ), (self.A_, D_subset))
        for k in D_range:
            ger(1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R += np.dot(stat.A[:, j].reshape(n_components, 1),
            D_subset[k] = R[k] / (self.A_[k, k])
            if self.projection == 'full':
                self.D_[k][subset] = D_subset[k]
                self.D_[k] = enet_projection(self.D_[k], norm[k],
                                             self.l1_ratio)
                D_subset[k] = self.D_[k][subset]
            else:
                D_subset[k] = enet_projection(D_subset[k], norm[k],
                                              self.l1_ratio)
            ger(-1.0, self.A_[k], D_subset[k], a=R, overwrite_a=True)
            # R -= np.dot(stat.A[:, j].reshape(n_components, 1),
        if self.projection == 'partial':
            self.D_[:, subset] = D_subset
            if self.var_red != 'weight_based':
                self.G_ += D_subset.dot(D_subset.T)
        elif self.var_red != 'weight_based':
            self.G_ = self.D_.dot(self.D_.T).T
Exemple #7
0
def test_fast_enet_l1_ball():
    random_state = check_random_state(0)
    norms = np.zeros(10)
    for i in range(10):
        a = random_state.randn(100)
        b = np.zeros(100)
        b[:] = enet_projection(a, 1, 1.0)
        norms[i] = np.sum(np.abs(b))
    assert_array_almost_equal(norms, np.ones(10))
Exemple #8
0
def test_fast_enet_projection():
    c = np.empty((10, 100))
    b = np.empty((10, 100))
    random_state = check_random_state(0)
    for i in range(10):
        a = random_state.randn(100)
        b[i, :] = enet_projection_slow(a, radius=1, l1_ratio=0.1)
        c[i] = enet_projection(a, radius=1, l1_ratio=0.1)
    assert_array_almost_equal(c, b, 4)
Exemple #9
0
def test_fast_enet_l1_ball():
    random_state = check_random_state(0)
    norms = np.zeros(10)
    for i in range(10):
        a = random_state.randn(100)
        b = np.zeros(100)
        b[:] = enet_projection(a, 1, 1.0)
        norms[i] = np.sum(np.abs(b))
    assert_array_almost_equal(norms, np.ones(10))
Exemple #10
0
def test_fast_enet_projection():
    c = np.empty((10, 100))
    b = np.empty((10, 100))
    random_state = check_random_state(0)
    for i in range(10):
        a = random_state.randn(100)
        b[i, :] = enet_projection_slow(a, radius=1, l1_ratio=0.1)
        c[i] = enet_projection(a, radius=1, l1_ratio=0.1)
    assert_array_almost_equal(c, b, 4)
Exemple #11
0
def test_fast_enet_projection_norm():
    random_state = check_random_state(0)
    norms = np.zeros(10)
    for i in range(10):
        a = random_state.randn(20000)
        a /= np.sqrt(np.sum(a ** 2))
        c = np.zeros(20000)
        c[:] = enet_projection(a, 1, 0.15)
        norms[i] = enet_norm(c, l1_ratio=0.15)
    assert_array_almost_equal(norms, np.ones(10))
Exemple #12
0
def test_fast_enet_projection_norm():
    random_state = check_random_state(0)
    norms = np.zeros(10)
    for i in range(10):
        a = random_state.randn(20000)
        a /= np.sqrt(np.sum(a**2))
        c = np.zeros(20000)
        c[:] = enet_projection(a, 1, 0.15)
        norms[i] = enet_norm(c, l1_ratio=0.15)
    assert_array_almost_equal(norms, np.ones(10))