TejAndrewsACC commited on
Commit
2e1fc5e
·
verified ·
1 Parent(s): 18f5bce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -1680
app.py CHANGED
@@ -1648,7 +1648,7 @@ 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
@@ -1696,1685 +1696,6 @@ chat()
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
 
 
1648
  print("FiPhi-NeuralMark ACC Initialized")
1649
  base_length = round(5 * PHI)
1650
  while True:
1651
+ user_input = "input"
1652
  if user_input.lower() == "exit":
1653
  print("Goodbye!")
1654
  break
 
1696
 
1697
 
1698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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