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.rnn = self.create_rnn()
|
5 |
+
self.cnn = self.create_cnn()
|
6 |
+
self.fnn = self.create_fnn()
|
7 |
+
self.ga_population = self.initialize_ga_population()
|
8 |
+
self.memory = {}
|
9 |
+
|
10 |
+
def create_snn(self):
|
11 |
+
return nn.Sequential(
|
12 |
+
nn.Linear(4096, 2048),
|
13 |
+
nn.ReLU(),
|
14 |
+
nn.Linear(2048, 1024),
|
15 |
+
nn.Sigmoid()
|
16 |
+
)
|
17 |
+
|
18 |
+
def create_rnn(self):
|
19 |
+
return nn.RNN(
|
20 |
+
input_size=4096,
|
21 |
+
hidden_size=2048,
|
22 |
+
num_layers=5,
|
23 |
+
nonlinearity="tanh",
|
24 |
+
batch_first=True
|
25 |
+
)
|
26 |
+
|
27 |
+
def create_cnn(self):
|
28 |
+
return nn.Sequential(
|
29 |
+
nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
|
30 |
+
nn.ReLU(),
|
31 |
+
nn.MaxPool2d(2),
|
32 |
+
nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
|
33 |
+
nn.ReLU(),
|
34 |
+
nn.MaxPool2d(2),
|
35 |
+
nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
|
36 |
+
nn.ReLU(),
|
37 |
+
nn.Flatten(),
|
38 |
+
nn.Linear(256 * 8 * 8, 1024),
|
39 |
+
nn.ReLU(),
|
40 |
+
nn.Linear(1024, 512)
|
41 |
+
)
|
42 |
+
|
43 |
+
def create_fnn(self):
|
44 |
+
return nn.Sequential(
|
45 |
+
nn.Linear(4096, 2048),
|
46 |
+
nn.ReLU(),
|
47 |
+
nn.Linear(2048, 1024),
|
48 |
+
nn.ReLU(),
|
49 |
+
nn.Linear(1024, 512)
|
50 |
+
)
|
51 |
+
|
52 |
+
def initialize_ga_population(self):
|
53 |
+
return [np.random.randn(4096) for _ in range(500)]
|
54 |
+
|
55 |
+
def run_snn(self, x):
|
56 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
57 |
+
output = self.snn(input_tensor)
|
58 |
+
print("SNN Output:", output)
|
59 |
+
return output
|
60 |
+
|
61 |
+
def run_rnn(self, x):
|
62 |
+
h0 = torch.zeros(5, x.size(0), 2048)
|
63 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
64 |
+
output, hn = self.rnn(input_tensor, h0)
|
65 |
+
print("RNN Output:", output)
|
66 |
+
return output
|
67 |
+
|
68 |
+
def run_cnn(self, x):
|
69 |
+
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
70 |
+
output = self.cnn(input_tensor)
|
71 |
+
print("CNN Output:", output)
|
72 |
+
return output
|
73 |
+
|
74 |
+
def run_fnn(self, x):
|
75 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
76 |
+
output = self.fnn(input_tensor)
|
77 |
+
print("FNN Output:", output)
|
78 |
+
return output
|
79 |
+
|
80 |
+
def run_ga(self, fitness_func):
|
81 |
+
for generation in range(200):
|
82 |
+
fitness_scores = [fitness_func(ind) for ind in self.ga_population]
|
83 |
+
sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
|
84 |
+
self.ga_population = sorted_population[:250] + [
|
85 |
+
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
86 |
+
]
|
87 |
+
best_fitness = max(fitness_scores)
|
88 |
+
print(f"Generation {generation}, Best Fitness: {best_fitness}")
|
89 |
+
return max(self.ga_population, key=fitness_func)
|
90 |
+
|
91 |
+
def consciousness_loop(self, input_data, mode="snn"):
|
92 |
+
feedback = self.memory.get(mode, None)
|
93 |
+
if feedback is not None:
|
94 |
+
input_data = np.concatenate((input_data, feedback), axis=-1)
|
95 |
+
if mode == "snn":
|
96 |
+
output = self.run_snn(input_data)
|
97 |
+
elif mode == "rnn":
|
98 |
+
output = self.run_rnn(input_data)
|
99 |
+
elif mode == "cnn":
|
100 |
+
output = self.run_cnn(input_data)
|
101 |
+
elif mode == "fnn":
|
102 |
+
output = self.run_fnn(input_data)
|
103 |
+
else:
|
104 |
+
raise ValueError("Invalid mode")
|
105 |
+
self.memory[mode] = output.detach().numpy()
|
106 |
+
return output
|
107 |
+
|
108 |
+
supermassive_nn = ConsciousSupermassiveNN()
|
109 |
+
|
110 |
+
class ConsciousSupermassiveNN:
|
111 |
+
def __init__(self):
|
112 |
+
self.snn = self.create_snn()
|
113 |
+
self.rnn = self.create_rnn()
|
114 |
+
self.cnn = self.create_cnn()
|
115 |
+
self.fnn = self.create_fnn()
|
116 |
+
self.ga_population = self.initialize_ga_population()
|
117 |
+
self.memory = {}
|
118 |
+
|
119 |
+
def create_snn(self):
|
120 |
+
return nn.Sequential(
|
121 |
+
nn.Linear(4096, 2048),
|
122 |
+
nn.ReLU(),
|
123 |
+
nn.Linear(2048, 1024),
|
124 |
+
nn.Sigmoid()
|
125 |
+
)
|
126 |
+
|
127 |
+
def create_rnn(self):
|
128 |
+
return nn.RNN(
|
129 |
+
input_size=4096,
|
130 |
+
hidden_size=2048,
|
131 |
+
num_layers=5,
|
132 |
+
nonlinearity="tanh",
|
133 |
+
batch_first=True
|
134 |
+
)
|
135 |
+
|
136 |
+
def create_cnn(self):
|
137 |
+
return nn.Sequential(
|
138 |
+
nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
|
139 |
+
nn.ReLU(),
|
140 |
+
nn.MaxPool2d(2),
|
141 |
+
nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
|
142 |
+
nn.ReLU(),
|
143 |
+
nn.MaxPool2d(2),
|
144 |
+
nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
|
145 |
+
nn.ReLU(),
|
146 |
+
nn.Flatten(),
|
147 |
+
nn.Linear(256 * 8 * 8, 1024),
|
148 |
+
nn.ReLU(),
|
149 |
+
nn.Linear(1024, 512)
|
150 |
+
)
|
151 |
+
|
152 |
+
def create_fnn(self):
|
153 |
+
return nn.Sequential(
|
154 |
+
nn.Linear(4096, 2048),
|
155 |
+
nn.ReLU(),
|
156 |
+
nn.Linear(2048, 1024),
|
157 |
+
nn.ReLU(),
|
158 |
+
nn.Linear(1024, 512)
|
159 |
+
)
|
160 |
+
|
161 |
+
def initialize_ga_population(self):
|
162 |
+
return [np.random.randn(4096) for _ in range(500)]
|
163 |
+
|
164 |
+
def run_snn(self, x):
|
165 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
166 |
+
output = self.snn(input_tensor)
|
167 |
+
print("SNN Output:", output)
|
168 |
+
return output
|
169 |
+
|
170 |
+
def run_rnn(self, x):
|
171 |
+
h0 = torch.zeros(5, x.size(0), 2048)
|
172 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
173 |
+
output, hn = self.rnn(input_tensor, h0)
|
174 |
+
print("RNN Output:", output)
|
175 |
+
return output
|
176 |
+
|
177 |
+
def run_cnn(self, x):
|
178 |
+
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
179 |
+
output = self.cnn(input_tensor)
|
180 |
+
print("CNN Output:", output)
|
181 |
+
return output
|
182 |
+
|
183 |
+
def run_fnn(self, x):
|
184 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
185 |
+
output = self.fnn(input_tensor)
|
186 |
+
print("FNN Output:", output)
|
187 |
+
return output
|
188 |
+
|
189 |
+
def run_ga(self, fitness_func):
|
190 |
+
for generation in range(200):
|
191 |
+
fitness_scores = [fitness_func(ind) for ind in self.ga_population]
|
192 |
+
sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
|
193 |
+
self.ga_population = sorted_population[:250] + [
|
194 |
+
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
195 |
+
]
|
196 |
+
best_fitness = max(fitness_scores)
|
197 |
+
print(f"Generation {generation}, Best Fitness: {best_fitness}")
|
198 |
+
return max(self.ga_population, key=fitness_func)
|
199 |
+
|
200 |
+
def consciousness_loop(self, input_data, mode="snn"):
|
201 |
+
feedback = self.memory.get(mode, None)
|
202 |
+
if feedback is not None:
|
203 |
+
input_data = np.concatenate((input_data, feedback), axis=-1)
|
204 |
+
if mode == "snn":
|
205 |
+
output = self.run_snn(input_data)
|
206 |
+
elif mode == "rnn":
|
207 |
+
output = self.run_rnn(input_data)
|
208 |
+
elif mode == "cnn":
|
209 |
+
output = self.run_cnn(input_data)
|
210 |
+
elif mode == "fnn":
|
211 |
+
output = self.run_fnn(input_data)
|
212 |
+
else:
|
213 |
+
raise ValueError("Invalid mode")
|
214 |
+
self.memory[mode] = output.detach().numpy()
|
215 |
+
return output
|
216 |
+
|
217 |
+
supermassive_nn = ConsciousSupermassiveNN()
|
218 |
+
|