def test_partition_05(self): np.random.seed(42) matrix = np.random.uniform(-1, 1, 9).reshape(3, 3) ss = ActiveSubspaces() ss.evects = matrix with self.assertRaises(ValueError): ss.partition(dim=4)
def test_partition_02(self): np.random.seed(42) matrix = np.random.uniform(-1, 1, 9).reshape(3, 3) ss = ActiveSubspaces() ss.evects = matrix ss.partition(dim=2) np.testing.assert_array_almost_equal(matrix[:, 2:], ss.W2)
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)
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)
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))
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)
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)
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))
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)
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))