Example #1
0
 def test_plot_eigenvalues_03(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     weights = np.ones((50, 1)) / 50
     ss = ActiveSubspaces()
     ss.compute(gradients=gradients, weights=weights, nboot=200)
     with assert_plot_figures_added():
         ss.plot_eigenvalues(n_evals=3, figsize=(7, 7), title='Eigenvalues')
Example #2
0
 def test_compute_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()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=150)
     true_evals = np.array([13.794711, 11.102377, 3.467318, 1.116324])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Example #3
0
 def test_compute_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()
     ss.compute(gradients=gradients, weights=weights, nboot=100)
     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)
Example #4
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()
     ss.compute(gradients=gradients, weights=weights, nboot=200)
     ss.partition(3)
     with self.assertRaises(ValueError):
         ss.plot_sufficient_summary(10, 10)
Example #5
0
 def test_compute_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()
     ss.compute(gradients=gradients, weights=weights, nboot=100)
     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)
Example #6
0
 def test_forward_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()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=250)
     ss.partition(2)
     active = ss.forward(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)
Example #7
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()
     ss.compute(gradients=gradients, weights=weights, nboot=200)
     with assert_plot_figures_added():
         ss.plot_eigenvectors(n_evects=2,
                              figsize=(5, 8),
                              labels=[r'$x$', r'$y$', r'$r$', r'$z$'])
Example #8
0
 def test_forward_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()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=250)
     ss.partition(2)
     inactive = ss.forward(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)
Example #9
0
 def test_forward_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()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=250)
     ss.partition(2)
     active = ss.forward(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)
Example #10
0
 def test_compute_06(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 60).reshape(15, 4)
     outputs = np.random.uniform(0, 5, 15)
     ss = ActiveSubspaces()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=200)
     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)
Example #11
0
 def test_plot_sufficient_summary_03(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 200).reshape(50, 4)
     weights = np.ones((50, 1)) / 50
     ss = ActiveSubspaces()
     ss.compute(gradients=gradients, weights=weights, nboot=200)
     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))
Example #12
0
 def test_compute_02(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 60).reshape(15, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces()
     ss.compute(gradients=gradients,
                weights=weights,
                method='exact',
                nboot=150)
     true_evals = np.array([0.571596, 0.465819, 0.272198, 0.175012])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Example #13
0
 def test_compute_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()
     ss.compute(gradients=gradients,
                weights=weights,
                method='exact',
                nboot=100)
     true_bounds_subspace = np.array([[0.00109331, 0.30254992, 0.90447872]])
     np.testing.assert_array_almost_equal(true_bounds_subspace, ss.subs_br)
Example #14
0
 def test_compute_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()
     ss.compute(gradients=gradients,
                weights=weights,
                method='exact',
                nboot=100)
     true_bounds_subspace = np.array([[0.00261813, 0.58863862, 0.99998352]])
     np.testing.assert_array_almost_equal(true_bounds_subspace, ss.subs_br)
Example #15
0
 def test_compute_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()
     ss.compute(gradients=gradients,
                weights=weights,
                method='exact',
                nboot=150)
     true_evals = np.array([1.32606312, 1.20519582, 0.94811868, 0.68505712])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Example #16
0
 def test_backward_02(self):
     np.random.seed(42)
     inputs = np.random.uniform(-1, 1, 80).reshape(16, 5)
     outputs = np.random.uniform(-1, 3, 16)
     ss = ActiveSubspaces()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=250)
     ss.partition(2)
     new_inputs = np.random.uniform(-1, 1, 15).reshape(3, 5)
     active = ss.forward(new_inputs)[0]
     new_inputs = ss.backward(reduced_inputs=active, n_points=500)[0]
     np.testing.assert_array_almost_equal(
         np.kron(active, np.ones((500, 1))), new_inputs.dot(ss.W1))
Example #17
0
 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 = ActiveSubspaces()
     ss.compute(inputs=inputs,
                outputs=outputs,
                weights=weights,
                method='local',
                nboot=150)
     true_evals = np.array(
         [84.08055975, 25.87980349, 9.61982202, 8.29248646])
     np.testing.assert_array_almost_equal(true_evals, ss.evals)
Example #18
0
 def test_compute_03(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 60).reshape(15, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces()
     ss.compute(gradients=gradients,
                weights=weights,
                method='exact',
                nboot=200)
     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)
Example #19
0
 def test_forward_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()
     ss.compute(inputs=inputs,
                outputs=outputs,
                method='local',
                nboot=250,
                metric=np.diag(np.ones(3)))
     ss.partition(2)
     new_inputs = np.random.uniform(-1, 1, 8).reshape(2, 4)
     active, inactive = ss.forward(new_inputs)
     reconstructed_inputs = active.dot(ss.W1.T) + inactive.dot(ss.W2.T)
     np.testing.assert_array_almost_equal(new_inputs, reconstructed_inputs)
Example #20
0
 def test_compute_08(self):
     np.random.seed(42)
     gradients = np.random.uniform(-1, 1, 180).reshape(15, 3, 4)
     weights = np.ones((15, 1)) / 15
     ss = ActiveSubspaces()
     ss.compute(gradients=gradients,
                weights=weights,
                method='exact',
                nboot=150)
     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)
Example #21
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()
     ss.compute(inputs=inputs, outputs=outputs, method='local', nboot=250)
     ss.partition(1)
     new_inputs = np.random.uniform(-1, 1, 8).reshape(2, 4)
     active = ss.forward(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))
Example #22
0
 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 = ActiveSubspaces()
     ss.compute(inputs=inputs,
                outputs=outputs,
                weights=weights,
                method='local',
                nboot=150,
                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)
Example #23
0
 def test_compute_01(self):
     ss = ActiveSubspaces()
     with self.assertRaises(ValueError):
         ss.compute()