Sephfox commited on
Commit
3bb2d21
·
verified ·
1 Parent(s): f1eea6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
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
- 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:
 
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: