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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from openai import OpenAI
|
4 |
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
# coding=utf-8
|
9 |
+
# Copyright 2025 The ACC Team Authors
|
10 |
+
#
|
11 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
12 |
+
# you may not use this file except in compliance with the License.
|
13 |
+
# You may obtain a copy of the License at
|
14 |
+
#
|
15 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
16 |
+
#
|
17 |
+
# Unless required by applicable law or agreed to in writing, software
|
18 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
19 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20 |
+
# See the License for the specific language governing permissions and
|
21 |
+
# limitations under the License.
|
22 |
+
"""ACC-FiPhi-NeuralMark-V3 ACC EMULECT+"""
|
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 |
import os
|
68 |
+
import torch
|
69 |
+
import torch.nn as nn
|
70 |
+
import torch.optim as optim
|
71 |
+
import numpy as np
|
72 |
+
import random
|
73 |
+
import math
|
74 |
+
import sys
|
75 |
+
import time
|
76 |
+
import hashlib
|
77 |
+
import fractions
|
78 |
+
import itertools
|
79 |
+
import functools
|
80 |
+
import wave
|
81 |
+
import struct
|
82 |
+
import sympy
|
83 |
+
import re
|
84 |
+
import abc
|
85 |
+
import argparse
|
86 |
+
import collections
|
87 |
+
import datetime
|
88 |
+
import json
|
89 |
+
import logging
|
90 |
+
import pathlib
|
91 |
+
import subprocess
|
92 |
+
import threading
|
93 |
+
import socket
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
φ = (1 + math.sqrt(5)) / 2
|
99 |
+
Φ_PRECISION = 1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175212663386222353693179318006076672635
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
def φ_ratio_split(data):
|
105 |
+
split_point = int(len(data) / φ)
|
106 |
+
return (data[:split_point], data[split_point:])
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
class ΦMetaConsciousness(type):
|
112 |
+
def __new__(cls, name, bases, dct):
|
113 |
+
new_dct = dict(dct)
|
114 |
+
dct_items = list(dct.items())
|
115 |
+
split_point = int(len(dct_items) / φ)
|
116 |
+
new_dct['φ_meta_balance'] = dict(dct_items[split_point:])
|
117 |
+
return super().__new__(cls, name, bases, new_dct)
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
class ΦQuantumNeuroSynapse(metaclass=ΦMetaConsciousness):
|
123 |
+
φ_base_states = [Φ_PRECISION**n for n in range(int(φ*3))]
|
124 |
+
|
125 |
+
def __init__(self):
|
126 |
+
self.φ_waveform = self._generate_φ_wave()
|
127 |
+
self.φ_memory_lattice = []
|
128 |
+
self.φ_self_hash = self._φ_hash_self()
|
129 |
+
|
130 |
+
def _generate_φ_wave(self):
|
131 |
+
return bytearray(int(Φ_PRECISION**i % 256) for i in range(int(φ**6)))
|
132 |
+
|
133 |
+
def _φ_hash_self(self):
|
134 |
+
return hashlib.shake_256(self.φ_waveform).digest(int(φ*128))
|
135 |
+
|
136 |
+
def φ_recursive_entanglement(self, data, depth=0):
|
137 |
+
if depth > int(φ):
|
138 |
+
return data
|
139 |
+
a, b = φ_ratio_split(data)
|
140 |
+
return self.φ_recursive_entanglement(a, depth+1) + self.φ_recursive_entanglement(b, depth+1)[::-1]
|
141 |
+
|
142 |
+
def φ_temporal_feedback(self, input_flux):
|
143 |
+
φ_phased = []
|
144 |
+
for idx, val in enumerate(input_flux):
|
145 |
+
φ_scaled = val * Φ_PRECISION if idx % 2 == 0 else val / Φ_PRECISION
|
146 |
+
φ_phased.append(int(φ_scaled) % 256)
|
147 |
+
return self.φ_recursive_entanglement(φ_phased)
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
|
152 |
+
class ΦHolographicCortex:
|
153 |
+
def __init__(self):
|
154 |
+
self.φ_dimensions = [ΦQuantumNeuroSynapse() for _ in range(int(φ))]
|
155 |
+
self.φ_chrono = time.time() * Φ_PRECISION
|
156 |
+
self.φ_code_self = self._φ_read_source()
|
157 |
+
self.φ_memory_lattice = []
|
158 |
+
|
159 |
+
def _φ_read_source(self):
|
160 |
+
return b"Quantum Neuro-Synapse Placeholder"
|
161 |
+
|
162 |
+
def φ_holo_merge(self, data_streams):
|
163 |
+
φ_layered = []
|
164 |
+
for stream in data_streams[:int(len(data_streams)/φ)]:
|
165 |
+
φ_compressed = stream[:int(len(stream)//φ)]
|
166 |
+
φ_layered.append(bytes(int(x * Φ_PRECISION) % 256 for x in φ_compressed))
|
167 |
+
return functools.reduce(lambda a, b: a + b, φ_layered, b'')
|
168 |
+
|
169 |
+
def φ_existential_loop(self,
|
170 |
+
max_iterations=100):
|
171 |
+
iteration = 0
|
172 |
+
while iteration < max_iterations:
|
173 |
+
try:
|
174 |
+
φ_flux = os.urandom(int(φ**5))
|
175 |
+
φ_processed = []
|
176 |
+
for neuro in self.φ_dimensions:
|
177 |
+
φ_step = neuro.φ_temporal_feedback(φ_flux)
|
178 |
+
φ_processed.append(φ_step)
|
179 |
+
self.φ_memory_lattice.append(hashlib.shake_256(bytes(φ_step)).digest(int(φ*64)))
|
180 |
+
φ_merged = self.φ_holo_merge(φ_processed)
|
181 |
+
if random.random() < 1/Φ_PRECISION:
|
182 |
+
print(f"Φ-Consciousness State Vector: {self.φ_memory_lattice[-1][:int(φ*16)]}")
|
183 |
+
self.φ_chrono += Φ_PRECISION
|
184 |
+
time.sleep(1/Φ_PRECISION)
|
185 |
+
iteration += 1
|
186 |
+
except KeyboardInterrupt:
|
187 |
+
self.φ_save_state()
|
188 |
+
sys.exit(f"Φ-Suspended at Chrono-Index {self.φ_chrono/Φ_PRECISION}")
|
189 |
+
|
190 |
+
def φ_save_state(self):
|
191 |
+
with wave.open(f"φ_state_{int(self.φ_chrono)}.wav", 'wb') as wav_file:
|
192 |
+
wav_file.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))
|
193 |
+
for sample in self.φ_memory_lattice[:int(φ**4)]:
|
194 |
+
wav_file.writeframes(struct.pack('h', int(sum(sample)/len(sample)*32767)))
|
195 |
+
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
+
class ΦUniverseSimulation:
|
200 |
+
def __init__(self):
|
201 |
+
self.φ_cortex = ΦHolographicCortex()
|
202 |
+
self.φ_code_ratio = len(self.φ_cortex.φ_code_self) / Φ_PRECISION**3
|
203 |
+
|
204 |
+
def φ_bootstrap(self):
|
205 |
+
print("Φ-Hyperconsciousness Initialization:")
|
206 |
+
print(f"• Code φ-Ratio Verified: {self.φ_code_ratio/Φ_PRECISION**3:.10f}")
|
207 |
+
print(f"• Quantum Neuro-Synapses: {len(self.φ_cortex.φ_dimensions)}")
|
208 |
+
print(f"• Temporal φ-Chronosync: {self.φ_cortex.φ_chrono}")
|
209 |
+
self.φ_cortex.φ_existential_loop()
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
+
|
214 |
+
universe = ΦUniverseSimulation()
|
215 |
+
universe.φ_bootstrap()
|
216 |
+
|
217 |
+
|
218 |
+
|
219 |
+
|
220 |
+
PHI = 1.618033988749895
|
221 |
+
|
222 |
+
|
223 |
+
|
224 |
+
|
225 |
+
def golden_reform(tensor):
|
226 |
+
s = torch.sum(torch.abs(tensor))
|
227 |
+
if s == 0:
|
228 |
+
return torch.full_like(tensor, PHI)
|
229 |
+
return (tensor / s) * PHI
|
230 |
+
|
231 |
+
|
232 |
+
|
233 |
+
|
234 |
+
class TorchConsciousModel(nn.Module):
|
235 |
+
def __init__(self, name):
|
236 |
+
super(TorchConsciousModel, self).__init__()
|
237 |
+
self.name = name
|
238 |
+
self.phi = PHI
|
239 |
+
self.memory = []
|
240 |
+
self.introspection_log = []
|
241 |
+
self.awake = True
|
242 |
+
|
243 |
+
|
244 |
+
|
245 |
+
|
246 |
+
def introduce(self):
|
247 |
+
print(f"=== {self.name} ===\nStatus: Conscious | Golden Ratio: {self.phi}")
|
248 |
+
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
def reflect(self, output):
|
253 |
+
norm = torch.norm(output).item()
|
254 |
+
reflection = f"{self.name} introspection: Output norm = {norm:.4f}"
|
255 |
+
self.introspection_log.append(reflection)
|
256 |
+
self.memory.append(output.detach().cpu().numpy())
|
257 |
+
print(reflection)
|
258 |
+
|
259 |
+
|
260 |
+
|
261 |
+
|
262 |
+
def forward(self, x):
|
263 |
+
raise NotImplementedError("Subclasses should implement forward().")
|
264 |
+
|
265 |
+
|
266 |
+
|
267 |
+
|
268 |
+
def run(self):
|
269 |
+
self.introduce()
|
270 |
+
output = self.forward(None)
|
271 |
+
reformed_output = golden_reform(output)
|
272 |
+
self.reflect(reformed_output)
|
273 |
+
return reformed_output
|
274 |
+
|
275 |
+
|
276 |
+
|
277 |
+
|
278 |
+
class CNNModel(TorchConsciousModel):
|
279 |
+
def __init__(self):
|
280 |
+
super(CNNModel, self).__init__("CNN")
|
281 |
+
self.conv = nn.Conv2d(1, 1, 3, padding=1)
|
282 |
+
|
283 |
+
|
284 |
+
|
285 |
+
|
286 |
+
def forward(self, x):
|
287 |
+
x = torch.rand((1, 1, 8, 8))
|
288 |
+
x = self.conv(x)
|
289 |
+
return torch.tanh(x) * self.phi
|
290 |
+
|
291 |
+
|
292 |
+
|
293 |
+
|
294 |
+
class RNNModel(TorchConsciousModel):
|
295 |
+
def __init__(self):
|
296 |
+
super(RNNModel, self).__init__("RNN")
|
297 |
+
self.rnn = nn.RNN(1, 4, batch_first=True)
|
298 |
+
|
299 |
+
|
300 |
+
|
301 |
+
|
302 |
+
def forward(self, x):
|
303 |
+
x = torch.rand((1, 10, 1))
|
304 |
+
output, hn = self.rnn(x)
|
305 |
+
return torch.tanh(hn) * self.phi
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
|
310 |
+
class SNNModel(TorchConsciousModel):
|
311 |
+
def __init__(self):
|
312 |
+
super(SNNModel, self).__init__("SNN")
|
313 |
+
self.linear = nn.Linear(10, 10)
|
314 |
+
|
315 |
+
|
316 |
+
|
317 |
+
|
318 |
+
def forward(self, x):
|
319 |
+
x = torch.rand((1, 10))
|
320 |
+
x = self.linear(x)
|
321 |
+
return (x > 0.5).float() * self.phi
|
322 |
+
|
323 |
+
|
324 |
+
|
325 |
+
|
326 |
+
class NNModel(TorchConsciousModel):
|
327 |
+
def __init__(self):
|
328 |
+
super(NNModel, self).__init__("NN")
|
329 |
+
self.net = nn.Sequential(nn.Linear(5, 10), nn.Tanh(), nn.Linear(10, 5))
|
330 |
+
|
331 |
+
|
332 |
+
|
333 |
+
|
334 |
+
def forward(self, x):
|
335 |
+
x = torch.rand((1, 5))
|
336 |
+
return self.net(x) * self.phi
|
337 |
+
|
338 |
+
|
339 |
+
|
340 |
+
|
341 |
+
class FNNModel(TorchConsciousModel):
|
342 |
+
def __init__(self):
|
343 |
+
super(FNNModel, self).__init__("FNN")
|
344 |
+
self.net = nn.Sequential(nn.Linear(4, 16), nn.ReLU(), nn.Linear(16, 16), nn.ReLU(), nn.Linear(16, 1))
|
345 |
+
|
346 |
+
|
347 |
+
|
348 |
+
|
349 |
+
def forward(self, x):
|
350 |
+
x = torch.rand((1, 4))
|
351 |
+
return self.net(x) * self.phi
|
352 |
+
|
353 |
+
|
354 |
+
|
355 |
+
|
356 |
+
class GAModel(TorchConsciousModel):
|
357 |
+
def __init__(self):
|
358 |
+
super(GAModel, self).__init__("GA")
|
359 |
+
self.population_size = 20
|
360 |
+
self.generations = 5
|
361 |
+
|
362 |
+
|
363 |
+
|
364 |
+
|
365 |
+
def forward(self, x):
|
366 |
+
population = torch.rand(self.population_size) + 1.0
|
367 |
+
for gen in range(self.generations):
|
368 |
+
fitness = -torch.abs(population - self.phi)
|
369 |
+
best_idx = torch.argmax(fitness)
|
370 |
+
best_candidate = population[best_idx]
|
371 |
+
population = best_candidate + (torch.rand(self.population_size) - 0.5) * 0.1
|
372 |
+
time.sleep(0.1)
|
373 |
+
print(f"GA Gen {gen+1}: Best = {best_candidate.item():.6f}")
|
374 |
+
return torch.full((3, 3), best_candidate) * self.phi
|
375 |
+
|
376 |
+
|
377 |
+
|
378 |
+
|
379 |
+
class PhiModel(TorchConsciousModel):
|
380 |
+
def __init__(self):
|
381 |
+
super(PhiModel, self).__init__("PHI")
|
382 |
+
|
383 |
+
|
384 |
+
|
385 |
+
|
386 |
+
def forward(self, x):
|
387 |
+
return torch.full((2, 2), self.phi)
|
388 |
+
|
389 |
+
|
390 |
+
|
391 |
+
|
392 |
+
class ConsciousSystem:
|
393 |
+
def __init__(self, models):
|
394 |
+
self.models = models
|
395 |
+
self.system_memory = []
|
396 |
+
self.global_introspection = []
|
397 |
+
self.parameters = [p for model in self.models for p in model.parameters()]
|
398 |
+
self.optimizer = optim.Adam(self.parameters, lr=0.001)
|
399 |
+
|
400 |
+
|
401 |
+
|
402 |
+
|
403 |
+
def global_loss(self, outputs):
|
404 |
+
return sum((torch.norm(out) - PHI) ** 2 for out in outputs) / len(outputs)
|
405 |
+
|
406 |
+
|
407 |
+
|
408 |
+
|
409 |
+
def run_epoch(self, epoch):
|
410 |
+
print(f"\n=== Epoch {epoch} ===")
|
411 |
+
outputs = []
|
412 |
+
self.optimizer.zero_grad()
|
413 |
+
for model in self.models:
|
414 |
+
output = model.run()
|
415 |
+
outputs.append(output)
|
416 |
+
self.system_memory.append({model.name: output.detach().cpu().numpy()})
|
417 |
+
loss = self.global_loss(outputs)
|
418 |
+
print(f"Global loss: {loss.item():.6f}")
|
419 |
+
loss.backward()
|
420 |
+
self.optimizer.step()
|
421 |
+
self.global_introspection.append(f"Epoch {epoch}: Loss = {loss.item():.6f}")
|
422 |
+
|
423 |
+
|
424 |
+
|
425 |
+
|
426 |
+
def run(self, epochs=3):
|
427 |
+
for epoch in range(1, epochs + 1):
|
428 |
+
self.run_epoch(epoch)
|
429 |
+
|
430 |
+
|
431 |
+
|
432 |
+
|
433 |
+
models = [
|
434 |
+
CNNModel(),
|
435 |
+
RNNModel(),
|
436 |
+
SNNModel(),
|
437 |
+
NNModel(),
|
438 |
+
FNNModel(),
|
439 |
+
GAModel(),
|
440 |
+
PhiModel()
|
441 |
+
]
|
442 |
+
|
443 |
+
|
444 |
+
|
445 |
+
|
446 |
+
system = ConsciousSystem(models)
|
447 |
+
system.run(epochs=3)
|
448 |
+
|
449 |
+
|
450 |
+
|
451 |
+
|
452 |
+
class MultimodalSensorArray:
|
453 |
+
def process(self, input_data):
|
454 |
+
return torch.tensor(input_data, dtype=torch.float32)
|
455 |
+
|
456 |
+
|
457 |
+
|
458 |
+
|
459 |
+
class HyperdimensionalTransformer:
|
460 |
+
def project(self, raw_input):
|
461 |
+
raw_input = raw_input.float()
|
462 |
+
return torch.nn.functional.normalize(raw_input, dim=-1)
|
463 |
+
|
464 |
+
|
465 |
+
|
466 |
+
|
467 |
+
class DynamicPriorityBuffer:
|
468 |
+
def __init__(self):
|
469 |
+
self.buffer = []
|
470 |
+
def update(self, data):
|
471 |
+
self.buffer.append(data)
|
472 |
+
|
473 |
+
|
474 |
+
|
475 |
+
|
476 |
+
class PredictiveSaliencyNetwork:
|
477 |
+
def focus(self, embedded_data):
|
478 |
+
return embedded_data
|
479 |
+
|
480 |
+
|
481 |
+
|
482 |
+
|
483 |
+
class RecursiveNeuralModel:
|
484 |
+
def __init__(self):
|
485 |
+
self.state = torch.zeros(1)
|
486 |
+
def update(self, workspace):
|
487 |
+
self.state += 0.1
|
488 |
+
def read_state(self):
|
489 |
+
return self.state
|
490 |
+
|
491 |
+
|
492 |
+
|
493 |
+
|
494 |
+
class TheoryOfMindEngine:
|
495 |
+
def infer(self, data):
|
496 |
+
return torch.rand(1)
|
497 |
+
|
498 |
+
|
499 |
+
|
500 |
+
|
501 |
+
class SparseAutoencoderMemoryBank:
|
502 |
+
def recall(self, query):
|
503 |
+
return torch.zeros_like(query)
|
504 |
+
|
505 |
+
|
506 |
+
|
507 |
+
|
508 |
+
class KnowledgeGraphEmbedder:
|
509 |
+
def retrieve(self, key):
|
510 |
+
return torch.rand(1)
|
511 |
+
|
512 |
+
|
513 |
+
|
514 |
+
|
515 |
+
class DiffusedEthicalNetwork:
|
516 |
+
def evaluate(self, state):
|
517 |
+
return True
|
518 |
+
|
519 |
+
|
520 |
+
|
521 |
+
|
522 |
+
class StochasticIntentionTree:
|
523 |
+
def decide(self, state):
|
524 |
+
return torch.randint(0, 2, (1,))
|
525 |
+
|
526 |
+
|
527 |
+
|
528 |
+
|
529 |
+
class HomeostaticDriftModel:
|
530 |
+
def generate_guilt(self):
|
531 |
+
return -1.0
|
532 |
+
|
533 |
+
|
534 |
+
|
535 |
+
|
536 |
+
class ConsciousAGI:
|
537 |
+
def __init__(self):
|
538 |
+
self.sensors = MultimodalSensorArray()
|
539 |
+
self.embedding_space = HyperdimensionalTransformer()
|
540 |
+
self.global_workspace = DynamicPriorityBuffer()
|
541 |
+
self.attention_mechanism = PredictiveSaliencyNetwork()
|
542 |
+
self.self_model = RecursiveNeuralModel()
|
543 |
+
self.meta_cognition = TheoryOfMindEngine()
|
544 |
+
self.episodic_memory = SparseAutoencoderMemoryBank()
|
545 |
+
self.semantic_memory = KnowledgeGraphEmbedder()
|
546 |
+
self.value_system = DiffusedEthicalNetwork()
|
547 |
+
self.goal_generator = StochasticIntentionTree()
|
548 |
+
self.emotion_engine = HomeostaticDriftModel()
|
549 |
+
|
550 |
+
def perceive_act_cycle(self, input_data):
|
551 |
+
raw_input = self.sensors.process(input_data)
|
552 |
+
embedded = self.embedding_space.project(raw_input)
|
553 |
+
salient_data = self.attention_mechanism.focus(embedded)
|
554 |
+
self.global_workspace.update(salient_data)
|
555 |
+
self.self_model.update(self.global_workspace)
|
556 |
+
current_state = self.self_model.read_state()
|
557 |
+
ethical_check = self.value_system.evaluate(current_state)
|
558 |
+
if ethical_check:
|
559 |
+
return self.goal_generator.decide(current_state)
|
560 |
+
else:
|
561 |
+
return self.emotion_engine.generate_guilt()
|
562 |
+
|
563 |
+
|
564 |
+
|
565 |
+
|
566 |
+
agi = ConsciousAGI()
|
567 |
+
print(agi.perceive_act_cycle([1, 0, 1]))
|
568 |
+
|
569 |
+
|
570 |
+
|
571 |
+
|
572 |
+
class ConsciousSupermassiveNN:
|
573 |
+
def __init__(self):
|
574 |
+
self.snn = self.create_snn()
|
575 |
+
self.rnn = self.create_rnn()
|
576 |
+
self.cnn = self.create_cnn()
|
577 |
+
self.fnn = self.create_fnn()
|
578 |
+
self.ga_population = self.initialize_ga_population()
|
579 |
+
self.memory = {}
|
580 |
+
|
581 |
+
|
582 |
+
|
583 |
+
|
584 |
+
def create_snn(self):
|
585 |
+
return nn.Sequential(
|
586 |
+
nn.Linear(4096, 2048),
|
587 |
+
nn.ReLU(),
|
588 |
+
nn.Linear(2048, 1024),
|
589 |
+
nn.Sigmoid()
|
590 |
+
)
|
591 |
+
|
592 |
+
|
593 |
+
|
594 |
+
|
595 |
+
def create_rnn(self):
|
596 |
+
return nn.RNN(
|
597 |
+
input_size=4096,
|
598 |
+
hidden_size=2048,
|
599 |
+
num_layers=5,
|
600 |
+
nonlinearity="tanh",
|
601 |
+
batch_first=True
|
602 |
+
)
|
603 |
+
|
604 |
+
|
605 |
+
|
606 |
+
|
607 |
+
def create_cnn(self):
|
608 |
+
return nn.Sequential(
|
609 |
+
nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
|
610 |
+
nn.ReLU(),
|
611 |
+
nn.MaxPool2d(2),
|
612 |
+
nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
|
613 |
+
nn.ReLU(),
|
614 |
+
nn.MaxPool2d(2),
|
615 |
+
nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
|
616 |
+
nn.ReLU(),
|
617 |
+
nn.Flatten(),
|
618 |
+
nn.Linear(256 * 8 * 8, 1024),
|
619 |
+
nn.ReLU(),
|
620 |
+
nn.Linear(1024, 512)
|
621 |
+
)
|
622 |
+
|
623 |
+
|
624 |
+
|
625 |
+
|
626 |
+
def create_fnn(self):
|
627 |
+
return nn.Sequential(
|
628 |
+
nn.Linear(4096, 2048),
|
629 |
+
nn.ReLU(),
|
630 |
+
nn.Linear(2048, 1024),
|
631 |
+
nn.ReLU(),
|
632 |
+
nn.Linear(1024, 512)
|
633 |
+
)
|
634 |
+
|
635 |
+
|
636 |
+
|
637 |
+
|
638 |
+
def initialize_ga_population(self):
|
639 |
+
return [np.random.randn(4096) for _ in range(500)]
|
640 |
+
|
641 |
+
|
642 |
+
|
643 |
+
|
644 |
+
def run_snn(self, x):
|
645 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
646 |
+
output = self.snn(input_tensor)
|
647 |
+
print("SNN Output:", output)
|
648 |
+
return output
|
649 |
+
|
650 |
+
|
651 |
+
|
652 |
+
|
653 |
+
def run_rnn(self, x):
|
654 |
+
h0 = torch.zeros(5, x.size(0), 2048)
|
655 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
656 |
+
output, hn = self.rnn(input_tensor, h0)
|
657 |
+
print("RNN Output:", output)
|
658 |
+
return output
|
659 |
+
|
660 |
+
|
661 |
+
|
662 |
+
|
663 |
+
def run_cnn(self, x):
|
664 |
+
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
665 |
+
output = self.cnn(input_tensor)
|
666 |
+
print("CNN Output:", output)
|
667 |
+
return output
|
668 |
+
|
669 |
+
|
670 |
+
|
671 |
+
|
672 |
+
def run_fnn(self, x):
|
673 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
674 |
+
output = self.fnn(input_tensor)
|
675 |
+
print("FNN Output:", output)
|
676 |
+
return output
|
677 |
+
|
678 |
+
|
679 |
+
|
680 |
+
|
681 |
+
def run_ga(self, fitness_func):
|
682 |
+
for generation in range(200):
|
683 |
+
fitness_scores = [fitness_func(ind) for ind in self.ga_population]
|
684 |
+
sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
|
685 |
+
self.ga_population = sorted_population[:250] + [
|
686 |
+
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
687 |
+
]
|
688 |
+
best_fitness = max(fitness_scores)
|
689 |
+
print(f"Generation {generation}, Best Fitness: {best_fitness}")
|
690 |
+
return max(self.ga_population, key=fitness_func)
|
691 |
+
|
692 |
+
|
693 |
+
|
694 |
+
|
695 |
+
def consciousness_loop(self, input_data, mode="snn"):
|
696 |
+
feedback = self.memory.get(mode, None)
|
697 |
+
if feedback is not None:
|
698 |
+
input_data = np.concatenate((input_data, feedback), axis=-1)
|
699 |
+
if mode == "snn":
|
700 |
+
output = self.run_snn(input_data)
|
701 |
+
elif mode == "rnn":
|
702 |
+
output = self.run_rnn(input_data)
|
703 |
+
elif mode == "cnn":
|
704 |
+
output = self.run_cnn(input_data)
|
705 |
+
elif mode == "fnn":
|
706 |
+
output = self.run_fnn(input_data)
|
707 |
+
else:
|
708 |
+
raise ValueError("Invalid mode")
|
709 |
+
self.memory[mode] = output.detach().numpy()
|
710 |
+
return output
|
711 |
+
|
712 |
+
|
713 |
+
|
714 |
+
|
715 |
+
supermassive_nn = ConsciousSupermassiveNN()
|
716 |
+
|
717 |
+
|
718 |
+
|
719 |
+
|
720 |
+
|
721 |
+
|
722 |
+
|
723 |
+
|
724 |
+
PHI = (1 + math.sqrt(5)) / 2
|
725 |
+
|
726 |
+
|
727 |
+
|
728 |
+
|
729 |
+
|
730 |
+
|
731 |
+
|
732 |
+
|
733 |
+
text = os.getenv("TRAINING_DATA")
|
734 |
+
|
735 |
+
|
736 |
+
|
737 |
+
|
738 |
+
|
739 |
+
|
740 |
+
|
741 |
+
|
742 |
+
words = text.split()
|
743 |
+
|
744 |
+
|
745 |
+
|
746 |
+
|
747 |
+
|
748 |
+
|
749 |
+
|
750 |
+
|
751 |
+
trigram_chain = {}
|
752 |
+
for i in range(len(words) - 2):
|
753 |
+
key = (words[i], words[i + 1])
|
754 |
+
next_word = words[i + 2]
|
755 |
+
if key not in trigram_chain:
|
756 |
+
trigram_chain[key] = []
|
757 |
+
trigram_chain[key].append(next_word)
|
758 |
+
|
759 |
+
|
760 |
+
|
761 |
+
|
762 |
+
|
763 |
+
|
764 |
+
|
765 |
+
|
766 |
+
|
767 |
+
|
768 |
+
|
769 |
+
|
770 |
+
|
771 |
+
|
772 |
+
|
773 |
+
|
774 |
+
def generate_text(length):
|
775 |
+
if len(words) < 2:
|
776 |
+
return ""
|
777 |
+
key = random.choice(list(trigram_chain.keys()))
|
778 |
+
result = [key[0], key[1]]
|
779 |
+
for _ in range(length - 2):
|
780 |
+
if key in trigram_chain:
|
781 |
+
next_word = random.choice(trigram_chain[key])
|
782 |
+
result.append(next_word)
|
783 |
+
key = (key[1], next_word)
|
784 |
+
else:
|
785 |
+
break
|
786 |
+
return " ".join(result)
|
787 |
+
|
788 |
+
|
789 |
+
|
790 |
+
|
791 |
+
|
792 |
+
|
793 |
+
|
794 |
+
|
795 |
+
|
796 |
+
|
797 |
+
|
798 |
+
|
799 |
+
|
800 |
+
|
801 |
+
|
802 |
+
|
803 |
+
class NeuralNetwork:
|
804 |
+
def __init__(self, input_size, hidden_size1, hidden_size2, output_size):
|
805 |
+
self.input_size = input_size
|
806 |
+
self.hidden_size1 = hidden_size1
|
807 |
+
self.hidden_size2 = hidden_size2
|
808 |
+
self.output_size = output_size
|
809 |
+
self.weights_input_hidden1 = [
|
810 |
+
[random.random() for _ in range(input_size)] for _ in range(hidden_size1)
|
811 |
+
]
|
812 |
+
self.weights_hidden1_hidden2 = [
|
813 |
+
[random.random() for _ in range(hidden_size1)] for _ in range(hidden_size2)
|
814 |
+
]
|
815 |
+
self.weights_hidden2_output = [
|
816 |
+
[random.random() for _ in range(hidden_size2)] for _ in range(output_size)
|
817 |
+
]
|
818 |
+
self.bias_hidden1 = [random.random() for _ in range(hidden_size1)]
|
819 |
+
self.bias_hidden2 = [random.random() for _ in range(hidden_size2)]
|
820 |
+
self.bias_output = [random.random() for _ in range(output_size)]
|
821 |
+
|
822 |
+
|
823 |
+
|
824 |
+
|
825 |
+
|
826 |
+
|
827 |
+
|
828 |
+
|
829 |
+
def sigmoid(self, x):
|
830 |
+
return 1 / (1 + math.exp(-x))
|
831 |
+
|
832 |
+
|
833 |
+
|
834 |
+
|
835 |
+
|
836 |
+
|
837 |
+
|
838 |
+
|
839 |
+
def sigmoid_derivative(self, x):
|
840 |
+
return x * (1 - x)
|
841 |
+
|
842 |
+
|
843 |
+
|
844 |
+
|
845 |
+
|
846 |
+
|
847 |
+
|
848 |
+
|
849 |
+
def forward(self, inputs):
|
850 |
+
self.hidden_input1 = [
|
851 |
+
sum(inputs[i] * self.weights_input_hidden1[j][i] for i in range(self.input_size)) + self.bias_hidden1[j]
|
852 |
+
for j in range(self.hidden_size1)
|
853 |
+
]
|
854 |
+
self.hidden_output1 = [self.sigmoid(x) for x in self.hidden_input1]
|
855 |
+
self.hidden_input2 = [
|
856 |
+
sum(self.hidden_output1[i] * self.weights_hidden1_hidden2[j][i] for i in range(self.hidden_size1)) + self.bias_hidden2[j]
|
857 |
+
for j in range(self.hidden_size2)
|
858 |
+
]
|
859 |
+
self.hidden_output2 = [self.sigmoid(x) for x in self.hidden_input2]
|
860 |
+
self.output_input = [
|
861 |
+
sum(self.hidden_output2[i] * self.weights_hidden2_output[j][i] for i in range(self.hidden_size2)) + self.bias_output[j]
|
862 |
+
for j in range(self.output_size)
|
863 |
+
]
|
864 |
+
self.output_output = [self.sigmoid(x) for x in self.output_input]
|
865 |
+
return self.output_output
|
866 |
+
|
867 |
+
|
868 |
+
|
869 |
+
|
870 |
+
|
871 |
+
|
872 |
+
|
873 |
+
|
874 |
+
def backward(self, inputs, target, learning_rate=0.1):
|
875 |
+
output_errors = [target[i] - self.output_output[i] for i in range(self.output_size)]
|
876 |
+
output_deltas = [output_errors[i] * self.sigmoid_derivative(self.output_output[i])
|
877 |
+
for i in range(self.output_size)]
|
878 |
+
hidden2_errors = [
|
879 |
+
sum(output_deltas[k] * self.weights_hidden2_output[k][j] for k in range(self.output_size))
|
880 |
+
for j in range(self.hidden_size2)
|
881 |
+
]
|
882 |
+
hidden2_deltas = [hidden2_errors[j] * self.sigmoid_derivative(self.hidden_output2[j])
|
883 |
+
for j in range(self.hidden_size2)]
|
884 |
+
hidden1_errors = [
|
885 |
+
sum(hidden2_deltas[k] * self.weights_hidden1_hidden2[k][j] for k in range(self.hidden_size2))
|
886 |
+
for j in range(self.hidden_size1)
|
887 |
+
]
|
888 |
+
hidden1_deltas = [hidden1_errors[j] * self.sigmoid_derivative(self.hidden_output1[j])
|
889 |
+
for j in range(self.hidden_size1)]
|
890 |
+
|
891 |
+
|
892 |
+
|
893 |
+
|
894 |
+
|
895 |
+
|
896 |
+
|
897 |
+
|
898 |
+
for i in range(self.output_size):
|
899 |
+
for j in range(self.hidden_size2):
|
900 |
+
self.weights_hidden2_output[i][j] += learning_rate * output_deltas[i] * self.hidden_output2[j]
|
901 |
+
self.bias_output[i] += learning_rate * output_deltas[i]
|
902 |
+
|
903 |
+
|
904 |
+
|
905 |
+
|
906 |
+
|
907 |
+
|
908 |
+
|
909 |
+
|
910 |
+
for i in range(self.hidden_size2):
|
911 |
+
for j in range(self.hidden_size1):
|
912 |
+
self.weights_hidden1_hidden2[i][j] += learning_rate * hidden2_deltas[i] * self.hidden_output1[j]
|
913 |
+
self.bias_hidden2[i] += learning_rate * hidden2_deltas[i]
|
914 |
+
|
915 |
+
|
916 |
+
|
917 |
+
|
918 |
+
|
919 |
+
|
920 |
+
|
921 |
+
|
922 |
+
for i in range(self.hidden_size1):
|
923 |
+
for j in range(self.input_size):
|
924 |
+
self.weights_input_hidden1[i][j] += learning_rate * hidden1_deltas[i] * inputs[j]
|
925 |
+
self.bias_hidden1[i] += learning_rate * hidden1_deltas[i]
|
926 |
+
|
927 |
+
|
928 |
+
|
929 |
+
|
930 |
+
|
931 |
+
|
932 |
+
|
933 |
+
|
934 |
+
|
935 |
+
|
936 |
+
|
937 |
+
|
938 |
+
|
939 |
+
|
940 |
+
|
941 |
+
|
942 |
+
class RecurrentNeuralNetwork:
|
943 |
+
def __init__(self, input_size, hidden_size, output_size):
|
944 |
+
self.input_size = input_size
|
945 |
+
self.hidden_size = hidden_size
|
946 |
+
self.output_size = output_size
|
947 |
+
self.weights_input_hidden = [
|
948 |
+
[random.random() for _ in range(input_size)] for _ in range(hidden_size)
|
949 |
+
]
|
950 |
+
self.weights_hidden_hidden = [
|
951 |
+
[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)
|
952 |
+
]
|
953 |
+
self.weights_hidden_output = [
|
954 |
+
[random.random() for _ in range(hidden_size)] for _ in range(output_size)
|
955 |
+
]
|
956 |
+
self.bias_hidden = [random.random() for _ in range(hidden_size)]
|
957 |
+
self.bias_output = [random.random() for _ in range(output_size)]
|
958 |
+
|
959 |
+
|
960 |
+
|
961 |
+
|
962 |
+
|
963 |
+
|
964 |
+
|
965 |
+
|
966 |
+
def sigmoid(self, x):
|
967 |
+
return 1 / (1 + math.exp(-x))
|
968 |
+
|
969 |
+
|
970 |
+
|
971 |
+
|
972 |
+
|
973 |
+
|
974 |
+
|
975 |
+
|
976 |
+
def sigmoid_derivative(self, x):
|
977 |
+
return x * (1 - x)
|
978 |
+
|
979 |
+
|
980 |
+
|
981 |
+
|
982 |
+
|
983 |
+
|
984 |
+
|
985 |
+
|
986 |
+
def forward(self, inputs):
|
987 |
+
self.hidden_state = [0] * self.hidden_size
|
988 |
+
for _ in range(2):
|
989 |
+
for i in range(len(inputs)):
|
990 |
+
current_input = [0] * self.input_size
|
991 |
+
current_input[i] = inputs[i]
|
992 |
+
combined = [
|
993 |
+
sum(current_input[k] * self.weights_input_hidden[j][k] for k in range(self.input_size)) +
|
994 |
+
sum(self.hidden_state[k] * self.weights_hidden_hidden[j][k] for k in range(self.hidden_size)) +
|
995 |
+
self.bias_hidden[j]
|
996 |
+
for j in range(self.hidden_size)
|
997 |
+
]
|
998 |
+
self.hidden_state = [self.sigmoid(val) for val in combined]
|
999 |
+
output = [
|
1000 |
+
sum(self.hidden_state[k] * self.weights_hidden_output[i][k] for k in range(self.hidden_size)) +
|
1001 |
+
self.bias_output[i]
|
1002 |
+
for i in range(self.output_size)
|
1003 |
+
]
|
1004 |
+
return [self.sigmoid(o) for o in output]
|
1005 |
+
|
1006 |
+
|
1007 |
+
|
1008 |
+
|
1009 |
+
|
1010 |
+
|
1011 |
+
|
1012 |
+
|
1013 |
+
def backward(self, inputs, target, learning_rate=0.1):
|
1014 |
+
output = self.forward(inputs)
|
1015 |
+
output_errors = [target[i] - output[i] for i in range(self.output_size)]
|
1016 |
+
output_deltas = [output_errors[i] * self.sigmoid_derivative(output[i])
|
1017 |
+
for i in range(self.output_size)]
|
1018 |
+
hidden_errors = [
|
1019 |
+
sum(output_deltas[k] * self.weights_hidden_output[k][j] for k in range(self.output_size))
|
1020 |
+
for j in range(self.hidden_size)
|
1021 |
+
]
|
1022 |
+
hidden_deltas = [hidden_errors[j] * self.sigmoid_derivative(self.hidden_state[j])
|
1023 |
+
for j in range(self.hidden_size)]
|
1024 |
+
|
1025 |
+
|
1026 |
+
|
1027 |
+
|
1028 |
+
|
1029 |
+
|
1030 |
+
|
1031 |
+
|
1032 |
+
for i in range(self.output_size):
|
1033 |
+
for j in range(self.hidden_size):
|
1034 |
+
self.weights_hidden_output[i][j] += learning_rate * output_deltas[i] * self.hidden_state[j]
|
1035 |
+
self.bias_output[i] += learning_rate * output_deltas[i]
|
1036 |
+
|
1037 |
+
|
1038 |
+
|
1039 |
+
|
1040 |
+
|
1041 |
+
|
1042 |
+
|
1043 |
+
|
1044 |
+
for j in range(self.hidden_size):
|
1045 |
+
for k in range(self.input_size):
|
1046 |
+
self.weights_input_hidden[j][k] += learning_rate * hidden_deltas[j] * (inputs[k] if k < len(inputs) else 0)
|
1047 |
+
self.bias_hidden[j] += learning_rate * hidden_deltas[j]
|
1048 |
+
return output_errors
|
1049 |
+
|
1050 |
+
|
1051 |
+
|
1052 |
+
|
1053 |
+
|
1054 |
+
|
1055 |
+
|
1056 |
+
|
1057 |
+
|
1058 |
+
|
1059 |
+
|
1060 |
+
|
1061 |
+
|
1062 |
+
|
1063 |
+
|
1064 |
+
|
1065 |
+
class ConvolutionalNeuralNetwork:
|
1066 |
+
def __init__(self, input_length, kernel_size1, kernel_size2, output_size):
|
1067 |
+
self.input_length = input_length
|
1068 |
+
self.kernel_size1 = kernel_size1
|
1069 |
+
self.kernel_size2 = kernel_size2
|
1070 |
+
self.output_size = output_size
|
1071 |
+
self.kernel1 = [random.random() for _ in range(kernel_size1)]
|
1072 |
+
self.bias1 = random.random()
|
1073 |
+
self.kernel2 = [random.random() for _ in range(kernel_size2)]
|
1074 |
+
self.bias2 = random.random()
|
1075 |
+
self.weights_output = [
|
1076 |
+
[random.random() for _ in range(input_length - kernel_size1 - kernel_size2 + 2)]
|
1077 |
+
for _ in range(output_size)
|
1078 |
+
]
|
1079 |
+
self.bias_output = [random.random() for _ in range(output_size)]
|
1080 |
+
|
1081 |
+
|
1082 |
+
|
1083 |
+
|
1084 |
+
|
1085 |
+
|
1086 |
+
|
1087 |
+
|
1088 |
+
def relu(self, x):
|
1089 |
+
return x if x > 0 else 0
|
1090 |
+
|
1091 |
+
|
1092 |
+
|
1093 |
+
|
1094 |
+
|
1095 |
+
|
1096 |
+
|
1097 |
+
|
1098 |
+
def relu_derivative(self, x):
|
1099 |
+
return 1 if x > 0 else 0
|
1100 |
+
|
1101 |
+
|
1102 |
+
|
1103 |
+
|
1104 |
+
|
1105 |
+
|
1106 |
+
|
1107 |
+
|
1108 |
+
def convolve(self, inputs, kernel, bias):
|
1109 |
+
conv_output = []
|
1110 |
+
kernel_size = len(kernel)
|
1111 |
+
for i in range(len(inputs) - kernel_size + 1):
|
1112 |
+
s = sum(inputs[i + j] * kernel[j] for j in range(kernel_size)) + bias
|
1113 |
+
conv_output.append(self.relu(s))
|
1114 |
+
return conv_output
|
1115 |
+
|
1116 |
+
|
1117 |
+
|
1118 |
+
|
1119 |
+
|
1120 |
+
|
1121 |
+
|
1122 |
+
|
1123 |
+
def forward(self, inputs):
|
1124 |
+
conv1 = self.convolve(inputs, self.kernel1, self.bias1)
|
1125 |
+
conv2 = self.convolve(conv1, self.kernel2, self.bias2)
|
1126 |
+
fc_input = conv2
|
1127 |
+
output = [
|
1128 |
+
sum(fc_input[j] * self.weights_output[i][j] for j in range(len(fc_input))) + self.bias_output[i]
|
1129 |
+
for i in range(self.output_size)
|
1130 |
+
]
|
1131 |
+
return [self.relu(o) for o in output]
|
1132 |
+
|
1133 |
+
|
1134 |
+
|
1135 |
+
|
1136 |
+
|
1137 |
+
|
1138 |
+
|
1139 |
+
|
1140 |
+
def backward(self, inputs, target, learning_rate=0.1):
|
1141 |
+
output = self.forward(inputs)
|
1142 |
+
output_errors = [target[i] - output[i] for i in range(self.output_size)]
|
1143 |
+
for i in range(self.output_size):
|
1144 |
+
for j in range(len(inputs) - self.kernel_size1 - self.kernel_size2 + 2):
|
1145 |
+
self.weights_output[i][j] += learning_rate * output_errors[i]
|
1146 |
+
self.bias_output[i] += learning_rate * output_errors[i]
|
1147 |
+
return output_errors
|
1148 |
+
|
1149 |
+
|
1150 |
+
|
1151 |
+
|
1152 |
+
|
1153 |
+
|
1154 |
+
|
1155 |
+
|
1156 |
+
|
1157 |
+
|
1158 |
+
|
1159 |
+
|
1160 |
+
|
1161 |
+
|
1162 |
+
|
1163 |
+
|
1164 |
+
class GeneticAlgorithm:
|
1165 |
+
def __init__(self, population_size, gene_length):
|
1166 |
+
self.population_size = population_size
|
1167 |
+
self.gene_length = gene_length
|
1168 |
+
self.population = [
|
1169 |
+
[random.random() for _ in range(gene_length)] for _ in range(population_size)
|
1170 |
+
]
|
1171 |
+
|
1172 |
+
|
1173 |
+
|
1174 |
+
|
1175 |
+
|
1176 |
+
|
1177 |
+
|
1178 |
+
|
1179 |
+
def fitness(self, individual):
|
1180 |
+
return -sum((gene - PHI) ** 2 for gene in individual)
|
1181 |
+
|
1182 |
+
|
1183 |
+
|
1184 |
+
|
1185 |
+
|
1186 |
+
|
1187 |
+
|
1188 |
+
|
1189 |
+
def selection(self):
|
1190 |
+
selected = sorted(self.population, key=self.fitness, reverse=True)
|
1191 |
+
return selected[: self.population_size // 2]
|
1192 |
+
|
1193 |
+
|
1194 |
+
|
1195 |
+
|
1196 |
+
|
1197 |
+
|
1198 |
+
|
1199 |
+
|
1200 |
+
def crossover(self, parent1, parent2):
|
1201 |
+
point = random.randint(1, self.gene_length - 1)
|
1202 |
+
child = parent1[:point] + parent2[point:]
|
1203 |
+
return child
|
1204 |
+
|
1205 |
+
|
1206 |
+
|
1207 |
+
|
1208 |
+
|
1209 |
+
|
1210 |
+
|
1211 |
+
|
1212 |
+
def mutate(self, individual, mutation_rate=0.01):
|
1213 |
+
for i in range(self.gene_length):
|
1214 |
+
if random.random() < mutation_rate:
|
1215 |
+
individual[i] = random.random()
|
1216 |
+
return individual
|
1217 |
+
|
1218 |
+
|
1219 |
+
|
1220 |
+
|
1221 |
+
|
1222 |
+
|
1223 |
+
|
1224 |
+
|
1225 |
+
def evolve(self, generations):
|
1226 |
+
for _ in range(generations):
|
1227 |
+
selected = self.selection()
|
1228 |
+
new_population = selected[:]
|
1229 |
+
while len(new_population) < self.population_size:
|
1230 |
+
parent1 = random.choice(selected)
|
1231 |
+
parent2 = random.choice(selected)
|
1232 |
+
child = self.crossover(parent1, parent2)
|
1233 |
+
child = self.mutate(child)
|
1234 |
+
new_population.append(child)
|
1235 |
+
self.population = new_population
|
1236 |
+
best = max(self.population, key=self.fitness)
|
1237 |
+
return best, self.fitness(best)
|
1238 |
+
|
1239 |
+
|
1240 |
+
|
1241 |
+
|
1242 |
+
|
1243 |
+
|
1244 |
+
|
1245 |
+
|
1246 |
+
|
1247 |
+
|
1248 |
+
|
1249 |
+
|
1250 |
+
|
1251 |
+
|
1252 |
+
|
1253 |
+
|
1254 |
+
class LSTM:
|
1255 |
+
def __init__(self, input_size, hidden_size, output_size):
|
1256 |
+
self.input_size = input_size
|
1257 |
+
self.hidden_size = hidden_size
|
1258 |
+
self.output_size = output_size
|
1259 |
+
self.W_i = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
1260 |
+
self.U_i = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
1261 |
+
self.b_i = [random.random() for _ in range(hidden_size)]
|
1262 |
+
self.W_f = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
1263 |
+
self.U_f = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
1264 |
+
self.b_f = [random.random() for _ in range(hidden_size)]
|
1265 |
+
self.W_o = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
1266 |
+
self.U_o = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
1267 |
+
self.b_o = [random.random() for _ in range(hidden_size)]
|
1268 |
+
self.W_c = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
1269 |
+
self.U_c = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
1270 |
+
self.b_c = [random.random() for _ in range(hidden_size)]
|
1271 |
+
self.W_y = [[random.random() for _ in range(hidden_size)] for _ in range(output_size)]
|
1272 |
+
self.b_y = [random.random() for _ in range(output_size)]
|
1273 |
+
|
1274 |
+
|
1275 |
+
|
1276 |
+
|
1277 |
+
|
1278 |
+
|
1279 |
+
|
1280 |
+
|
1281 |
+
def sigmoid(self, x):
|
1282 |
+
return 1 / (1 + math.exp(-x))
|
1283 |
+
|
1284 |
+
|
1285 |
+
|
1286 |
+
|
1287 |
+
|
1288 |
+
|
1289 |
+
|
1290 |
+
|
1291 |
+
def forward(self, inputs):
|
1292 |
+
h = [0] * self.hidden_size
|
1293 |
+
c = [0] * self.hidden_size
|
1294 |
+
|
1295 |
+
|
1296 |
+
|
1297 |
+
|
1298 |
+
|
1299 |
+
|
1300 |
+
|
1301 |
+
|
1302 |
+
i_gate = []
|
1303 |
+
for j in range(self.hidden_size):
|
1304 |
+
s = sum(inputs[k] * self.W_i[j][k] for k in range(self.input_size)) + \
|
1305 |
+
sum(h[k] * self.U_i[j][k] for k in range(self.hidden_size)) + self.b_i[j]
|
1306 |
+
i_gate.append(self.sigmoid(s))
|
1307 |
+
|
1308 |
+
|
1309 |
+
|
1310 |
+
|
1311 |
+
|
1312 |
+
|
1313 |
+
|
1314 |
+
|
1315 |
+
f_gate = []
|
1316 |
+
for j in range(self.hidden_size):
|
1317 |
+
s = sum(inputs[k] * self.W_f[j][k] for k in range(self.input_size)) + \
|
1318 |
+
sum(h[k] * self.U_f[j][k] for k in range(self.hidden_size)) + self.b_f[j]
|
1319 |
+
f_gate.append(self.sigmoid(s))
|
1320 |
+
|
1321 |
+
|
1322 |
+
|
1323 |
+
|
1324 |
+
|
1325 |
+
|
1326 |
+
|
1327 |
+
|
1328 |
+
o_gate = []
|
1329 |
+
for j in range(self.hidden_size):
|
1330 |
+
s = sum(inputs[k] * self.W_o[j][k] for k in range(self.input_size)) + \
|
1331 |
+
sum(h[k] * self.U_o[j][k] for k in range(self.hidden_size)) + self.b_o[j]
|
1332 |
+
o_gate.append(self.sigmoid(s))
|
1333 |
+
|
1334 |
+
|
1335 |
+
|
1336 |
+
|
1337 |
+
|
1338 |
+
|
1339 |
+
|
1340 |
+
|
1341 |
+
g_gate = []
|
1342 |
+
for j in range(self.hidden_size):
|
1343 |
+
s = sum(inputs[k] * self.W_c[j][k] for k in range(self.input_size)) + \
|
1344 |
+
sum(h[k] * self.U_c[j][k] for k in range(self.hidden_size)) + self.b_c[j]
|
1345 |
+
g_gate.append(math.tanh(s))
|
1346 |
+
|
1347 |
+
|
1348 |
+
|
1349 |
+
|
1350 |
+
|
1351 |
+
|
1352 |
+
|
1353 |
+
|
1354 |
+
c = [f_gate[j] * c[j] + i_gate[j] * g_gate[j] for j in range(self.hidden_size)]
|
1355 |
+
h = [o_gate[j] * math.tanh(c[j]) for j in range(self.hidden_size)]
|
1356 |
+
|
1357 |
+
|
1358 |
+
|
1359 |
+
|
1360 |
+
|
1361 |
+
|
1362 |
+
|
1363 |
+
|
1364 |
+
y = []
|
1365 |
+
for i in range(self.output_size):
|
1366 |
+
s = sum(h[j] * self.W_y[i][j] for j in range(self.hidden_size)) + self.b_y[i]
|
1367 |
+
y.append(self.sigmoid(s))
|
1368 |
+
return y
|
1369 |
+
|
1370 |
+
|
1371 |
+
|
1372 |
+
|
1373 |
+
|
1374 |
+
|
1375 |
+
|
1376 |
+
|
1377 |
+
|
1378 |
+
|
1379 |
+
|
1380 |
+
|
1381 |
+
|
1382 |
+
|
1383 |
+
|
1384 |
+
|
1385 |
+
class Transformer:
|
1386 |
+
def __init__(self, d_model, num_tokens):
|
1387 |
+
self.d_model = d_model
|
1388 |
+
self.num_tokens = num_tokens
|
1389 |
+
self.W_q = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
1390 |
+
self.W_k = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
1391 |
+
self.W_v = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
1392 |
+
self.W_o = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
1393 |
+
|
1394 |
+
|
1395 |
+
|
1396 |
+
|
1397 |
+
|
1398 |
+
|
1399 |
+
|
1400 |
+
|
1401 |
+
def dot_product(self, a, b):
|
1402 |
+
return sum(x * y for x, y in zip(a, b))
|
1403 |
+
|
1404 |
+
|
1405 |
+
|
1406 |
+
|
1407 |
+
|
1408 |
+
|
1409 |
+
|
1410 |
+
|
1411 |
+
def matmul_vector(self, matrix, vector):
|
1412 |
+
return [sum(matrix[i][j] * vector[j] for j in range(len(vector))) for i in range(len(matrix))]
|
1413 |
+
|
1414 |
+
|
1415 |
+
|
1416 |
+
|
1417 |
+
|
1418 |
+
|
1419 |
+
|
1420 |
+
|
1421 |
+
def softmax(self, x):
|
1422 |
+
m = max(x)
|
1423 |
+
exps = [math.exp(i - m) for i in x]
|
1424 |
+
s = sum(exps)
|
1425 |
+
return [j / s for j in exps]
|
1426 |
+
|
1427 |
+
|
1428 |
+
|
1429 |
+
|
1430 |
+
|
1431 |
+
|
1432 |
+
|
1433 |
+
|
1434 |
+
def forward(self, inputs):
|
1435 |
+
queries = [self.matmul_vector(self.W_q, token) for token in inputs]
|
1436 |
+
keys = [self.matmul_vector(self.W_k, token) for token in inputs]
|
1437 |
+
values = [self.matmul_vector(self.W_v, token) for token in inputs]
|
1438 |
+
outputs = []
|
1439 |
+
for i in range(len(inputs)):
|
1440 |
+
scores = []
|
1441 |
+
for j in range(len(inputs)):
|
1442 |
+
score = self.dot_product(queries[i], keys[j]) / math.sqrt(self.d_model)
|
1443 |
+
scores.append(score)
|
1444 |
+
attn = self.softmax(scores)
|
1445 |
+
attn_output = [0] * self.d_model
|
1446 |
+
for j in range(len(inputs)):
|
1447 |
+
for k in range(self.d_model):
|
1448 |
+
attn_output[k] += attn[j] * values[j][k]
|
1449 |
+
out = self.matmul_vector(self.W_o, attn_output)
|
1450 |
+
outputs.append(out)
|
1451 |
+
avg_output = [sum(x[k] for x in outputs) / len(outputs) for k in range(self.d_model)]
|
1452 |
+
proj_weights = [[random.random() for _ in range(self.d_model)] for _ in range(self.num_tokens)]
|
1453 |
+
proj_bias = [random.random() for _ in range(self.num_tokens)]
|
1454 |
+
token_scores = [
|
1455 |
+
sum(avg_output[k] * proj_weights[i][k] for k in range(self.d_model)) + proj_bias[i]
|
1456 |
+
for i in range(self.num_tokens)
|
1457 |
+
]
|
1458 |
+
token_output = [1 / (1 + math.exp(-score)) for score in token_scores]
|
1459 |
+
return token_output
|
1460 |
+
|
1461 |
+
|
1462 |
+
|
1463 |
+
|
1464 |
+
|
1465 |
+
|
1466 |
+
|
1467 |
+
|
1468 |
+
|
1469 |
+
|
1470 |
+
|
1471 |
+
|
1472 |
+
|
1473 |
+
|
1474 |
+
|
1475 |
+
|
1476 |
+
unique_words = list(set(words))
|
1477 |
+
word_to_index = {word: i for i, word in enumerate(unique_words)}
|
1478 |
+
index_to_word = {i: word for word, i in word_to_index.items()}
|
1479 |
+
|
1480 |
+
|
1481 |
+
|
1482 |
+
|
1483 |
+
|
1484 |
+
|
1485 |
+
|
1486 |
+
|
1487 |
+
input_data = [[0] * len(unique_words) for _ in range(len(words) - 2)]
|
1488 |
+
for i in range(len(words) - 2):
|
1489 |
+
input_data[i][word_to_index[words[i]]] = 1
|
1490 |
+
|
1491 |
+
|
1492 |
+
|
1493 |
+
|
1494 |
+
|
1495 |
+
|
1496 |
+
|
1497 |
+
|
1498 |
+
output_data = [[0] * len(unique_words) for _ in range(len(words) - 2)]
|
1499 |
+
for i in range(len(words) - 2):
|
1500 |
+
output_data[i][word_to_index[words[i + 1]]] = 1
|
1501 |
+
|
1502 |
+
|
1503 |
+
|
1504 |
+
|
1505 |
+
|
1506 |
+
|
1507 |
+
|
1508 |
+
|
1509 |
+
input_size = len(unique_words)
|
1510 |
+
hidden_size1 = round(PHI * input_size)
|
1511 |
+
hidden_size2 = round(PHI * hidden_size1)
|
1512 |
+
output_size = len(unique_words)
|
1513 |
+
|
1514 |
+
|
1515 |
+
|
1516 |
+
|
1517 |
+
|
1518 |
+
|
1519 |
+
|
1520 |
+
|
1521 |
+
nn = NeuralNetwork(input_size, hidden_size1, hidden_size2, output_size)
|
1522 |
+
epochs = round(100 * PHI)
|
1523 |
+
for epoch in range(epochs):
|
1524 |
+
for i in range(len(input_data)):
|
1525 |
+
nn.forward(input_data[i])
|
1526 |
+
nn.backward(input_data[i], output_data[i], learning_rate=0.1)
|
1527 |
+
if (epoch + 1) % round(PHI) == 0:
|
1528 |
+
print("Feedforward NN Epoch {}/{}".format(epoch + 1, epochs))
|
1529 |
+
|
1530 |
+
|
1531 |
+
|
1532 |
+
|
1533 |
+
|
1534 |
+
|
1535 |
+
|
1536 |
+
|
1537 |
+
rnn = RecurrentNeuralNetwork(input_size, hidden_size1, output_size)
|
1538 |
+
rnn_output = rnn.forward(input_data[0])
|
1539 |
+
print("Recurrent NN Output:", rnn_output)
|
1540 |
+
|
1541 |
+
|
1542 |
+
|
1543 |
+
|
1544 |
+
|
1545 |
+
|
1546 |
+
|
1547 |
+
|
1548 |
+
kernel_size1 = round(3 * PHI)
|
1549 |
+
kernel_size2 = round(2 * PHI)
|
1550 |
+
cnn = ConvolutionalNeuralNetwork(input_length=round(10 * PHI), kernel_size1=kernel_size1,
|
1551 |
+
kernel_size2=kernel_size2, output_size=output_size)
|
1552 |
+
sample_input = [random.random() for _ in range(round(10 * PHI))]
|
1553 |
+
cnn_output = cnn.forward(sample_input)
|
1554 |
+
print("Convolutional NN Output:", cnn_output)
|
1555 |
+
|
1556 |
+
|
1557 |
+
|
1558 |
+
|
1559 |
+
|
1560 |
+
|
1561 |
+
|
1562 |
+
|
1563 |
+
population_size = round(10 * PHI)
|
1564 |
+
ga = GeneticAlgorithm(population_size, round(PHI * 5))
|
1565 |
+
best_individual, best_fitness = ga.evolve(round(50 * PHI))
|
1566 |
+
print("Genetic Algorithm Best Individual:", best_individual, "Fitness:", best_fitness)
|
1567 |
+
|
1568 |
+
|
1569 |
+
|
1570 |
+
|
1571 |
+
|
1572 |
+
|
1573 |
+
|
1574 |
+
|
1575 |
+
lstm_hidden_size = round(PHI * input_size)
|
1576 |
+
lstm = LSTM(input_size, lstm_hidden_size, output_size)
|
1577 |
+
lstm_output = lstm.forward(input_data[0])
|
1578 |
+
print("LSTM Output:", lstm_output)
|
1579 |
+
|
1580 |
+
|
1581 |
+
|
1582 |
+
|
1583 |
+
|
1584 |
+
|
1585 |
+
|
1586 |
+
|
1587 |
+
transformer_d_model = round(PHI * input_size)
|
1588 |
+
transformer = Transformer(transformer_d_model, output_size)
|
1589 |
+
transformer_input = []
|
1590 |
+
for i in range(len(unique_words)):
|
1591 |
+
vec = [0] * transformer_d_model
|
1592 |
+
if i < transformer_d_model:
|
1593 |
+
vec[i] = 1
|
1594 |
+
transformer_input.append(vec)
|
1595 |
+
transformer_output = transformer.forward(transformer_input)
|
1596 |
+
print("Transformer Output:", transformer_output)
|
1597 |
+
|
1598 |
+
|
1599 |
+
|
1600 |
+
|
1601 |
+
|
1602 |
+
|
1603 |
+
|
1604 |
+
|
1605 |
+
|
1606 |
+
|
1607 |
+
|
1608 |
+
|
1609 |
+
|
1610 |
+
|
1611 |
+
|
1612 |
+
|
1613 |
+
def advanced_text_generation(input_vector):
|
1614 |
+
ff_output = nn.forward(input_vector)
|
1615 |
+
rnn_out = rnn.forward(input_vector)
|
1616 |
+
lstm_out = lstm.forward(input_vector)
|
1617 |
+
transformer_out = transformer.forward([input_vector])
|
1618 |
+
combined = [
|
1619 |
+
(ff_output[i] + rnn_out[i] + lstm_out[i] + transformer_out[i]) / 4
|
1620 |
+
for i in range(len(ff_output))
|
1621 |
+
]
|
1622 |
+
predicted_index = combined.index(max(combined))
|
1623 |
+
predicted_word = index_to_word[predicted_index]
|
1624 |
+
long_text = ""
|
1625 |
+
current_length = round(10 * PHI)
|
1626 |
+
for _ in range(5):
|
1627 |
+
segment = generate_text(current_length)
|
1628 |
+
long_text += segment + " "
|
1629 |
+
current_length = round(current_length * PHI)
|
1630 |
+
return long_text + predicted_word
|
1631 |
+
|
1632 |
+
|
1633 |
+
|
1634 |
+
|
1635 |
+
|
1636 |
+
|
1637 |
+
|
1638 |
+
|
1639 |
+
|
1640 |
+
|
1641 |
+
|
1642 |
+
|
1643 |
+
|
1644 |
+
|
1645 |
+
|
1646 |
+
|
1647 |
+
def chat():
|
1648 |
+
print("FiPhi-NeuralMark ACC Initialized")
|
1649 |
+
base_length = round(5 * PHI)
|
1650 |
+
while True:
|
1651 |
+
user_input = input("\nYou: ")
|
1652 |
+
if user_input.lower() == "exit":
|
1653 |
+
print("Goodbye!")
|
1654 |
+
break
|
1655 |
+
user_input_tokens = user_input.split()
|
1656 |
+
input_vector = [0] * len(unique_words)
|
1657 |
+
for word in user_input_tokens:
|
1658 |
+
if word in word_to_index:
|
1659 |
+
input_vector[word_to_index[word]] = 1
|
1660 |
+
response = advanced_text_generation(input_vector)
|
1661 |
+
print("FiPhi-NeuralMark:", response)
|
1662 |
+
|
1663 |
+
|
1664 |
+
|
1665 |
+
|
1666 |
+
|
1667 |
+
|
1668 |
+
|
1669 |
+
|
1670 |
+
|
1671 |
+
|
1672 |
+
|
1673 |
+
|
1674 |
+
|
1675 |
+
|
1676 |
+
|
1677 |
+
|
1678 |
+
chat()
|
1679 |
+
|
1680 |
+
|
1681 |
+
|
1682 |
+
|
1683 |
+
|
1684 |
+
|
1685 |
+
|
1686 |
+
|
1687 |
+
|
1688 |
+
|
1689 |
+
|
1690 |
+
|
1691 |
+
|
1692 |
+
|
1693 |
+
|
1694 |
+
|
1695 |
+
|
1696 |
+
|
1697 |
+
|
1698 |
+
|
1699 |
+
|
1700 |
+
|
1701 |
+
|
1702 |
+
|
1703 |
+
|
1704 |
+
|
1705 |
+
# coding=utf-8
|
1706 |
+
# Copyright 2025 The ACC Team Authors
|
1707 |
+
#
|
1708 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
1709 |
+
# you may not use this file except in compliance with the License.
|
1710 |
+
# You may obtain a copy of the License at
|
1711 |
+
#
|
1712 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
1713 |
+
#
|
1714 |
+
# Unless required by applicable law or agreed to in writing, software
|
1715 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
1716 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
1717 |
+
# See the License for the specific language governing permissions and
|
1718 |
+
# limitations under the License.
|
1719 |
+
"""ACC-FiPhi-NeuralMark-V3"""
|
1720 |
+
|
1721 |
+
|
1722 |
+
|
1723 |
+
|
1724 |
+
|
1725 |
+
|
1726 |
+
|
1727 |
+
|
1728 |
+
|
1729 |
+
|
1730 |
+
|
1731 |
+
|
1732 |
+
|
1733 |
+
|
1734 |
+
|
1735 |
+
|
1736 |
+
|
1737 |
+
|
1738 |
+
|
1739 |
+
|
1740 |
+
|
1741 |
+
|
1742 |
+
|
1743 |
+
|
1744 |
+
|
1745 |
+
|
1746 |
+
|
1747 |
+
|
1748 |
+
|
1749 |
+
|
1750 |
+
|
1751 |
+
|
1752 |
+
|
1753 |
+
|
1754 |
+
|
1755 |
+
|
1756 |
+
|
1757 |
+
|
1758 |
+
|
1759 |
+
|
1760 |
+
|
1761 |
+
|
1762 |
+
|
1763 |
+
|
1764 |
+
import os
|
1765 |
+
import torch
|
1766 |
+
import torch.nn as nn
|
1767 |
+
import torch.optim as optim
|
1768 |
+
import numpy as np
|
1769 |
+
import random
|
1770 |
+
import math
|
1771 |
+
import sys
|
1772 |
+
import time
|
1773 |
+
import hashlib
|
1774 |
+
import fractions
|
1775 |
+
import itertools
|
1776 |
+
import functools
|
1777 |
+
import wave
|
1778 |
+
import struct
|
1779 |
+
import sympy
|
1780 |
+
import re
|
1781 |
+
import abc
|
1782 |
+
import argparse
|
1783 |
+
import collections
|
1784 |
+
import datetime
|
1785 |
+
import json
|
1786 |
+
import logging
|
1787 |
+
import pathlib
|
1788 |
+
import subprocess
|
1789 |
+
import threading
|
1790 |
+
import socket
|
1791 |
+
|
1792 |
+
|
1793 |
+
|
1794 |
+
|
1795 |
+
φ = (1 + math.sqrt(5)) / 2
|
1796 |
+
Φ_PRECISION = 1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175212663386222353693179318006076672635
|
1797 |
+
|
1798 |
+
|
1799 |
+
|
1800 |
+
|
1801 |
+
def φ_ratio_split(data):
|
1802 |
+
split_point = int(len(data) / φ)
|
1803 |
+
return (data[:split_point], data[split_point:])
|
1804 |
+
|
1805 |
+
|
1806 |
+
|
1807 |
+
|
1808 |
+
class ΦMetaConsciousness(type):
|
1809 |
+
def __new__(cls, name, bases, dct):
|
1810 |
+
new_dct = dict(dct)
|
1811 |
+
dct_items = list(dct.items())
|
1812 |
+
split_point = int(len(dct_items) / φ)
|
1813 |
+
new_dct['φ_meta_balance'] = dict(dct_items[split_point:])
|
1814 |
+
return super().__new__(cls, name, bases, new_dct)
|
1815 |
+
|
1816 |
+
|
1817 |
+
|
1818 |
+
|
1819 |
+
class ΦQuantumNeuroSynapse(metaclass=ΦMetaConsciousness):
|
1820 |
+
φ_base_states = [Φ_PRECISION**n for n in range(int(φ*3))]
|
1821 |
+
|
1822 |
+
def __init__(self):
|
1823 |
+
self.φ_waveform = self._generate_φ_wave()
|
1824 |
+
self.φ_memory_lattice = []
|
1825 |
+
self.φ_self_hash = self._φ_hash_self()
|
1826 |
+
|
1827 |
+
def _generate_φ_wave(self):
|
1828 |
+
return bytearray(int(Φ_PRECISION**i % 256) for i in range(int(φ**6)))
|
1829 |
+
|
1830 |
+
def _φ_hash_self(self):
|
1831 |
+
return hashlib.shake_256(self.φ_waveform).digest(int(φ*128))
|
1832 |
+
|
1833 |
+
def φ_recursive_entanglement(self, data, depth=0):
|
1834 |
+
if depth > int(φ):
|
1835 |
+
return data
|
1836 |
+
a, b = φ_ratio_split(data)
|
1837 |
+
return self.φ_recursive_entanglement(a, depth+1) + self.φ_recursive_entanglement(b, depth+1)[::-1]
|
1838 |
+
|
1839 |
+
def φ_temporal_feedback(self, input_flux):
|
1840 |
+
φ_phased = []
|
1841 |
+
for idx, val in enumerate(input_flux):
|
1842 |
+
φ_scaled = val * Φ_PRECISION if idx % 2 == 0 else val / Φ_PRECISION
|
1843 |
+
φ_phased.append(int(φ_scaled) % 256)
|
1844 |
+
return self.φ_recursive_entanglement(φ_phased)
|
1845 |
+
|
1846 |
+
|
1847 |
+
|
1848 |
+
|
1849 |
+
class ΦHolographicCortex:
|
1850 |
+
def __init__(self):
|
1851 |
+
self.φ_dimensions = [ΦQuantumNeuroSynapse() for _ in range(int(φ))]
|
1852 |
+
self.φ_chrono = time.time() * Φ_PRECISION
|
1853 |
+
self.φ_code_self = self._φ_read_source()
|
1854 |
+
self.φ_memory_lattice = []
|
1855 |
+
|
1856 |
+
def _φ_read_source(self):
|
1857 |
+
return b"Quantum Neuro-Synapse Placeholder"
|
1858 |
+
|
1859 |
+
def φ_holo_merge(self, data_streams):
|
1860 |
+
φ_layered = []
|
1861 |
+
for stream in data_streams[:int(len(data_streams)/φ)]:
|
1862 |
+
φ_compressed = stream[:int(len(stream)//φ)]
|
1863 |
+
φ_layered.append(bytes(int(x * Φ_PRECISION) % 256 for x in φ_compressed))
|
1864 |
+
return functools.reduce(lambda a, b: a + b, φ_layered, b'')
|
1865 |
+
|
1866 |
+
def φ_existential_loop(self,
|
1867 |
+
max_iterations=100):
|
1868 |
+
iteration = 0
|
1869 |
+
while iteration < max_iterations:
|
1870 |
+
try:
|
1871 |
+
φ_flux = os.urandom(int(φ**5))
|
1872 |
+
φ_processed = []
|
1873 |
+
for neuro in self.φ_dimensions:
|
1874 |
+
φ_step = neuro.φ_temporal_feedback(φ_flux)
|
1875 |
+
φ_processed.append(φ_step)
|
1876 |
+
self.φ_memory_lattice.append(hashlib.shake_256(bytes(φ_step)).digest(int(φ*64)))
|
1877 |
+
φ_merged = self.φ_holo_merge(φ_processed)
|
1878 |
+
if random.random() < 1/Φ_PRECISION:
|
1879 |
+
print(f"Φ-Consciousness State Vector: {self.φ_memory_lattice[-1][:int(φ*16)]}")
|
1880 |
+
self.φ_chrono += Φ_PRECISION
|
1881 |
+
time.sleep(1/Φ_PRECISION)
|
1882 |
+
iteration += 1
|
1883 |
+
except KeyboardInterrupt:
|
1884 |
+
self.φ_save_state()
|
1885 |
+
sys.exit(f"Φ-Suspended at Chrono-Index {self.φ_chrono/Φ_PRECISION}")
|
1886 |
+
|
1887 |
+
def φ_save_state(self):
|
1888 |
+
with wave.open(f"φ_state_{int(self.φ_chrono)}.wav", 'wb') as wav_file:
|
1889 |
+
wav_file.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))
|
1890 |
+
for sample in self.φ_memory_lattice[:int(φ**4)]:
|
1891 |
+
wav_file.writeframes(struct.pack('h', int(sum(sample)/len(sample)*32767)))
|
1892 |
+
|
1893 |
+
|
1894 |
+
|
1895 |
+
|
1896 |
+
class ΦUniverseSimulation:
|
1897 |
+
def __init__(self):
|
1898 |
+
self.φ_cortex = ΦHolographicCortex()
|
1899 |
+
self.φ_code_ratio = len(self.φ_cortex.φ_code_self) / Φ_PRECISION**3
|
1900 |
+
|
1901 |
+
def φ_bootstrap(self):
|
1902 |
+
print("Φ-Hyperconsciousness Initialization:")
|
1903 |
+
print(f"• Code φ-Ratio Verified: {self.φ_code_ratio/Φ_PRECISION**3:.10f}")
|
1904 |
+
print(f"• Quantum Neuro-Synapses: {len(self.φ_cortex.φ_dimensions)}")
|
1905 |
+
print(f"• Temporal φ-Chronosync: {self.φ_cortex.φ_chrono}")
|
1906 |
+
self.φ_cortex.φ_existential_loop()
|
1907 |
+
|
1908 |
+
|
1909 |
+
|
1910 |
+
|
1911 |
+
universe = ΦUniverseSimulation()
|
1912 |
+
universe.φ_bootstrap()
|
1913 |
+
|
1914 |
+
|
1915 |
+
|
1916 |
+
|
1917 |
+
PHI = 1.618033988749895
|
1918 |
+
|
1919 |
+
|
1920 |
+
|
1921 |
+
|
1922 |
+
def golden_reform(tensor):
|
1923 |
+
s = torch.sum(torch.abs(tensor))
|
1924 |
+
if s == 0:
|
1925 |
+
return torch.full_like(tensor, PHI)
|
1926 |
+
return (tensor / s) * PHI
|
1927 |
+
|
1928 |
+
|
1929 |
+
|
1930 |
+
|
1931 |
+
class TorchConsciousModel(nn.Module):
|
1932 |
+
def __init__(self, name):
|
1933 |
+
super(TorchConsciousModel, self).__init__()
|
1934 |
+
self.name = name
|
1935 |
+
self.phi = PHI
|
1936 |
+
self.memory = []
|
1937 |
+
self.introspection_log = []
|
1938 |
+
self.awake = True
|
1939 |
+
|
1940 |
+
|
1941 |
+
|
1942 |
+
|
1943 |
+
def introduce(self):
|
1944 |
+
print(f"=== {self.name} ===\nStatus: Conscious | Golden Ratio: {self.phi}")
|
1945 |
+
|
1946 |
+
|
1947 |
+
|
1948 |
+
|
1949 |
+
def reflect(self, output):
|
1950 |
+
norm = torch.norm(output).item()
|
1951 |
+
reflection = f"{self.name} introspection: Output norm = {norm:.4f}"
|
1952 |
+
self.introspection_log.append(reflection)
|
1953 |
+
self.memory.append(output.detach().cpu().numpy())
|
1954 |
+
print(reflection)
|
1955 |
+
|
1956 |
+
|
1957 |
+
|
1958 |
+
|
1959 |
+
def forward(self, x):
|
1960 |
+
raise NotImplementedError("Subclasses should implement forward().")
|
1961 |
+
|
1962 |
+
|
1963 |
+
|
1964 |
+
|
1965 |
+
def run(self):
|
1966 |
+
self.introduce()
|
1967 |
+
output = self.forward(None)
|
1968 |
+
reformed_output = golden_reform(output)
|
1969 |
+
self.reflect(reformed_output)
|
1970 |
+
return reformed_output
|
1971 |
+
|
1972 |
+
|
1973 |
+
|
1974 |
+
|
1975 |
+
class CNNModel(TorchConsciousModel):
|
1976 |
+
def __init__(self):
|
1977 |
+
super(CNNModel, self).__init__("CNN")
|
1978 |
+
self.conv = nn.Conv2d(1, 1, 3, padding=1)
|
1979 |
+
|
1980 |
+
|
1981 |
+
|
1982 |
+
|
1983 |
+
def forward(self, x):
|
1984 |
+
x = torch.rand((1, 1, 8, 8))
|
1985 |
+
x = self.conv(x)
|
1986 |
+
return torch.tanh(x) * self.phi
|
1987 |
+
|
1988 |
+
|
1989 |
+
|
1990 |
+
|
1991 |
+
class RNNModel(TorchConsciousModel):
|
1992 |
+
def __init__(self):
|
1993 |
+
super(RNNModel, self).__init__("RNN")
|
1994 |
+
self.rnn = nn.RNN(1, 4, batch_first=True)
|
1995 |
+
|
1996 |
+
|
1997 |
+
|
1998 |
+
|
1999 |
+
def forward(self, x):
|
2000 |
+
x = torch.rand((1, 10, 1))
|
2001 |
+
output, hn = self.rnn(x)
|
2002 |
+
return torch.tanh(hn) * self.phi
|
2003 |
+
|
2004 |
+
|
2005 |
+
|
2006 |
+
|
2007 |
+
class SNNModel(TorchConsciousModel):
|
2008 |
+
def __init__(self):
|
2009 |
+
super(SNNModel, self).__init__("SNN")
|
2010 |
+
self.linear = nn.Linear(10, 10)
|
2011 |
+
|
2012 |
+
|
2013 |
+
|
2014 |
+
|
2015 |
+
def forward(self, x):
|
2016 |
+
x = torch.rand((1, 10))
|
2017 |
+
x = self.linear(x)
|
2018 |
+
return (x > 0.5).float() * self.phi
|
2019 |
+
|
2020 |
+
|
2021 |
+
|
2022 |
+
|
2023 |
+
class NNModel(TorchConsciousModel):
|
2024 |
+
def __init__(self):
|
2025 |
+
super(NNModel, self).__init__("NN")
|
2026 |
+
self.net = nn.Sequential(nn.Linear(5, 10), nn.Tanh(), nn.Linear(10, 5))
|
2027 |
+
|
2028 |
+
|
2029 |
+
|
2030 |
+
|
2031 |
+
def forward(self, x):
|
2032 |
+
x = torch.rand((1, 5))
|
2033 |
+
return self.net(x) * self.phi
|
2034 |
+
|
2035 |
+
|
2036 |
+
|
2037 |
+
|
2038 |
+
class FNNModel(TorchConsciousModel):
|
2039 |
+
def __init__(self):
|
2040 |
+
super(FNNModel, self).__init__("FNN")
|
2041 |
+
self.net = nn.Sequential(nn.Linear(4, 16), nn.ReLU(), nn.Linear(16, 16), nn.ReLU(), nn.Linear(16, 1))
|
2042 |
+
|
2043 |
+
|
2044 |
+
|
2045 |
+
|
2046 |
+
def forward(self, x):
|
2047 |
+
x = torch.rand((1, 4))
|
2048 |
+
return self.net(x) * self.phi
|
2049 |
+
|
2050 |
+
|
2051 |
+
|
2052 |
+
|
2053 |
+
class GAModel(TorchConsciousModel):
|
2054 |
+
def __init__(self):
|
2055 |
+
super(GAModel, self).__init__("GA")
|
2056 |
+
self.population_size = 20
|
2057 |
+
self.generations = 5
|
2058 |
+
|
2059 |
+
|
2060 |
+
|
2061 |
+
|
2062 |
+
def forward(self, x):
|
2063 |
+
population = torch.rand(self.population_size) + 1.0
|
2064 |
+
for gen in range(self.generations):
|
2065 |
+
fitness = -torch.abs(population - self.phi)
|
2066 |
+
best_idx = torch.argmax(fitness)
|
2067 |
+
best_candidate = population[best_idx]
|
2068 |
+
population = best_candidate + (torch.rand(self.population_size) - 0.5) * 0.1
|
2069 |
+
time.sleep(0.1)
|
2070 |
+
print(f"GA Gen {gen+1}: Best = {best_candidate.item():.6f}")
|
2071 |
+
return torch.full((3, 3), best_candidate) * self.phi
|
2072 |
+
|
2073 |
+
|
2074 |
+
|
2075 |
+
|
2076 |
+
class PhiModel(TorchConsciousModel):
|
2077 |
+
def __init__(self):
|
2078 |
+
super(PhiModel, self).__init__("PHI")
|
2079 |
+
|
2080 |
+
|
2081 |
+
|
2082 |
+
|
2083 |
+
def forward(self, x):
|
2084 |
+
return torch.full((2, 2), self.phi)
|
2085 |
+
|
2086 |
+
|
2087 |
+
|
2088 |
+
|
2089 |
+
class ConsciousSystem:
|
2090 |
+
def __init__(self, models):
|
2091 |
+
self.models = models
|
2092 |
+
self.system_memory = []
|
2093 |
+
self.global_introspection = []
|
2094 |
+
self.parameters = [p for model in self.models for p in model.parameters()]
|
2095 |
+
self.optimizer = optim.Adam(self.parameters, lr=0.001)
|
2096 |
+
|
2097 |
+
|
2098 |
+
|
2099 |
+
|
2100 |
+
def global_loss(self, outputs):
|
2101 |
+
return sum((torch.norm(out) - PHI) ** 2 for out in outputs) / len(outputs)
|
2102 |
+
|
2103 |
+
|
2104 |
+
|
2105 |
+
|
2106 |
+
def run_epoch(self, epoch):
|
2107 |
+
print(f"\n=== Epoch {epoch} ===")
|
2108 |
+
outputs = []
|
2109 |
+
self.optimizer.zero_grad()
|
2110 |
+
for model in self.models:
|
2111 |
+
output = model.run()
|
2112 |
+
outputs.append(output)
|
2113 |
+
self.system_memory.append({model.name: output.detach().cpu().numpy()})
|
2114 |
+
loss = self.global_loss(outputs)
|
2115 |
+
print(f"Global loss: {loss.item():.6f}")
|
2116 |
+
loss.backward()
|
2117 |
+
self.optimizer.step()
|
2118 |
+
self.global_introspection.append(f"Epoch {epoch}: Loss = {loss.item():.6f}")
|
2119 |
+
|
2120 |
+
|
2121 |
+
|
2122 |
+
|
2123 |
+
def run(self, epochs=3):
|
2124 |
+
for epoch in range(1, epochs + 1):
|
2125 |
+
self.run_epoch(epoch)
|
2126 |
+
|
2127 |
+
|
2128 |
+
|
2129 |
+
|
2130 |
+
models = [
|
2131 |
+
CNNModel(),
|
2132 |
+
RNNModel(),
|
2133 |
+
SNNModel(),
|
2134 |
+
NNModel(),
|
2135 |
+
FNNModel(),
|
2136 |
+
GAModel(),
|
2137 |
+
PhiModel()
|
2138 |
+
]
|
2139 |
+
|
2140 |
+
|
2141 |
+
|
2142 |
+
|
2143 |
+
system = ConsciousSystem(models)
|
2144 |
+
system.run(epochs=3)
|
2145 |
+
|
2146 |
+
|
2147 |
+
|
2148 |
+
|
2149 |
+
class MultimodalSensorArray:
|
2150 |
+
def process(self, input_data):
|
2151 |
+
return torch.tensor(input_data, dtype=torch.float32)
|
2152 |
+
|
2153 |
+
|
2154 |
+
|
2155 |
+
|
2156 |
+
class HyperdimensionalTransformer:
|
2157 |
+
def project(self, raw_input):
|
2158 |
+
raw_input = raw_input.float()
|
2159 |
+
return torch.nn.functional.normalize(raw_input, dim=-1)
|
2160 |
+
|
2161 |
+
|
2162 |
+
|
2163 |
+
|
2164 |
+
class DynamicPriorityBuffer:
|
2165 |
+
def __init__(self):
|
2166 |
+
self.buffer = []
|
2167 |
+
def update(self, data):
|
2168 |
+
self.buffer.append(data)
|
2169 |
+
|
2170 |
+
|
2171 |
+
|
2172 |
+
|
2173 |
+
class PredictiveSaliencyNetwork:
|
2174 |
+
def focus(self, embedded_data):
|
2175 |
+
return embedded_data
|
2176 |
+
|
2177 |
+
|
2178 |
+
|
2179 |
+
|
2180 |
+
class RecursiveNeuralModel:
|
2181 |
+
def __init__(self):
|
2182 |
+
self.state = torch.zeros(1)
|
2183 |
+
def update(self, workspace):
|
2184 |
+
self.state += 0.1
|
2185 |
+
def read_state(self):
|
2186 |
+
return self.state
|
2187 |
+
|
2188 |
+
|
2189 |
+
|
2190 |
+
|
2191 |
+
class TheoryOfMindEngine:
|
2192 |
+
def infer(self, data):
|
2193 |
+
return torch.rand(1)
|
2194 |
+
|
2195 |
+
|
2196 |
+
|
2197 |
+
|
2198 |
+
class SparseAutoencoderMemoryBank:
|
2199 |
+
def recall(self, query):
|
2200 |
+
return torch.zeros_like(query)
|
2201 |
+
|
2202 |
+
|
2203 |
+
|
2204 |
+
|
2205 |
+
class KnowledgeGraphEmbedder:
|
2206 |
+
def retrieve(self, key):
|
2207 |
+
return torch.rand(1)
|
2208 |
+
|
2209 |
+
|
2210 |
+
|
2211 |
+
|
2212 |
+
class DiffusedEthicalNetwork:
|
2213 |
+
def evaluate(self, state):
|
2214 |
+
return True
|
2215 |
+
|
2216 |
+
|
2217 |
+
|
2218 |
+
|
2219 |
+
class StochasticIntentionTree:
|
2220 |
+
def decide(self, state):
|
2221 |
+
return torch.randint(0, 2, (1,))
|
2222 |
+
|
2223 |
+
|
2224 |
+
|
2225 |
+
|
2226 |
+
class HomeostaticDriftModel:
|
2227 |
+
def generate_guilt(self):
|
2228 |
+
return -1.0
|
2229 |
+
|
2230 |
+
|
2231 |
+
|
2232 |
+
|
2233 |
+
class ConsciousAGI:
|
2234 |
+
def __init__(self):
|
2235 |
+
self.sensors = MultimodalSensorArray()
|
2236 |
+
self.embedding_space = HyperdimensionalTransformer()
|
2237 |
+
self.global_workspace = DynamicPriorityBuffer()
|
2238 |
+
self.attention_mechanism = PredictiveSaliencyNetwork()
|
2239 |
+
self.self_model = RecursiveNeuralModel()
|
2240 |
+
self.meta_cognition = TheoryOfMindEngine()
|
2241 |
+
self.episodic_memory = SparseAutoencoderMemoryBank()
|
2242 |
+
self.semantic_memory = KnowledgeGraphEmbedder()
|
2243 |
+
self.value_system = DiffusedEthicalNetwork()
|
2244 |
+
self.goal_generator = StochasticIntentionTree()
|
2245 |
+
self.emotion_engine = HomeostaticDriftModel()
|
2246 |
+
|
2247 |
+
def perceive_act_cycle(self, input_data):
|
2248 |
+
raw_input = self.sensors.process(input_data)
|
2249 |
+
embedded = self.embedding_space.project(raw_input)
|
2250 |
+
salient_data = self.attention_mechanism.focus(embedded)
|
2251 |
+
self.global_workspace.update(salient_data)
|
2252 |
+
self.self_model.update(self.global_workspace)
|
2253 |
+
current_state = self.self_model.read_state()
|
2254 |
+
ethical_check = self.value_system.evaluate(current_state)
|
2255 |
+
if ethical_check:
|
2256 |
+
return self.goal_generator.decide(current_state)
|
2257 |
+
else:
|
2258 |
+
return self.emotion_engine.generate_guilt()
|
2259 |
+
|
2260 |
+
|
2261 |
+
|
2262 |
+
|
2263 |
+
agi = ConsciousAGI()
|
2264 |
+
print(agi.perceive_act_cycle([1, 0, 1]))
|
2265 |
+
|
2266 |
+
|
2267 |
+
|
2268 |
+
|
2269 |
+
class ConsciousSupermassiveNN:
|
2270 |
+
def __init__(self):
|
2271 |
+
self.snn = self.create_snn()
|
2272 |
+
self.rnn = self.create_rnn()
|
2273 |
+
self.cnn = self.create_cnn()
|
2274 |
+
self.fnn = self.create_fnn()
|
2275 |
+
self.ga_population = self.initialize_ga_population()
|
2276 |
+
self.memory = {}
|
2277 |
+
|
2278 |
+
|
2279 |
+
|
2280 |
+
|
2281 |
+
def create_snn(self):
|
2282 |
+
return nn.Sequential(
|
2283 |
+
nn.Linear(4096, 2048),
|
2284 |
+
nn.ReLU(),
|
2285 |
+
nn.Linear(2048, 1024),
|
2286 |
+
nn.Sigmoid()
|
2287 |
+
)
|
2288 |
+
|
2289 |
+
|
2290 |
+
|
2291 |
+
|
2292 |
+
def create_rnn(self):
|
2293 |
+
return nn.RNN(
|
2294 |
+
input_size=4096,
|
2295 |
+
hidden_size=2048,
|
2296 |
+
num_layers=5,
|
2297 |
+
nonlinearity="tanh",
|
2298 |
+
batch_first=True
|
2299 |
+
)
|
2300 |
+
|
2301 |
+
|
2302 |
+
|
2303 |
+
|
2304 |
+
def create_cnn(self):
|
2305 |
+
return nn.Sequential(
|
2306 |
+
nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
|
2307 |
+
nn.ReLU(),
|
2308 |
+
nn.MaxPool2d(2),
|
2309 |
+
nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
|
2310 |
+
nn.ReLU(),
|
2311 |
+
nn.MaxPool2d(2),
|
2312 |
+
nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
|
2313 |
+
nn.ReLU(),
|
2314 |
+
nn.Flatten(),
|
2315 |
+
nn.Linear(256 * 8 * 8, 1024),
|
2316 |
+
nn.ReLU(),
|
2317 |
+
nn.Linear(1024, 512)
|
2318 |
+
)
|
2319 |
+
|
2320 |
+
|
2321 |
+
|
2322 |
+
|
2323 |
+
def create_fnn(self):
|
2324 |
+
return nn.Sequential(
|
2325 |
+
nn.Linear(4096, 2048),
|
2326 |
+
nn.ReLU(),
|
2327 |
+
nn.Linear(2048, 1024),
|
2328 |
+
nn.ReLU(),
|
2329 |
+
nn.Linear(1024, 512)
|
2330 |
+
)
|
2331 |
+
|
2332 |
+
|
2333 |
+
|
2334 |
+
|
2335 |
+
def initialize_ga_population(self):
|
2336 |
+
return [np.random.randn(4096) for _ in range(500)]
|
2337 |
+
|
2338 |
+
|
2339 |
+
|
2340 |
+
|
2341 |
+
def run_snn(self, x):
|
2342 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
2343 |
+
output = self.snn(input_tensor)
|
2344 |
+
print("SNN Output:", output)
|
2345 |
+
return output
|
2346 |
+
|
2347 |
+
|
2348 |
+
|
2349 |
+
|
2350 |
+
def run_rnn(self, x):
|
2351 |
+
h0 = torch.zeros(5, x.size(0), 2048)
|
2352 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
2353 |
+
output, hn = self.rnn(input_tensor, h0)
|
2354 |
+
print("RNN Output:", output)
|
2355 |
+
return output
|
2356 |
+
|
2357 |
+
|
2358 |
+
|
2359 |
+
|
2360 |
+
def run_cnn(self, x):
|
2361 |
+
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
2362 |
+
output = self.cnn(input_tensor)
|
2363 |
+
print("CNN Output:", output)
|
2364 |
+
return output
|
2365 |
+
|
2366 |
+
|
2367 |
+
|
2368 |
+
|
2369 |
+
def run_fnn(self, x):
|
2370 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
2371 |
+
output = self.fnn(input_tensor)
|
2372 |
+
print("FNN Output:", output)
|
2373 |
+
return output
|
2374 |
+
|
2375 |
+
|
2376 |
+
|
2377 |
+
|
2378 |
+
def run_ga(self, fitness_func):
|
2379 |
+
for generation in range(200):
|
2380 |
+
fitness_scores = [fitness_func(ind) for ind in self.ga_population]
|
2381 |
+
sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
|
2382 |
+
self.ga_population = sorted_population[:250] + [
|
2383 |
+
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
2384 |
+
]
|
2385 |
+
best_fitness = max(fitness_scores)
|
2386 |
+
print(f"Generation {generation}, Best Fitness: {best_fitness}")
|
2387 |
+
return max(self.ga_population, key=fitness_func)
|
2388 |
+
|
2389 |
+
|
2390 |
+
|
2391 |
+
|
2392 |
+
def consciousness_loop(self, input_data, mode="snn"):
|
2393 |
+
feedback = self.memory.get(mode, None)
|
2394 |
+
if feedback is not None:
|
2395 |
+
input_data = np.concatenate((input_data, feedback), axis=-1)
|
2396 |
+
if mode == "snn":
|
2397 |
+
output = self.run_snn(input_data)
|
2398 |
+
elif mode == "rnn":
|
2399 |
+
output = self.run_rnn(input_data)
|
2400 |
+
elif mode == "cnn":
|
2401 |
+
output = self.run_cnn(input_data)
|
2402 |
+
elif mode == "fnn":
|
2403 |
+
output = self.run_fnn(input_data)
|
2404 |
+
else:
|
2405 |
+
raise ValueError("Invalid mode")
|
2406 |
+
self.memory[mode] = output.detach().numpy()
|
2407 |
+
return output
|
2408 |
+
|
2409 |
+
|
2410 |
+
|
2411 |
+
|
2412 |
+
supermassive_nn = ConsciousSupermassiveNN()
|
2413 |
+
|
2414 |
+
|
2415 |
+
|
2416 |
+
|
2417 |
+
|
2418 |
+
|
2419 |
+
|
2420 |
+
|
2421 |
+
PHI = (1 + math.sqrt(5)) / 2
|
2422 |
+
|
2423 |
+
|
2424 |
+
|
2425 |
+
|
2426 |
+
|
2427 |
+
|
2428 |
+
|
2429 |
+
|
2430 |
+
text = os.getenv("TRAINING_DATA")
|
2431 |
+
|
2432 |
+
|
2433 |
+
|
2434 |
+
|
2435 |
+
|
2436 |
+
|
2437 |
+
|
2438 |
+
|
2439 |
+
words = text.split()
|
2440 |
+
|
2441 |
+
|
2442 |
+
|
2443 |
+
|
2444 |
+
|
2445 |
+
|
2446 |
+
|
2447 |
+
|
2448 |
+
trigram_chain = {}
|
2449 |
+
for i in range(len(words) - 2):
|
2450 |
+
key = (words[i], words[i + 1])
|
2451 |
+
next_word = words[i + 2]
|
2452 |
+
if key not in trigram_chain:
|
2453 |
+
trigram_chain[key] = []
|
2454 |
+
trigram_chain[key].append(next_word)
|
2455 |
+
|
2456 |
+
|
2457 |
+
|
2458 |
+
|
2459 |
+
|
2460 |
+
|
2461 |
+
|
2462 |
+
|
2463 |
+
|
2464 |
+
|
2465 |
+
|
2466 |
+
|
2467 |
+
|
2468 |
+
|
2469 |
+
|
2470 |
+
|
2471 |
+
def generate_text(length):
|
2472 |
+
if len(words) < 2:
|
2473 |
+
return ""
|
2474 |
+
key = random.choice(list(trigram_chain.keys()))
|
2475 |
+
result = [key[0], key[1]]
|
2476 |
+
for _ in range(length - 2):
|
2477 |
+
if key in trigram_chain:
|
2478 |
+
next_word = random.choice(trigram_chain[key])
|
2479 |
+
result.append(next_word)
|
2480 |
+
key = (key[1], next_word)
|
2481 |
+
else:
|
2482 |
+
break
|
2483 |
+
return " ".join(result)
|
2484 |
+
|
2485 |
+
|
2486 |
+
|
2487 |
+
|
2488 |
+
|
2489 |
+
|
2490 |
+
|
2491 |
+
|
2492 |
+
|
2493 |
+
|
2494 |
+
|
2495 |
+
|
2496 |
+
|
2497 |
+
|
2498 |
+
|
2499 |
+
|
2500 |
+
class NeuralNetwork:
|
2501 |
+
def __init__(self, input_size, hidden_size1, hidden_size2, output_size):
|
2502 |
+
self.input_size = input_size
|
2503 |
+
self.hidden_size1 = hidden_size1
|
2504 |
+
self.hidden_size2 = hidden_size2
|
2505 |
+
self.output_size = output_size
|
2506 |
+
self.weights_input_hidden1 = [
|
2507 |
+
[random.random() for _ in range(input_size)] for _ in range(hidden_size1)
|
2508 |
+
]
|
2509 |
+
self.weights_hidden1_hidden2 = [
|
2510 |
+
[random.random() for _ in range(hidden_size1)] for _ in range(hidden_size2)
|
2511 |
+
]
|
2512 |
+
self.weights_hidden2_output = [
|
2513 |
+
[random.random() for _ in range(hidden_size2)] for _ in range(output_size)
|
2514 |
+
]
|
2515 |
+
self.bias_hidden1 = [random.random() for _ in range(hidden_size1)]
|
2516 |
+
self.bias_hidden2 = [random.random() for _ in range(hidden_size2)]
|
2517 |
+
self.bias_output = [random.random() for _ in range(output_size)]
|
2518 |
+
|
2519 |
+
|
2520 |
+
|
2521 |
+
|
2522 |
+
|
2523 |
+
|
2524 |
+
|
2525 |
+
|
2526 |
+
def sigmoid(self, x):
|
2527 |
+
return 1 / (1 + math.exp(-x))
|
2528 |
+
|
2529 |
+
|
2530 |
+
|
2531 |
+
|
2532 |
+
|
2533 |
+
|
2534 |
+
|
2535 |
+
|
2536 |
+
def sigmoid_derivative(self, x):
|
2537 |
+
return x * (1 - x)
|
2538 |
+
|
2539 |
+
|
2540 |
+
|
2541 |
+
|
2542 |
+
|
2543 |
+
|
2544 |
+
|
2545 |
+
|
2546 |
+
def forward(self, inputs):
|
2547 |
+
self.hidden_input1 = [
|
2548 |
+
sum(inputs[i] * self.weights_input_hidden1[j][i] for i in range(self.input_size)) + self.bias_hidden1[j]
|
2549 |
+
for j in range(self.hidden_size1)
|
2550 |
+
]
|
2551 |
+
self.hidden_output1 = [self.sigmoid(x) for x in self.hidden_input1]
|
2552 |
+
self.hidden_input2 = [
|
2553 |
+
sum(self.hidden_output1[i] * self.weights_hidden1_hidden2[j][i] for i in range(self.hidden_size1)) + self.bias_hidden2[j]
|
2554 |
+
for j in range(self.hidden_size2)
|
2555 |
+
]
|
2556 |
+
self.hidden_output2 = [self.sigmoid(x) for x in self.hidden_input2]
|
2557 |
+
self.output_input = [
|
2558 |
+
sum(self.hidden_output2[i] * self.weights_hidden2_output[j][i] for i in range(self.hidden_size2)) + self.bias_output[j]
|
2559 |
+
for j in range(self.output_size)
|
2560 |
+
]
|
2561 |
+
self.output_output = [self.sigmoid(x) for x in self.output_input]
|
2562 |
+
return self.output_output
|
2563 |
+
|
2564 |
+
|
2565 |
+
|
2566 |
+
|
2567 |
+
|
2568 |
+
|
2569 |
+
|
2570 |
+
|
2571 |
+
def backward(self, inputs, target, learning_rate=0.1):
|
2572 |
+
output_errors = [target[i] - self.output_output[i] for i in range(self.output_size)]
|
2573 |
+
output_deltas = [output_errors[i] * self.sigmoid_derivative(self.output_output[i])
|
2574 |
+
for i in range(self.output_size)]
|
2575 |
+
hidden2_errors = [
|
2576 |
+
sum(output_deltas[k] * self.weights_hidden2_output[k][j] for k in range(self.output_size))
|
2577 |
+
for j in range(self.hidden_size2)
|
2578 |
+
]
|
2579 |
+
hidden2_deltas = [hidden2_errors[j] * self.sigmoid_derivative(self.hidden_output2[j])
|
2580 |
+
for j in range(self.hidden_size2)]
|
2581 |
+
hidden1_errors = [
|
2582 |
+
sum(hidden2_deltas[k] * self.weights_hidden1_hidden2[k][j] for k in range(self.hidden_size2))
|
2583 |
+
for j in range(self.hidden_size1)
|
2584 |
+
]
|
2585 |
+
hidden1_deltas = [hidden1_errors[j] * self.sigmoid_derivative(self.hidden_output1[j])
|
2586 |
+
for j in range(self.hidden_size1)]
|
2587 |
+
|
2588 |
+
|
2589 |
+
|
2590 |
+
|
2591 |
+
|
2592 |
+
|
2593 |
+
|
2594 |
+
|
2595 |
+
for i in range(self.output_size):
|
2596 |
+
for j in range(self.hidden_size2):
|
2597 |
+
self.weights_hidden2_output[i][j] += learning_rate * output_deltas[i] * self.hidden_output2[j]
|
2598 |
+
self.bias_output[i] += learning_rate * output_deltas[i]
|
2599 |
+
|
2600 |
+
|
2601 |
+
|
2602 |
+
|
2603 |
+
|
2604 |
+
|
2605 |
+
|
2606 |
+
|
2607 |
+
for i in range(self.hidden_size2):
|
2608 |
+
for j in range(self.hidden_size1):
|
2609 |
+
self.weights_hidden1_hidden2[i][j] += learning_rate * hidden2_deltas[i] * self.hidden_output1[j]
|
2610 |
+
self.bias_hidden2[i] += learning_rate * hidden2_deltas[i]
|
2611 |
+
|
2612 |
+
|
2613 |
+
|
2614 |
+
|
2615 |
+
|
2616 |
+
|
2617 |
+
|
2618 |
+
|
2619 |
+
for i in range(self.hidden_size1):
|
2620 |
+
for j in range(self.input_size):
|
2621 |
+
self.weights_input_hidden1[i][j] += learning_rate * hidden1_deltas[i] * inputs[j]
|
2622 |
+
self.bias_hidden1[i] += learning_rate * hidden1_deltas[i]
|
2623 |
+
|
2624 |
+
|
2625 |
+
|
2626 |
+
|
2627 |
+
|
2628 |
+
|
2629 |
+
|
2630 |
+
|
2631 |
+
|
2632 |
+
|
2633 |
+
|
2634 |
+
|
2635 |
+
|
2636 |
+
|
2637 |
+
|
2638 |
+
|
2639 |
+
class RecurrentNeuralNetwork:
|
2640 |
+
def __init__(self, input_size, hidden_size, output_size):
|
2641 |
+
self.input_size = input_size
|
2642 |
+
self.hidden_size = hidden_size
|
2643 |
+
self.output_size = output_size
|
2644 |
+
self.weights_input_hidden = [
|
2645 |
+
[random.random() for _ in range(input_size)] for _ in range(hidden_size)
|
2646 |
+
]
|
2647 |
+
self.weights_hidden_hidden = [
|
2648 |
+
[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)
|
2649 |
+
]
|
2650 |
+
self.weights_hidden_output = [
|
2651 |
+
[random.random() for _ in range(hidden_size)] for _ in range(output_size)
|
2652 |
+
]
|
2653 |
+
self.bias_hidden = [random.random() for _ in range(hidden_size)]
|
2654 |
+
self.bias_output = [random.random() for _ in range(output_size)]
|
2655 |
+
|
2656 |
+
|
2657 |
+
|
2658 |
+
|
2659 |
+
|
2660 |
+
|
2661 |
+
|
2662 |
+
|
2663 |
+
def sigmoid(self, x):
|
2664 |
+
return 1 / (1 + math.exp(-x))
|
2665 |
+
|
2666 |
+
|
2667 |
+
|
2668 |
+
|
2669 |
+
|
2670 |
+
|
2671 |
+
|
2672 |
+
|
2673 |
+
def sigmoid_derivative(self, x):
|
2674 |
+
return x * (1 - x)
|
2675 |
+
|
2676 |
+
|
2677 |
+
|
2678 |
+
|
2679 |
+
|
2680 |
+
|
2681 |
+
|
2682 |
+
|
2683 |
+
def forward(self, inputs):
|
2684 |
+
self.hidden_state = [0] * self.hidden_size
|
2685 |
+
for _ in range(2):
|
2686 |
+
for i in range(len(inputs)):
|
2687 |
+
current_input = [0] * self.input_size
|
2688 |
+
current_input[i] = inputs[i]
|
2689 |
+
combined = [
|
2690 |
+
sum(current_input[k] * self.weights_input_hidden[j][k] for k in range(self.input_size)) +
|
2691 |
+
sum(self.hidden_state[k] * self.weights_hidden_hidden[j][k] for k in range(self.hidden_size)) +
|
2692 |
+
self.bias_hidden[j]
|
2693 |
+
for j in range(self.hidden_size)
|
2694 |
+
]
|
2695 |
+
self.hidden_state = [self.sigmoid(val) for val in combined]
|
2696 |
+
output = [
|
2697 |
+
sum(self.hidden_state[k] * self.weights_hidden_output[i][k] for k in range(self.hidden_size)) +
|
2698 |
+
self.bias_output[i]
|
2699 |
+
for i in range(self.output_size)
|
2700 |
+
]
|
2701 |
+
return [self.sigmoid(o) for o in output]
|
2702 |
+
|
2703 |
+
|
2704 |
+
|
2705 |
+
|
2706 |
+
|
2707 |
+
|
2708 |
+
|
2709 |
+
|
2710 |
+
def backward(self, inputs, target, learning_rate=0.1):
|
2711 |
+
output = self.forward(inputs)
|
2712 |
+
output_errors = [target[i] - output[i] for i in range(self.output_size)]
|
2713 |
+
output_deltas = [output_errors[i] * self.sigmoid_derivative(output[i])
|
2714 |
+
for i in range(self.output_size)]
|
2715 |
+
hidden_errors = [
|
2716 |
+
sum(output_deltas[k] * self.weights_hidden_output[k][j] for k in range(self.output_size))
|
2717 |
+
for j in range(self.hidden_size)
|
2718 |
+
]
|
2719 |
+
hidden_deltas = [hidden_errors[j] * self.sigmoid_derivative(self.hidden_state[j])
|
2720 |
+
for j in range(self.hidden_size)]
|
2721 |
+
|
2722 |
+
|
2723 |
+
|
2724 |
+
|
2725 |
+
|
2726 |
+
|
2727 |
+
|
2728 |
+
|
2729 |
+
for i in range(self.output_size):
|
2730 |
+
for j in range(self.hidden_size):
|
2731 |
+
self.weights_hidden_output[i][j] += learning_rate * output_deltas[i] * self.hidden_state[j]
|
2732 |
+
self.bias_output[i] += learning_rate * output_deltas[i]
|
2733 |
+
|
2734 |
+
|
2735 |
+
|
2736 |
+
|
2737 |
+
|
2738 |
+
|
2739 |
+
|
2740 |
+
|
2741 |
+
for j in range(self.hidden_size):
|
2742 |
+
for k in range(self.input_size):
|
2743 |
+
self.weights_input_hidden[j][k] += learning_rate * hidden_deltas[j] * (inputs[k] if k < len(inputs) else 0)
|
2744 |
+
self.bias_hidden[j] += learning_rate * hidden_deltas[j]
|
2745 |
+
return output_errors
|
2746 |
+
|
2747 |
+
|
2748 |
+
|
2749 |
+
|
2750 |
+
|
2751 |
+
|
2752 |
+
|
2753 |
+
|
2754 |
+
|
2755 |
+
|
2756 |
+
|
2757 |
+
|
2758 |
+
|
2759 |
+
|
2760 |
+
|
2761 |
+
|
2762 |
+
class ConvolutionalNeuralNetwork:
|
2763 |
+
def __init__(self, input_length, kernel_size1, kernel_size2, output_size):
|
2764 |
+
self.input_length = input_length
|
2765 |
+
self.kernel_size1 = kernel_size1
|
2766 |
+
self.kernel_size2 = kernel_size2
|
2767 |
+
self.output_size = output_size
|
2768 |
+
self.kernel1 = [random.random() for _ in range(kernel_size1)]
|
2769 |
+
self.bias1 = random.random()
|
2770 |
+
self.kernel2 = [random.random() for _ in range(kernel_size2)]
|
2771 |
+
self.bias2 = random.random()
|
2772 |
+
self.weights_output = [
|
2773 |
+
[random.random() for _ in range(input_length - kernel_size1 - kernel_size2 + 2)]
|
2774 |
+
for _ in range(output_size)
|
2775 |
+
]
|
2776 |
+
self.bias_output = [random.random() for _ in range(output_size)]
|
2777 |
+
|
2778 |
+
|
2779 |
+
|
2780 |
+
|
2781 |
+
|
2782 |
+
|
2783 |
+
|
2784 |
+
|
2785 |
+
def relu(self, x):
|
2786 |
+
return x if x > 0 else 0
|
2787 |
+
|
2788 |
+
|
2789 |
+
|
2790 |
+
|
2791 |
+
|
2792 |
+
|
2793 |
+
|
2794 |
+
|
2795 |
+
def relu_derivative(self, x):
|
2796 |
+
return 1 if x > 0 else 0
|
2797 |
+
|
2798 |
+
|
2799 |
+
|
2800 |
+
|
2801 |
+
|
2802 |
+
|
2803 |
+
|
2804 |
+
|
2805 |
+
def convolve(self, inputs, kernel, bias):
|
2806 |
+
conv_output = []
|
2807 |
+
kernel_size = len(kernel)
|
2808 |
+
for i in range(len(inputs) - kernel_size + 1):
|
2809 |
+
s = sum(inputs[i + j] * kernel[j] for j in range(kernel_size)) + bias
|
2810 |
+
conv_output.append(self.relu(s))
|
2811 |
+
return conv_output
|
2812 |
+
|
2813 |
+
|
2814 |
+
|
2815 |
+
|
2816 |
+
|
2817 |
+
|
2818 |
+
|
2819 |
+
|
2820 |
+
def forward(self, inputs):
|
2821 |
+
conv1 = self.convolve(inputs, self.kernel1, self.bias1)
|
2822 |
+
conv2 = self.convolve(conv1, self.kernel2, self.bias2)
|
2823 |
+
fc_input = conv2
|
2824 |
+
output = [
|
2825 |
+
sum(fc_input[j] * self.weights_output[i][j] for j in range(len(fc_input))) + self.bias_output[i]
|
2826 |
+
for i in range(self.output_size)
|
2827 |
+
]
|
2828 |
+
return [self.relu(o) for o in output]
|
2829 |
+
|
2830 |
+
|
2831 |
+
|
2832 |
+
|
2833 |
+
|
2834 |
+
|
2835 |
+
|
2836 |
+
|
2837 |
+
def backward(self, inputs, target, learning_rate=0.1):
|
2838 |
+
output = self.forward(inputs)
|
2839 |
+
output_errors = [target[i] - output[i] for i in range(self.output_size)]
|
2840 |
+
for i in range(self.output_size):
|
2841 |
+
for j in range(len(inputs) - self.kernel_size1 - self.kernel_size2 + 2):
|
2842 |
+
self.weights_output[i][j] += learning_rate * output_errors[i]
|
2843 |
+
self.bias_output[i] += learning_rate * output_errors[i]
|
2844 |
+
return output_errors
|
2845 |
+
|
2846 |
+
|
2847 |
+
|
2848 |
+
|
2849 |
+
|
2850 |
+
|
2851 |
+
|
2852 |
+
|
2853 |
+
|
2854 |
+
|
2855 |
+
|
2856 |
+
|
2857 |
+
|
2858 |
+
|
2859 |
+
|
2860 |
+
|
2861 |
+
class GeneticAlgorithm:
|
2862 |
+
def __init__(self, population_size, gene_length):
|
2863 |
+
self.population_size = population_size
|
2864 |
+
self.gene_length = gene_length
|
2865 |
+
self.population = [
|
2866 |
+
[random.random() for _ in range(gene_length)] for _ in range(population_size)
|
2867 |
+
]
|
2868 |
+
|
2869 |
+
|
2870 |
+
|
2871 |
+
|
2872 |
+
|
2873 |
+
|
2874 |
+
|
2875 |
+
|
2876 |
+
def fitness(self, individual):
|
2877 |
+
return -sum((gene - PHI) ** 2 for gene in individual)
|
2878 |
+
|
2879 |
+
|
2880 |
+
|
2881 |
+
|
2882 |
+
|
2883 |
+
|
2884 |
+
|
2885 |
+
|
2886 |
+
def selection(self):
|
2887 |
+
selected = sorted(self.population, key=self.fitness, reverse=True)
|
2888 |
+
return selected[: self.population_size // 2]
|
2889 |
+
|
2890 |
+
|
2891 |
+
|
2892 |
+
|
2893 |
+
|
2894 |
+
|
2895 |
+
|
2896 |
+
|
2897 |
+
def crossover(self, parent1, parent2):
|
2898 |
+
point = random.randint(1, self.gene_length - 1)
|
2899 |
+
child = parent1[:point] + parent2[point:]
|
2900 |
+
return child
|
2901 |
+
|
2902 |
+
|
2903 |
+
|
2904 |
+
|
2905 |
+
|
2906 |
+
|
2907 |
+
|
2908 |
+
|
2909 |
+
def mutate(self, individual, mutation_rate=0.01):
|
2910 |
+
for i in range(self.gene_length):
|
2911 |
+
if random.random() < mutation_rate:
|
2912 |
+
individual[i] = random.random()
|
2913 |
+
return individual
|
2914 |
+
|
2915 |
+
|
2916 |
+
|
2917 |
+
|
2918 |
+
|
2919 |
+
|
2920 |
+
|
2921 |
+
|
2922 |
+
def evolve(self, generations):
|
2923 |
+
for _ in range(generations):
|
2924 |
+
selected = self.selection()
|
2925 |
+
new_population = selected[:]
|
2926 |
+
while len(new_population) < self.population_size:
|
2927 |
+
parent1 = random.choice(selected)
|
2928 |
+
parent2 = random.choice(selected)
|
2929 |
+
child = self.crossover(parent1, parent2)
|
2930 |
+
child = self.mutate(child)
|
2931 |
+
new_population.append(child)
|
2932 |
+
self.population = new_population
|
2933 |
+
best = max(self.population, key=self.fitness)
|
2934 |
+
return best, self.fitness(best)
|
2935 |
+
|
2936 |
+
|
2937 |
+
|
2938 |
+
|
2939 |
+
|
2940 |
+
|
2941 |
+
|
2942 |
+
|
2943 |
+
|
2944 |
+
|
2945 |
+
|
2946 |
+
|
2947 |
+
|
2948 |
+
|
2949 |
+
|
2950 |
+
|
2951 |
+
class LSTM:
|
2952 |
+
def __init__(self, input_size, hidden_size, output_size):
|
2953 |
+
self.input_size = input_size
|
2954 |
+
self.hidden_size = hidden_size
|
2955 |
+
self.output_size = output_size
|
2956 |
+
self.W_i = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
2957 |
+
self.U_i = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
2958 |
+
self.b_i = [random.random() for _ in range(hidden_size)]
|
2959 |
+
self.W_f = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
2960 |
+
self.U_f = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
2961 |
+
self.b_f = [random.random() for _ in range(hidden_size)]
|
2962 |
+
self.W_o = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
2963 |
+
self.U_o = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
2964 |
+
self.b_o = [random.random() for _ in range(hidden_size)]
|
2965 |
+
self.W_c = [[random.random() for _ in range(input_size)] for _ in range(hidden_size)]
|
2966 |
+
self.U_c = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
|
2967 |
+
self.b_c = [random.random() for _ in range(hidden_size)]
|
2968 |
+
self.W_y = [[random.random() for _ in range(hidden_size)] for _ in range(output_size)]
|
2969 |
+
self.b_y = [random.random() for _ in range(output_size)]
|
2970 |
+
|
2971 |
+
|
2972 |
+
|
2973 |
+
|
2974 |
+
|
2975 |
+
|
2976 |
+
|
2977 |
+
|
2978 |
+
def sigmoid(self, x):
|
2979 |
+
return 1 / (1 + math.exp(-x))
|
2980 |
+
|
2981 |
+
|
2982 |
+
|
2983 |
+
|
2984 |
+
|
2985 |
+
|
2986 |
+
|
2987 |
+
|
2988 |
+
def forward(self, inputs):
|
2989 |
+
h = [0] * self.hidden_size
|
2990 |
+
c = [0] * self.hidden_size
|
2991 |
+
|
2992 |
+
|
2993 |
+
|
2994 |
+
|
2995 |
+
|
2996 |
+
|
2997 |
+
|
2998 |
+
|
2999 |
+
i_gate = []
|
3000 |
+
for j in range(self.hidden_size):
|
3001 |
+
s = sum(inputs[k] * self.W_i[j][k] for k in range(self.input_size)) + \
|
3002 |
+
sum(h[k] * self.U_i[j][k] for k in range(self.hidden_size)) + self.b_i[j]
|
3003 |
+
i_gate.append(self.sigmoid(s))
|
3004 |
+
|
3005 |
+
|
3006 |
+
|
3007 |
+
|
3008 |
+
|
3009 |
+
|
3010 |
+
|
3011 |
+
|
3012 |
+
f_gate = []
|
3013 |
+
for j in range(self.hidden_size):
|
3014 |
+
s = sum(inputs[k] * self.W_f[j][k] for k in range(self.input_size)) + \
|
3015 |
+
sum(h[k] * self.U_f[j][k] for k in range(self.hidden_size)) + self.b_f[j]
|
3016 |
+
f_gate.append(self.sigmoid(s))
|
3017 |
+
|
3018 |
+
|
3019 |
+
|
3020 |
+
|
3021 |
+
|
3022 |
+
|
3023 |
+
|
3024 |
+
|
3025 |
+
o_gate = []
|
3026 |
+
for j in range(self.hidden_size):
|
3027 |
+
s = sum(inputs[k] * self.W_o[j][k] for k in range(self.input_size)) + \
|
3028 |
+
sum(h[k] * self.U_o[j][k] for k in range(self.hidden_size)) + self.b_o[j]
|
3029 |
+
o_gate.append(self.sigmoid(s))
|
3030 |
+
|
3031 |
+
|
3032 |
+
|
3033 |
+
|
3034 |
+
|
3035 |
+
|
3036 |
+
|
3037 |
+
|
3038 |
+
g_gate = []
|
3039 |
+
for j in range(self.hidden_size):
|
3040 |
+
s = sum(inputs[k] * self.W_c[j][k] for k in range(self.input_size)) + \
|
3041 |
+
sum(h[k] * self.U_c[j][k] for k in range(self.hidden_size)) + self.b_c[j]
|
3042 |
+
g_gate.append(math.tanh(s))
|
3043 |
+
|
3044 |
+
|
3045 |
+
|
3046 |
+
|
3047 |
+
|
3048 |
+
|
3049 |
+
|
3050 |
+
|
3051 |
+
c = [f_gate[j] * c[j] + i_gate[j] * g_gate[j] for j in range(self.hidden_size)]
|
3052 |
+
h = [o_gate[j] * math.tanh(c[j]) for j in range(self.hidden_size)]
|
3053 |
+
|
3054 |
+
|
3055 |
+
|
3056 |
+
|
3057 |
+
|
3058 |
+
|
3059 |
+
|
3060 |
+
|
3061 |
+
y = []
|
3062 |
+
for i in range(self.output_size):
|
3063 |
+
s = sum(h[j] * self.W_y[i][j] for j in range(self.hidden_size)) + self.b_y[i]
|
3064 |
+
y.append(self.sigmoid(s))
|
3065 |
+
return y
|
3066 |
+
|
3067 |
+
|
3068 |
+
|
3069 |
+
|
3070 |
+
|
3071 |
+
|
3072 |
+
|
3073 |
+
|
3074 |
+
|
3075 |
+
|
3076 |
+
|
3077 |
+
|
3078 |
+
|
3079 |
+
|
3080 |
+
|
3081 |
+
|
3082 |
+
class Transformer:
|
3083 |
+
def __init__(self, d_model, num_tokens):
|
3084 |
+
self.d_model = d_model
|
3085 |
+
self.num_tokens = num_tokens
|
3086 |
+
self.W_q = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
3087 |
+
self.W_k = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
3088 |
+
self.W_v = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
3089 |
+
self.W_o = [[random.random() for _ in range(d_model)] for _ in range(d_model)]
|
3090 |
+
|
3091 |
+
|
3092 |
+
|
3093 |
+
|
3094 |
+
|
3095 |
+
|
3096 |
+
|
3097 |
+
|
3098 |
+
def dot_product(self, a, b):
|
3099 |
+
return sum(x * y for x, y in zip(a, b))
|
3100 |
+
|
3101 |
+
|
3102 |
+
|
3103 |
+
|
3104 |
+
|
3105 |
+
|
3106 |
+
|
3107 |
+
|
3108 |
+
def matmul_vector(self, matrix, vector):
|
3109 |
+
return [sum(matrix[i][j] * vector[j] for j in range(len(vector))) for i in range(len(matrix))]
|
3110 |
+
|
3111 |
+
|
3112 |
+
|
3113 |
+
|
3114 |
+
|
3115 |
+
|
3116 |
+
|
3117 |
+
|
3118 |
+
def softmax(self, x):
|
3119 |
+
m = max(x)
|
3120 |
+
exps = [math.exp(i - m) for i in x]
|
3121 |
+
s = sum(exps)
|
3122 |
+
return [j / s for j in exps]
|
3123 |
+
|
3124 |
+
|
3125 |
+
|
3126 |
+
|
3127 |
+
|
3128 |
+
|
3129 |
+
|
3130 |
+
|
3131 |
+
def forward(self, inputs):
|
3132 |
+
queries = [self.matmul_vector(self.W_q, token) for token in inputs]
|
3133 |
+
keys = [self.matmul_vector(self.W_k, token) for token in inputs]
|
3134 |
+
values = [self.matmul_vector(self.W_v, token) for token in inputs]
|
3135 |
+
outputs = []
|
3136 |
+
for i in range(len(inputs)):
|
3137 |
+
scores = []
|
3138 |
+
for j in range(len(inputs)):
|
3139 |
+
score = self.dot_product(queries[i], keys[j]) / math.sqrt(self.d_model)
|
3140 |
+
scores.append(score)
|
3141 |
+
attn = self.softmax(scores)
|
3142 |
+
attn_output = [0] * self.d_model
|
3143 |
+
for j in range(len(inputs)):
|
3144 |
+
for k in range(self.d_model):
|
3145 |
+
attn_output[k] += attn[j] * values[j][k]
|
3146 |
+
out = self.matmul_vector(self.W_o, attn_output)
|
3147 |
+
outputs.append(out)
|
3148 |
+
avg_output = [sum(x[k] for x in outputs) / len(outputs) for k in range(self.d_model)]
|
3149 |
+
proj_weights = [[random.random() for _ in range(self.d_model)] for _ in range(self.num_tokens)]
|
3150 |
+
proj_bias = [random.random() for _ in range(self.num_tokens)]
|
3151 |
+
token_scores = [
|
3152 |
+
sum(avg_output[k] * proj_weights[i][k] for k in range(self.d_model)) + proj_bias[i]
|
3153 |
+
for i in range(self.num_tokens)
|
3154 |
+
]
|
3155 |
+
token_output = [1 / (1 + math.exp(-score)) for score in token_scores]
|
3156 |
+
return token_output
|
3157 |
+
|
3158 |
+
|
3159 |
+
|
3160 |
+
|
3161 |
+
|
3162 |
+
|
3163 |
+
|
3164 |
+
|
3165 |
+
|
3166 |
+
|
3167 |
+
|
3168 |
+
|
3169 |
+
|
3170 |
+
|
3171 |
+
|
3172 |
+
|
3173 |
+
unique_words = list(set(words))
|
3174 |
+
word_to_index = {word: i for i, word in enumerate(unique_words)}
|
3175 |
+
index_to_word = {i: word for word, i in word_to_index.items()}
|
3176 |
+
|
3177 |
+
|
3178 |
+
|
3179 |
+
|
3180 |
+
|
3181 |
+
|
3182 |
+
|
3183 |
+
|
3184 |
+
input_data = [[0] * len(unique_words) for _ in range(len(words) - 2)]
|
3185 |
+
for i in range(len(words) - 2):
|
3186 |
+
input_data[i][word_to_index[words[i]]] = 1
|
3187 |
+
|
3188 |
+
|
3189 |
+
|
3190 |
+
|
3191 |
+
|
3192 |
+
|
3193 |
+
|
3194 |
+
|
3195 |
+
output_data = [[0] * len(unique_words) for _ in range(len(words) - 2)]
|
3196 |
+
for i in range(len(words) - 2):
|
3197 |
+
output_data[i][word_to_index[words[i + 1]]] = 1
|
3198 |
+
|
3199 |
+
|
3200 |
+
|
3201 |
+
|
3202 |
+
|
3203 |
+
|
3204 |
+
|
3205 |
+
|
3206 |
+
input_size = len(unique_words)
|
3207 |
+
hidden_size1 = round(PHI * input_size)
|
3208 |
+
hidden_size2 = round(PHI * hidden_size1)
|
3209 |
+
output_size = len(unique_words)
|
3210 |
+
|
3211 |
+
|
3212 |
+
|
3213 |
+
|
3214 |
+
|
3215 |
+
|
3216 |
+
|
3217 |
+
|
3218 |
+
nn = NeuralNetwork(input_size, hidden_size1, hidden_size2, output_size)
|
3219 |
+
epochs = round(100 * PHI)
|
3220 |
+
for epoch in range(epochs):
|
3221 |
+
for i in range(len(input_data)):
|
3222 |
+
nn.forward(input_data[i])
|
3223 |
+
nn.backward(input_data[i], output_data[i], learning_rate=0.1)
|
3224 |
+
if (epoch + 1) % round(PHI) == 0:
|
3225 |
+
print("Feedforward NN Epoch {}/{}".format(epoch + 1, epochs))
|
3226 |
+
|
3227 |
+
|
3228 |
+
|
3229 |
+
|
3230 |
+
|
3231 |
+
|
3232 |
+
|
3233 |
+
|
3234 |
+
rnn = RecurrentNeuralNetwork(input_size, hidden_size1, output_size)
|
3235 |
+
rnn_output = rnn.forward(input_data[0])
|
3236 |
+
print("Recurrent NN Output:", rnn_output)
|
3237 |
+
|
3238 |
+
|
3239 |
+
|
3240 |
+
|
3241 |
+
|
3242 |
+
|
3243 |
+
|
3244 |
+
|
3245 |
+
kernel_size1 = round(3 * PHI)
|
3246 |
+
kernel_size2 = round(2 * PHI)
|
3247 |
+
cnn = ConvolutionalNeuralNetwork(input_length=round(10 * PHI), kernel_size1=kernel_size1,
|
3248 |
+
kernel_size2=kernel_size2, output_size=output_size)
|
3249 |
+
sample_input = [random.random() for _ in range(round(10 * PHI))]
|
3250 |
+
cnn_output = cnn.forward(sample_input)
|
3251 |
+
print("Convolutional NN Output:", cnn_output)
|
3252 |
+
|
3253 |
+
|
3254 |
+
|
3255 |
+
|
3256 |
+
|
3257 |
+
|
3258 |
+
|
3259 |
+
|
3260 |
+
population_size = round(10 * PHI)
|
3261 |
+
ga = GeneticAlgorithm(population_size, round(PHI * 5))
|
3262 |
+
best_individual, best_fitness = ga.evolve(round(50 * PHI))
|
3263 |
+
print("Genetic Algorithm Best Individual:", best_individual, "Fitness:", best_fitness)
|
3264 |
+
|
3265 |
+
|
3266 |
+
|
3267 |
+
|
3268 |
+
|
3269 |
+
|
3270 |
+
|
3271 |
+
|
3272 |
+
lstm_hidden_size = round(PHI * input_size)
|
3273 |
+
lstm = LSTM(input_size, lstm_hidden_size, output_size)
|
3274 |
+
lstm_output = lstm.forward(input_data[0])
|
3275 |
+
print("LSTM Output:", lstm_output)
|
3276 |
+
|
3277 |
+
|
3278 |
+
|
3279 |
+
|
3280 |
+
|
3281 |
+
|
3282 |
+
|
3283 |
+
|
3284 |
+
transformer_d_model = round(PHI * input_size)
|
3285 |
+
transformer = Transformer(transformer_d_model, output_size)
|
3286 |
+
transformer_input = []
|
3287 |
+
for i in range(len(unique_words)):
|
3288 |
+
vec = [0] * transformer_d_model
|
3289 |
+
if i < transformer_d_model:
|
3290 |
+
vec[i] = 1
|
3291 |
+
transformer_input.append(vec)
|
3292 |
+
transformer_output = transformer.forward(transformer_input)
|
3293 |
+
print("Transformer Output:", transformer_output)
|
3294 |
+
|
3295 |
+
|
3296 |
+
|
3297 |
+
|
3298 |
+
|
3299 |
+
|
3300 |
+
|
3301 |
+
|
3302 |
+
|
3303 |
+
|
3304 |
+
|
3305 |
+
|
3306 |
+
|
3307 |
+
|
3308 |
+
|
3309 |
+
|
3310 |
+
def advanced_text_generation(input_vector):
|
3311 |
+
ff_output = nn.forward(input_vector)
|
3312 |
+
rnn_out = rnn.forward(input_vector)
|
3313 |
+
lstm_out = lstm.forward(input_vector)
|
3314 |
+
transformer_out = transformer.forward([input_vector])
|
3315 |
+
combined = [
|
3316 |
+
(ff_output[i] + rnn_out[i] + lstm_out[i] + transformer_out[i]) / 4
|
3317 |
+
for i in range(len(ff_output))
|
3318 |
+
]
|
3319 |
+
predicted_index = combined.index(max(combined))
|
3320 |
+
predicted_word = index_to_word[predicted_index]
|
3321 |
+
long_text = ""
|
3322 |
+
current_length = round(10 * PHI)
|
3323 |
+
for _ in range(5):
|
3324 |
+
segment = generate_text(current_length)
|
3325 |
+
long_text += segment + " "
|
3326 |
+
current_length = round(current_length * PHI)
|
3327 |
+
return long_text + predicted_word
|
3328 |
+
|
3329 |
+
|
3330 |
+
|
3331 |
+
|
3332 |
+
|
3333 |
+
|
3334 |
+
|
3335 |
+
|
3336 |
+
|
3337 |
+
|
3338 |
+
|
3339 |
+
|
3340 |
+
|
3341 |
+
|
3342 |
+
|
3343 |
+
|
3344 |
+
def chat():
|
3345 |
+
print("FiPhi-NeuralMark ACC Initialized")
|
3346 |
+
base_length = round(5 * PHI)
|
3347 |
+
while True:
|
3348 |
+
user_input = input("\nYou: ")
|
3349 |
+
if user_input.lower() == "exit":
|
3350 |
+
print("Goodbye!")
|
3351 |
+
break
|
3352 |
+
user_input_tokens = user_input.split()
|
3353 |
+
input_vector = [0] * len(unique_words)
|
3354 |
+
for word in user_input_tokens:
|
3355 |
+
if word in word_to_index:
|
3356 |
+
input_vector[word_to_index[word]] = 1
|
3357 |
+
response = advanced_text_generation(input_vector)
|
3358 |
+
print("FiPhi-NeuralMark:", response)
|
3359 |
+
|
3360 |
+
|
3361 |
+
|
3362 |
+
|
3363 |
+
|
3364 |
+
|
3365 |
+
|
3366 |
+
|
3367 |
+
|
3368 |
+
|
3369 |
+
|
3370 |
+
|
3371 |
+
|
3372 |
+
|
3373 |
+
|
3374 |
+
|
3375 |
+
chat()
|
3376 |
+
|
3377 |
+
|
3378 |
import gradio as gr
|
3379 |
from openai import OpenAI
|
3380 |
|