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.

  • output (bool) – Whether to set the activation function for the output layer.

clone() SelfEvolvableModule

Returns clone of an EvolvableModule with identical parameters.

Returns:

A clone of the EvolvableModule.

Return type:

SelfEvolvableModule

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 nested 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 as dictionary of method names to mutation methods.

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.

Warning

This overrides the behavior of nn.Module.modules() and only returns

the evolvable modules. If you need the torch modules, use torch_modules() instead.

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. If the mutation methods of an EvolvableModule are only attributed to its nested modules, then the recreate_network method should be implemented in the nested modules and it is not required on the parent.

Parameters:

kwargs (Dict[str, Any]) – Keyword arguments to pass to the network constructor.

register_mutation_hook(hook: Callable) None

Register a hook to be called after a mutation has been applied to a nested 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.

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

torch_modules() Dict[str, Module]

Returns the attributes related to the torch modules in the algorithm. Includes attributes that are either torch modules or a list of torch modules.

Returns:

A dictionary of network attributes.

Return type:

dict[str, Any]

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.

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 inheritance of the mutation methods of nested 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, ModuleType]]

Return an iterable of the ModuleDict key/value pairs.

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]

values() Iterable[ModuleType]

Return an iterable of the ModuleDict values.