Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laplace approximation to posterior #20

Merged
merged 29 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/CI_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ jobs:
cd ./hippylibX/test &&
mpirun -n 2 python3 test_eigendecomposition.py

- name: low rank Hessian testing
run: |
cd ./hippylibX/test &&
mpirun -n 2 python3 test_lowRankHessian.py

- name: run serial check
run: |
cd ./hippylibX/test &&
Expand Down
90 changes: 77 additions & 13 deletions example/poisson_dirichlet_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from matplotlib import pyplot as plt
from typing import Sequence, Dict


sys.path.append(os.environ.get("HIPPYLIBX_BASE_DIR", "../"))
import hippylibX as hpx

Expand Down Expand Up @@ -215,17 +214,78 @@ def top_bottom_boundary(x: Sequence[float]) -> Sequence[bool]:

hpx.parRandom.normal(1.0, Omega)

d, U = hpx.doublePassG(
Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1, check=False
)
d, U = hpx.doublePassG(Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1)

eigen_decomposition_results = {
"A": Hmisfit.mat,
"B": prior.R,
"k": k,
"d": d,
"U": U,
}
lap_aprx = hpx.LaplaceApproximator(prior, d, U)
lap_aprx.mean = prior.generate_parameter(0)
lap_aprx.mean.array[:] = x[hpx.PARAMETER].array[:]

m_prior = prior.generate_parameter(0)
m_post = prior.generate_parameter(0)

noise = prior.generate_parameter("noise")

######################################################
uvilla marked this conversation as resolved.
Show resolved Hide resolved
hpx.parRandom.normal(1.0, noise)
lap_aprx.sample(noise, m_prior, m_post)

