Modelsο
models.base moduleο
Module for implementing and comparing different learning diffusion models using JAX.
This module provides an interface for writing code that seamlessly interface with the training and testing procedures.
- class models.base.LearningDiffusionModel[source]ο
Bases:
objectAn abstract base class representing a learning diffusion model, designed for use with JAX.
This interface serves as a blueprint for implementing new models to learn the different diffusion terms.
- abstract create_state(rng: PRNGKey) Tuple[TrainState, TrainState, TrainState][source]ο
Abstract method to initialize and return the modelβs state. Must be implemented by subclasses.
- Parameters:
rng (PRNGKey) β The random key to use for initializing the state.
- Returns:
A tuple containing the modelβs state for the potential, internal, and interaction terms.
Note: You can use less than three states if your model does not require all three terms, or if it uses a single, shared, state.
- Return type:
Tuple[TrainState, TrainState, TrainState]
- get_beta(state: Tuple[TrainState, TrainState, TrainState]) float[source]ο
Get the beta value from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
The beta value from the internal energy model.
- Return type:
float
- get_interaction(state: Tuple[TrainState, TrainState, TrainState]) Callable[[Array], Array][source]ο
Get the interaction function from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Function that computes the interaction.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- get_potential(state: Tuple[TrainState, TrainState, TrainState]) Callable[[Array], Array][source]ο
Get the potential function from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Function that computes the potential.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- load_dataset(dataset_name: str) Dataset[source]ο
Loads and prepares the specified dataset.
- Parameters:
dataset_name (str) β The name of the dataset to load.
- Returns:
The loaded dataset.
- Return type:
Dataset
- abstract train_step(state: Tuple[TrainState, TrainState, TrainState], sample: Tuple[Array, Array, Array, Array, Array, Array]) Tuple[Array, Tuple[TrainState, TrainState, TrainState]][source]ο
Perform a single training step.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
sample (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray]) β Training data sample consisting of xs, ys, t, ws, rho, and rho_grad.
- Returns:
The loss value and the updated training states.
- Return type:
Tuple[jnp.ndarray, Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]]
models.jkonet moduleο
This module contains the implementation of the JKOnet model and of the Monge gap regularization.
For JKOnet see https://arxiv.org/abs/2106.06345
For the Monge gap regularizer see https://arxiv.org/abs/2302.04953
Source: https://github.com/bunnech/jkonet
- class models.jkonet.JKOnet(config: Dict, data_dim: int, tau: float)[source]ο
Bases:
LearningDiffusionModelThis code ports https://github.com/bunnech/jkonet to our interface. See https://arxiv.org/abs/2106.06345
- create_state(rng: PRNGKey) Any[source]ο
Abstract method to initialize and return the modelβs state. Must be implemented by subclasses.
- Parameters:
rng (PRNGKey) β The random key to use for initializing the state.
- Returns:
A tuple containing the modelβs state for the potential, internal, and interaction terms.
Note: You can use less than three states if your model does not require all three terms, or if it uses a single, shared, state.
- Return type:
Tuple[TrainState, TrainState, TrainState]
- get_beta(_) float[source]ο
Get the beta value from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
The beta value from the internal energy model.
- Return type:
float
- get_interaction(_) Callable[[Array], Array][source]ο
Get the interaction function from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Function that computes the interaction.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- get_potential(state: TrainState) Callable[[Array], Array][source]ο
Get the potential function from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Function that computes the potential.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- load_dataset(dataset_name: str) PopulationDataset[source]ο
Loads and prepares the specified dataset.
- Parameters:
dataset_name (str) β The name of the dataset to load.
- Returns:
The loaded dataset.
- Return type:
Dataset
- loss_fn_energy(params_energy: Dict, rng_psi: PRNGKey, batch: Array, t: int) Tuple[float, Tuple[Array, Array]][source]ο
- train_step(state: TrainState, sample: List[List[ndarray]]) Tuple[Array, TrainState][source]ο
Perform a single training step.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
sample (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray]) β Training data sample consisting of xs, ys, t, ws, rho, and rho_grad.
- Returns:
The loss value and the updated training states.
- Return type:
Tuple[jnp.ndarray, Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]]
- class models.jkonet.JKOnetMongeGap(config: Dict, data_dim: int, tau: float)[source]ο
Bases:
JKOnetVanillaA variant of the JKOnet model without the use of ICNN (Input Convex Neural Networks) but instead using the Monge gap regularization.
models.jkonet_star moduleο
Module that implements the JKOnet* model based on the base interface.
The models are implemented using JAX and the FLAX library, following a functional paradigm to support efficient differentiation and optimization. The core classes include:
JKOnetStar: The full JKOnet* method, used for learning all the energy terms.JKOnetStarPotentialInternal: A variant focusing on potential and internal energies.JKOnetStarPotential: A variant focusing solely on the potential energy term.JKOnetStarTimePotential: A time-extended variant ofJKOnetStarPotential.JKOnetStarLinear: A model using linear parametrizations with various feature functions.
- class models.jkonet_star.JKOnetStar(config: dict, data_dim: int, tau: float)[source]ο
Bases:
LearningDiffusionModelThe full JKOnet* model for learning all energy terms.
- __init__(config: dict, data_dim: int, tau: float) None[source]ο
Initialize the JKOnetStar model.
- Parameters:
config (dict) β Configuration dictionary containing model and optimizer settings.
data_dim (int) β Dimension of the input data.
tau (float) β Represents the time scale over which the diffusion process described by the Fokker-Planck equation is considered.
- create_state(rng: PRNGKey) Tuple[TrainState, TrainState, TrainState][source]ο
Create initial training states for the potential, internal, and interaction models.
- Parameters:
rng (jax.random.PRNGKey) β Random key for initialization.
- Returns:
Tuple containing the training states for the potential, internal, and interaction models.
- Return type:
Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]
- create_state_from_params(potential_params: dict, internal_params: dict, interaction_params: dict) Tuple[TrainState, TrainState, TrainState][source]ο
Create training states from the provided parameters.
- Parameters:
potential_params (dict) β Parameters for the potential model.
internal_params (dict) β Parameters for the internal model.
interaction_params (dict) β Parameters for the interaction model.
- Returns:
Tuple containing the training states for the potential, internal, and interaction models.
- Return type:
Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]
- get_beta(state: Tuple[TrainState, TrainState, TrainState]) float[source]ο
Get the beta value from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
The beta value from the internal energy model.
- Return type:
float
- get_interaction(state: Tuple[TrainState, TrainState, TrainState]) Callable[[Array], Array][source]ο
Get the interaction function from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Function that computes the interaction.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- get_params(state: Tuple[TrainState, TrainState, TrainState]) Tuple[FrozenDict[str, Any], FrozenDict[str, Any], FrozenDict[str, Any]][source]ο
Get parameters from the training state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Tuple containing the parameters for the potential, internal, and interaction models.
- Return type:
Tuple[dict, dict, dict]
- get_potential(state: Tuple[TrainState, TrainState, TrainState]) Callable[[Array], Array][source]ο
Get the potential function from the model state.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
- Returns:
Function that computes the potential.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- load_dataset(dataset_name: str) CouplingsDataset[source]ο
Load and return a dataset based on the given dataset name.
This method creates an instance of the CouplingsDataset class using the specified dataset name.
- Parameters:
dataset_name (str) β The name of the dataset to load. This name is used to locate and initialize the dataset.
- Returns:
An instance of the CouplingsDataset class, which contains the loaded dataset.
- Return type:
- loss(potential_params: dict, internal_params: dict, interaction_params: dict, xs: Array, ys: Array, ws: Array, rho: Array, rho_grad: Array) Array[source]ο
Compute the total loss for the model by combining potential, internal, and interaction terms.
- Parameters:
potential_params (dict) β Parameters for the potential model.
internal_params (dict) β Parameters for the internal model.
interaction_params (dict) β Parameters for the interaction model.
xs (jnp.ndarray) β Initial particle distribution.
ys (jnp.ndarray) β Target particle distribution.
ws (jnp.ndarray) β Weights of the couplings.
rho (jnp.ndarray) β Density values.
rho_grad (jnp.ndarray) β Gradient of density values.
- Returns:
Total loss value.
- Return type:
jnp.ndarray
- train_step(state: Tuple[TrainState, TrainState, TrainState], sample: Tuple[Array, Array, Array, Array, Array, Array]) Tuple[Array, Tuple[TrainState, TrainState, TrainState]][source]ο
Perform a single training step.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β Training state containing potential, internal, and interaction models.
sample (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray]) β Training data sample consisting of xs, ys, t, ws, rho, and rho_grad.
- Returns:
The loss value and the updated training states.
- Return type:
Tuple[jnp.ndarray, Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]]
- class models.jkonet_star.JKOnetStarLinear(config: Dict[str, Dict[str, int | float | bool] | float], data_dim: int, tau: float)[source]ο
Bases:
LearningDiffusionModelThe linear parametrization of the JKOnet* model.
- __init__(config: Dict[str, Dict[str, int | float | bool] | float], data_dim: int, tau: float) None[source]ο
Initializes the JKOnetStarLinear model with configuration and data dimensions.
- Parameters:
config (dict) β Configuration dictionary specifying model parameters and feature settings.
data_dim (int) β Dimensionality of the input data.
tau (float) β Represents the time scale over which the diffusion process described by the Fokker-Planck equation is considered.
- create_state(_) Tuple[Array, Array, Array][source]ο
Creates the initial state for the model.
This method returns a tuple of zero-initialized arrays corresponding to the potential, interaction, and internal parameters.
- Parameters:
_ (Ignored) β Placeholder for compatibility, not used.
- Returns:
The initial state consisting of zero-initialized potential, interaction, and internal parameters.
- Return type:
Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]
- create_state_from_params(potential_params: Array, interaction_params: Array, internal_params: Array) Tuple[Array, Array, Array][source]ο
Creates the state from given parameters.
- Parameters:
potential_params (jnp.ndarray) β The parameters for the potential term.
interaction_params (jnp.ndarray) β The parameters for the interaction term.
internal_params (jnp.ndarray) β The parameters for the internal term.
- Returns:
A tuple containing the potential, interaction, and internal parameters.
- Return type:
Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]
- features(x: Array) Array[source]ο
Computes the feature functions for the input data x.
- Parameters:
x (jnp.ndarray) β Input data for which to compute feature functions.
- Returns:
The computed feature functions for the input data.
- Return type:
jnp.ndarray
- property features_dim: intο
The dimension of the feature space.
Computes and caches the dimension of the feature space based on the feature functions.
- Returns:
The dimension of the feature space.
- Return type:
int
- features_grad(x: Array) Array[source]ο
Computes the gradients of the feature functions with respect to x.
- Parameters:
x (jnp.ndarray) β Input data for which to compute gradients of feature functions.
- Returns:
The gradients of the feature functions with respect to the input data.
- Return type:
jnp.ndarray
- get_beta(state: Tuple[Array, Array, Array]) float[source]ο
Returns the value of the internal parameter (beta) based on the current state.
- Parameters:
state (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]) β The current state containing the potential, interaction, and internal parameters.
- Returns:
The beta value from the internal energy model.
- Return type:
float
- get_interaction(state: Tuple[Array, Array, Array]) Callable[[Array], Array][source]ο
Returns the interaction function based on the current state.
- Parameters:
state (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]) β The current state containing the potential, interaction, and internal parameters.
- Returns:
A function that computes the interaction for a given input x.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- get_params(state: Tuple[Array, Array, Array]) Tuple[Array, Array, Array][source]ο
Retrieves the parameters from the given state.
- Parameters:
state (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]) β The current state containing the potential, interaction, and internal parameters.
- Returns:
The potential, interaction, and internal parameters extracted from the state.
- Return type:
Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]
- get_potential(state: Tuple[Array, Array, Array]) Callable[[Array], Array][source]ο
Returns the potential function based on the current state.
- Parameters:
state (Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]) β The current state containing the potential, interaction, and internal parameters.
- Returns:
A function that computes the potential for a given input x.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- load_dataset(dataset_name: str) LinearParametrizationDataset[source]ο
Loads and returns the dataset for linear parametrizations.
- Parameters:
dataset_name (str) β Name of the dataset to load.
- Returns:
The dataset object for linear parametrizations.
- Return type:
- train_step(_, all_samples: Tuple) Tuple[Array, Tuple[Array, Array, Array]][source]ο
Performs a single training step, updating the modelβs parameters.
This method solves the least squares problem to update the model parameters based on the provided samples.
- Parameters:
_ (Ignored) β Placeholder for compatibility, not used.
all_samples (Tuple) β A tuple of all the samples, where each sample contains (xs, ys, t, ws, rho, rho_grad).
- Returns:
The error and the updated parameters (potential, interaction, internal).
- Return type:
Tuple[jnp.ndarray, Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]]
- class models.jkonet_star.JKOnetStarPotential(config: dict, data_dim: int, tau: float)[source]ο
Bases:
JKOnetStarPotentialInternalA variant of the JKOnetStar model to learn only the potential term.
- get_beta(state: Tuple[TrainState, TrainState, TrainState]) float[source]ο
Return a constant zero value for the beta term of the internal energy model.
- Parameters:
state (Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]) β The current states of the potential, internal, and interaction models.
- Returns:
The constant zero value for the beta term of the internal energy model.
- Return type:
float
- loss(potential_params: dict, xs: Array, ys: Array, ws: Array, rho: Array, rho_grad: Array) Array[source]ο
Compute the loss for the potential term of the model.
- Parameters:
potential_params (dict) β Parameters of the potential model.
xs (jnp.ndarray) β Initial particle distribution.
ys (jnp.ndarray) β Target particle distribution.
ws (jnp.ndarray) β Weights of the couplings.
rho (jnp.ndarray) β Density values for the data samples.
rho_grad (jnp.ndarray) β Gradient of the density values.
- Returns:
The computed loss value.
- Return type:
jnp.ndarray
- class models.jkonet_star.JKOnetStarPotentialInternal(config: dict, data_dim: int, tau: float)[source]ο
Bases:
JKOnetStarA specialized variant of the JKOnetStar model that only considers potential and internal terms.
- get_interaction(_) Callable[[Array], Array][source]ο
Returns a function representing the interaction term.
This implementation returns a constant zero function, as the interaction is not used in this variant.
- Parameters:
_ (Any) β Unused parameter in this context.
- Returns:
A function that always returns 0.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- loss(potential_params: dict, internal_params: dict, xs: Array, ys: Array, ws: Array, rho: Array, rho_grad: Array) Array[source]ο
Compute the loss for the potential and internal terms of the model.
- Parameters:
potential_params (dict) β Parameters of the potential model.
internal_params (dict) β Parameters of the internal model.
xs (jnp.ndarray) β Initial particle distribution.
ys (jnp.ndarray) β Target particle distribution.
ws (jnp.ndarray) β Weights of the couplings.
rho (jnp.ndarray) β Density values for the data samples.
rho_grad (jnp.ndarray) β Gradient of the density values.
- Returns:
The computed loss value.
- Return type:
jnp.ndarray
- class models.jkonet_star.JKOnetStarTimePotential(config: dict, data_dim: int, tau: float)[source]ο
Bases:
JKOnetStarPotentialA variant of the JKOnetStarPotential model that incorporates time information in the potential term.
- create_state(rng: PRNGKey) Tuple[TrainState, TrainState, TrainState][source]ο
Creates initial training states for the potential, internal, and interaction models.
- Parameters:
rng (jax.random.PRNGKey) β Random key for JAX-based random number generation.
- Returns:
The initial states for the potential, internal, and interaction models.
- Return type:
Tuple[train_state.TrainState, train_state.TrainState, train_state.TrainState]
- loss(potential_params: dict, xs: Array, ys: Array, t: Array, ws: Array, rho: Array, rho_grad: Array) Array[source]ο
Computes the total loss for the model, considering the potential term with time information.
- Parameters:
potential_params (dict) β Parameters of the potential model.
xs (jnp.ndarray) β Initial particle distribution.
ys (jnp.ndarray) β Target particle distribution.
t (jnp.ndarray) β Timestep of the target particle distribution.
ws (jnp.ndarray) β Weights of the couplings.
rho (jnp.ndarray) β Density values for the data samples.
rho_grad (jnp.ndarray) β Gradient of the density values.
- Returns:
The computed loss value.
- Return type:
jnp.ndarray