def test_init_W2(self): ss = ActiveSubspaces(dim=1) self.assertIsNone(ss.W2)
def test_init_evals_br(self): ss = ActiveSubspaces(dim=1) self.assertIsNone(ss.evals_br)
def test_plot_sufficient_summary_01(self): ss = ActiveSubspaces() with self.assertRaises(ValueError): ss.plot_sufficient_summary(10, 10)
def test_activity_scores_03(self): np.random.seed(42) gradients = np.random.uniform(-1, 1, 180).reshape(15, 3, 4) ss = ActiveSubspaces(dim=2, method='exact', n_boot=150) with self.assertRaises(TypeError): ss.activity_scores
def test_plot_eigenvectors_01(self): ss = ActiveSubspaces() with self.assertRaises(ValueError): ss.plot_eigenvectors(figsize=(7, 7), title='Eigenvalues')
def test_compute_01(self): ss = ActiveSubspaces() with self.assertRaises(ValueError): ss.compute()
def test_init_dim(self): ss = ActiveSubspaces() self.assertIsNone(ss.dim)
def test_init_cov_matrix(self): ss = ActiveSubspaces() self.assertIsNone(ss.cov_matrix)
def test_init_evects(self): ss = ActiveSubspaces() self.assertIsNone(ss.evects)
def test_init_subs_br(self): ss = ActiveSubspaces() self.assertIsNone(ss.subs_br)
def test_init_W1(self): ss = ActiveSubspaces() self.assertIsNone(ss.W1)
nor = Normalizer(lb, ub) x = nor.fit_transform(X) # Define the output of interest and compute the gradients func = sin_2d dfunc = egrad(func) f = func(x) df_exact = dfunc(x) # exact gradients df_gp = eval_gp_grad( x, f, n_samples, input_dim) # gradients approximated with Gaussian process regression title = "sin" # Compute the active subspace with approximated gradients asub1 = ActiveSubspaces(dim=1, method='exact', n_boot=100) asub1.fit(gradients=df_gp) asub1.plot_eigenvalues(figsize=(6, 4), title=title) asub1.plot_eigenvectors(figsize=(6, 4), title=title) asub1.plot_sufficient_summary(x, f, figsize=(6, 4), title=title) asub2 = ActiveSubspaces(dim=2, method='exact', n_boot=100) asub2.fit(gradients=df_gp) asub2.plot_sufficient_summary(x, f, figsize=(6, 4), title=title) # Compute the active subspace with exact gradients asub1_ = ActiveSubspaces(dim=1, method='exact', n_boot=100) asub1_.fit(gradients=df_exact) asub1_.plot_eigenvalues(figsize=(6, 4), title=title) asub1_.plot_eigenvectors(figsize=(6, 4), title=title) asub1_.plot_sufficient_summary(x, f, figsize=(6, 4), title=title)
def test_fit_01(self): ss = ActiveSubspaces(dim=1) with self.assertRaises(ValueError): ss.fit()
def test_init_dim(self): ss = ActiveSubspaces(dim=1) self.assertEqual(ss.dim, 1)
torch.set_default_tensor_type(torch.DoubleTensor) np.random.seed(42) # global parameters n_train = 300 n_params = 2 x_np = np.random.uniform(size=(n_train, n_params)) f = x_np[:, 0]**3 + x_np[:, 1]**3 + 0.2 * x_np[:, 0] + 0.6 * x_np[:, 1] df_np = np.empty((n_train, n_params)) df_np[:, 0] = 3.0 * x_np[:, 0]**2 + 0.2 df_np[:, 1] = 3.0 * x_np[:, 1]**2 + 0.6 ss = ActiveSubspaces(1) ss.fit(inputs=x_np, gradients=df_np) ss.plot_eigenvalues(figsize=(6, 4)) ss.plot_sufficient_summary(x_np, f, figsize=(6, 4)) nll = NonlinearLevelSet( n_layers=10, active_dim=1, lr=0.008, epochs=1000, dh=0.25, optimizer=torch.optim.SGD, scheduler=torch.optim.lr_scheduler.StepLR, ) x_torch = torch.as_tensor(x_np, dtype=torch.double) df_torch = torch.as_tensor(df_np, dtype=torch.double)