prior_sample = hpx.vector2Function(m_prior, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "dirichlet_Poisson_prior_sample_1_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(prior_sample)

posterior_sample = hpx.vector2Function(m_post, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm,
"dirichlet_Poisson_posterior_sample_1_np{0:d}_X.xdmf".format(nproc),
"w",
) as file:
file.write_mesh(msh)
file.write_function(posterior_sample)

######################################################
hpx.parRandom.normal(1.0, noise)
lap_aprx.sample(noise, m_prior, m_post)

prior_sample = hpx.vector2Function(m_prior, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "dirichlet_Poisson_prior_sample_2_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(prior_sample)

posterior_sample = hpx.vector2Function(m_post, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm,
"dirichlet_Poisson_posterior_sample_2_np{0:d}_X.xdmf".format(nproc),
"w",
) as file:
file.write_mesh(msh)
file.write_function(posterior_sample)

######################################################
hpx.parRandom.normal(1.0, noise)
lap_aprx.sample(noise, m_prior, m_post)

prior_sample = hpx.vector2Function(m_prior, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "dirichlet_Poisson_prior_sample_3_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(prior_sample)

posterior_sample = hpx.vector2Function(m_post, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm,
"dirichlet_Poisson_posterior_sample_3_np{0:d}_X.xdmf".format(nproc),
"w",
) as file:
file.write_mesh(msh)
file.write_function(posterior_sample)

eigen_decomposition_results = {"A": Hmisfit.mat, "B": prior, "k": k, "d": d, "U": U}

final_results = {
"data_misfit_True": data_misfit_True,
Expand All @@ -241,8 +301,12 @@ def top_bottom_boundary(x: Sequence[float]) -> Sequence[bool]:
if __name__ == "__main__":
nx = 64
ny = 64
noise_variance = 1e-4
prior_param = {"gamma": 0.03, "delta": 0.3}
# noise_variance = 1e-4
# prior_param = {"gamma": 0.03, "delta": 0.3}

noise_variance = (1 / 100) * 1e-4
prior_param = {"gamma": 10 * 0.03, "delta": 10 * 0.3}

final_results = run_inversion(nx, ny, noise_variance, prior_param)
k, d = (
final_results["eigen_decomposition_results"]["k"],
Expand Down
6 changes: 2 additions & 4 deletions example/poisson_dirichlet_example_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,11 @@ def top_bottom_boundary(x: Sequence[float]) -> Sequence[bool]:

hpx.parRandom.normal(1.0, Omega)

d, U = hpx.doublePassG(
Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1, check=False
)
d, U = hpx.doublePassG(Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1)

eigen_decomposition_results = {
"A": Hmisfit.mat,
"B": prior.R,
"B": prior,
"k": k,
"d": d,
"U": U,
Expand Down
6 changes: 2 additions & 4 deletions example/poisson_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,11 @@ def run_inversion(

hpx.parRandom.normal(1.0, Omega)

d, U = hpx.doublePassG(
Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1, check=False
)
d, U = hpx.doublePassG(Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1)

eigen_decomposition_results = {
"A": Hmisfit.mat,
"B": prior.R,
"B": prior,
"k": k,
"d": d,
"U": U,
Expand Down
6 changes: 2 additions & 4 deletions example/poisson_example_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,11 @@ def run_inversion(

hpx.parRandom.normal(1.0, Omega)

d, U = hpx.doublePassG(
Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1, check=False
)
d, U = hpx.doublePassG(Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1)

eigen_decomposition_results = {
"A": Hmisfit.mat,
"B": prior.R,
"B": prior,
"k": k,
"d": d,
"U": U,
Expand Down
92 changes: 74 additions & 18 deletions example/sfsi_toy_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,6 @@ def run_inversion(
optimizer_results["optimizer"] = True
else:
optimizer_results["optimizer"] = False
final_results = {
"data_misfit_True": data_misfit_True,
"data_misfit_False": data_misfit_False,
"optimizer_results": optimizer_results,
}

Hmisfit = hpx.ReducedHessian(model, misfit_only=True)

Expand All @@ -228,17 +223,72 @@ def run_inversion(

hpx.parRandom.normal(1.0, Omega)

d, U = hpx.doublePassG(
Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1, check=False
)
d, U = hpx.doublePassG(Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1)

eigen_decomposition_results = {
"A": Hmisfit.mat,
"B": prior.R,
"k": k,
"d": d,
"U": U,
}
lap_aprx = hpx.LaplaceApproximator(prior, d, U)
lap_aprx.mean = prior.generate_parameter(0)
lap_aprx.mean.array[:] = x[hpx.PARAMETER].array[:]

m_prior = prior.generate_parameter(0)
m_post = prior.generate_parameter(0)

noise = prior.generate_parameter("noise")

######################################################
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above (multiple samples in the same file)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification made.

hpx.parRandom.normal(1.0, noise)
lap_aprx.sample(noise, m_prior, m_post)

prior_sample = hpx.vector2Function(m_prior, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "pact_prior_sample_1_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(prior_sample)

posterior_sample = hpx.vector2Function(m_post, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "pact_posterior_sample_1_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(posterior_sample)

######################################################
hpx.parRandom.normal(1.0, noise)
lap_aprx.sample(noise, m_prior, m_post)

prior_sample = hpx.vector2Function(m_prior, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "pact_prior_sample_2_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(prior_sample)

posterior_sample = hpx.vector2Function(m_post, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "pact_posterior_sample_2_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(posterior_sample)

######################################################
hpx.parRandom.normal(1.0, noise)
lap_aprx.sample(noise, m_prior, m_post)

prior_sample = hpx.vector2Function(m_prior, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "pact_prior_sample_3_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(prior_sample)

posterior_sample = hpx.vector2Function(m_post, Vh[hpx.PARAMETER])
with dlx.io.XDMFFile(
msh.comm, "pact_posterior_sample_3_np{0:d}_X.xdmf".format(nproc), "w"
) as file:
file.write_mesh(msh)
file.write_function(posterior_sample)

eigen_decomposition_results = {"A": Hmisfit.mat, "B": prior, "k": k, "d": d, "U": U}

final_results = {
"data_misfit_True": data_misfit_True,
Expand All @@ -247,15 +297,21 @@ def run_inversion(
"eigen_decomposition_results": eigen_decomposition_results,
}

print(comm.rank, ":", misfit.cost(x))
uvilla marked this conversation as resolved.
Show resolved Hide resolved

return final_results
#######################################
######################################


if __name__ == "__main__":
nx = 64
ny = 64
noise_variance = 1e-6
prior_param = {"gamma": 0.040, "delta": 0.8}
# noise_variance = 1e-6
# prior_param = {"gamma": 0.040, "delta": 0.8}

noise_variance = (1 / 100) * 1e-6
prior_param = {"gamma": 10 * 0.040, "delta": 10 * 0.8}

mesh_filename = "./meshes/circle.xdmf"
final_results = run_inversion(mesh_filename, nx, ny, noise_variance, prior_param)
k, d = (
Expand Down
6 changes: 2 additions & 4 deletions example/sfsi_toy_gaussian_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,11 @@ def run_inversion(

hpx.parRandom.normal(1.0, Omega)

d, U = hpx.doublePassG(
Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1, check=False
)
d, U = hpx.doublePassG(Hmisfit.mat, prior.R, prior.Rsolver, Omega, k, s=1)

eigen_decomposition_results = {
"A": Hmisfit.mat,
"B": prior.R,
"B": prior,
"k": k,
"d": d,
"U": U,
Expand Down
1 change: 1 addition & 0 deletions hippylibX/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .NewtonCG import ReducedSpaceNewtonCG, ReducedSpaceNewtonCG_ParameterList # noqa
from .multivector import MultiVector, MatMvMult, MatMvTranspmult, MvDSmatMult # noqa
from .randomizedEigensolver import doublePassG # noqa
from .lowRankOperator import LowRankOperator # noqa
Loading
Loading