I've been working on a genetic algorithm, where I'm trying to take the best model of my population (based on score) and make the entire population into that model. I have done this successfully, but when I try to mutate each model separately, all the models mutate to the same parameters. I know this is because I use an object and just clone it into a list, but I don't know what to change so that it doesn't work this way. Below I've made a reproducible example to be run in Python 3.9. I know the code isn't particularly small, but this is as small as I can make it. Thanks in advance, any help is appreciated.
import torch
import torch.nn as nn
torch.manual_seed(0) #Reproducibility
population_size = 3 #Defining the size of my population
population = [nn.Linear(1,1) for _ in range(population_size)] #Initializing the population
input = torch.rand(1)# Creating dummy input
def mutate(m): #Function to mutate a model
if type(m) == nn.Linear:
m.weight = nn.Parameter(m.weight torch.randn(m.weight.shape))
m.bias = nn.Parameter(m.bias torch.randn(m.bias.shape))
for i in population:
print (i(input))
population = [x.apply(mutate) for x in population]
print ('\n')
for i in population:
print (i(input))
#The above works as expected
#I want to fill my entire population with that model.
#I've been filling the population with the best model by doing the following:
best_model = population[0] #Say the first model in the list was the best performing one
population = [best_model for _ in range(population_size)] #This is the line I think needs to change, I just don't know what to change it to.
#This does fill the population with my best model, but when I try to mutate it, every model is mutated to the same parameters
population = [x.apply(mutate) for x in population] #I know this is because I am using best_model while replacing the population, but I don't know how else to copy the model
for i in population:
print (i(input)) #Just to show that the population all gives the same result
CodePudding user response:
You can make a deep copy of the model. Make sure to import copy
, and then change
population = [best_model for _ in range(population_size)]
to
population = [copy.deepcopy(best_model) for _ in range(population_size)]