def _init(self, X): """Initialize statistic and dictionary""" if self.projection not in ["partial", "full"]: raise ValueError("projection should be in {'partial', 'full'}," " got %s" % self.projection) X = check_array(X, dtype="float", order="F", accept_sparse="csr") self.sparse_ = sp.issparse(X) n_rows, n_cols = X.shape if self.n_samples is not None: self.n_samples_ = self.n_samples else: self.n_samples_ = n_rows self.random_state_ = check_random_state(self.random_state) # D dictionary if self.dict_init is not None: if self.dict_init.shape != (self.n_components, n_cols): raise ValueError( "Initial dictionary and X shape mismatch: %r != %r" % (self.dict_init.shape, (self.n_components, n_cols)) ) self.D_ = check_array(self.dict_init, order="C", dtype="float", copy=True) if self.fit_intercept: if not (np.all(self.D_[0] == self.D_[0].mean())): raise ValueError( "When fitting intercept and providing " "initial dictionary, first component of" " the dictionary should be " "proportional to [1, ..., 1]" ) self.D_[0] = 1 else: self.D_ = np.empty((self.n_components, n_cols), order="C") if self.fit_intercept: self.D_[0] = 1 U = self.random_state_.randn(n_cols, self.n_components - 1) Q, _ = np.linalg.qr(U) self.D_[1:] = Q.T else: self.D_[:] = self.random_state_.randn(self.n_components, n_cols) self.D_ = np.asfortranarray(enet_scale(self.D_, l1_ratio=self.l1_ratio, radius=1)) self.A_ = np.zeros((self.n_components, self.n_components), order="F") self.B_ = np.zeros((self.n_components, n_cols), order="F") self.counter_ = np.zeros(n_cols + 1, dtype="int") self.n_iter_ = np.zeros(1, dtype="long") self.code_ = np.zeros((self.n_samples_, self.n_components))
def test_enet_scale(): random_state = check_random_state(0) a = random_state.randn(10, 100) a1 = random_state.randn(100) for r in [1, 2]: for this_a in [a, a1]: for l1_ratio in [0., 0.5, 1]: b = enet_scale(this_a, r, l1_ratio) norms = enet_norm(b, l1_ratio) assert_array_almost_equal(norms, r)
def get_init_objective(output_dir): mask, func_filenames = get_hcp_data(data_dir=data_dir, raw=True) masker = NiftiMasker(mask_img=mask, smoothing_fwhm=None, standardize=False) masker.fit() rsn70 = fetch_atlas_smith_2009().rsn70 components = masker.transform(rsn70) print(components.shape) enet_scale(components.T, inplace=True) print(np.sum(np.abs(components), axis=1)) test_data = func_filenames[(-n_test_records * 2)::2] n_samples, n_voxels = np.load(test_data[-1], mmap_mode='r').shape X = np.empty((n_test_records * n_samples, n_voxels)) for i, this_data in enumerate(test_data): X[i * n_samples:(i + 1) * n_samples] = np.load(this_data, mmap_mode='r') exp_var = {} for alpha in [1e-2, 1e-3, 1e-4]: exp_var[alpha] = objective_function(X, components, alpha) json.dump(open(join(output_dir, 'init_objective.json'), 'r'))
def get_init_objective(output_dir): mask, func_filenames = get_hcp_data(raw=True) masker = NiftiMasker(mask_img=mask, smoothing_fwhm=None, standardize=False) masker.fit() rsn70 = fetch_atlas_smith_2009().rsn70 components = masker.transform(rsn70) print(components.shape) enet_scale(components.T, inplace=True) print(np.sum(np.abs(components), axis=1)) test_data = func_filenames[(-n_test_records * 2)::2] n_samples, n_voxels = np.load(test_data[-1], mmap_mode='r').shape X = np.empty((n_test_records * n_samples, n_voxels)) for i, this_data in enumerate(test_data): X[i * n_samples:(i + 1) * n_samples] = np.load(this_data, mmap_mode='r') exp_var = {} for alpha in [1e-2, 1e-3, 1e-4]: exp_var[alpha] = objective_function(X, components, alpha) json.dump(open(join(output_dir, 'init_objective.json'), 'r'))
def _init(self, X): """Initialize statistic and dictionary""" if self.var_red not in ['none', 'code_only', 'weight_based', 'sample_based', 'combo']: raise ValueError("var_red should be in {'none', 'code_only'," " 'weight_based', 'sample_based'}," " got %s" % self.var_red) if self.projection not in ['partial', 'full']: raise ValueError("projection should be in {'partial', 'full'}," " got %s" % self.projection) X = check_array(X, dtype='float', order='F', accept_sparse='csr') self.sparse_ = sp.issparse(X) if self.sparse_ and self.var_red != 'weight_based': raise NotImplementedError("Sparse input can only be used with " "var_red='weight_based', got %s" % self.var_red) n_rows, n_cols = X.shape if self.n_samples is not None: self.n_samples_ = self.n_samples else: self.n_samples_ = n_rows self.random_state_ = check_random_state(self.random_state) # D dictionary if self.dict_init is not None: if self.dict_init.shape != (self.n_components, n_cols): raise ValueError( 'Initial dictionary and X shape mismatch: %r != %r' % ( self.dict_init.shape, (self.n_components, n_cols))) self.D_ = check_array(self.dict_init, order='C', dtype='float', copy=True) if self.fit_intercept: if not (np.all(self.D_[0] == self.D_[0].mean())): raise ValueError('When fitting intercept and providing ' 'initial dictionary, first component of' ' the dictionary should be ' 'proportional to [1, ..., 1]') self.D_[0] = 1 else: self.D_ = np.empty((self.n_components, n_cols), order='C') if self.fit_intercept: self.D_[0] = 1 U = self.random_state_.randn( n_cols, self.n_components - 1, ) Q, _ = np.linalg.qr(U) self.D_[1:] = Q.T else: self.D_[:] = self.random_state_.randn( self.n_components, n_cols) self.D_ = np.asfortranarray( enet_scale(self.D_, l1_ratio=self.l1_ratio, radius=1)) self.A_ = np.zeros((self.n_components, self.n_components), order='F') self.B_ = np.zeros((self.n_components, n_cols), order="F") self.counter_ = np.zeros(n_cols + 1, dtype='int') self.n_iter_ = np.zeros(1, dtype='long') if self.var_red != 'weight_based': self.G_ = self.D_.dot(self.D_.T).T self.multiplier_ = np.array([1.]) if self.var_red in ['code_only', 'sample_based', 'combo']: self.row_counter_ = np.zeros(self.n_samples_, dtype='int') self.beta_ = np.zeros((self.n_samples_, self.n_components), order="F") self.code_ = np.zeros((self.n_samples_, self.n_components)) if self.backend == 'c': if not hasattr(self, 'row_counter_'): self.row_counter_ = self._dummy_1d_int if not hasattr(self, 'G_'): self.G_ = self._dummy_2d_float if not hasattr(self, 'beta_'): self.beta_ = self._dummy_2d_float if not hasattr(self, 'multiplier_'): self.multiplier_ = self._dummy_1d_float if self.debug: self.loss_ = np.empty(self.max_n_iter) self.loss_indep_ = 0.
def _init(self, X): """Initialize statistic and dictionary""" if self.projection not in ['partial', 'full']: raise ValueError("projection should be in {'partial', 'full'}," " got %s" % self.projection) X = check_array(X, dtype='float', order='F', accept_sparse='csr') self.sparse_ = sp.issparse(X) n_rows, n_cols = X.shape if self.n_samples is not None: self.n_samples_ = self.n_samples else: self.n_samples_ = n_rows self.random_state_ = check_random_state(self.random_state) # D dictionary if self.dict_init is not None: if self.dict_init.shape != (self.n_components, n_cols): raise ValueError( 'Initial dictionary and X shape mismatch: %r != %r' % (self.dict_init.shape, (self.n_components, n_cols))) self.D_ = check_array(self.dict_init, order='C', dtype='float', copy=True) if self.fit_intercept: if not (np.all(self.D_[0] == self.D_[0].mean())): raise ValueError('When fitting intercept and providing ' 'initial dictionary, first component of' ' the dictionary should be ' 'proportional to [1, ..., 1]') self.D_[0] = 1 else: self.D_ = np.empty((self.n_components, n_cols), order='C') if self.fit_intercept: self.D_[0] = 1 U = self.random_state_.randn( n_cols, self.n_components - 1, ) Q, _ = np.linalg.qr(U) self.D_[1:] = Q.T else: self.D_[:] = self.random_state_.randn(self.n_components, n_cols) self.D_ = np.asfortranarray( enet_scale(self.D_, l1_ratio=self.l1_ratio, radius=1)) self.A_ = np.zeros((self.n_components, self.n_components), order='F') self.B_ = np.zeros((self.n_components, n_cols), order="F") self.counter_ = np.zeros(n_cols + 1, dtype='int') self.n_iter_ = np.zeros(1, dtype='long') self.code_ = np.zeros((self.n_samples_, self.n_components))
def _init(self, X): """Initialize statistic and dictionary""" if self.var_red not in [ 'none', 'code_only', 'weight_based', 'sample_based', 'combo' ]: raise ValueError("var_red should be in {'none', 'code_only'," " 'weight_based', 'sample_based'}," " got %s" % self.var_red) if self.projection not in ['partial', 'full']: raise ValueError("projection should be in {'partial', 'full'}," " got %s" % self.projection) X = check_array(X, dtype='float', order='F', accept_sparse='csr') self.sparse_ = sp.issparse(X) if self.sparse_ and self.var_red != 'weight_based': raise NotImplementedError("Sparse input can only be used with " "var_red='weight_based', got %s" % self.var_red) n_rows, n_cols = X.shape if self.n_samples is not None: self.n_samples_ = self.n_samples else: self.n_samples_ = n_rows self.random_state_ = check_random_state(self.random_state) # D dictionary if self.dict_init is not None: if self.dict_init.shape != (self.n_components, n_cols): raise ValueError( 'Initial dictionary and X shape mismatch: %r != %r' % (self.dict_init.shape, (self.n_components, n_cols))) self.D_ = check_array(self.dict_init, order='C', dtype='float', copy=True) if self.fit_intercept: if not (np.all(self.D_[0] == self.D_[0].mean())): raise ValueError('When fitting intercept and providing ' 'initial dictionary, first component of' ' the dictionary should be ' 'proportional to [1, ..., 1]') self.D_[0] = 1 else: self.D_ = np.empty((self.n_components, n_cols), order='C') if self.fit_intercept: self.D_[0] = 1 U = self.random_state_.randn( n_cols, self.n_components - 1, ) Q, _ = np.linalg.qr(U) self.D_[1:] = Q.T else: self.D_[:] = self.random_state_.randn(self.n_components, n_cols) self.D_ = np.asfortranarray( enet_scale(self.D_, l1_ratio=self.l1_ratio, radius=1)) self.A_ = np.zeros((self.n_components, self.n_components), order='F') self.B_ = np.zeros((self.n_components, n_cols), order="F") self.counter_ = np.zeros(n_cols + 1, dtype='int') self.n_iter_ = np.zeros(1, dtype='long') if self.var_red != 'weight_based': self.G_ = self.D_.dot(self.D_.T).T self.multiplier_ = np.array([1.]) if self.var_red in ['code_only', 'sample_based', 'combo']: self.row_counter_ = np.zeros(self.n_samples_, dtype='int') self.beta_ = np.zeros((self.n_samples_, self.n_components), order="F") self.code_ = np.zeros((self.n_samples_, self.n_components)) if self.backend == 'c': if not hasattr(self, 'row_counter_'): self.row_counter_ = self._dummy_1d_int if not hasattr(self, 'G_'): self.G_ = self._dummy_2d_float if not hasattr(self, 'beta_'): self.beta_ = self._dummy_2d_float if not hasattr(self, 'multiplier_'): self.multiplier_ = self._dummy_1d_float if self.debug: self.loss_ = np.empty(self.max_n_iter) self.loss_indep_ = 0.