Sephfox commited on
Commit
e0651bb
1 Parent(s): eb95235

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -23
app.py CHANGED
@@ -45,43 +45,31 @@ class GeneticAlgorithm:
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")
 
45
 
46
  def crossover(self):
47
  offspring = []
48
+ X = np.random.rand(1, 10) # dummy input to build the layers
49
  for _ in range(self.population_size//2):
50
  parent1, parent2 = random.sample(self.population, 2)
51
  child = Net()
52
+ child(X) # build the layers
53
+ parent1(X) # build the layers
54
+ parent2(X) # build the layers
55
 
56
  # Average the weights of the two parents
57
+ parent1_weights = parent1.get_weights()
58
+ parent2_weights = parent2.get_weights()
59
  child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
60
+ child.set_weights(child_weights)
 
 
 
 
 
 
 
 
 
 
61
 
62
  offspring.append(child)
63
  self.population += offspring
64
 
65
  def mutation(self):
66
+ X = np.random.rand(1, 10) # dummy input to build the layers
67
  for net in self.population:
68
+ net(X) # build the layers
69
  if random.random() < 0.1:
70
+ weights = net.get_weights()
 
 
 
 
 
 
 
 
71
  new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
72
+ net.set_weights(new_weights)
73
 
74
  # Streamlit app
75
  st.title("Evolution of Sub-Models")