Sephfox commited on
Commit
9ed5b44
1 Parent(s): ff0e406

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -24
app.py CHANGED
@@ -44,32 +44,44 @@ class GeneticAlgorithm:
44
  self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
45
 
46
  def crossover(self):
47
- offspring = []
48
- for _ in range(self.population_size//2):
49
- parent1, parent2 = random.sample(self.population, 2)
50
- child = Net()
51
- child.fc1.set_weights((np.array(parent1.fc1.get_weights()) + np.array(parent2.fc1.get_weights())) / 2)
52
- child.fc2.set_weights((np.array(parent1.fc2.get_weights()) + np.array(parent2.fc2.get_weights())) / 2)
53
- child.fc3.set_weights((np.array(parent1.fc3.get_weights()) + np.array(parent2.fc3.get_weights())) / 2)
54
- offspring.append(child)
55
- self.population += offspring
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  def mutation(self):
58
- for net in self.population:
59
- if random.random() < 0.1:
60
- weights = net.fc1.get_weights()
61
- weights[0] += np.random.randn(*weights[0].shape) * 0.1
62
- weights[1] += np.random.randn(*weights[1].shape) * 0.1
63
- net.fc1.set_weights(weights)
64
- weights = net.fc2.get_weights()
65
- weights[0] += np.random.randn(*weights[0].shape) * 0.1
66
- weights[1] += np.random.randn(*weights[1].shape) * 0.1
67
- net.fc2.set_weights(weights)
68
- weights = net.fc3.get_weights()
69
- weights[0] += np.random.randn(*weights[0].shape) * 0.1
70
- weights[1] += np.random.randn(*weights[1].shape) * 0.1
71
- net.fc3.set_weights(weights)
72
-
73
  # Streamlit app
74
  st.title("Evolution of Sub-Models")
75
 
 
44
  self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
45
 
46
  def crossover(self):
47
+ offspring = []
48
+ for _ in range(self.population_size//2):
49
+ parent1, parent2 = random.sample(self.population, 2)
50
+ child = Net()
51
+
52
+ # Average the weights of the two parents
53
+ parent1_weights = parent1.fc1.get_weights()
54
+ parent2_weights = parent2.fc1.get_weights()
55
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
56
+ child.fc1.set_weights(child_weights)
57
+
58
+ parent1_weights = parent1.fc2.get_weights()
59
+ parent2_weights = parent2.fc2.get_weights()
60
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
61
+ child.fc2.set_weights(child_weights)
62
+
63
+ parent1_weights = parent1.fc3.get_weights()
64
+ parent2_weights = parent2.fc3.get_weights()
65
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
66
+ child.fc3.set_weights(child_weights)
67
+
68
+ offspring.append(child)
69
+ self.population += offspring
70
 
71
  def mutation(self):
72
+ for net in self.population:
73
+ if random.random() < 0.1:
74
+ weights = net.fc1.get_weights()
75
+ new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
76
+ net.fc1.set_weights(new_weights)
77
+
78
+ weights = net.fc2.get_weights()
79
+ new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
80
+ net.fc2.set_weights(new_weights)
81
+
82
+ weights = net.fc3.get_weights()
83
+ new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
84
+ net.fc3.set_weights(new_weights)
 
 
85
  # Streamlit app
86
  st.title("Evolution of Sub-Models")
87