Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -45,43 +45,44 @@ class GeneticAlgorithm:
|
|
45 |
|
46 |
def crossover(self):
|
47 |
offspring = []
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
def mutation(self):
|
72 |
for net in self.population:
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
# Streamlit app
|
86 |
st.title("Evolution of Sub-Models")
|
87 |
|
|
|
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 |
+
|
86 |
# Streamlit app
|
87 |
st.title("Evolution of Sub-Models")
|
88 |
|