Sephfox commited on
Commit
d73abfa
·
verified ·
1 Parent(s): 23c547f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -43,8 +43,8 @@ class GeneticAlgorithm:
43
  X_train, X_test, y_train, y_test = generate_dataset(self.task_id)
44
  fitness = []
45
  for i, net in enumerate(self.population):
46
- net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
47
  net.build(input_shape=(None, 10))
 
48
  net.fit(X_train, y_train, epochs=10, verbose=0)
49
  loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
50
  fitness.append(accuracy)
@@ -53,21 +53,19 @@ class GeneticAlgorithm:
53
 
54
  def crossover(self):
55
  offspring = []
56
- for _ in range(self.population_size//2):
57
- parent1, parent2 = random.sample(self.population, 2)
58
- child = Net()
59
- child.build(input_shape=(None, 10))
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
- offspring.append(child)
65
- self.population += offspring
66
 
67
  def mutation(self):
68
- X = np.random.rand(1, 10)
69
  for net in self.population:
70
- net.build(input_shape=(None, 10))
71
  if random.random() < 0.1:
72
  weights = net.get_weights()
73
  new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
@@ -94,22 +92,19 @@ if st.button("Run evolution"):
94
  ga.mutation()
95
  st.write(f"Generation {generation+1} complete")
96
 
97
- # Evaluate the final population
98
  if gas is not None:
99
  final_accuracy = []
100
- for task_id, ga in enumerate(gas):
101
  X_train, X_test, y_train, y_test = generate_dataset(task_id)
102
  accuracy = []
103
  for net in ga.population:
104
  net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
105
- net.build(input_shape=(None, 10))
106
  net.fit(X_train, y_train, epochs=10, verbose=0)
107
  loss, acc = net.evaluate(X_test, y_test, verbose=0)
108
  accuracy.append(acc)
109
  if len(accuracy) > 0:
110
  final_accuracy.append(np.mean(accuracy))
111
- if len(final_accuracy) > 0:
112
- st.write(f"Final accuracy: {np.mean(final_accuracy)}")
113
 
114
  # Trade populations between tasks
115
  if gas is not None:
@@ -133,7 +128,6 @@ if st.button("Run evolution"):
133
  accuracy = []
134
  for net in ga.population:
135
  net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
136
- net.build(input_shape=(None, 10))
137
  net.fit(X_train, y_train, epochs=10, verbose=0)
138
  loss, acc = net.evaluate(X_test, y_test, verbose=0)
139
  accuracy.append(acc)
 
43
  X_train, X_test, y_train, y_test = generate_dataset(self.task_id)
44
  fitness = []
45
  for i, net in enumerate(self.population):
 
46
  net.build(input_shape=(None, 10))
47
+ net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
48
  net.fit(X_train, y_train, epochs=10, verbose=0)
49
  loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
50
  fitness.append(accuracy)
 
53
 
54
  def crossover(self):
55
  offspring = []
56
+ for _ in range(self.population_size//2):
57
+ parent1, parent2 = random.sample(self.population, 2)
58
+ child = Net()
59
+ child.build(input_shape=(None, 10))
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
+ offspring.append(child)
65
+ self.population += offspring
66
 
67
  def mutation(self):
 
68
  for net in self.population:
 
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]
 
92
  ga.mutation()
93
  st.write(f"Generation {generation+1} complete")
94
 
95
+ # Evaluate the final population
96
  if gas is not None:
97
  final_accuracy = []
98
+ for task_id, ga in enumerate(gas):
99
  X_train, X_test, y_train, y_test = generate_dataset(task_id)
100
  accuracy = []
101
  for net in ga.population:
102
  net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 
103
  net.fit(X_train, y_train, epochs=10, verbose=0)
104
  loss, acc = net.evaluate(X_test, y_test, verbose=0)
105
  accuracy.append(acc)
106
  if len(accuracy) > 0:
107
  final_accuracy.append(np.mean(accuracy))
 
 
108
 
109
  # Trade populations between tasks
110
  if gas is not None:
 
128
  accuracy = []
129
  for net in ga.population:
130
  net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 
131
  net.fit(X_train, y_train, epochs=10, verbose=0)
132
  loss, acc = net.evaluate(X_test, y_test, verbose=0)
133
  accuracy.append(acc)