EvolvableModule

Parameters

class agilerl.modules.base.EvolvableModule(*args, **kwargs)

Base class for evolvable neural networks.

Parameters:

device (str) – The device to run the network on.

change_activation(activation: str, output: bool) None

Set the activation function for the network.

Parameters:

activation (str) – Activation function to use.

clone() SelfEvolvableModule

Returns clone of neural net with identical parameters.

disable_mutations(mut_type: MutationType | None = None) None

Disable all or some mutation methods from the evolvable module. It recursively disables the mutation methods of underlying evolvable modules as well.

Parameters:

mut_type (Optional[MutationType]) – The type of mutation method to disable.

filter_mutation_methods(remove: str) None

Filter out mutation methods that contain the specified string in their name.

param remove: The string to remove. type remove: str

forward(*args, **kwargs) Tensor

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.

get_init_dict() Dict[str, Any]

Get the dictionary of constructor arguments for the network.

Returns:

The dictionary of constructor arguments.

Return type:

Dict[str, Any]

get_mutation_methods() Dict[str, MutationMethod]

Get all mutation methods for the network.

Returns:

A dictionary of mutation methods.

Return type:

Dict[str, MutationMethod]

get_mutation_probs(new_layer_prob: float) List[float]

Get the mutation probabilities for each mutation method.

param new_layer_prob: The probability of selecting a layer mutation method. type new_layer_prob: float return: A list of probabilities for each mutation method. rtype: List[float]

get_output_dense() Module | None

Get the output dense layer of the network.

Returns:

The output dense layer.

Return type:

nn.Module

static init_weights_gaussian(module: Module, std_coeff: float) None

Initialize the weights of the neural network using a Gaussian distribution.

Parameters:
  • module (nn.Module) – The neural network module.

  • std_coeff (float) – The standard deviation coefficient.

modules() Dict[str, EvolvableModule]

Returns the attributes related to the evolvable modules in the algorithm. Includes attributes that are either evolvable modules or a list of evolvable modules, as well as the optimizers associated with the networks.

Returns:

A dictionary of network attributes.

Return type:

dict[str, Any]

static preserve_parameters(old_net: Module, new_net: Module) Module

Returns new neural network with copied parameters from old network. Specifically, it handles tensors with different sizes by copying the minimum number of elements.

Parameters:
  • old_net (nn.Module) – Old neural network

  • new_net (nn.Module) – New neural network

Returns:

New neural network with copied parameters

Return type:

nn.Module

recreate_network(**kwargs) None

Recreate the network after a mutation has been applied.

register_mutation_hook(hook: Callable) None

Register a hook to be called after a mutation has been applied to an underlying evolvable module. The hook function should not take any arguments.

Parameters:

hook (Callable) – The hook function.

reset_noise() None

Reset noise for all NoisyLinear layers in the network.

Parameters:

networks (nn.Module) – The networks to reset noise for.

sample_mutation_method(new_layer_prob: float, rng: Generator | None = None) MutationMethod

Sample a mutation method based on the mutation probabilities.

param new_layer_prob: The probability of selecting a layer mutation method. type new_layer_prob: float param rng: The random number generator. type rng: Optional[Generator] return: The sampled mutation method. rtype: MutationMethod

EvolvableWrapper

Parameters

class agilerl.modules.base.EvolvableWrapper(*args, **kwargs)

Wrapper class for evolvable neural networks. Can be used to provide some additional functionality to an EvolvableModule while maintaining its mutation methods at the top-level.

Parameters:

module (EvolvableModule) – The evolvable module.

modules() Dict[str, EvolvableModule]

Returns the attributes related to the evolvable modules in the algorithm. Includes attributes that are either evolvable modules or a list of evolvable modules, as well as the optimizers associated with the networks.

Returns:

A dictionary of network attributes.

Return type:

dict[str, Any]

ModuleDict

Parameters

class agilerl.modules.base.ModuleDict(*args, **kwargs)

Analogous to nn.ModuleDict, but allows for the recursive inheritance of the mutation methods of underlying evolvable modules.

get_mutation_methods() Dict[str, MutationMethod]

Get all mutation methods for the network.

Returns:

A dictionary of mutation methods.

Return type:

Dict[str, MutationMethod]

items() Iterable[Tuple[str, EvolvableModule]]

Return an iterable of the ModuleDict key/value pairs.

values() Iterable[EvolvableModule]

Return an iterable of the ModuleDict values.