Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,6 +39,7 @@ class GeneticAlgorithm:
|
|
39 |
fitness = []
|
40 |
for i, net in enumerate(self.population):
|
41 |
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
|
|
42 |
net.fit(X_train, y_train, epochs=10, verbose=0)
|
43 |
loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
|
44 |
fitness.append(accuracy)
|
@@ -99,34 +100,38 @@ if st.button("Run evolution"):
|
|
99 |
accuracy = []
|
100 |
for net in ga.population:
|
101 |
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
|
|
102 |
net.fit(X_train, y_train, epochs=10, verbose=0)
|
103 |
-
loss, acc = net.evaluate(
|
104 |
-
accuracy.append(acc)
|
105 |
-
final_accuracy.append(np.mean(accuracy))
|
106 |
-
st.write(f"Final accuracy: {np.mean(final_accuracy)}")
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
ga2 = gas[j]
|
113 |
-
population1 = ga1.population
|
114 |
-
population2 = ga2.population
|
115 |
-
num_trade = int(0.1 * population_size)
|
116 |
-
trade1 = random.sample(population1, num_trade)
|
117 |
-
trade2 = random.sample(population2, num_trade)
|
118 |
-
ga1.population = population1 + trade2
|
119 |
-
ga2.population = population2 + trade1
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
for
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
100 |
accuracy = []
|
101 |
for net in ga.population:
|
102 |
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
103 |
+
net.build(input_shape=(None, 10)) # Compile the model before training
|
104 |
net.fit(X_train, y_train, epochs=10, verbose=0)
|
105 |
+
loss, acc = net.evaluate(X
|
|
|
|
|
|
|
106 |
|
107 |
+
loss, acc = net.evaluate(X_test, y_test, verbose=0)
|
108 |
+
accuracy.append(acc)
|
109 |
+
final_accuracy.append(np.mean(accuracy))
|
110 |
+
st.write(f"Final accuracy: {np.mean(final_accuracy)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
# Trade populations between tasks
|
113 |
+
for i in range(num_tasks):
|
114 |
+
for j in range(i+1, num_tasks):
|
115 |
+
ga1 = gas[i]
|
116 |
+
ga2 = gas[j]
|
117 |
+
population1 = ga1.population
|
118 |
+
population2 = ga2.population
|
119 |
+
num_trade = int(0.1 * population_size)
|
120 |
+
trade1 = random.sample(population1, num_trade)
|
121 |
+
trade2 = random.sample(population2, num_trade)
|
122 |
+
ga1.population = population1 + trade2
|
123 |
+
ga2.population = population2 + trade1
|
124 |
+
|
125 |
+
# Evaluate the final population after trading
|
126 |
+
final_accuracy_after_trade = []
|
127 |
+
for task_id, ga in enumerate(gas):
|
128 |
+
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
129 |
+
accuracy = []
|
130 |
+
for net in ga.population:
|
131 |
+
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
132 |
+
net.build(input_shape=(None, 10)) # Compile the model before training
|
133 |
+
net.fit(X_train, y_train, epochs=10, verbose=0)
|
134 |
+
loss, acc = net.evaluate(X_test, y_test, verbose=0)
|
135 |
+
accuracy.append(acc)
|
136 |
+
final_accuracy_after_trade.append(np.mean(accuracy))
|
137 |
+
st.write(f"Final accuracy after trading: {np.mean(final_accuracy_after_trade)}")
|