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

Use Hessian formulation of Fisher information in make_empirical_fisher_vp #430

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Changes from all commits
Commits
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
18 changes: 11 additions & 7 deletions chirho/robust/internals/linearize.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,21 @@ def make_empirical_fisher_vp(
randomness="different",
)

N = data[next(iter(data))].shape[0] # type: ignore
mean_vector = 1 / N * torch.ones(N)

def bound_batched_func_log_prob(params: ParamDict) -> torch.Tensor:
return batched_func_log_prob(params, data)

def jvp_fn(v: ParamDict) -> torch.Tensor:
return torch.func.jvp(bound_batched_func_log_prob, (log_prob_params,), (v,))[1]

vjp_fn = torch.func.vjp(bound_batched_func_log_prob, log_prob_params)[1]

def _empirical_fisher_vp(v: ParamDict) -> ParamDict:
jvp_log_prob_v = jvp_fn(v)
return vjp_fn(jvp_log_prob_v / jvp_log_prob_v.shape[0])[0]
def jvp_fn(log_prob_params: ParamDict) -> torch.Tensor:
return torch.func.jvp(
bound_batched_func_log_prob, (log_prob_params,), (v,)
)[1]

# Perlmutter's trick
vjp_fn = torch.func.vjp(jvp_fn, log_prob_params)[1]
return vjp_fn(-1 * mean_vector)[0] # Fisher = -E[Hessian]

return _empirical_fisher_vp

Expand Down
Loading