Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,28 +45,28 @@ class GeneticAlgorithm:
|
|
| 45 |
self.population = [self.population[i] for i in np.argsort(fitness)[-len(self.population)//2:]]
|
| 46 |
|
| 47 |
def crossover(self):
|
| 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:
|
|
|
|
| 45 |
self.population = [self.population[i] for i in np.argsort(fitness)[-len(self.population)//2:]]
|
| 46 |
|
| 47 |
def crossover(self):
|
| 48 |
+
offspring = []
|
| 49 |
+
for _ in range(len(self.population)//2):
|
| 50 |
+
parent1, parent2 = random.sample(self.population, 2)
|
| 51 |
+
child = Net()
|
| 52 |
+
child.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
| 53 |
+
|
| 54 |
+
# Get the weights of the parent networks
|
| 55 |
+
parent1_weights = parent1.get_weights()
|
| 56 |
+
parent2_weights = parent2.get_weights()
|
| 57 |
+
|
| 58 |
+
# Average the weights of the two parents
|
| 59 |
+
child_weights = []
|
| 60 |
+
for w1, w2 in zip(parent1_weights, parent2_weights):
|
| 61 |
+
child_weights.append((w1 + w2) / 2)
|
| 62 |
+
|
| 63 |
+
# Create the child instance with the correct weights
|
| 64 |
+
child = Net()
|
| 65 |
+
child.set_weights(child_weights)
|
| 66 |
+
child.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
| 67 |
+
|
| 68 |
+
offspring.append(child)
|
| 69 |
+
self.population += offspring
|
| 70 |
|
| 71 |
def mutation(self):
|
| 72 |
for net in self.population:
|