|
| 1 | +from ..cnn import cnn |
| 2 | +import copy |
| 3 | + |
| 4 | +def population_as_vectors(population_networks): |
| 5 | + |
| 6 | + """ |
| 7 | + Accepts the population as networks and returns a list holding all weights of the CNN layers of each solution (i.e. network) in the population as a vector. |
| 8 | + If the population has 6 solutions (i.e. networks), this function accepts references to such networks and returns a list with 6 vectors, one for each network (i.e. solution). Each vector holds the weights for all layers for a single CNN. |
| 9 | +
|
| 10 | + population_networks: A list holding references to the CNN models used in the population. |
| 11 | +
|
| 12 | + Returns a list holding the weights vectors for all solutions (i.e. networks). |
| 13 | + """ |
| 14 | + |
| 15 | + population_vectors = [] |
| 16 | + for solution in population_networks: |
| 17 | + # Converting the weights of single layer from the current CNN (i.e. solution) to a vector. |
| 18 | + solution_weights_vector = cnn.layers_weights_as_vector(solution) |
| 19 | + # Appending the weights vector of the current layer of a CNN (i.e. solution) to the weights of the previous layers of the same CNN (i.e. solution). |
| 20 | + population_vectors.append(solution_weights_vector) |
| 21 | + |
| 22 | + return population_vectors |
| 23 | + |
| 24 | +def population_as_matrices(population_networks, population_vectors): |
| 25 | + |
| 26 | + """ |
| 27 | + Accepts the population as both networks and weights vectors and returns the weights of all layers of each solution (i.e. CNN) in the population as a matrix. |
| 28 | + If the population has 6 solutions (i.e. networks), this function returns a list with 6 matrices, one for each network holding its weights for all layers. |
| 29 | +
|
| 30 | + population_networks: A list holding references to the output (last) layers of the neural networks used in the population. |
| 31 | + population_vectors: A list holding the weights of all networks as vectors. Such vectors are to be converted into matrices. |
| 32 | +
|
| 33 | + Returns a list holding the weights matrices for all solutions (i.e. networks). |
| 34 | + """ |
| 35 | + |
| 36 | + population_matrices = [] |
| 37 | + for solution, solution_weights_vector in zip(population_networks, population_vectors): |
| 38 | + # Converting the weights of single layer from the current CNN (i.e. solution) from a vector to a matrix. |
| 39 | + solution_weights_matrix = cnn.layers_weights_as_matrix(solution, solution_weights_vector) |
| 40 | + # Appending the weights matrix of the current layer of a CNN (i.e. solution) to the weights of the previous layers of the same network (i.e. solution). |
| 41 | + population_matrices.append(solution_weights_matrix) |
| 42 | + |
| 43 | + return population_matrices |
| 44 | + |
| 45 | +class GACNN: |
| 46 | + |
| 47 | + def create_population(self): |
| 48 | + |
| 49 | + """ |
| 50 | + Creates the initial population of the genetic algorithm as a list of CNNs (i.e. solutions). Each element in the list holds a reference to the instance of the cnn.Model class. |
| 51 | +
|
| 52 | + The method returns the list holding the references to the CNN models. |
| 53 | + """ |
| 54 | + |
| 55 | + population_networks = [] |
| 56 | + for solution in range(self.num_solutions): |
| 57 | + |
| 58 | + network = copy.deepcopy(self.model) |
| 59 | + |
| 60 | + # Appending the CNN model to the list of population networks. |
| 61 | + population_networks.append(network) |
| 62 | + |
| 63 | + return population_networks |
| 64 | + |
| 65 | + def __init__(self, model, num_solutions): |
| 66 | + |
| 67 | + """ |
| 68 | + Creates an instance of the GACNN class for training a CNN using the genetic algorithm. |
| 69 | + The constructor of the GACNN class creates an initial population of multiple CNNs using the create_population() method. |
| 70 | + The population returned holds references to instances of the cnn.Model class. |
| 71 | +
|
| 72 | + model: An instance of the pygad.cnn.Model class representing the architecture of all solutions in the population. |
| 73 | + num_solutions: Number of CNNs (i.e. solutions) in the population. Based on the value passed to this parameter, a number of identical CNNs are created where their parameters are optimized using the genetic algorithm. |
| 74 | + """ |
| 75 | + |
| 76 | + self.model = model |
| 77 | + |
| 78 | + self.num_solutions = num_solutions |
| 79 | + |
| 80 | + # A list holding references to all the solutions (i.e. CNNs) used in the population. |
| 81 | + self.population_networks = self.create_population() |
| 82 | + |
| 83 | + def update_population_trained_weights(self, population_trained_weights): |
| 84 | + |
| 85 | + """ |
| 86 | + The `update_population_trained_weights()` method updates the `trained_weights` attribute of each CNN according to the weights passed in the `population_trained_weights` parameter. |
| 87 | +
|
| 88 | + population_trained_weights: A list holding the trained weights of all networks as matrices. Such matrices are to be assigned to the 'trained_weights' attribute of all layers of all CNNs. |
| 89 | + """ |
| 90 | + |
| 91 | + idx = 0 |
| 92 | + # Fetches all layers weights matrices for a single solution (i.e. CNN) |
| 93 | + for solution in self.population_networks: |
| 94 | + # Calling the cnn.update_layers_trained_weights() function for updating the 'trained_weights' attribute for all layers in the current solution (i.e. CNN). |
| 95 | + cnn.update_layers_trained_weights(model=solution, |
| 96 | + final_weights=population_trained_weights[idx]) |
| 97 | + idx = idx + 1 |
0 commit comments