nevopy.fixed_topology package¶
Subpackages¶
Submodules¶
nevopy.fixed_topology.genomes module¶
Implements genomes (subclasses of BaseGenome
) that encode neural
networks with a fixed topology.
-
class
nevopy.fixed_topology.genomes.
FixedTopologyGenome
(layers, config=None, input_shape=None)¶ Bases:
nevopy.base_genome.BaseGenome
Genome that encodes a fixed-topology multilayer neural network.
This genome directly encodes a multilayer neural network with fixed topology. The network is defined by its layers (instances of a subclass of
BaseLayer
), specified during the genome’s creation.Note
The config objects of individual layers are forcefully replaced by the config object of the genome when its assigned with a new one!
- Parameters
layers (List[BaseLayer]) – List with the layers of the network (instances of a subclass of
BaseLayer
). It’s not required to set the input shape of each individual layer. If the input shapes are not set, they will be automatically set when a call toprocess()
is made. There is no need to pass the config object to the layers (it’s done automatically when this class is instantiated).config (Optional[GeneticAlgorithmConfig]) – Settings of the current evolutionary session. If None, a config object must be assigned to this genome latter.
input_shape (Optional[Tuple[int, ..]]) – Shape of the inputs that will be fed to the genome. If a value is specified, the genome’s layers are built (they have their weights initialized). If None, an input shape will be inferred later when an input is fed to the genome (note, however, that the weights won’t be initialized until it occurs).
-
layers
¶ List with the layers of the network (instances of a subclass of
BaseLayer
).- Type
List[BaseLayer]
-
property
config
¶ Settings of the current evolutionary session.
If None, a config object hasn’t been assigned to this genome yet.
- Return type
Optional
[GeneticAlgorithmConfig
]
-
deep_copy
()¶ Makes an exact/deep copy of the genome.
- Return type
- Returns
An exact/deep copy of the genome. It has the same topology and connections weights of the original genome.
-
distance
(other)¶ Calculates the distance between the two genomes.
The distance is calculated based on the euclidean distance (the L2 norm of the difference) between correspondent weight matrices of the genomes layers.
- Parameters
other (FixedTopologyGenome) – The other fixed-topology genome.
- Return type
float
- Returns
A float representing the distance between the two genomes. The lower the distance, the more similar the two genomes are.
-
property
input_shape
¶ The input shape expected by the genome’s input layer.
- Return type
Optional
[Tuple
[int
, …]]
-
mate
(other)¶ Mates two genomes to produce a new genome (offspring).
Implements the sexual reproduction between a pair of genomes. The new genome inherits information from both parents.
Currently available mating modes for individual layers:
The mating mode of a layer is specified during its instantiation.
- Parameters
other (Any) – The second genome . If it’s not compatible for mating with the current genome (self), an exception will be raised.
- Return type
- Returns
A new genome (the offspring born from the sexual reproduction between the current genome and the genome passed as argument).
- Raises
IncompatibleGenomesError – If the genome passed as argument to
other
is incompatible with the current genome (self).
-
mutate_weights
()¶ Randomly mutates the weights of the genome’s connections.
- Return type
None
-
process
(x)¶ Feeds the given input to the neural network encoded by the genome.
- Parameters
x (Any) – The input(s) to be fed to the neural network encoded by the genome. Usually a NumPy ndarray or a TensorFlow tensor.
- Return type
Any
- Returns
The output of the network. Usually a NumPy ndarray or a TensorFlow tensor.
- Raises
InvalidInputError – If the shape of
X
doesn’t match the input shape expected by the network.
-
random_copy
()¶ Makes a deep copy of the genome, but with random weights.
- Return type
- Returns
A deep copy of the genome with the same topology of the original genome, but random connections weights.
-
reset
()¶ This method doesn’t do anything.
In this implementation, the default fixed topology networks do not need to reset any of its internal states before the start of a new generation.
- Return type
None
-
visualize
(show=True, to_file='genome.png', **kwargs)¶ Utility method for visualizing the genome’s neural network.
This currently only works with genomes that use TensorFlow layers.
Todo
Make it possible to visualize neurons and connections.
-
show
¶ Whether to show the generated image or not.
- Type
bool
-
to_file
¶ Path in which the image file will be saved to.
- Type
str
-
\*\*kwargs
Optional named arguments to be passed to
tensorflow.keras.utils.plot_model()
.
- Return type
Image
- Returns
The generated
PIL.Image.Image
object.
-
Module contents¶
Imports core names of nevopy.fixed_topology
.