def test_ensemble_nystrom_full_prec_one_learner(): # test if keep all the dimensions is the nystrom kernel matrix equals to the exact kernel n_sample = 150 n_feat = n_sample input_val1 = torch.DoubleTensor( np.random.normal(size=[n_sample, n_feat])).double() input_val2 = input_val1 # input_val2 = torch.DoubleTensor(np.random.normal(size=[n_sample - 1, n_feat] ) ).double() # get exact gaussian kernel kernel = GaussianKernel(sigma=10.0) kernel_mat = kernel.get_kernel_matrix(input_val1, input_val2) # nystrom method approx = Nystrom(n_feat, kernel=kernel) approx.setup(input_val1) feat = approx.get_feat(input_val1) approx_kernel_mat = approx.get_kernel_matrix(input_val1, input_val2) # ensembleed nystrom method approx_ensemble = EnsembleNystrom(n_feat, n_learner=1, kernel=kernel) approx_ensemble.setup(input_val1) feat_ensemble = approx_ensemble.get_feat(input_val1) approx_kernel_mat_ensemble = approx_ensemble.get_kernel_matrix( input_val1, input_val2) np.testing.assert_array_almost_equal( np.sum(feat.cpu().numpy()**2), np.sum(feat_ensemble.cpu().numpy()**2)) np.testing.assert_array_almost_equal( np.sum(approx_kernel_mat.cpu().numpy()**2), np.sum(approx_kernel_mat_ensemble.cpu().numpy()**2)) print("single learner ensembled nystrom test passed!")
def test_ensemble_nystrom_full_prec_three_learner(): # test if keep all the dimensions is the nystrom kernel matrix equals to the exact kernel n_sample = 150 n_feat = n_sample input_val1 = torch.DoubleTensor( np.random.normal(size=[n_sample, n_feat])).double() input_val2 = input_val1 # input_val2 = torch.DoubleTensor(np.random.normal(size=[n_sample - 1, n_feat] ) ).double() # get exact gaussian kernel kernel = GaussianKernel(sigma=10.0) kernel_mat = kernel.get_kernel_matrix(input_val1, input_val2) # nystrom method approx = Nystrom(n_feat, kernel=kernel) approx.setup(input_val1) feat = approx.get_feat(input_val1) approx_kernel_mat = approx.get_kernel_matrix(input_val1, input_val2) # ensembleed nystrom method approx_ensemble = EnsembleNystrom(n_feat, n_learner=3, kernel=kernel) approx_ensemble.setup(input_val1) feat_ensemble = approx_ensemble.get_feat(input_val1) assert feat_ensemble.size(0) == n_sample assert feat_ensemble.size(1) == n_feat approx_kernel_mat_ensemble = approx_ensemble.get_kernel_matrix( input_val1, input_val2) print("single learner ensembled nystrom test passed!")
def test_ensemble_nystrom_low_prec(): # test if keep all the dimensions is the nystrom kernel matrix equals to the exact kernel n_sample = 150 n_feat = n_sample input_val1 = torch.DoubleTensor( np.random.normal(size=[n_sample, n_feat])).double() input_val2 = input_val1 # input_val2 = torch.DoubleTensor(np.random.normal(size=[n_sample - 1, n_feat] ) ).double() # get exact gaussian kernel kernel = GaussianKernel(sigma=10.0) kernel_mat = kernel.get_kernel_matrix(input_val1, input_val2) # setup quantizer quantizer = Quantizer(4, torch.min(input_val1), torch.max(input_val1), rand_seed=2, use_cuda=False) # nystrom method approx = Nystrom(n_feat, kernel=kernel) approx.setup(input_val1) feat = approx.get_feat(input_val1) approx_kernel_mat = approx.get_kernel_matrix(input_val1, input_val2, quantizer, quantizer) # ensembleed nystrom method approx_ensemble = EnsembleNystrom(n_feat, n_learner=1, kernel=kernel) approx_ensemble.setup(input_val1) feat_ensemble = approx_ensemble.get_feat(input_val1) approx_kernel_mat_ensemble = approx_ensemble.get_kernel_matrix( input_val1, input_val2, quantizer, quantizer, consistent_quant_seed=True) approx_kernel_mat_ensemble = approx_ensemble.get_kernel_matrix( input_val1, input_val2, quantizer, quantizer, consistent_quant_seed=True) print("single learner ensembled nystrom quantizerd version test passed!")
rand_seed=args.random_seed) quantizer = None else: raise Exception( "kernel approximation type not specified or not supported!") kernel.torch(cuda=use_cuda) kernel_approx.torch(cuda=use_cuda) if args.fixed_design or args.closed_form_sol: # for fixed design experiments and closed form solution form real setting if args.fixed_design_auto_l2_reg: # get kernel matrix and get the decomposition assert isinstance(X_train, torch.DoubleTensor) print("fixed design lambda calculation using kernel ", type(kernel_approx)) kernel_mat = kernel_approx.get_kernel_matrix( X_train, X_train, quantizer, quantizer) assert isinstance(kernel_mat, torch.DoubleTensor) U, S, _ = np.linalg.svd(kernel_mat.cpu().numpy().astype( np.float64)) # numerically figure out the best lambda in the fixed design setting x0 = 1.0 f = lambda lam: expected_loss(lam, U, S, Y_train_orig, args. fixed_design_noise_sigma) res = minimize(f, x0, bounds=[(0.0, None)], options={ 'xtol': 1e-6, 'disp': True }) loss = f(res.x)