Beispiel #1
0
 def test_activity_scores_01(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 180).reshape(15, 3, 4)
     ss = ActiveSubspaces(dim=1, method='exact', n_boot=150)
     ss.fit(gradients=gradients)
     true_scores = np.array([0.599489, 0.055179, 0.37102 , 0.300374])
     np.testing.assert_array_almost_equal(true_scores, ss.activity_scores)
Beispiel #2
0
 def test_activity_scores_02(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)
     ss.fit(gradients=gradients)
     true_scores = np.array([0.89979047, 0.58309172, 0.38654072, 0.6618360])
     np.testing.assert_array_almost_equal(true_scores, ss.activity_scores)
Beispiel #3
0
 def test_fit_06(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 180).reshape(15, 3, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces(dim=1, n_boot=150)
     ss.fit(gradients=gradients, weights=weights)
     true_evals = np.array([1.32606312, 1.20519582, 0.94811868, 0.68505712])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Beispiel #4
0
 def test_fit_11(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(4, 15)
     gradients = (inputs[i, :] for i in range(4))
     ss = ActiveSubspaces(dim=2, method='exact', n_boot=150)
     ss.fit(gradients=gradients)
     true_evals = [7.317766, 2.849997]
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Beispiel #5
0
 def test_fit_bootstrap_ranges_02(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 60).reshape(30, 2)
     weights = np.ones((30, 1)) / 30
     ss = ActiveSubspaces(dim=1, method='exact', n_boot=100)
     ss.fit(gradients=gradients, weights=weights)
     true_bounds_subspace = np.array([[0.00261813, 0.58863862, 0.99998352]])
     np.testing.assert_array_almost_equal(true_bounds_subspace, ss.subs_br)
Beispiel #6
0
 def test_fit_02(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 60).reshape(15, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces(dim=1, method='exact', n_boot=150)
     ss.fit(gradients=gradients, weights=weights)
     true_evals = np.array([0.571596, 0.465819, 0.272198, 0.175012])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Beispiel #7
0
 def test_fit_bootstrap_ranges_04(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 180).reshape(30, 3, 2)
     weights = np.ones((30, 1)) / 30
     ss = ActiveSubspaces(dim=1, n_boot=100)
     ss.fit(gradients=gradients, weights=weights)
     true_bounds_subspace = np.array([[0.00109331, 0.30254992, 0.90447872]])
     np.testing.assert_array_almost_equal(true_bounds_subspace, ss.subs_br)
Beispiel #8
0
 def test_plot_eigenvectors_04(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     weights = np.ones((50, 1)) / 50
     ss = ActiveSubspaces(dim=1, n_boot=200)
     ss.fit(gradients=gradients, weights=weights)
     with self.assertRaises(ValueError):
         ss.plot_eigenvectors(n_evects=10, figsize=(7, 7))
Beispiel #9
0
 def test_plot_eigenvectors_02(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     weights = np.ones((50, 1)) / 50
     ss = ActiveSubspaces(dim=1, n_boot=200)
     ss.fit(gradients=gradients, weights=weights)
     with assert_plot_figures_added():
         ss.plot_eigenvectors(figsize=(7, 7), title='Eigenvectors')
Beispiel #10
0
 def test_plot_sufficient_summary_02(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     weights = np.ones((50, 1)) / 50
     ss = ActiveSubspaces(dim=3, n_boot=200)
     ss.fit(gradients=gradients, weights=weights)
     with self.assertRaises(ValueError):
         ss.plot_sufficient_summary(10, 10)
Beispiel #11
0
 def test_fit_04(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces(dim=1, method='local', n_boot=150)
     ss.fit(inputs=inputs, outputs=outputs)
     true_evals = np.array([13.794711, 11.102377, 3.467318, 1.116324])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Beispiel #12
0
 def test_plot_eigenvectors_05(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 50).reshape(5, 10)
     gradients = (inputs[i, :] for i in range(5))
     ss = ActiveSubspaces(dim=4, n_boot=200)
     ss.fit(gradients=gradients)
     with assert_plot_figures_added():
         ss.plot_eigenvectors(figsize=(7, 7), title='Eigenvectors')
Beispiel #13
0
 def test_transform_01(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces(dim=2, method='local', n_boot=250)
     ss.fit(inputs=inputs, outputs=outputs)
     active = ss.transform(np.random.uniform(-1, 1, 8).reshape(2, 4))[0]
     true_active = np.array([[-0.004748, 0.331107], [-0.949099, 0.347534]])
     np.testing.assert_array_almost_equal(true_active, active)
Beispiel #14
0
 def test_fit_bootstrap_ranges_03(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 180).reshape(30, 3, 2)
     weights = np.ones((30, 1)) / 30
     ss = ActiveSubspaces(dim=1, n_boot=100)
     ss.fit(gradients=gradients, weights=weights)
     true_bounds_evals = np.array([[0.99330673, 1.62694823],
                                   [0.65987633, 1.11751475]])
     np.testing.assert_array_almost_equal(true_bounds_evals, ss.evals_br)
Beispiel #15
0
 def test_fit_bootstrap_ranges_01(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 60).reshape(30, 2)
     weights = np.ones((30, 1)) / 30
     ss = ActiveSubspaces(dim=1, n_boot=100)
     ss.fit(gradients=gradients, weights=weights)
     true_bounds_evals = np.array([[0.3000497, 0.59008536],
                                   [0.17398718, 0.40959827]])
     np.testing.assert_array_almost_equal(true_bounds_evals, ss.evals_br)
Beispiel #16
0
 def test_plot_sufficient_summary_04(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     ss = ActiveSubspaces(dim=1, n_boot=100)
     ss.fit(gradients=gradients)
     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))
Beispiel #17
0
 def test_plot_eigenvectors_03(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     weights = np.ones((50, 1)) / 50
     ss = ActiveSubspaces(dim=1, n_boot=200)
     ss.fit(gradients=gradients, weights=weights)
     with assert_plot_figures_added():
         ss.plot_eigenvectors(n_evects=2,
                              labels=[r'$x$', r'$y$', r'$r$', r'$z$'])
Beispiel #18
0
 def test_fit_09(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 = ActiveSubspaces(dim=1, method='local', n_boot=150)
     ss.fit(inputs=inputs, outputs=outputs, weights=weights)
     true_evals = np.array(
         [84.08055975, 25.87980349, 9.61982202, 8.29248646])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Beispiel #19
0
 def test_activity_scores_04(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(4, 15)
     gradients = (inputs[i, :] for i in range(4))
     ss = ActiveSubspaces(dim=2, method='exact', n_boot=150)
     ss.fit(gradients=gradients)
     true_scores = np.array([0.061844, 0.914259, 0.868018, 0.830417, 1.081719, 0.652845,
     0.486826, 0.974479, 0.165313, 0.111259, 1.135542, 0.547697,
     1.099458, 0.860475, 0.377612])
     np.testing.assert_array_almost_equal(true_scores, ss.activity_scores)
Beispiel #20
0
 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 = ActiveSubspaces(dim=2, method='local', n_boot=250)
     ss.fit(inputs=inputs, outputs=outputs, metric=np.diag(np.ones(3)))
     new_inputs = np.random.uniform(-1, 1, 8).reshape(2, 4)
     active, inactive = ss.transform(new_inputs)
     reconstructed_inputs = active.dot(ss.W1.T) + inactive.dot(ss.W2.T)
     np.testing.assert_array_almost_equal(new_inputs, reconstructed_inputs)
Beispiel #21
0
 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, 45).reshape(15, 3)
     ss = ActiveSubspaces(dim=2, method='local', n_boot=250)
     ss.fit(inputs=inputs, outputs=outputs)
     active = ss.transform(np.random.uniform(-1, 1, 8).reshape(2, 4))[0]
     true_active = np.array([[0.15284753, 0.67109407],
                             [0.69006622, -0.4165206]])
     np.testing.assert_array_almost_equal(true_active, active)
Beispiel #22
0
 def test_transform_05(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces(dim=2, method='local', n_boot=250)
     ss.fit(inputs=inputs, outputs=outputs)
     inactive = ss.transform(np.random.uniform(-1, 1, 8).reshape(2, 4))[1]
     true_inactive = np.array([[-1.03574242, -0.04662904],
                               [-0.49850367, -0.37146678]])
     np.testing.assert_array_almost_equal(true_inactive, inactive)
Beispiel #23
0
 def test_fit_05(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces(dim=1, method='local', n_boot=200)
     ss.fit(inputs=inputs, outputs=outputs)
     true_evects = np.array([[-0.164383, 0.717021, -0.237246, -0.634486],
                             [-0.885808, 0.177628, 0.004112, 0.428691],
                             [0.255722, 0.558199, 0.734083, 0.290071],
                             [0.350612, 0.377813, -0.636254, 0.574029]])
     np.testing.assert_array_almost_equal(true_evects, ss.evects)
Beispiel #24
0
 def test_fit_03(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 60).reshape(15, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces(dim=1, n_boot=200)
     ss.fit(gradients=gradients, weights=weights)
     true_evects = np.array([[-0.019091, -0.408566, 0.861223, -0.301669],
                             [-0.767799, 0.199069, 0.268823, 0.546434],
                             [-0.463451, -0.758442, -0.427696, -0.164486],
                             [-0.441965, 0.467131, -0.055723, -0.763774]])
     np.testing.assert_array_almost_equal(true_evects, ss.evects)
Beispiel #25
0
 def test_inverse_transform_01(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces(dim=1, method='local', n_boot=250)
     ss.fit(inputs=inputs, outputs=outputs)
     new_inputs = np.random.uniform(-1, 1, 8).reshape(2, 4)
     active = ss.transform(new_inputs)[0]
     new_inputs = ss.inverse_transform(reduced_inputs=active, n_points=5)[0]
     np.testing.assert_array_almost_equal(np.kron(active, np.ones((5, 1))),
                                          new_inputs.dot(ss.W1))
Beispiel #26
0
 def test_fit_07(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 180).reshape(15, 3, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces(dim=1, method='exact', n_boot=150)
     ss.fit(gradients=gradients, weights=weights)
     true_evects = np.array(
         [[0.67237041, 0.49917148, 0.50889687, 0.1994238],
          [0.20398894, -0.66183856, 0.09970486, 0.71443486],
          [-0.52895262, -0.11348076, 0.83802337, -0.07104981],
          [0.47593663, -0.54764923, 0.16970489, -0.66690696]])
     np.testing.assert_array_almost_equal(true_evects, ss.evects)
Beispiel #27
0
 def test_plot_eigenvectors_05(self):
     np.random.seed(42)
     grad = np.array(
         [[-0.50183952, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0],
          [-1.26638196, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0],
          [ 0.43017941, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0],
          [ 0.65008914, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0]])
     gradients = (grad[i, :] for i in range(4))
     ss = ActiveSubspaces(dim=2, method='exact', n_boot=200)
     ss.fit(gradients=gradients)
     with assert_plot_figures_added():
         ss.plot_eigenvectors(figsize=(7, 7), title='Eigenvectors')
Beispiel #28
0
 def test_fit_11(self):
     np.random.seed(42)
     grad = np.array(
         [[-0.50183952, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0],
          [-1.26638196, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0],
          [ 0.43017941, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0],
          [ 0.65008914, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0]])
     gradients = (grad[i, :] for i in range(4))
     ss = ActiveSubspaces(dim=2, method='exact', n_boot=150)
     ss.fit(gradients=gradients)
     true_evals = [2.040621, 0.]
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Beispiel #29
0
 def test_fit_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
     metric = np.diag(2 * np.ones(3))
     ss = ActiveSubspaces(dim=1, method='local', n_boot=150)
     ss.fit(inputs=inputs, outputs=outputs, weights=weights, metric=metric)
     true_evects = np.array(
         [[0.75159386, 0.34814972, 0.5093598, 0.23334745],
          [-0.44047075, 0.89306499, -0.00249322, 0.09172904],
          [0.35007644, 0.2587911, -0.30548878, -0.84684725],
          [0.34429445, 0.11938954, -0.8045017, 0.46902504]])
     np.testing.assert_array_almost_equal(true_evects, ss.evects)
Beispiel #30
0
 def test_hit_and_run_inactive_01(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces(dim=1, method='local', n_boot=250)
     ss.fit(inputs=inputs, outputs=outputs)
     new_inputs = np.random.uniform(-1, 1, 8).reshape(2, 4)
     active = ss.transform(new_inputs)[0]
     inactive_swap = np.array([
         ss._hit_and_run_inactive(reduced_input=red_inp, n_points=1)
         for red_inp in active
     ])
     inactive_inputs = np.swapaxes(inactive_swap, 1, 2)
     new_inputs = ss._rotate_x(reduced_inputs=active,
                               inactive_inputs=inactive_inputs)[0]
     np.testing.assert_array_almost_equal(active, new_inputs.dot(ss.W1))