-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
197 lines (153 loc) · 6.16 KB
/
Copy pathmain.py
File metadata and controls
197 lines (153 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
X = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]], dtype=np.float32)
# XOR gate
y = np.array([[0],[1],[1],[0]], dtype=np.float32)
# AND gate
# y = np.array([[0], [0], [0], [1]], dtype=np.float32)
# OR gate
# y = np.array([[0], [1], [1], [1]], dtype=np.float32)
# NAND gate
# y = np.array([[1], [1], [1], [0]], dtype=np.float32)
# NOR gate
# y = np.array([[1], [0], [0], [0]], dtype=np.float32)
# Simple Feed Forward Perceptron
class Layer_Dense:
def __init__(self, n_inputs, n_neurons):
self.weights = 0.10 * np.random.randn(n_inputs, n_neurons)
self.biases = np.zeros((1, n_neurons))
def forward(self, inputs):
self.inputs = inputs
self.output = np.dot(inputs, self.weights) + self.biases
# Simeple forward RELU activation function
class Activation_ReLU:
def forward(self, inputs):
self.inputs = inputs
self.output = np.maximum(0, inputs)
# Simple Forward Sigmoid Activation Function
class Activation_Sigmoid:
def forward(self, inputs):
self.inputs = inputs
self.output = 1 / (1 + np.exp(-np.clip(inputs, -500, 500)))
# Simple Neural Network to get the weights and biases
class NeuralNetwork:
def __init__(self):
self.layer1 = Layer_Dense(2, 4)
self.activation1 = Activation_ReLU()
self.layer2 = Layer_Dense(4, 1)
self.activation2 = Activation_Sigmoid()
def forward(self, X):
self.layer1.forward(X)
self.activation1.forward(self.layer1.output)
self.layer2.forward(self.activation1.output)
self.activation2.forward(self.layer2.output)
return self.activation2.output
def get_weights(self):
return np.concatenate([
self.layer1.weights.flatten(),
self.layer1.biases.flatten(),
self.layer2.weights.flatten(),
self.layer2.biases.flatten()
])
def set_weights(self, flat_weights):
idx = 0
# Layer 1 weights
size = self.layer1.weights.size
self.layer1.weights = flat_weights[idx:idx+size].reshape(self.layer1.weights.shape)
idx += size
# Layer 1 biases
size = self.layer1.biases.size
self.layer1.biases = flat_weights[idx:idx+size].reshape(self.layer1.biases.shape)
idx += size
# Layer 2 weights
size = self.layer2.weights.size
self.layer2.weights = flat_weights[idx:idx+size].reshape(self.layer2.weights.shape)
idx += size
# Layer 2 biases
size = self.layer2.biases.size
self.layer2.biases = flat_weights[idx:idx+size].reshape(self.layer2.biases.shape)
# Genetic Algorithm
class Individual:
def __init__(self, num_genes):
self.chromosome = np.random.uniform(-1, 1, num_genes)
self.fitness = 0.0
# Create Population
def Create_Population(pop_size, num_genes):
return [Individual(num_genes) for _ in range(pop_size)]
# Evaluate Population
def Evaluate_Population(population, network, X, y):
for individual in population:
network.set_weights(individual.chromosome)
individual.fitness = Fitness_Function(network, X, y)
# Simple Fitness Function
def Fitness_Function(network, X, y):
predictions = network.forward(X)
mse = np.mean((predictions - y) ** 2)
fitness = 1 / (1 + mse)
correct = np.sum(np.round(predictions) == y)
if correct == 4:
fitness += 1.0
return fitness
# Tournament Slection
def Tournament(population, tour_size = 3):
selected = np.random.choice(population, tour_size, replace=False)
return max(selected, key = lambda ind: ind.fitness)
# Crossover
def CrossOver(parent1, parent2):
child1_chromosome = np.empty_like(parent1.chromosome)
child2_chromosome = np.empty_like(parent1.chromosome)
for i in range(len(parent1.chromosome)):
if np.random.rand() < 0.5:
child1_chromosome[i] = parent1.chromosome[i]
child2_chromosome[i] = parent2.chromosome[i]
else:
child1_chromosome[i] = parent2.chromosome[i]
child2_chromosome[i] = parent1.chromosome[i]
child1 = Individual(len(child1_chromosome))
child1.chromosome = child1_chromosome
child2 = Individual(len(child2_chromosome))
child2.chromosome = child2_chromosome
return child1, child2
# Mutate
def Mutate(individual, mutation_rate = 0.12, mutation_strength = 0.15):
for i in range(len(individual.chromosome)):
if np.random.rand() < mutation_rate:
individual.chromosome[i] += np.random.normal(0, mutation_strength)
individual.chromosome[i] = np.clip(individual.chromosome[i], -5, 5)
# Main Function to run the complete GA
def main(population, network, X, y, generations=500):
for gen in range(generations):
Evaluate_Population(population, network, X, y)
population.sort(key=lambda ind: ind.fitness, reverse=True)
best = population[0]
avg = np.mean([ind.fitness for ind in population])
if gen % 20 == 0 or best.fitness > 1.9:
network.set_weights(best.chromosome)
preds = network.forward(X).flatten()
print(f"Gen {gen:3d} | Best: {best.fitness:.4f} | Avg: {avg:.4f} | Preds: {np.round(preds, 3)}")
if best.fitness >= 1.99:
print("\n=== XOR SOLVED! ===")
break
new_population = [population[0], population[1]]
while len(new_population) < len(population):
p1 = Tournament(population)
p2 = Tournament(population)
c1, c2 = CrossOver(p1, p2)
Mutate(c1)
Mutate(c2)
new_population.extend([c1, c2])
population = new_population[:len(population)]
return population[0]
if __name__ == "__main__":
nn = NeuralNetwork()
num_genes = len(nn.get_weights())
population = Create_Population(pop_size=80, num_genes=num_genes)
print("Starting improved evolution...\n")
best = main(population, nn, X, y, generations=600)
# Final test
nn.set_weights(best.chromosome)
print("\nFinal predictions:")
print(nn.forward(X))
print(f"Expected: {np.round(y.flatten(), 3)}")