-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from Joshuaalbert/develop
Develop
- Loading branch information
Showing
13 changed files
with
188 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import time | ||
|
||
import jax | ||
import jax.numpy as jnp | ||
import numpy as np | ||
import pkg_resources | ||
import tensorflow_probability.substrates.jax as tfp | ||
from jax._src.scipy.linalg import solve_triangular | ||
|
||
from jaxns import Model, Prior, DefaultNestedSampler | ||
|
||
tfpd = tfp.distributions | ||
|
||
|
||
def run_model(key): | ||
def log_normal(x, mean, cov): | ||
L = jnp.linalg.cholesky(cov) | ||
dx = x - mean | ||
dx = solve_triangular(L, dx, lower=True) | ||
return -0.5 * x.size * jnp.log(2. * jnp.pi) - jnp.sum(jnp.log(jnp.diag(L))) \ | ||
- 0.5 * dx @ dx | ||
|
||
ndims = 8 | ||
prior_mu = 15 * jnp.ones(ndims) | ||
prior_cov = jnp.diag(jnp.ones(ndims)) ** 2 | ||
|
||
data_mu = jnp.zeros(ndims) | ||
data_cov = jnp.diag(jnp.ones(ndims)) ** 2 | ||
data_cov = jnp.where(data_cov == 0., 0.99, data_cov) | ||
|
||
log_Z_true = log_normal(data_mu, prior_mu, prior_cov + data_cov) | ||
# not super happy with this being 1.58 and being off by like 0.1. Probably related to the ESS. | ||
post_mu = prior_cov @ jnp.linalg.inv(prior_cov + data_cov) @ data_mu + data_cov @ jnp.linalg.inv( | ||
prior_cov + data_cov) @ prior_mu | ||
|
||
# print(f"True post mu:{post_mu}") | ||
# print(f"True log Z: {log_Z_true}") | ||
|
||
def prior_model(): | ||
x = yield Prior( | ||
tfpd.MultivariateNormalTriL(loc=prior_mu, scale_tril=jnp.linalg.cholesky(prior_cov)), | ||
name='x') | ||
return x | ||
|
||
def log_likelihood(x): | ||
return tfpd.MultivariateNormalTriL(loc=data_mu, scale_tril=jnp.linalg.cholesky(data_cov)).log_prob(x) | ||
|
||
model = Model(prior_model=prior_model, log_likelihood=log_likelihood) | ||
|
||
ns = DefaultNestedSampler(model=model, max_samples=100000, verbose=False) | ||
|
||
termination_reason, state = ns(key) | ||
results = ns.to_results(termination_reason=termination_reason, state=state, trim=False) | ||
return results.log_Z_mean - log_Z_true, results.log_Z_uncert | ||
|
||
|
||
def main(): | ||
jaxns_version = pkg_resources.get_distribution("jaxns").version | ||
m = 10 | ||
run_model_aot = jax.jit(run_model).lower(jax.random.PRNGKey(0)).compile() | ||
dt = [] | ||
|
||
errors = [] | ||
uncerts = [] | ||
|
||
for i in range(m): | ||
t0 = time.time() | ||
log_Z_error, log_Z_uncert = run_model_aot(jax.random.PRNGKey(i)) | ||
log_Z_error.block_until_ready() | ||
t1 = time.time() | ||
dt.append(t1 - t0) | ||
errors.append(log_Z_error) | ||
uncerts.append(log_Z_uncert) | ||
total_time = sum(dt) | ||
best_3 = sum(sorted(dt)[:3]) / 3. | ||
# print(f"Errors: {errors}") | ||
# print(f"Uncerts: {uncerts}") | ||
print(f"JAXNS {jaxns_version}\n" | ||
f"\tMean error: {np.mean(errors)}\n" | ||
f"\tMean uncert: {np.mean(uncerts)}\n" | ||
f"Avg. time taken: {total_time / m:.5f} seconds.\n" | ||
f"The best 3 of {m} runs took {best_3:.5f} seconds.") | ||
|
||
with open('results', 'a') as fp: | ||
fp.write(f"{jaxns_version},{np.mean(errors)},{np.mean(uncerts)},{total_time / m},{best_3}\n") | ||
|
||
# Before fix | ||
# 2.5.0,2.851858615875244,0.3351728320121765,0.7272443532943725,0.7075355052947998 | ||
|
||
# After fix | ||
# 2.4.12,0.42119064927101135,0.3309990465641022,1.001870584487915,0.9800511995951334 | ||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
2.4.6,0.42119064927101135,0.3309990465641022,1.0280845165252686,0.9580310185750326 | ||
2.4.7,0.42119064927101135,0.3309990465641022,0.9979925632476807,0.961020310719808 | ||
2.4.8,0.42119064927101135,0.3309990465641022,1.006078839302063,0.9840319156646729 | ||
2.4.10,0.42119064927101135,0.3309990465641022,0.9860331773757934,0.9623709519704183 | ||
2.4.11,0.42119064927101135,0.3309990465641022,1.037106418609619,1.0193532307942708 | ||
2.4.12,0.42119064927101135,0.3309990465641022,1.001870584487915,0.9800511995951334 | ||
2.4.13,2.851858615875244,0.3351728320121765,0.7272443532943725,0.7075355052947998 | ||
2.5.0,2.851858615875244,0.3351728320121765,0.7534827709197998,0.739771842956543 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
|
||
# Array of jaxns versions to be installed | ||
declare -a jaxns_versions=("2.4.6" "2.4.7" "2.4.8" "2.4.10" "2.4.11" "2.4.12" "2.4.13" "2.5.0") | ||
|
||
# Path to your benchmark script | ||
benchmark_script="main.py" | ||
|
||
# Name for the conda environment | ||
conda_env_name="jaxns_benchmarks_env" | ||
|
||
# Function to create Conda environment and install jaxns | ||
create_and_activate_env() { | ||
local version=$1 | ||
echo "Creating Conda environment for jaxns version $version with Python 3.11..." | ||
conda create --name $conda_env_name python=3.11 -y | ||
eval "$(conda shell.bash hook)" | ||
conda activate $conda_env_name | ||
pip install jaxns==$version | ||
} | ||
|
||
# Function to tear down Conda environment | ||
tear_down_env() { | ||
echo "Tearing down Conda environment..." | ||
conda deactivate | ||
conda env remove --name $conda_env_name | ||
} | ||
|
||
# Main loop to install each version, run benchmark, and tear down env | ||
for version in "${jaxns_versions[@]}"; do | ||
create_and_activate_env $version | ||
python $benchmark_script | ||
tear_down_env | ||
done | ||
|
||
echo "Benchmarking complete." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.