Sephfox commited on
Commit
b42f263
1 Parent(s): 57786e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -26
app.py CHANGED
@@ -17,17 +17,14 @@ def generate_dataset(task_id):
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 build(self, input_shape):
25
- self.fc1.build(input_shape)
26
- output_shape = self.fc1.compute_output_shape(input_shape)
27
- self.fc2.build(output_shape)
28
- output_shape = self.fc2.compute_output_shape(output_shape)
29
- self.fc3.build(output_shape)
30
- self.built = True
31
 
32
  def call(self, x):
33
  x = self.fc1(x)
@@ -55,23 +52,17 @@ class GeneticAlgorithm:
55
  self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
56
 
57
  def crossover(self):
58
- offspring = []
59
- X = np.random.rand(1, 10)
60
- for _ in range(self.population_size//2):
61
- parent1, parent2 = random.sample(self.population, 2)
62
- child = Net()
63
- child.build(input_shape=(None, 10))
64
- parent1.build(input_shape=(None, 10))
65
- parent2.build(input_shape=(None, 10))
66
-
67
- # Average the weights of the two parents
68
- parent1_weights = parent1.get_weights()
69
- parent2_weights = parent2.get_weights()
70
- child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
71
- child.set_weights(child_weights)
72
-
73
- offspring.append(child)
74
- self.population += offspring
75
 
76
  def mutation(self):
77
  X = np.random.rand(1, 10)
 
17
  class Net(keras.Model):
18
  def __init__(self):
19
  super(Net, self).__init__()
20
+ self.units = 20
21
+ self.units2 = 10
22
+ self.units3 = 2
23
 
24
  def build(self, input_shape):
25
+ self.fc1 = keras.layers.Dense(self.units, activation='relu', input_shape=(10,))
26
+ self.fc2 = keras.layers.Dense(self.units2, activation='relu')
27
+ self.fc3 = keras.layers.Dense(self.units3)
 
 
 
28
 
29
  def call(self, x):
30
  x = self.fc1(x)
 
52
  self.population = [self.population[i] for i in np.argsort(fitness)[-self.population_size//2:]]
53
 
54
  def crossover(self):
55
+ offspring = []
56
+ for _ in range(self.population_size//2):
57
+ parent1, parent2 = random.sample(self.population, 2)
58
+ child = Net()
59
+ child.build(input_shape=(None, 10))
60
+ parent1_weights = parent1.get_weights()
61
+ parent2_weights = parent2.get_weights()
62
+ child_weights = [(np.array(w1) + np.array(w2)) / 2 for w1, w2 in zip(parent1_weights, parent2_weights)]
63
+ child.set_weights(child_weights)
64
+ offspring.append(child)
65
+ self.population += offspring
 
 
 
 
 
 
66
 
67
  def mutation(self):
68
  X = np.random.rand(1, 10)