-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
polish examples; add jax as dependency; better error in sample function
- Loading branch information
1 parent
892540c
commit 0a3a4cf
Showing
7 changed files
with
75 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import equinox as eqx | ||
import jax.numpy as jnp | ||
from model import hists, model, observation | ||
|
||
import evermore as evm | ||
|
||
nll = evm.loss.PoissonNLL() | ||
|
||
|
||
@eqx.filter_jit | ||
def loss(model, hists, observation): | ||
expectations = model(hists) | ||
constraints = evm.loss.get_param_constraints(model) | ||
loss_val = nll( | ||
expectation=evm.util.sum_leaves(expectations), | ||
observation=observation, | ||
) | ||
# add constraint | ||
loss_val += evm.util.sum_leaves(constraints) | ||
return -jnp.sum(loss_val) | ||
|
||
|
||
loss_val = loss(model, hists, observation) | ||
grads = eqx.filter_grad(loss)(model, hists, observation) |
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,39 @@ | ||
import equinox as eqx | ||
import jax.numpy as jnp | ||
import optax | ||
from model import hists, model, observation | ||
|
||
import evermore as evm | ||
|
||
optim = optax.sgd(learning_rate=1e-2) | ||
opt_state = optim.init(eqx.filter(model, eqx.is_inexact_array)) | ||
|
||
nll = evm.loss.PoissonNLL() | ||
|
||
|
||
@eqx.filter_jit | ||
def loss(model, hists, observation): | ||
expectations = model(hists) | ||
constraints = evm.loss.get_param_constraints(model) | ||
loss_val = nll( | ||
expectation=evm.util.sum_leaves(expectations), | ||
observation=observation, | ||
) | ||
# add constraint | ||
loss_val += evm.util.sum_leaves(constraints) | ||
return -jnp.sum(loss_val) | ||
|
||
|
||
@eqx.filter_jit | ||
def make_step(model, opt_state, events, observation): | ||
# differentiate full analysis | ||
grads = eqx.filter_grad(loss)(model, events, observation) | ||
updates, opt_state = optim.update(grads, opt_state) | ||
# apply nuisance parameter and DNN weight updates | ||
model = eqx.apply_updates(model, updates) | ||
return model, opt_state | ||
|
||
|
||
# minimize model with 1000 steps | ||
for _ in range(1000): | ||
model, opt_state = make_step(model, opt_state, hists, observation) |
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