Sephfox commited on
Commit
2f37935
·
verified ·
1 Parent(s): 9ce35f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -23
app.py CHANGED
@@ -34,31 +34,46 @@ class GeneticAlgorithm:
34
  self.population = [Net() for _ in range(population_size)]
35
 
36
  def selection(self):
37
- X_train, X_test, y_train, y_test = generate_dataset(self.task_id)
38
- fitness = []
39
- for i, net in enumerate(self.population):
40
- net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
41
- net.fit(X_train, y_train, epochs=10, verbose=0)
42
- loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
43
- fitness.append(accuracy)
44
- if len(fitness) > 0:
45
- self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
46
 
47
  def crossover(self):
48
- offspring = []
49
- for _ in range(self.population_size//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
- # Average the weights of the two parents
58
- child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
59
- child.set_weights(child_weights)
60
- offspring.append(child)
61
- self.population += offspring
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  def mutation(self):
64
  for net in self.population:
 
34
  self.population = [Net() for _ in range(population_size)]
35
 
36
  def selection(self):
37
+ X_train, X_test, y_train, y_test = generate_dataset(self.task_id)
38
+ fitness = []
39
+ for i, net in enumerate(self.population):
40
+ net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
41
+ net.fit(X_train, y_train, epochs=10, verbose=0)
42
+ loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
43
+ fitness.append(accuracy)
44
+ if len(fitness) > 0:
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
+ # Set the weights of the child network
64
+ child.set_weights(child_weights)
65
+
66
+ offspring.append(child)
67
+ self.population += offspring
68
+
69
+ # Get the weights of the parent networks
70
+ parent1_weights = parent1.get_weights()
71
+ parent2_weights = parent2.get_weights()
72
+ # Average the weights of the two parents
73
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
74
+ child.set_weights(child_weights)
75
+ offspring.append(child)
76
+ self.population += offspring
77
 
78
  def mutation(self):
79
  for net in self.population: