Networksο
networks.energies moduleο
Models for energy functions.
- class networks.energies.MLP(dim_hidden: ~typing.Sequence[int], act_fn: ~typing.Callable = <PjitFunction of <function softplus>>, parent: ~typing.Type[~flax.linen.module.Module] | ~flax.core.scope.Scope | ~typing.Type[~flax.linen.module._Sentinel] | None = <flax.linen.module._Sentinel object>, name: str | None = None)[source]ο
Bases:
ModuleSimple energy model.
Source: https://github.com/bunnech/jkonet
- act_fn() Arrayο
Softplus activation function.
Computes the element-wise function
\[\mathrm{softplus}(x) = \log(1 + e^x)\]- Parameters:
x β input array
- name: str | None = Noneο
- parent: Type[Module] | Scope | Type[_Sentinel] | None = Noneο
- scope: Scope | None = Noneο
- setup()[source]ο
Initializes a Module lazily (similar to a lazy
__init__).setupis called once lazily on a module instance when a module is bound, immediately before any other methods like__call__are invoked, or before asetup-defined attribute onselfis accessed.This can happen in three cases:
Immediately when invoking
apply(),init()orinit_and_output().Once the module is given a name by being assigned to an attribute of another module inside the other moduleβs
setupmethod (see__setattr__()):>>> class MyModule(nn.Module): ... def setup(self): ... submodule = nn.Conv(...) ... # Accessing `submodule` attributes does not yet work here. ... # The following line invokes `self.__setattr__`, which gives ... # `submodule` the name "conv1". ... self.conv1 = submodule ... # Accessing `submodule` attributes or methods is now safe and ... # either causes setup() to be called once.
Once a module is constructed inside a method wrapped with
compact(), immediately before another method is called orsetupdefined attribute is accessed.
networks.fixpoint_loop moduleο
This module provides a backprop-friendly fixed point loop implementation for JAX.
Authors: Jonathan Heek, Marco Cuturi, Charlotte Bunne Source: https://github.com/bunnech/jkonet
- networks.fixpoint_loop.fixpoint_iter_bwd(cond_fn: Callable[[int, Any, Any], bool], body_fn: Callable[[int, Any, Any, bool], Any], min_iterations: int, max_iterations: int, inner_iterations: int, res: Tuple[Any, int, Any], g: Any) Tuple[Any, Any][source]ο
Backward pass for the fixed-point iteration loop.
- Parameters:
cond_fn (Callable[[int, Any, Any], bool]) β A function that was used in the forward pass to determine whether the loop should continue.
body_fn (Callable[[int, Any, Any, bool], Any]) β A function that defines the operations performed in each iteration during the forward pass.
min_iterations (int) β The minimum number of iterations that was performed in the forward pass.
max_iterations (int) β The maximum number of iterations that was performed in the forward pass.
inner_iterations (int) β The number of iterations in each inner loop during the forward pass.
res (Tuple[Any, int, Any]) β A tuple containing the constants, final iteration count, and recorded intermediate states from the forward pass.
g (Any) β The gradient with respect to the final state.
- Returns:
A tuple containing the gradients with respect to the constants and the initial state.
- Return type:
Tuple[Any, Any]
- networks.fixpoint_loop.fixpoint_iter_fwd(cond_fn: Callable[[int, Any, Any], bool], body_fn: Callable[[int, Any, Any, bool], Any], min_iterations: int, max_iterations: int, inner_iterations: int, constants: Any, state: Any) Tuple[Any, Tuple[Any, int, Any]][source]ο
Forward pass for the fixed-point iteration loop, storing intermediate states.
- Parameters:
cond_fn (Callable[[int, Any, Any], bool]) β A function that determines whether the loop should continue based on the current iteration, constants, and state.
body_fn (Callable[[int, Any, Any, bool], Any]) β A function that defines the operations to perform in each iteration. It takes the current iteration number, constants, state, and a boolean indicating whether to compute an error.
min_iterations (int) β Lower bound on the total number of fixed point iterations.
max_iterations (int) β Upper bound on the total number of fixed point iterations.
inner_iterations (int) β Default number of iterations in the inner loop.
constants (Any) β Constant parameters passed to the body function during the loop.
state (Any) β The initial state of the loop.
- Returns:
The final state after the loop terminates, and a tuple containing the constants, the final iteration number, and the recorded intermediate states.
- Return type:
Tuple[Any, Tuple[Any, int, Any]]
networks.icnns moduleο
This module contains the implementation of the Input Convex Neural Network (ICNN) model.
Source: https://github.com/bunnech/jkonet
- class networks.icnns.Dense(dim_hidden: int, beta: float = 1.0, use_bias: bool = True, dtype: Any = <class 'jax.numpy.float32'>, precision: Any = None, kernel_init: Callable[[Any, Tuple[int], Any], Any] = <function variance_scaling.<locals>.init at 0x79afb9706b60>, bias_init: Callable[[Any, Tuple[int], Any], Any] = <function zeros at 0x79b076036b60>, parent: Union[Type[flax.linen.module.Module], flax.core.scope.Scope, Type[flax.linen.module._Sentinel], NoneType] = <flax.linen.module._Sentinel object at 0x79afbb953cb0>, name: Optional[str] = None)[source]ο
Bases:
Module- beta: float = 1.0ο
- bias_init(shape: ~collections.abc.Sequence[int | ~typing.Any], dtype: ~typing.Any = <class 'jax.numpy.float64'>) Arrayο
An initializer that returns a constant array full of zeros.
The
keyargument is ignored.>>> import jax, jax.numpy as jnp >>> jax.nn.initializers.zeros(jax.random.key(42), (2, 3), jnp.float32) Array([[0., 0., 0.], [0., 0., 0.]], dtype=float32)
- dtypeο
alias of
float32
- kernel_init(shape: ~collections.abc.Sequence[int | ~typing.Any], dtype: ~typing.Any = <class 'jax.numpy.float64'>) Arrayο
- name: str | None = Noneο
- parent: Type[Module] | Scope | Type[_Sentinel] | None = Noneο
- precision: Any = Noneο
- scope: Scope | None = Noneο
- use_bias: bool = Trueο
- class networks.icnns.ICNN(dim_hidden: Sequence[int], init_std: float = 0.1, init_fn: str = 'normal', act_fn: Callable = <PjitFunction of <function leaky_relu at 0x79b0760def20>>, pos_weights: bool = True, parent: Union[Type[flax.linen.module.Module], flax.core.scope.Scope, Type[flax.linen.module._Sentinel], NoneType] = <flax.linen.module._Sentinel object at 0x79afbb953cb0>, name: Optional[str] = None)[source]ο
Bases:
Module- act_fn(negative_slope: Array | ndarray | bool_ | number | bool | int | float | complex = 0.01) Arrayο
Leaky rectified linear unit activation function.
Computes the element-wise function:
\[\begin{split}\mathrm{leaky\_relu}(x) = \begin{cases} x, & x \ge 0\\ \alpha x, & x < 0 \end{cases}\end{split}\]where \(\alpha\) =
negative_slope.- Parameters:
x β input array
negative_slope β array or scalar specifying the negative slope (default: 0.01)
- Returns:
An array.
See also
relu()
- init_fn: str = 'normal'ο
- init_std: float = 0.1ο
- name: str | None = Noneο
- parent: Type[Module] | Scope | Type[_Sentinel] | None = Noneο
- pos_weights: bool = Trueο
- scope: Scope | None = Noneο
- setup()[source]ο
Initializes a Module lazily (similar to a lazy
__init__).setupis called once lazily on a module instance when a module is bound, immediately before any other methods like__call__are invoked, or before asetup-defined attribute onselfis accessed.This can happen in three cases:
Immediately when invoking
apply(),init()orinit_and_output().Once the module is given a name by being assigned to an attribute of another module inside the other moduleβs
setupmethod (see__setattr__()):>>> class MyModule(nn.Module): ... def setup(self): ... submodule = nn.Conv(...) ... # Accessing `submodule` attributes does not yet work here. ... # The following line invokes `self.__setattr__`, which gives ... # `submodule` the name "conv1". ... self.conv1 = submodule ... # Accessing `submodule` attributes or methods is now safe and ... # either causes setup() to be called once.
Once a module is constructed inside a method wrapped with
compact(), immediately before another method is called orsetupdefined attribute is accessed.
networks.optim moduleο
This module contains optimization utils used to train the models.
Source: https://github.com/bunnech/jkonet
Functionsο
get_optimizerReturns an Optax optimizer object based on the provided configuration.
create_train_stateCreates an initial TrainState for the given model and optimizer.
create_train_state_from_paramsCreates a TrainState from existing model parameters.
global_normComputes the global norm of gradients across a nested structure of tensors.
clip_weights_icnnClip the weights of an Input Convex Neural Network (ICNN).
- networks.optim.clip_weights_icnn(params: FrozenDict) FrozenDict[source]ο
Clip the weights of an Input Convex Neural Network (ICNN).
This function modifies the weights of the ICNN by clipping the values in kernels that start with βWzβ to ensure they are non-negative. This is necessary to maintain the convexity property of the ICNN.
- Parameters:
params (FrozenDict) β A frozen dictionary containing the parameters of the ICNN.
- Returns:
A frozen dictionary with the same structure as params, but with the relevant weights clipped to be non-negative.
- Return type:
Any
- networks.optim.create_train_state(rng: PRNGKey, model: Module, optimizer: GradientTransformation, input_shape: int) TrainState[source]ο
Creates an initial TrainState for the given model and optimizer.
- Parameters:
rng (jax.random.PRNGKey) β Random key used for initializing the model parameters.
model (nn.Module) β Flax model used for creating the initial state.
optimizer (optax.GradientTransformation) β Optimizer object used for updating the model parameters.
input_shape (int) β Shape of the input data used to initialize the model.
- Returns:
The initialized train state containing model parameters and optimizer.
- Return type:
train_state.TrainState
- networks.optim.create_train_state_from_params(model: Module, params: Dict[str, Any], optimizer: GradientTransformation) TrainState[source]ο
Creates a TrainState from existing model parameters.
- Parameters:
model (nn.Module) β Flax model used for creating the initial state.
params (Dict[str, Any]) β Dictionary of model parameters.
optimizer (optax.GradientTransformation) β Optimizer object used for updating the model parameters.
- Returns:
The train state containing the provided model parameters and optimizer.
- Return type:
train_state.TrainState
- networks.optim.get_optimizer(config: Dict[str, Any]) GradientTransformation[source]ο
Returns an Optax optimizer object based on the provided configuration.
- Parameters:
config (Dict[str, Any]) β
Dictionary containing optimizer configuration. Expected keys are:
βoptimizerβ: The name of the optimizer (βAdamβ or βSGDβ).
βlrβ: Learning rate for the optimizer.
βbeta1β: Beta1 parameter for the Adam optimizer.
βbeta2β: Beta2 parameter for the Adam optimizer.
βepsβ: Epsilon parameter for the Adam optimizer.
βgrad_clipβ: Optional maximum global norm for gradient clipping.
- Returns:
The configured Optax optimizer object.
- Return type:
optax.GradientTransformation
- Raises:
NotImplementedError β If the optimizer name is not supported.
- networks.optim.global_norm(updates: Dict[str, Array]) Array[source]ο
Computes the global norm of gradients across a nested structure of tensors.
- Parameters:
updates (Dict[str, jnp.ndarray]) β Dictionary where values are tensors (e.g., gradients).
- Returns:
The global norm of the gradients.
- Return type:
jnp.ndarray
- networks.optim.penalize_weights_icnn(params: FrozenDict) Array[source]ο
Compute a penalty for negative weights in an ICNN.
This function calculates a penalty term based on the L2 norm of any negative values in the weights that start with βWzβ. This penalty can be added to the loss function during training to encourage the network to maintain non-negative weights in those layers, which is important for the ICNNβs convexity.
- Parameters:
params (FrozenDict) β A frozen dictionary containing the parameters of the ICNN.
- Returns:
A scalar penalty value representing the sum of the L2 norms of the negative weights.
- Return type:
jnp.ndarray
networks.utils moduleο
Module for gradient computation and parameter analysis in Flax neural networks using JAX.
Functionsο
network_gradComputes the gradient of the networkβs output with respect to its input. The gradient is evaluated for each sample using vectorized mapping (vmap).
network_grad_timeComputes the gradient of the networkβs output with respect to its input, excluding the time component.
count_parametersReturns the total number of parameters in the given Flax neural network model.
- networks.utils.count_parameters(model: Module) int[source]ο
Counts the total number of parameters in the model.
- Parameters:
model (nn.Module) β The Flax neural network module.
- Returns:
The total number of parameters in the model.
- Return type:
int
- networks.utils.network_grad(network: Module, params: Dict[str, Array]) Callable[[Array], Array][source]ο
Computes the gradient of the networkβs output with respect to its input for each sample.
- Parameters:
network (nn.Module) β The Flax neural network module.
params (Dict[str, jnp.ndarray]) β Dictionary containing model parameters.
- Returns:
A function that computes gradients with respect to the networkβs input.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]
- networks.utils.network_grad_time(network: Module, params: Dict[str, Array]) Callable[[Array], Array][source]ο
Computes the gradient of the networkβs output with respect to the input, excluding the time component.
In the time-varying JKOnet* model, the gradient in the loss is computed with respect to the input, excluding the time component.
- Parameters:
network (nn.Module) β The Flax neural network module.
params (Dict[str, jnp.ndarray]) β Dictionary containing model parameters.
- Returns:
A function that computes gradients with respect to the input, excluding the time component.
- Return type:
Callable[[jnp.ndarray], jnp.ndarray]