Sephfox commited on
Commit
cfd33ea
·
verified ·
1 Parent(s): 9a1cd91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -46,27 +46,27 @@ class GeneticAlgorithm:
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.fc1.set_weights(child_weights[:2])
65
- child.fc2.set_weights(child_weights[2:4])
66
- child.fc3.set_weights(child_weights[4:])
67
-
68
- offspring.append(child)
69
- self.population += offspring
70
 
71
  def mutation(self):
72
  for net in self.population:
@@ -74,6 +74,7 @@ class GeneticAlgorithm:
74
  weights = net.get_weights()
75
  new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
76
  net.set_weights(new_weights)
 
77
  # Streamlit app
78
  st.title("Evolution of Sub-Models")
79
 
 
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.fc1.set_weights(child_weights[:2])
65
+ child.fc2.set_weights(child_weights[2:4])
66
+ child.fc3.set_weights(child_weights[4:])
67
+
68
+ offspring.append(child)
69
+ self.population += offspring
70
 
71
  def mutation(self):
72
  for net in self.population:
 
74
  weights = net.get_weights()
75
  new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
76
  net.set_weights(new_weights)
77
+
78
  # Streamlit app
79
  st.title("Evolution of Sub-Models")
80