Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
|
5 |
-
import random
|
6 |
from sklearn.datasets import make_classification
|
7 |
from sklearn.model_selection import train_test_split
|
8 |
from sklearn.metrics import accuracy_score
|
|
|
9 |
|
10 |
# Define a function to generate a dataset
|
11 |
def generate_dataset(task_id):
|
@@ -14,16 +14,16 @@ def generate_dataset(task_id):
|
|
14 |
return X_train, X_test, y_train, y_test
|
15 |
|
16 |
# Define a neural network class
|
17 |
-
class Net(
|
18 |
def __init__(self):
|
19 |
super(Net, self).__init__()
|
20 |
-
self.fc1 =
|
21 |
-
self.fc2 =
|
22 |
-
self.fc3 =
|
23 |
|
24 |
-
def
|
25 |
-
x =
|
26 |
-
x =
|
27 |
x = self.fc3(x)
|
28 |
return x
|
29 |
|
@@ -37,21 +37,9 @@ class GeneticAlgorithm:
|
|
37 |
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
38 |
fitness = []
|
39 |
for net in self.population:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
optimizer.zero_grad()
|
44 |
-
inputs = torch.tensor(X_train, dtype=torch.float32)
|
45 |
-
labels = torch.tensor(y_train, dtype=torch.long)
|
46 |
-
outputs = net(inputs)
|
47 |
-
loss = criterion(outputs, labels)
|
48 |
-
loss.backward()
|
49 |
-
optimizer.step()
|
50 |
-
inputs = torch.tensor(X_test, dtype=torch.float32)
|
51 |
-
labels = torch.tensor(y_test, dtype=torch.long)
|
52 |
-
outputs = net(inputs)
|
53 |
-
_, predicted = torch.max(outputs, 1)
|
54 |
-
accuracy = accuracy_score(labels, predicted)
|
55 |
fitness.append(accuracy)
|
56 |
self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
|
57 |
|
@@ -60,18 +48,27 @@ class GeneticAlgorithm:
|
|
60 |
for _ in range(self.population_size//2):
|
61 |
parent1, parent2 = random.sample(self.population, 2)
|
62 |
child = Net()
|
63 |
-
child.fc1.
|
64 |
-
child.fc2.
|
65 |
-
child.fc3.
|
66 |
offspring.append(child)
|
67 |
self.population += offspring
|
68 |
|
69 |
def mutation(self):
|
70 |
for net in self.population:
|
71 |
if random.random() < 0.1:
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
# Streamlit app
|
77 |
st.title("Evolution of Sub-Models")
|
@@ -98,13 +95,9 @@ if st.button("Run evolution"):
|
|
98 |
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
99 |
accuracy = []
|
100 |
for net in ga.population:
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
outputs = net(inputs)
|
108 |
-
loss = criterion(outputs, labels)
|
109 |
-
loss.backward()
|
110 |
-
optimizer.step()
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow import keras
|
|
|
5 |
from sklearn.datasets import make_classification
|
6 |
from sklearn.model_selection import train_test_split
|
7 |
from sklearn.metrics import accuracy_score
|
8 |
+
import random
|
9 |
|
10 |
# Define a function to generate a dataset
|
11 |
def generate_dataset(task_id):
|
|
|
14 |
return X_train, X_test, y_train, y_test
|
15 |
|
16 |
# Define a neural network class
|
17 |
+
class Net(keras.Model):
|
18 |
def __init__(self):
|
19 |
super(Net, self).__init__()
|
20 |
+
self.fc1 = keras.layers.Dense(20, activation='relu', input_shape=(10,))
|
21 |
+
self.fc2 = keras.layers.Dense(10, activation='relu')
|
22 |
+
self.fc3 = keras.layers.Dense(2)
|
23 |
|
24 |
+
def call(self, x):
|
25 |
+
x = self.fc1(x)
|
26 |
+
x = self.fc2(x)
|
27 |
x = self.fc3(x)
|
28 |
return x
|
29 |
|
|
|
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'])
|
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 |
self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
|
45 |
|
|
|
48 |
for _ in range(self.population_size//2):
|
49 |
parent1, parent2 = random.sample(self.population, 2)
|
50 |
child = Net()
|
51 |
+
child.fc1.set_weights((np.array(parent1.fc1.get_weights()) + np.array(parent2.fc1.get_weights())) / 2)
|
52 |
+
child.fc2.set_weights((np.array(parent1.fc2.get_weights()) + np.array(parent2.fc2.get_weights())) / 2)
|
53 |
+
child.fc3.set_weights((np.array(parent1.fc3.get_weights()) + np.array(parent2.fc3.get_weights())) / 2)
|
54 |
offspring.append(child)
|
55 |
self.population += offspring
|
56 |
|
57 |
def mutation(self):
|
58 |
for net in self.population:
|
59 |
if random.random() < 0.1:
|
60 |
+
weights = net.fc1.get_weights()
|
61 |
+
weights[0] += np.random.randn(*weights[0].shape) * 0.1
|
62 |
+
weights[1] += np.random.randn(*weights[1].shape) * 0.1
|
63 |
+
net.fc1.set_weights(weights)
|
64 |
+
weights = net.fc2.get_weights()
|
65 |
+
weights[0] += np.random.randn(*weights[0].shape) * 0.1
|
66 |
+
weights[1] += np.random.randn(*weights[1].shape) * 0.1
|
67 |
+
net.fc2.set_weights(weights)
|
68 |
+
weights = net.fc3.get_weights()
|
69 |
+
weights[0] += np.random.randn(*weights[0].shape) * 0.1
|
70 |
+
weights[1] += np.random.randn(*weights[1].shape) * 0.1
|
71 |
+
net.fc3.set_weights(weights)
|
72 |
|
73 |
# Streamlit app
|
74 |
st.title("Evolution of Sub-Models")
|
|
|
95 |
X_train, X_test, y_train, y_test = generate_dataset(task_id)
|
96 |
accuracy = []
|
97 |
for net in ga.population:
|
98 |
+
net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
99 |
+
net.fit(X_train, y_train, epochs=10, verbose=0)
|
100 |
+
loss, acc = net.evaluate(X_test, y_test, verbose=0)
|
101 |
+
accuracy.append(acc)
|
102 |
+
final_accuracy.append(np.mean(accuracy))
|
103 |
+
st.write(f"Final accuracy: {np.mean(final_accuracy)}")
|
|
|
|
|
|
|
|