beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils module

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.compute_eigvals_eigvecs(score: torch.Tensor, node_val: torch.Tensor) Tuple[bool, torch.Tensor, torch.Tensor, torch.Tensor]

Compute hessian and returns eigen values and eigen vectors of the negative hessian inverse.

Parameters
  • score – the score function

  • node_val – the value to compute the hessian against

Returns

first gradient, eigen values and eigen vectors of the negative

hessian inverse

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.compute_first_gradient(score: torch.Tensor, node_val: torch.Tensor, create_graph: bool = False, retain_graph: Optional[bool] = None) Tuple[bool, torch.Tensor]

Computes the first gradient.

Parameters
  • score – the score to compute the gradient of

  • node_val – the value to compute the gradient against

Returns

the first gradient

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.compute_hessian(first_gradient: torch.Tensor, node_val: torch.Tensor) Tuple[bool, torch.Tensor]

Computes the hessian

Parameters

first_gradient – the first gradient of score with respect to

node_val :param node_val: the value to compute the hessian against :returns: computes hessian

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.hessian_of_log_prob(world: beanmachine.ppl.world.world.World, node: beanmachine.ppl.model.rv_identifier.RVIdentifier, transformed_node_val: torch.Tensor, hessian_fn: Callable, transform: torch.distributions.transforms.Transform = ComposeTransform()) Tuple[torch.Tensor, torch.Tensor]
beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.is_scalar(val: Union[float, torch.Tensor]) bool
Returns

whether val is a scalar

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.is_valid(vec: torch.Tensor) bool
Returns

whether a tensor is valid or not (not nan and not inf)

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.soft_abs_inverse(neg_hessian: torch.Tensor, alpha: float = 1000000.0) Tuple[torch.Tensor, torch.Tensor]

Compute inverse of a symmetric matrix and returns inverse, eigen values and eigen vectors.

Parameters
  • neg_hessian – the value that we’d like to compute the inverse of

  • alpha – the hardness parameter alpha for the SoftAbs map, see

(https://arxiv.org/pdf/1212.4693.pdf) :returns: eigen value and eigen vector of the negative hessian inverse

beanmachine.ppl.inference.proposer.newtonian_monte_carlo_utils.zero_grad(node_val: torch.Tensor) None

Zeros the gradient.