Sephfox commited on
Commit
56fb854
1 Parent(s): 87504ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -36
app.py CHANGED
@@ -26,7 +26,6 @@ class Net(keras.Model):
26
  x = self.fc2(x)
27
  x = self.fc3(x)
28
  return x
29
-
30
  # Define a genetic algorithm class
31
  class GeneticAlgorithm:
32
  def __init__(self, population_size, task_id):
@@ -35,44 +34,37 @@ class GeneticAlgorithm:
35
  self.population = [Net() for _ in range(population_size)]
36
 
37
  def selection(self):
38
- X_train, X_test, y_train, y_test = generate_dataset(self.task_id)
39
- fitness = []
40
- for i, net in enumerate(self.population):
41
- net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
42
- net.build(input_shape=(None, 10)) # Compile the model before training
43
- net.fit(X_train, y_train, epochs=10, verbose=0)
44
- loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
45
- fitness.append(accuracy)
46
- if len(fitness) > 0:
47
- self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
48
-
49
  def crossover(self):
50
- offspring = []
51
- X = np.random.rand(1, 10) # dummy input to build the layers
52
- for _ in range(self.population_size//2):
53
- parent1, parent2 = random.sample(self.population, 2)
54
- child = Net()
55
- child(X) # build the layers
56
- parent1(X) # build the layers
57
- parent2(X) # build the layers
58
-
59
- # Average the weights of the two parents
60
- parent1_weights = parent1.get_weights()
61
- parent2_weights = parent2.get_weights()
62
- child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
63
- child.set_weights(child_weights)
64
-
65
- offspring.append(child)
66
- self.population += offspring
67
 
68
  def mutation(self):
69
- X = np.random.rand(1, 10) # dummy input to build the layers
70
- for net in self.population:
71
- net(X) # build the layers
72
- if random.random() < 0.1:
73
- weights = net.get_weights()
74
- new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
75
- net.set_weights(new_weights)
76
 
77
  # Streamlit app
78
  st.title("Evolution of Sub-Models")
 
26
  x = self.fc2(x)
27
  x = self.fc3(x)
28
  return x
 
29
  # Define a genetic algorithm class
30
  class GeneticAlgorithm:
31
  def __init__(self, population_size, task_id):
 
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
  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
+ child.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
52
+
53
+ # Get the weights of the parent networks
54
+ parent1_weights = parent1.get_weights()
55
+ parent2_weights = parent2.get_weights()
56
+ # Average the weights of the two parents
57
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
58
+ child.set_weights(child_weights)
59
+ offspring.append(child)
60
+ self.population += offspring
 
 
 
61
 
62
  def mutation(self):
63
+ for net in self.population:
64
+ if random.random() < 0.1:
65
+ weights = net.get_weights()
66
+ new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
67
+ net.set_weights(new_weights)
 
 
68
 
69
  # Streamlit app
70
  st.title("Evolution of Sub-Models")