Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,12 +29,13 @@ class Net(keras.Model):
|
|
29 |
|
30 |
# Define a genetic algorithm class
|
31 |
class GeneticAlgorithm:
|
32 |
-
def __init__(self, population_size):
|
33 |
self.population_size = population_size
|
|
|
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(task_id)
|
38 |
fitness = []
|
39 |
for net in self.population:
|
40 |
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
@@ -82,17 +83,17 @@ num_generations = st.sidebar.slider("Number of generations", 1, 100, 10)
|
|
82 |
|
83 |
# Run the evolution
|
84 |
if st.button("Run evolution"):
|
85 |
-
|
86 |
for generation in range(num_generations):
|
87 |
-
for
|
88 |
-
ga.selection(
|
89 |
ga.crossover()
|
90 |
ga.mutation()
|
91 |
st.write(f"Generation {generation+1} complete")
|
92 |
|
93 |
# Evaluate the final population
|
94 |
final_accuracy = []
|
95 |
-
for task_id in
|
96 |
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
97 |
accuracy = []
|
98 |
for net in ga.population:
|
@@ -101,4 +102,30 @@ if st.button("Run evolution"):
|
|
101 |
loss, acc = net.evaluate(X_test, y_test, verbose=0)
|
102 |
accuracy.append(acc)
|
103 |
final_accuracy.append(np.mean(accuracy))
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Define a genetic algorithm class
|
31 |
class GeneticAlgorithm:
|
32 |
+
def __init__(self, population_size, task_id):
|
33 |
self.population_size = population_size
|
34 |
+
self.task_id = task_id
|
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 net in self.population:
|
41 |
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
|
|
83 |
|
84 |
# Run the evolution
|
85 |
if st.button("Run evolution"):
|
86 |
+
gas = [GeneticAlgorithm(population_size, task_id) for task_id in range(num_tasks)]
|
87 |
for generation in range(num_generations):
|
88 |
+
for ga in gas:
|
89 |
+
ga.selection()
|
90 |
ga.crossover()
|
91 |
ga.mutation()
|
92 |
st.write(f"Generation {generation+1} complete")
|
93 |
|
94 |
# Evaluate the final population
|
95 |
final_accuracy = []
|
96 |
+
for task_id, ga in enumerate(gas):
|
97 |
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
98 |
accuracy = []
|
99 |
for net in ga.population:
|
|
|
102 |
loss, acc = net.evaluate(X_test, y_test, verbose=0)
|
103 |
accuracy.append(acc)
|
104 |
final_accuracy.append(np.mean(accuracy))
|
105 |
+
st.write(f"Final accuracy: {np.mean(final_accuracy)}")
|
106 |
+
|
107 |
+
# Trade populations between tasks
|
108 |
+
for i in range(num_tasks):
|
109 |
+
for j in range(i+1, num_tasks):
|
110 |
+
ga1 = gas[i]
|
111 |
+
ga2 = gas[j]
|
112 |
+
population1 = ga1.population
|
113 |
+
population2 = ga2.population
|
114 |
+
num_trade = int(0.1 * population_size)
|
115 |
+
trade1 = random.sample(population1, num_trade)
|
116 |
+
trade2 = random.sample(population2, num_trade)
|
117 |
+
ga1.population = population1 + trade2
|
118 |
+
ga2.population = population2 + trade1
|
119 |
+
|
120 |
+
# Evaluate the final population after trading
|
121 |
+
final_accuracy_after_trade = []
|
122 |
+
for task_id, ga in enumerate(gas):
|
123 |
+
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
124 |
+
accuracy = []
|
125 |
+
for net in ga.population:
|
126 |
+
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
127 |
+
net.fit(X_train, y_train, epochs=10, verbose=0)
|
128 |
+
loss, acc = net.evaluate(X_test, y_test, verbose=0)
|
129 |
+
accuracy.append(acc)
|
130 |
+
final_accuracy_after_trade.append(np.mean(accuracy))
|
131 |
+
st.write(f"Final accuracy after trading: {np.mean(final_accuracy_after_trade)}")
|