def job_scf_gwgrid(sample_source, tr, te, r): with util.ContextTimer() as t: rand_state = np.random.get_state() np.random.seed(r + 92856) d = tr.dim() T_randn = np.random.randn(J, d) np.random.set_state(rand_state) # grid search to determine the initial gwidth mean_sd = tr.mean_std() scales = 2.0**np.linspace(-4, 4, 20) list_gwidth = np.hstack( (mean_sd * scales * (d**0.5), 2**np.linspace(-8, 8, 20))) list_gwidth.sort() besti, powers = tst.SmoothCFTest.grid_search_gwidth( tr, T_randn, list_gwidth, alpha) # initialize with the best width from the grid search best_width = list_gwidth[besti] scf_gwgrid = tst.SmoothCFTest(T_randn, best_width, alpha) test_result = scf_gwgrid.perform_test(te) result = { 'test_method': scf_gwgrid, 'test_result': test_result, 'time_secs': t.secs } return result
def job_scf_gwopt(prob_label, tr, te, r, ni, n): """SmoothCFTest. Optimize only the Gaussian width. Fix the test frequencies""" raise ValueError('Use job_scf_gwgrid instead') with util.ContextTimer() as t: op_gwidth = { 'max_iter': 200, 'gwidth_step_size': 0.1, 'batch_proportion': 1.0, 'tol_fun': 1e-3 } # optimize on the training set rand_state = np.random.get_state() np.random.seed(seed=r + 92856) ss, _ = get_sample_source(prob_label) T_randn = np.random.randn(J, ss.dim()) np.random.set_state(rand_state) gwidth, info = tst.SmoothCFTest.optimize_gwidth( tr, T_randn, **op_gwidth) scf_gwopt = tst.SmoothCFTest(T_randn, gwidth, alpha) return { #'test_method': scf_gwopt, 'test_result': scf_gwopt.perform_test(te), 'time_secs': t.secs }
def job_scf_opt10(sample_source, tr, te, r): """SmoothCFTest with frequencies optimized.""" op = {'n_test_freqs': J, 'max_iter': 100, 'freqs_step_size': 1.0, 'gwidth_step_size': 0.1, 'seed': r+92856, 'tol_fun': 1e-3} test_freqs, gwidth, info = tst.SmoothCFTest.optimize_freqs_width(tr, alpha, **op) scf_opt = tst.SmoothCFTest(test_freqs, gwidth, alpha) scf_opt_test = scf_opt.perform_test(te) return scf_opt_test
def job_scf_opt(sample_source, tr, te, r): """SmoothCFTest with frequencies optimized.""" with util.ContextTimer() as t: op = {'n_test_freqs': J, 'max_iter': 500, 'freqs_step_size': 1.0, 'gwidth_step_size': 0.1, 'seed': r+92856, 'tol_fun': 1e-3} test_freqs, gwidth, info = tst.SmoothCFTest.optimize_freqs_width(tr, alpha, **op) scf_opt = tst.SmoothCFTest(test_freqs, gwidth, alpha) scf_opt_test = scf_opt.perform_test(te) result = {'test_method': scf_opt, 'test_result': scf_opt_test, 'time_secs': t.secs} return result
def test(self, X, Y): XY = self.preprocess(X, Y) train, test = XY.split_tr_te(tr_proportion=self.split_ratio) with contextlib.redirect_stdout(None): with contextlib.redirect_stderr(None): test_freqs, gwidth, info = fot_tst.SmoothCFTest.optimize_freqs_width( train, self.alpha, n_test_freqs=self.J) SCF = fot_tst.SmoothCFTest(test_freqs, gwidth, alpha=self.alpha) result = SCF.perform_test(test) p_val = result['pvalue'] return p_val
def job_scf_gwopt(sample_source, tr, te, r): """SmoothCFTest. Optimize only the Gaussian width. Fix the test frequencies""" op_gwidth = {'max_iter': 200, 'gwidth_step_size': 0.1, 'batch_proportion': 1.0, 'tol_fun': 1e-3} # optimize on the training set rand_state = np.random.get_state() np.random.seed(seed=r+92856) T_randn = np.random.randn(J, sample_source.dim()) np.random.set_state(rand_state) gwidth, info = tst.SmoothCFTest.optimize_gwidth(tr, T_randn, **op_gwidth) scf_gwopt = tst.SmoothCFTest(T_randn, gwidth, alpha) raise ValueError('Use job_scf_gwgrid instead') return scf_gwopt.perform_test(te)
def TST_SCF(Fea, N1, alpha, is_train, test_freqs, gwidth, J = 1, seed = 15): """run SCF test.""" Fea = get_item(Fea,is_cuda) tst_data = data.TSTData(Fea[0:N1,:], Fea[N1:,:]) h = 0 if is_train: op = {'n_test_freqs': J, 'seed': seed, 'max_iter': 300, 'batch_proportion': 1.0, 'freqs_step_size': 0.1, 'gwidth_step_size': 0.01, 'tol_fun': 1e-4} test_freqs, gwidth, info = tst.SmoothCFTest.optimize_freqs_width(tst_data, alpha, **op) return test_freqs, gwidth else: scf_opt = tst.SmoothCFTest(test_freqs, gwidth, alpha=alpha) test_result = scf_opt.perform_test(tst_data) if test_result['h0_rejected']: h = 1 return h
def job_scf_gwgrid(sample_source, tr, te, r, J): rand_state = np.random.get_state() np.random.seed(r+92856) d = tr.dim() T_randn = np.random.randn(J, d) np.random.set_state(rand_state) # grid search to determine the initial gwidth mean_sd = tr.mean_std() scales = 2.0**np.linspace(-4, 4, 20) list_gwidth = np.hstack( (mean_sd*scales*(d**0.5), 2**np.linspace(-10, 10, 20) )) list_gwidth.sort() besti, powers = tst.SmoothCFTest.grid_search_gwidth(tr, T_randn, list_gwidth, alpha) # initialize with the best width from the grid search best_width = list_gwidth[besti] scf_gwgrid = tst.SmoothCFTest(T_randn, best_width, alpha) return scf_gwgrid.perform_test(te)
def job_scf_opt10(prob_label, tr, te, r, ni, n): """SmoothCFTest with frequencies optimized.""" with util.ContextTimer() as t: op = { 'n_test_freqs': J, 'max_iter': 200, 'freqs_step_size': 2.0, 'gwidth_step_size': 0.2, 'seed': r + 92856, 'tol_fun': 1e-4 } test_freqs, gwidth, info = tst.SmoothCFTest.optimize_freqs_width( tr, alpha, **op) scf_opt = tst.SmoothCFTest(test_freqs, gwidth, alpha) scf_opt_test = scf_opt.perform_test(te) return { #'test_method': scf_opt, 'test_result': scf_opt_test, 'time_secs': t.secs }
def test(self, X, Y): XY = self.preprocess(X, Y) train, test = XY.split_tr_te(tr_proportion=self.split_ratio) freqs = np.random.randn(self.J, XY.dim()) mean_sd = train.mean_std() scales = 2.**np.linspace(-4, 4, 30) list_gwidth = np.hstack( [mean_sd * scales * (XY.dim()**0.5), 2**np.linspace(-8, 8, 20)]) list_gwidth.sort() with contextlib.redirect_stdout(None): best_i, powers = fot_tst.SmoothCFTest.grid_search_gwidth( train, freqs, list_gwidth, self.alpha) best_width = list_gwidth[best_i] SCF = fot_tst.SmoothCFTest(freqs, best_width, self.alpha) result = SCF.perform_test(test) p_val = result['pvalue'] return p_val