Skip to content

Commit

Permalink
Merge 22f1624 into 63e208c
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraMeneghetti committed Apr 20, 2021
2 parents 63e208c + 22f1624 commit 1103c18
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
6 changes: 6 additions & 0 deletions athena/active.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def fit(self,
:param numpy.ndarray metric: metric matrix output_dim-by-output-dim for
vectorial active subspaces.
:raises: TypeError
:Example:
>>> #gradients: GeneratorType n_samples-by-n_params
>>> ss = ActiveSubspaces(dim=2, method='exact', n_boot=150)
>>> ss.fit(gradients=gradients)
"""
if self.method == 'exact':
if gradients is None:
Expand Down
64 changes: 39 additions & 25 deletions tests/test_active.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from contextlib import contextmanager
import matplotlib.pyplot as plt


@contextmanager
def assert_plot_figures_added():
"""
Expand Down Expand Up @@ -156,28 +155,31 @@ def test_fit_10(self):

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))
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 = [7.317766, 2.849997]
true_evals = [2.040621, 0.]
np.testing.assert_array_almost_equal(true_evals, ss.evals)

def test_fit_12(self):
np.random.seed(42)
inputs = np.random.uniform(-1, 1, 60).reshape(4, 15)
gradients = (inputs[i, :] for i in range(4))
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_evects = np.array(
[[ 0.09139276, 0.01590518], [-0.32654025, -0.21681653],
[-0.34395421, 0.02836894], [0.27991908, -0.30031324],
[ 0.38410556, 0.02699885], [0.28666797, 0.13440149],
[-0.04537087, 0.40685452], [-0.35762976, -0.11629401],
[ 0.09873013, -0.18159307], [-0.08712574, -0.13981308],
[-0.14280701, 0.58827903], [-0.1142136, -0.39834724],
[-0.38691805, -0.03721504], [ 0.34202222, 0.03950987],
[-0.10594613, 0.32198557]])
[[1., 0.], [0., 1.], [0., 0.], [0., 0.], [0., 0.], [0., 0.],
[0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.],
[0., 0.], [0., 0.], [0., 0.]])
np.testing.assert_array_almost_equal(true_evects, ss.evects)

def test_activity_scores_01(self):
Expand Down Expand Up @@ -519,9 +521,13 @@ def test_plot_eigenvalues_04(self):

def test_plot_eigenvalues_05(self):
np.random.seed(42)
inputs = np.random.uniform(-1, 1, 500).reshape(10, 50)
gradients = (inputs[i, :] for i in range(10))
ss = ActiveSubspaces(dim=4, n_boot=200)
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_eigenvalues(figsize=(7, 7), title='Eigenvalues')
Expand Down Expand Up @@ -561,9 +567,13 @@ def test_plot_eigenvectors_04(self):

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)
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')
Expand Down Expand Up @@ -605,9 +615,13 @@ def test_plot_sufficient_summary_04(self):

def test_frequent_directions_01(self):
np.random.seed(42)
inputs = np.random.rand(100, 500)
gradients = (inputs[i, :] for i in range(100))
ss = ActiveSubspaces(dim=50, method='exact')
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)
evals, v = ss._frequent_directions(gradients=gradients)
self.assertEqual(v.shape, (500, 50))
self.assertEqual(evals.shape, (50,))
self.assertEqual(v.shape, (15, 2))
self.assertEqual(evals.shape, (2,))

0 comments on commit 1103c18

Please sign in to comment.