gluonts.torch.model.mqf2.icnn_utils module#

class gluonts.torch.model.mqf2.icnn_utils.DeepConvexNet(picnn: Module, dim: int, is_energy_score: bool = False, estimate_logdet: bool = False, m1: int = 10, m2: Optional[int] = None, rtol: float = 0.0, atol: float = 0.001)[source]#

Bases: DeepConvexFlow

Class that takes a partially input convex neural network (picnn) as input and equips it with functions of logdet computation (both estimation and exact computation)

This class is based on DeepConvexFlow of the CP-Flow repo (https://github.com/CW-Huang/CP-Flow)

For details of the logdet estimator, see Convex potential flows: Universal probability distributions with optimal transport and convex optimization

Parameters:
  • picnn – A partially input convex neural network (picnn)

  • dim – Dimension of the input

  • is_energy_score – Indicates if energy score is used as the objective function If yes, the network is not required to be strictly convex, so we can just use the picnn otherwise, a quadratic term is added to the output of picnn to render it strictly convex

  • m1 – Dimension of the Krylov subspace of the Lanczos tridiagonalization used in approximating H of logdet(H)

  • m2 – Iteration number of the conjugate gradient algorithm used to approximate logdet(H)

  • rtol – relative tolerance of the conjugate gradient algorithm

  • atol – absolute tolerance of the conjugate gradient algorithm

forward_transform(x: Tensor, logdet: Optional[Union[float, Tensor]] = 0.0, context: Optional[Tensor] = None, extra: Optional[Tensor] = None) Union[Tensor, Tuple[Tensor, Tensor]][source]#
get_potential(x: Tensor, context: Optional[Tensor] = None) Tensor[source]#
class gluonts.torch.model.mqf2.icnn_utils.SequentialNet(networks: List[Module])[source]#

Bases: SequentialFlow

Class that combines a list of DeepConvexNet and ActNorm layers and provides energy score computation.

This class is based on SequentialFlow of the CP-Flow repo (https://github.com/CW-Huang/CP-Flow)

Parameters:

networks – list of DeepConvexNet and/or ActNorm instances

energy_score(z: Tensor, hidden_state: Tensor, es_num_samples: int = 50, beta: float = 1.0) Tensor[source]#

Computes the (approximated) energy score sum_i ES(g,z_i), where ES(g,z_i) =

-1/(2*es_num_samples^2) * sum_{w,w’} ||w-w’||_2^beta + 1/es_num_samples * sum_{w’’} ||w’’-z_i||_2^beta, w’s are samples drawn from the quantile function g(., h_i) (gradient of picnn), h_i is the hidden state associated with z_i, and es_num_samples is the number of samples drawn for each of w, w’, w’’ in energy score approximation

Parameters:
  • z – Observations (numel_batch, dimension)

  • hidden_state – Hidden state (numel_batch, hidden_size)

  • es_num_samples – Number of samples drawn for each of w, w’, w’’ in energy score approximation

  • beta – Hyperparameter of the energy score, see the formula above

Returns:

energy score (numel_batch)

Return type:

loss

es_sample(hidden_state: Tensor, dimension: int) Tensor[source]#

Auxiliary function for energy score computation Drawing samples conditioned on the hidden state.

Parameters:
  • hidden_state – hidden_state which the samples conditioned on (num_samples, hidden_size)

  • dimension – dimension of the input

Returns:

samples drawn (num_samples, dimension)

Return type:

samples

forward(x: Tensor, context: Optional[Tensor] = None) Tensor[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.