def test_compute_09(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 60).reshape(15, 4) gradients = np.random.uniform(-1, 1, 180).reshape(15, 3, 4) weights = np.ones((15, 1)) / 15 metric = np.diag(2 * np.ones(3)) ss = KernelActiveSubspaces(dim=2, n_features=4, method='exact', n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights, metric=metric) true_evects = np.array( [[0.00126244, 0.99791389, 0.02926469, 0.05753138], [0.04385229, -0.05833941, 0.78953331, 0.60935265], [-0.99902507, -0.001436, 0.03167332, 0.03071887], [0.00492877, -0.02761026, -0.61219077, 0.79021253]]) np.testing.assert_array_almost_equal(true_evects, ss.evects)
def test_plot_sufficient_summary_02(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces() ss.compute(inputs=inputs, gradients=gradients, weights=weights, method='exact', nboot=49, n_features=8, feature_map=None) ss.partition(3) with self.assertRaises(ValueError): ss.plot_sufficient_summary(10, 10)
def test_plot_sufficient_summary_02(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces(dim=3, n_features=8, n_boot=5) ss.fit(inputs=inputs, gradients=gradients, weights=weights) with self.assertRaises(ValueError): ss.plot_sufficient_summary(10, 10)
def test_plot_eigenvalues_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces(dim=2, n_features=8, n_boot=5) ss.fit(inputs=inputs, gradients=gradients, weights=weights) with assert_plot_figures_added(): ss.plot_eigenvalues(n_evals=3, figsize=(7, 7), title='Eigenvalues')
def test_plot_sufficient_summary_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces() ss.compute(inputs=inputs, gradients=gradients, weights=weights, method='exact', nboot=49, n_features=8, feature_map=None) ss.partition(2) with assert_plot_figures_added(): ss.plot_sufficient_summary( np.random.uniform(-1, 1, 100).reshape(25, 4), np.random.uniform(-1, 1, 25).reshape(-1, 1))
def test_cross_validation_run_02(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 10).reshape(5, 2) outputs = np.random.uniform(0, 5, 10).reshape(5, 2) gradients = np.random.uniform(-1, 1, 20).reshape(5, 2, 2) fm = FeatureMap(distr='laplace', bias=np.random.uniform(-1, 1, 3), input_dim=2, n_features=3, params=np.zeros(1), sigma_f=outputs.var()) ss = KernelActiveSubspaces(dim=1, feature_map=fm) csv = CrossValidation(inputs=inputs, outputs=outputs, gradients=gradients, folds=2, subspace=ss) true_value = (2.26333743325053, 0.43902733603381605) np.testing.assert_array_almost_equal(csv.run(), true_value)
def test_plot_eigenvectors_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces(dim=2, n_features=5, method='exact', n_boot=5) ss.fit(inputs=inputs, gradients=gradients, weights=weights) with assert_plot_figures_added(): ss.plot_eigenvectors(n_evects=2, figsize=(5, 8), labels=[r'$x$', r'$y$', 'q', r'$r$', r'$z$'])
def test_plot_sufficient_summary_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces(dim=2, n_features=8, method='exact', n_boot=5) ss.fit(inputs=inputs, gradients=gradients, weights=weights) with assert_plot_figures_added(): ss.plot_sufficient_summary( np.random.uniform(-1, 1, 100).reshape(25, 4), np.random.uniform(-1, 1, 25).reshape(-1, 1))
def test_forward_01(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 30).reshape(15, 1, 2) inputs = np.random.uniform(-1, 1, 30).reshape(15, 2) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces() ss.compute(inputs=inputs, gradients=gradients, weights=weights, method='exact', nboot=49, n_features=4, feature_map=None) ss.partition(2) active = ss.forward(np.random.uniform(-1, 1, 4).reshape(2, 2))[0] true_active = np.array([[1.34199032, 0.02509303], [1.55021982, -0.29461026]]) np.testing.assert_array_almost_equal(true_active, active)
def test_plot_eigenvalues_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 200).reshape(50, 1, 4) inputs = np.random.uniform(-1, 1, 200).reshape(50, 4) weights = np.ones((50, 1)) / 50 ss = KernelActiveSubspaces() ss.compute(inputs=inputs, gradients=gradients, weights=weights, method='exact', nboot=49, n_features=8, feature_map=None) with assert_plot_figures_added(): ss.plot_eigenvalues(n_evals=3, figsize=(7, 7), title='Eigenvalues')
def test_forward_02(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 30).reshape(15, 1, 2) inputs = np.random.uniform(-1, 1, 30).reshape(15, 2) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces() ss.compute(inputs=inputs, gradients=gradients, weights=weights, method='exact', nboot=49, n_features=4, feature_map=None) ss.partition(2) inactive = ss.forward(np.random.uniform(-1, 1, 4).reshape(2, 2))[1] print(inactive) true_inactive = np.array([[-0.47449407, 0.51271165], [-0.27475082, 0.36433068]]) np.testing.assert_array_almost_equal(true_inactive, inactive)
def test_average_rrmse_01(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 10).reshape(5, 2) outputs = np.random.uniform(0, 5, 10).reshape(5, 2) gradients = np.random.uniform(-1, 1, 20).reshape(5, 2, 2) fm = FeatureMap(distr='laplace', bias=np.random.uniform(-1, 1, 3), input_dim=2, n_features=3, params=np.zeros(1), sigma_f=outputs.var()) ss = KernelActiveSubspaces(dim=1, feature_map=fm) csv = CrossValidation(inputs=inputs, outputs=outputs, gradients=gradients, folds=2, subspace=ss) best = [0.1, np.zeros((3, 2))] hyperparams = np.array([-1.]) score = average_rrmse(hyperparams, csv, best, resample=1) np.testing.assert_equal(best[0], 0.1)
def test_brute(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 10).reshape(5, 2) outputs = np.random.uniform(0, 5, 10).reshape(5, 2) gradients = np.random.uniform(-1, 1, 20).reshape(5, 2, 2) fm = FeatureMap(distr='laplace', bias=np.random.uniform(-1, 1, 3), input_dim=2, n_features=3, params=np.array([5.34265038]), sigma_f=outputs.var()) ss = KernelActiveSubspaces(feature_map=fm) csv = CrossValidation(inputs=inputs, outputs=outputs, gradients=gradients, folds=2, subspace=ss) best = fm.tune_pr_matrix(func=average_rrmse, bounds=[slice(-1, 1.2, 0.2) for i in range(1)], args=(csv, ), maxiter=10, save_file=False)[1] true = np.array([[0.03857183, -0.45825228], [-1.06057884, 0.9981594], [1.01812996, 0.19529565]]) np.testing.assert_array_almost_equal(true, best)
def test_average_rrmse_02(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 10).reshape(5, 2) outputs = np.random.uniform(0, 5, 10).reshape(5, 2) gradients = np.random.uniform(-1, 1, 20).reshape(5, 2, 2) fm = FeatureMap(distr='laplace', bias=np.random.uniform(-1, 1, 3), input_dim=2, n_features=3, params=np.zeros(1), sigma_f=outputs.var()) ss = KernelActiveSubspaces(dim=1, feature_map=fm, method='exact') csv = CrossValidation(inputs=inputs, outputs=outputs, gradients=gradients, folds=2, subspace=ss) best = [0.1, np.zeros((3, 2))] hyperparams = np.array([-1.]) score = average_rrmse(hyperparams, best, csv, verbose=True, resample=1) true = 2.1357048222078703 np.testing.assert_array_almost_equal(score, true)
def test_brute(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 10).reshape(5, 2) outputs = np.random.uniform(0, 5, 10).reshape(5, 2) gradients = np.random.uniform(-1, 1, 20).reshape(5, 2, 2) fm = FeatureMap(distr='laplace', bias=np.random.uniform(-1, 1, 3), input_dim=2, n_features=3, params=np.array([5.34265038]), sigma_f=outputs.var()) ss = KernelActiveSubspaces(dim=1, feature_map=fm) csv = CrossValidation(inputs=inputs, outputs=outputs, gradients=gradients, folds=2, subspace=ss) best = fm.tune_pr_matrix(func=average_rrmse, bounds=[slice(-2, 1, 0.2) for i in range(1)], fn_args={'csv':csv}, maxiter=10, save_file=False)[1] true = np.array([[0., 0.], [0., 0.], [0., 0.]]) np.testing.assert_array_almost_equal(true, best)
# this is the number of parameters to tune after n_params = 1 # sample the bias term b = np.random.uniform(0, 2 * np.pi, n_features) # define the feature map fm = FeatureMap(distr='laplace', bias=b, input_dim=input_dim, n_features=n_features, params=np.zeros(n_params), sigma_f=f.var()) # instantiate a KernelActiveSubspaces object with associated feature map kss = KernelActiveSubspaces(feature_map=fm, dim=1, n_features=n_features) # number of folds for the cross-validation algorithm folds = 3 verbose = True # Skip if bias and projection matrix are loaded csv = CrossValidation(inputs=xx, outputs=f.reshape(-1, 1), gradients=df.reshape(n_samples, 1, input_dim), folds=folds, subspace=kss) best = fm.tune_pr_matrix(func=average_rrmse, bounds=[slice(-2, 0., 0.2) for i in range(n_params)], fn_args={
def test_compute_01(self): ss = KernelActiveSubspaces(dim=2) with self.assertRaises(TypeError): ss.fit()
def test_init_pseudo_gradients(self): ss = KernelActiveSubspaces(dim=2) self.assertIsNone(ss.pseudo_gradients)
def test_init_features(self): ss = KernelActiveSubspaces(dim=2) self.assertIsNone(ss.features)
def test_init_evects(self): ss = KernelActiveSubspaces(dim=2) self.assertIsNone(ss.evects)
def test_init_cov_matrix(self): ss = KernelActiveSubspaces() self.assertIsNone(ss.cov_matrix)
def test_plot_sufficient_summary_01(self): ss = KernelActiveSubspaces(dim=2) with self.assertRaises(TypeError): ss.plot_sufficient_summary(10, 10)
def test_init_feature_map(self): ss = KernelActiveSubspaces() self.assertIsNone(ss.feature_map)
def test_init_dim(self): ss = KernelActiveSubspaces(dim=2) self.assertEqual(ss.dim, 2)
def test_plot_eigenvectors_01(self): ss = KernelActiveSubspaces(dim=2) with self.assertRaises(TypeError): ss.plot_eigenvectors(figsize=(7, 7), title='Eigenvalues')
def test_compute_01(self): ss = KernelActiveSubspaces() with self.assertRaises(ValueError): ss.compute()
def test_init_subs_br(self): ss = KernelActiveSubspaces(dim=2) self.assertIsNone(ss.subs_br)
def test_init_evals_br(self): ss = KernelActiveSubspaces() self.assertIsNone(ss.evals_br)
def test_init_dim(self): ss = KernelActiveSubspaces() self.assertIsNone(ss.dim)
def test_init_W2(self): ss = KernelActiveSubspaces(dim=2) self.assertIsNone(ss.W2)