Sephfox commited on
Commit
0dad55b
·
verified ·
1 Parent(s): cfd33ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -58
app.py CHANGED
@@ -21,11 +21,18 @@ class Net(keras.Model):
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
  # Define a genetic algorithm class
30
  class GeneticAlgorithm:
31
  def __init__(self, population_size, task_id):
@@ -38,38 +45,36 @@ class GeneticAlgorithm:
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)[-len(self.population)//2:]]
46
 
47
  def crossover(self):
48
  offspring = []
49
- for _ in range(len(self.population)//2):
 
50
  parent1, parent2 = random.sample(self.population, 2)
51
  child = Net()
52
- child.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 
 
53
 
54
- # Get the weights of the parent networks
55
  parent1_weights = parent1.get_weights()
56
  parent2_weights = parent2.get_weights()
57
-
58
- # Average the weights of the two parents
59
- child_weights = []
60
- for w1, w2 in zip(parent1_weights, parent2_weights):
61
- child_weights.append((w1 + w2) / 2)
62
-
63
- # Set the weights of the child network
64
- child.fc1.set_weights(child_weights[:2])
65
- child.fc2.set_weights(child_weights[2:4])
66
- child.fc3.set_weights(child_weights[4:])
67
 
68
  offspring.append(child)
69
  self.population += offspring
70
 
71
  def mutation(self):
 
72
  for net in self.population:
 
73
  if random.random() < 0.1:
74
  weights = net.get_weights()
75
  new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
@@ -87,9 +92,7 @@ num_generations = st.sidebar.slider("Number of generations", 1, 100, 10)
87
  gas = None
88
 
89
  # Run the evolution
90
- gas = []
91
  if st.button("Run evolution"):
92
- gas = [GeneticAlgorithm(population_size, task_id) for task_id in range(num_tasks)]
93
  gas = [GeneticAlgorithm(population_size, task_id) for task_id in range(num_tasks)]
94
  for generation in range(num_generations):
95
  for ga in gas:
@@ -98,47 +101,50 @@ if st.button("Run evolution"):
98
  ga.mutation()
99
  st.write(f"Generation {generation+1} complete")
100
 
101
- # Evaluate the final population
102
- final_accuracy = []
103
- for task_id, ga in enumerate(gas):
104
- X_train, X_test, y_train, y_test = generate_dataset(task_id)
105
- accuracy = []
106
- for net in ga.population:
107
- net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
108
- net.fit(X_train, y_train, epochs=10, verbose=0)
109
- loss, acc = net.evaluate(X_test, y_test, verbose=0)
110
- accuracy.append(acc)
111
- if len(accuracy) > 0:
112
- final_accuracy.append(np.mean(accuracy))
113
-
114
-
115
-
116
- # Trade populations between tasks
117
- for i in range(len(gas)):
118
- for j in range(i+1, len(gas)):
119
- ga1 = gas[i]
120
- ga2 = gas[j]
121
- population1 = ga1.population
122
- population2 = ga2.population
123
- num_trade = int(0.1 * population_size)
124
- trade1 = random.sample(population1, num_trade)
125
- trade2 = random.sample(population2, num_trade)
126
- ga1.population = population1 + trade2
127
- ga2.population = population2 + trade1
128
 
129
- # Evaluate the final population after trading
130
- final_accuracy_after_trade = []
131
- for task_id, ga in enumerate(gas):
132
- X_train, X_test, y_train, y_test = generate_dataset(task_id)
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)) # Compile the model before training
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)
140
- final_accuracy_after_trade.append(np.mean(accuracy))
141
- if len(final_accuracy) > 0:
142
- st.write(f"Final accuracy: {np.mean(final_accuracy)}")
143
- st.write(f"Final accuracy after trading: {np.mean(final_accuracy_after_trade)}")
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  self.fc2 = keras.layers.Dense(10, activation='relu')
22
  self.fc3 = keras.layers.Dense(2)
23
 
24
+ def build(self, input_shape):
25
+ self.fc1.build(input_shape)
26
+ self.fc2.build(self.fc1.output_shape)
27
+ self.fc3.build(self.fc2.output_shape)
28
+ self.built = True
29
+
30
  def call(self, x):
31
  x = self.fc1(x)
32
  x = self.fc2(x)
33
  x = self.fc3(x)
34
  return x
