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_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_transform_04(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 60).reshape(15, 4) outputs = np.random.uniform(0, 5, 15) ss = KernelActiveSubspaces(dim=2, method='local', n_boot=49) ss.fit(inputs=inputs, outputs=outputs) inactive = ss.transform(np.random.uniform(-1, 1, 8).reshape(2, 4))[1] true_inactive = np.array([[0.27110018, -0.29359021], [0.76399199, -0.02233936]]) np.testing.assert_array_almost_equal(true_inactive, inactive)
def test_compute_07(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 ss = KernelActiveSubspaces(dim=2, n_features=4, n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) true_evals = np.array( [874.84255146, 62.83226559, 3.60417077, 2.84686573]) np.testing.assert_array_almost_equal(true_evals, ss.evals)
def test_transform_03(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 60).reshape(15, 4) outputs = np.random.uniform(0, 5, 45).reshape(15, 3) ss = KernelActiveSubspaces(dim=2, method='local', n_boot=50) ss.fit(inputs=inputs, outputs=outputs, metric=np.diag(np.ones(3))) active = ss.transform(np.random.uniform(-1, 1, 8).reshape(2, 4))[0] true_active = np.array([[-0.18946138, 0.31916713], [-0.25310859, -0.30280365]]) np.testing.assert_array_almost_equal(true_active, active)
def test_compute_bootstrap_ranges_04(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 60).reshape(30, 1, 2) inputs = np.random.uniform(-1, 1, 60).reshape(30, 2) weights = np.ones((30, 1)) / 30 ss = KernelActiveSubspaces(dim=2, n_features=4, n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) true_bounds_subspace = np.array([[0.01734317, 0.09791063, 0.19840464], [0.05112582, 0.43105485, 0.92323839], [0.05890817, 0.27517302, 0.89262039]]) np.testing.assert_array_almost_equal(true_bounds_subspace, ss.subs_br)
def test_transform_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(dim=2, n_features=4, n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) active = ss.transform(np.random.uniform(-1, 1, 4).reshape(2, 2))[0] true_active = np.array([[0.94893046, 0.01774345], [1.09617095, -0.20832091]]) np.testing.assert_array_almost_equal(true_active, active)
def test_compute_02(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 30).reshape(15, 2) inputs = np.random.uniform(-1, 1, 30).reshape(15, 2) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces(dim=2, n_features=4, method='exact', n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) true_evals = np.array([0.42588097, 0.19198234, 0.08228976, 0.0068496]) np.testing.assert_array_almost_equal(true_evals, ss.evals)
def test_compute_08(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 ss = KernelActiveSubspaces(dim=2, n_features=4, n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) 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_compute_10(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 60).reshape(15, 4) outputs = np.random.uniform(0, 5, 45).reshape(15, 3) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces(dim=2, n_features=4, method='local', n_boot=49) ss.fit(inputs=inputs, outputs=outputs, weights=weights) true_evals = np.array( [7.93870724e+04, 1.18699831e+02, 4.36634158e+01, 1.49812189e+01]) np.testing.assert_allclose(true_evals, ss.evals)
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_compute_05(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 30).reshape(15, 2) outputs = np.random.uniform(0, 5, 15).reshape(15, 1) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces(dim=2, feature_map=None, n_features=4, method='local', n_boot=49) ss.fit(inputs=inputs, outputs=outputs, weights=weights) true_evals = np.array( [173.56222204, 96.19314922, 29.05560411, 0.85385631]) np.testing.assert_array_almost_equal(true_evals, ss.evals)
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_transform_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(dim=2, n_features=4, method='exact', n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) inactive = ss.transform(np.random.uniform(-1, 1, 4).reshape(2, 2))[1] true_inactive = np.array([[-0.33551797, 0.36254188], [-0.19427817, 0.2576207]]) np.testing.assert_array_almost_equal(true_inactive, inactive)
def test_compute_11(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 60).reshape(15, 4) outputs = np.random.uniform(0, 5, 45).reshape(15, 3) weights = np.ones((15, 1)) / 15 metric = np.diag(2 * np.ones(3)) ss = KernelActiveSubspaces(dim=2, n_features=4, method='local', n_boot=49) ss.fit(inputs=inputs, outputs=outputs, weights=weights, metric=metric) true_evals = np.array( [1.58774145e+05, 2.37399662e+02, 8.73268317e+01, 2.99624379e+01]) np.testing.assert_allclose(true_evals, ss.evals)
def test_compute_bootstrap_ranges_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 60).reshape(30, 1, 2) inputs = np.random.uniform(-1, 1, 60).reshape(30, 2) weights = np.ones((30, 1)) / 30 ss = KernelActiveSubspaces(dim=2, n_features=4, method='exact', n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) true_bounds_evals = np.array([[2.59177494, 7.11443789], [0.5456548, 1.94294036], [0.05855044, 0.84178668], [0.01530059, 0.187785]]) np.testing.assert_array_almost_equal(true_bounds_evals, ss.evals_br)
def test_compute_06(self): np.random.seed(42) inputs = np.random.uniform(-1, 1, 30).reshape(15, 2) outputs = np.random.uniform(0, 5, 15).reshape(15, 1) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces(dim=2, n_features=4, method='local', n_boot=49) ss.fit(inputs=inputs, outputs=outputs, weights=weights) true_evects = np.array( [[0.27316542, 0.65012729, 0.24857554, 0.66402211], [-0.34261047, 0.46028689, 0.61561027, -0.54016483], [-0.68249783, -0.37635433, 0.35472274, 0.51645514], [0.58497472, -0.47310455, 0.65833576, -0.02388905]]) np.testing.assert_array_almost_equal(true_evects, ss.evects)
def test_compute_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 30).reshape(15, 2) inputs = np.random.uniform(-1, 1, 30).reshape(15, 2) weights = np.ones((15, 1)) / 15 ss = KernelActiveSubspaces(dim=2, n_features=4, method='exact', n_boot=49) ss.fit(inputs=inputs, gradients=gradients, weights=weights) true_evects = np.array( [[0.74714817, 0.6155644, 0.23414206, 0.08959675], [0.35380297, -0.10917583, -0.91115623, 0.18082704], [-0.50287165, 0.76801638, -0.33072226, -0.21884635], [-0.25241469, 0.1389674, 0.07479708, 0.95466239]]) np.testing.assert_array_almost_equal(true_evects, ss.evects)
bounds=[slice(-2, 0., 0.2) for i in range(n_params)], fn_args={ 'csv': csv, 'verbose': verbose }, method='bso', maxiter=10, save_file=False) print('The lowest rrmse is {}%'.format(best[0])) W = np.load('opt_pr_matrix.npy') b = np.load('bias.npy') fm._pr_matrix = W fm.bias = b kss.fit(gradients=dt.reshape(N, 1, input_dim), outputs=t, inputs=y) kss.plot_eigenvalues(n_evals=5, figsize=(6, 4)) kss.plot_sufficient_summary(xx, f, figsize=(6, 4)) # comparison with Nonlinear Level-set Learning explained in detail in tutorial 07 from athena.nll import NonlinearLevelSet import torch # create NonlinearLevelSet object, eventually passing an optimizer of choice nll = NonlinearLevelSet(n_layers=10, active_dim=1, lr=0.01, epochs=1000, dh=0.25, optimizer=torch.optim.Adam)
def test_compute_01(self): ss = KernelActiveSubspaces(dim=2) with self.assertRaises(TypeError): ss.fit()