Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
|
48 |
-
|
49 |
def crossover(self):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
offspring.append(child)
|
66 |
-
self.population += offspring
|
67 |
|
68 |
def mutation(self):
|
69 |
-
|
70 |
-
|
71 |
-
net(
|
72 |
-
|
73 |
-
|
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")
|