Base_individuals
Nn_individual
- class gaggle.population.base_individuals.nn_individual.NNIndividual(individual_args: IndividualArgs, sys_args: SysArgs = None, model: Module = None, *args, **kwargs)[source]
Bases:
Individual
An Individual whose initial parameters are a torch.nn.Module.
- CONFIG_BASE_MODEL_STATE_DICT = 'base_model_state_dict'
- eval()[source]
Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.See locally-disable-grad-doc for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns:
self
- Return type:
Module
- forward(x)[source]
By default we returned a flattened tensor of the model parameters.
Notes
We do not return the metadata that can be used to reconstruct the gene_pool dictionary as this should not be used to modify the parameters directly (the tensor does not link back to the parameters).
- Parameters:
*args –
**kwargs –
- Returns:
A flattened pytorch tensor of length self.genome_size.
- generate_gene_pool()[source]
Should return a dictionary of dictionaries where the inner dictionary has a “param” and a “size” key for each of the nn.Parameters in the dictionary
- initialize()[source]
Represents the initialization rule for an individual. It should modify the gene pool.
- Parameters:
*args –
**kwargs –
- Returns:
self
- parameters(recurse: bool = True) Iterator[Parameter] [source]
Returns an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) – if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter – module parameter
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- save(outdir_args: OutdirArgs = None) dict [source]
Saves the individual to a file in the path provided by outdir_args.
- Parameters:
outdir_args –
Returns:
- train(mode: bool = True)[source]
Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters:
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns:
self
- Return type:
Module
Numpy_individual
- class gaggle.population.base_individuals.numpy_individual.NumpyIndividual(np_params: dict[slice(<class 'int'>, <class 'numpy.ndarray'>, None)] = None, individual_args: ~gaggle.arguments.individual_args.IndividualArgs = None, sys_args: ~gaggle.arguments.sys_args.SysArgs = None)[source]
Bases:
Individual
An Individual whose initial parameters are numpy arrays.
- forward(*args, **kwargs)[source]
By default we returned a flattened tensor of the model parameters.
Notes
We do not return the metadata that can be used to reconstruct the gene_pool dictionary as this should not be used to modify the parameters directly (the tensor does not link back to the parameters).
- Parameters:
*args –
**kwargs –
- Returns:
A flattened pytorch tensor of length self.genome_size.
- generate_gene_pool(np_params: dict[slice(<class 'int'>, <class 'numpy.ndarray'>, None)], *args, **kwargs) dict[slice(<class 'int'>, dict[slice(<class 'str'>, <class 'torch.nn.parameter.Parameter'>, None), slice(<class 'str'>, <class 'int'>, None)], None)] [source]
Should return a dictionary of dictionaries where the inner dictionary has a “param” and a “size” key for each of the nn.Parameters in the dictionary
Pytorch_individual
- class gaggle.population.base_individuals.pytorch_individual.PytorchIndividual(tensors: dict[slice(<class 'int'>, <class 'torch.Tensor'>, None)] = None, individual_args: ~gaggle.arguments.individual_args.IndividualArgs = None, sys_args: ~gaggle.arguments.sys_args.SysArgs = None)[source]
Bases:
Individual
An Individual whose initial parameters are pytorch tensors.
- forward(*args, **kwargs)[source]
By default we returned a flattened tensor of the model parameters.
Notes
We do not return the metadata that can be used to reconstruct the gene_pool dictionary as this should not be used to modify the parameters directly (the tensor does not link back to the parameters).
- Parameters:
*args –
**kwargs –
- Returns:
A flattened pytorch tensor of length self.genome_size.
- generate_gene_pool(tensors: dict[slice(<class 'int'>, <class 'torch.Tensor'>, None)], *args, **kwargs) dict[slice(<class 'int'>, dict[slice(<class 'str'>, <class 'torch.nn.parameter.Parameter'>, None), slice(<class 'str'>, <class 'int'>, None)], None)] [source]
Should return a dictionary of dictionaries where the inner dictionary has a “param” and a “size” key for each of the nn.Parameters in the dictionary
Rl_individual
- class gaggle.population.base_individuals.rl_individual.RLIndividual(individual_args: IndividualArgs, sys_args: SysArgs = None, model: Module = None, *args, **kwargs)[source]
Bases:
NNIndividual
NNIndividual wrapper that adds an argmax to the prediction as RL problem usually require the output of the model to be an action rather than logits.
- forward(x)[source]
By default we returned a flattened tensor of the model parameters.
Notes
We do not return the metadata that can be used to reconstruct the gene_pool dictionary as this should not be used to modify the parameters directly (the tensor does not link back to the parameters).
- Parameters:
*args –
**kwargs –
- Returns:
A flattened pytorch tensor of length self.genome_size.