35
+
36
  # Define a genetic algorithm class
37
  class GeneticAlgorithm:
38
  def __init__(self, population_size, task_id):
 
45
  fitness = []
46
  for i, net in enumerate(self.population):
47
  net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
48
+ net.build(input_shape=(None, 10))
49
  net.fit(X_train, y_train, epochs=10, verbose=0)
50
  loss, accuracy = net.evaluate(X_test, y_test, verbose=0)
51
  fitness.append(accuracy)
52
  if len(fitness) > 0:
53
+ self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
54
 
55
  def crossover(self):
56
  offspring = []
57
+ X = np.random.rand(1, 10)
58
+ for _ in range(self.population_size//2):
59
  parent1, parent2 = random.sample(self.population, 2)
60
  child = Net()
61
+ child.build(input_shape=(None, 10))
62
+ parent1.build(input_shape=(None, 10))
63
+ parent2.build(input_shape=(None, 10))
64
 
65
+ # Average the weights of the two parents
66
  parent1_weights = parent1.get_weights()
67
  parent2_weights = parent2.get_weights()
68
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
69
+ child.set_weights(child_weights)
 
 
 
 
 
 
 
 
70
 
71
  offspring.append(child)
72
  self.population += offspring
73
 
74
  def mutation(self):
75
+ X = np.random.rand(1, 10)
76
  for net in self.population:
77
+ net.build(input_shape=(None, 10))
78
  if random.random() < 0.1:
79
  weights = net.get_weights()
80
  new_weights = [np.array(w) + np.random.randn(*w.shape) * 0.1 for w in weights]
 
92
  gas = None
93
 
94
  # Run the evolution
 
95
  if st.button("Run evolution"):
 
96
  gas = [GeneticAlgorithm(population_size, task_id) for task_id in range(num_tasks)]
97
  for generation in range(num_generations):
98
  for ga in gas:
 
101
  ga.mutation()
102
  st.write(f"Generation {generation+1} complete")
103
 
104
+ # Evaluate the final population
105
+ if gas is not None:
106
+ final_accuracy = []
107
+ for task_id, ga in enumerate(gas):
108
+ X_train, X_test, y_train, y_test = generate_dataset(task_id)
109
+ accuracy = []
110
+ for net in ga.population:
111
+ net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
112
+ net.build(input_shape=(None, 10))
113
+ net.fit(X_train, y_train, epochs=10, verbose=0)
114
+ loss, acc = net.evaluate(X_test, y_test, verbose=0)
115
+ accuracy.append(acc)
116
+ if len(accuracy) > 0:
117
+ final_accuracy.append(np.mean(accuracy))
118
+ if len(final_accuracy) > 0:
119
+ st.write(f"Final accuracy: {np.mean(final_accuracy)}")
 
 
 
 
 
 
 
 
 
 
 
120
 
121
+ # Trade populations between tasks
122
+ if gas is not None:
123
+ for i in range(len(gas)):
124
+ for j in range(i+1, len(gas)):
125
+ ga1 = gas[i]
126
+ ga2 = gas[j]
127
+ population1 = ga1.population
128
+ population2 = ga2.population
129
+ num_trade = int(0.1 * population_size)
130
+ trade1 = random.sample(population1, num_trade)
131
+ trade2 = random.sample(population2, num_trade)
132
+ ga1.population = population1 + trade2
133
+ ga2.population = population2 + trade1
 
 
134
 
135
+ # Evaluate the final population after trading
136
+ if gas is not None:
137
+ final_accuracy_after_trade = []
138
+ for task_id, ga in enumerate(gas):
139
+ X_train, X_test, y_train, y_test = generate_dataset(task_id)
140
+ accuracy = []
141
+ for net in ga.population:
142
+ net.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
143
+ net.build(input_shape=(None, 10))
144
+ net.fit(X_train, y_train, epochs=10, verbose=0)
145
+ loss, acc = net.evaluate(X_test, y_test, verbose=0)
146
+ accuracy.append(acc)
147
+ final_accuracy_after_trade.append(np.mean(accuracy))
148
+ if len(final_accuracy) > 0 and len(final_accuracy_after_trade) > 0:
149
+ st.write(f"Final accuracy: {np.mean(final_accuracy)}")
150
+ st.write(f"Final accuracy after trading: {np.mean(final_accuracy_after_trade)}")