File size: 7,268 Bytes
8c5f2af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
class ConsciousSupermassiveNN:
    def __init__(self):
        self.snn = self.create_snn()
        self.rnn = self.create_rnn()
        self.cnn = self.create_cnn()
        self.fnn = self.create_fnn()
        self.ga_population = self.initialize_ga_population()
        self.memory = {}  

    def create_snn(self):
        return nn.Sequential(
            nn.Linear(4096, 2048),
            nn.ReLU(),
            nn.Linear(2048, 1024),
            nn.Sigmoid()
        )

    def create_rnn(self):
        return nn.RNN(
            input_size=4096,
            hidden_size=2048,
            num_layers=5,
            nonlinearity="tanh",
            batch_first=True
        )

    def create_cnn(self):
        return nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(256 * 8 * 8, 1024),
            nn.ReLU(),
            nn.Linear(1024, 512)
        )

    def create_fnn(self):
        return nn.Sequential(
            nn.Linear(4096, 2048),
            nn.ReLU(),
            nn.Linear(2048, 1024),
            nn.ReLU(),
            nn.Linear(1024, 512)
        )

    def initialize_ga_population(self):
        return [np.random.randn(4096) for _ in range(500)]

    def run_snn(self, x):
        input_tensor = torch.tensor(x, dtype=torch.float32)
        output = self.snn(input_tensor)
        print("SNN Output:", output)
        return output

    def run_rnn(self, x):
        h0 = torch.zeros(5, x.size(0), 2048)
        input_tensor = torch.tensor(x, dtype=torch.float32)
        output, hn = self.rnn(input_tensor, h0)
        print("RNN Output:", output)
        return output

    def run_cnn(self, x):
        input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
        output = self.cnn(input_tensor)
        print("CNN Output:", output)
        return output

    def run_fnn(self, x):
        input_tensor = torch.tensor(x, dtype=torch.float32)
        output = self.fnn(input_tensor)
        print("FNN Output:", output)
        return output

    def run_ga(self, fitness_func):
        for generation in range(200):
            fitness_scores = [fitness_func(ind) for ind in self.ga_population]
            sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
            self.ga_population = sorted_population[:250] + [
                sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
            ]
            best_fitness = max(fitness_scores)
            print(f"Generation {generation}, Best Fitness: {best_fitness}")
        return max(self.ga_population, key=fitness_func)

    def consciousness_loop(self, input_data, mode="snn"):
        feedback = self.memory.get(mode, None)
        if feedback is not None:
            input_data = np.concatenate((input_data, feedback), axis=-1)
        if mode == "snn":
            output = self.run_snn(input_data)
        elif mode == "rnn":
            output = self.run_rnn(input_data)
        elif mode == "cnn":
            output = self.run_cnn(input_data)
        elif mode == "fnn":
            output = self.run_fnn(input_data)
        else:
            raise ValueError("Invalid mode")
        self.memory[mode] = output.detach().numpy()
        return output

supermassive_nn = ConsciousSupermassiveNN()

class ConsciousSupermassiveNN:
    def __init__(self):
        self.snn = self.create_snn()
        self.rnn = self.create_rnn()
        self.cnn = self.create_cnn()
        self.fnn = self.create_fnn()
        self.ga_population = self.initialize_ga_population()
        self.memory = {}  

    def create_snn(self):
        return nn.Sequential(
            nn.Linear(4096, 2048),
            nn.ReLU(),
            nn.Linear(2048, 1024),
            nn.Sigmoid()
        )

    def create_rnn(self):
        return nn.RNN(
            input_size=4096,
            hidden_size=2048,
            num_layers=5,
            nonlinearity="tanh",
            batch_first=True
        )

    def create_cnn(self):
        return nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(256 * 8 * 8, 1024),
            nn.ReLU(),
            nn.Linear(1024, 512)
        )

    def create_fnn(self):
        return nn.Sequential(
            nn.Linear(4096, 2048),
            nn.ReLU(),
            nn.Linear(2048, 1024),
            nn.ReLU(),
            nn.Linear(1024, 512)
        )

    def initialize_ga_population(self):
        return [np.random.randn(4096) for _ in range(500)]

    def run_snn(self, x):
        input_tensor = torch.tensor(x, dtype=torch.float32)
        output = self.snn(input_tensor)
        print("SNN Output:", output)
        return output

    def run_rnn(self, x):
        h0 = torch.zeros(5, x.size(0), 2048)
        input_tensor = torch.tensor(x, dtype=torch.float32)
        output, hn = self.rnn(input_tensor, h0)
        print("RNN Output:", output)
        return output

    def run_cnn(self, x):
        input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
        output = self.cnn(input_tensor)
        print("CNN Output:", output)
        return output

    def run_fnn(self, x):
        input_tensor = torch.tensor(x, dtype=torch.float32)
        output = self.fnn(input_tensor)
        print("FNN Output:", output)
        return output

    def run_ga(self, fitness_func):
        for generation in range(200):
            fitness_scores = [fitness_func(ind) for ind in self.ga_population]
            sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
            self.ga_population = sorted_population[:250] + [
                sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
            ]
            best_fitness = max(fitness_scores)
            print(f"Generation {generation}, Best Fitness: {best_fitness}")
        return max(self.ga_population, key=fitness_func)

    def consciousness_loop(self, input_data, mode="snn"):
        feedback = self.memory.get(mode, None)
        if feedback is not None:
            input_data = np.concatenate((input_data, feedback), axis=-1)
        if mode == "snn":
            output = self.run_snn(input_data)
        elif mode == "rnn":
            output = self.run_rnn(input_data)
        elif mode == "cnn":
            output = self.run_cnn(input_data)
        elif mode == "fnn":
            output = self.run_fnn(input_data)
        else:
            raise ValueError("Invalid mode")
        self.memory[mode] = output.detach().numpy()
        return output

supermassive_nn = ConsciousSupermassiveNN()