Exemple #1
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))
Exemple #2
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')
Exemple #3
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')
Exemple #4
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$'])
Exemple #5
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')
Exemple #6
0
 def test_plot_eigenvectors_01(self):
     ss = ActiveSubspaces()
     with self.assertRaises(ValueError):
         ss.plot_eigenvectors(figsize=(7, 7), title='Eigenvalues')
Exemple #7
0
# nor = Normalizer(lb, ub)
# x = nor.fit_transform(x_raw)
x_raw = inputs_gaussian(n_samples, mean, cov)
x = (x_raw-mean).dot(linalg.sqrtm(np.linalg.inv(cov)))

# Define the output of interest and compute the gradients
# func = partial(output, normalizer=nor, r=generatrix)
func = sin_2d
f = func(x)
df = egrad(func)(x)

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x[:,0], x[:,1], f)
plt.show()

# compute the active subspace
asub = ActiveSubspaces(dim=1, method='exact', n_boot=100)
asub.fit(gradients=df)

title = '2D sine'
asub.plot_eigenvalues(figsize=(6, 4), title=title)
print("Eigenvalues: {}".format(np.squeeze(asub.evals)))

asub.plot_eigenvectors(figsize=(6, 4), title=title)
asub.plot_sufficient_summary(x, f, figsize=(6, 4), title=title)

asub_2d = ActiveSubspaces(dim=2, method='exact', n_boot=100)
asub_2d.fit(gradients=df)
asub_2d.plot_sufficient_summary(x, f, figsize=(6, 4), title=title)
Exemple #8
0
#simulation parameters
np.random.seed(42)
n_samples = x_.shape[0]
input_dim = x_.shape[1]
d = fa_.shape[1]
dim = 1

#process data
x, f, df = x_, f_, df_
print("data", x.shape, f.shape, df.shape)

#AS
ss = ActiveSubspaces(dim=1)
ss.fit(inputs=x, outputs=f, gradients=df)
ss.plot_eigenvalues()
ss.plot_eigenvectors()
ss.plot_sufficient_summary(inputs=x, outputs=f)

## Active Subspaces with vectorial outputs
#process data
x, f, df = x_, fa_, dfa_.reshape(n_samples, d, input_dim)
print("data", x.shape, f.shape, df.shape)

#vectorial AS
vss = ActiveSubspaces(dim=5, n_boot=10)
vss.fit(inputs=x, outputs=f, gradients=df, metric=metric)
np.save("data/modes_AS", vss.W1)
vss.dim = 1
vss._partition()
vss.plot_eigenvalues()
vss.plot_eigenvectors()