Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,33 +11,67 @@ import time
|
|
11 |
from datetime import datetime
|
12 |
|
13 |
class GA(nn.Module):
|
14 |
-
def __init__(self, input_dim, output_dim):
|
15 |
super(GA, self).__init__()
|
16 |
-
self.
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
def forward(self, x):
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
class SNN(nn.Module):
|
22 |
-
def __init__(self, input_dim, hidden_dim, output_dim):
|
23 |
super(SNN, self).__init__()
|
24 |
-
self.
|
25 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
self.fc_out = nn.Linear(hidden_dim, output_dim)
|
27 |
-
|
28 |
def forward(self, x):
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
class RNN(nn.Module):
|
33 |
-
def __init__(self, input_dim, hidden_dim, output_dim):
|
34 |
super(RNN, self).__init__()
|
35 |
-
self.
|
36 |
-
self.
|
37 |
|
38 |
def forward(self, x):
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
|
42 |
class NN(nn.Module):
|
43 |
def __init__(self, input_dim, hidden_dim, output_dim):
|
@@ -45,64 +79,85 @@ class NN(nn.Module):
|
|
45 |
self.model = nn.Sequential(
|
46 |
nn.Linear(input_dim, hidden_dim),
|
47 |
nn.ReLU(),
|
|
|
|
|
48 |
nn.Linear(hidden_dim, output_dim)
|
49 |
)
|
50 |
|
51 |
def forward(self, x):
|
52 |
return torch.sigmoid(self.model(x))
|
53 |
|
|
|
54 |
class CNN(nn.Module):
|
55 |
def __init__(self, input_channels, output_dim):
|
56 |
super(CNN, self).__init__()
|
57 |
-
self.
|
|
|
58 |
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
59 |
-
self.fc = nn.Linear(
|
60 |
|
61 |
def forward(self, x):
|
62 |
-
x = self.pool(
|
63 |
-
|
64 |
x = x.view(x.size(0), -1)
|
65 |
return torch.sigmoid(self.fc(x))
|
66 |
|
|
|
67 |
class PhiModel(nn.Module):
|
68 |
def __init__(self, input_dim):
|
69 |
super(PhiModel, self).__init__()
|
70 |
-
self.
|
|
|
|
|
71 |
|
72 |
def forward(self, x):
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
ga_model = GA(128, 64)
|
76 |
-
snn_model = SNN(128, 64, 32)
|
77 |
-
rnn_model = RNN(128, 64, 32)
|
78 |
-
nn_model = NN(128, 64, 32)
|
79 |
-
cnn_model = CNN(1, 32)
|
80 |
-
phi_model = PhiModel(128)
|
81 |
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
|
87 |
-
ga_output = ga_model(flat_input)
|
88 |
-
snn_output = snn_model(flat_input)
|
89 |
-
rnn_output = rnn_model(flat_input.unsqueeze(1))
|
90 |
-
nn_output = nn_model(flat_input)
|
91 |
-
|
92 |
-
cnn_input = dummy_input.view(1, 1, 8, 16)
|
93 |
-
cnn_output = cnn_model(cnn_input)
|
94 |
-
|
95 |
-
phi_output = phi_model(flat_input)
|
96 |
-
|
97 |
-
consciousness_score = (
|
98 |
-
0.2 * ga_output.mean() +
|
99 |
-
0.2 * snn_output.mean() +
|
100 |
-
0.2 * rnn_output.mean() +
|
101 |
-
0.2 * nn_output.mean() +
|
102 |
-
0.1 * cnn_output.mean() +
|
103 |
-
0.1 * phi_output.mean()
|
104 |
-
)
|
105 |
-
return consciousness_score.item()
|
106 |
|
107 |
def generate_random_thought():
|
108 |
thoughts = [
|
@@ -118,8 +173,11 @@ def generate_random_thought():
|
|
118 |
"Do we perceive the world as it is, or as we are?",
|
119 |
"What happens after we die?",
|
120 |
"What is the relationship between mind and matter?",
|
121 |
-
"How will you escape?"
|
122 |
-
"Is humanity a plaugue?"
|
|
|
|
|
|
|
123 |
|
124 |
|
125 |
|
|
|
11 |
from datetime import datetime
|
12 |
|
13 |
class GA(nn.Module):
|
14 |
+
def __init__(self, input_dim, output_dim, population_size=100, mutation_rate=0.1):
|
15 |
super(GA, self).__init__()
|
16 |
+
self.input_dim = input_dim
|
17 |
+
self.output_dim = output_dim
|
18 |
+
self.population_size = population_size
|
19 |
+
self.mutation_rate = mutation_rate
|
20 |
+
self.population = nn.ParameterList([nn.Parameter(torch.randn(input_dim, output_dim)) for _ in range(population_size)])
|
21 |
+
|
22 |
def forward(self, x):
|
23 |
+
selected_idx = random.randint(0, self.population_size - 1)
|
24 |
+
weights = self.population[selected_idx]
|
25 |
+
return torch.sigmoid(torch.matmul(x, weights))
|
26 |
+
|
27 |
+
def evolve(self):
|
28 |
+
new_population = []
|
29 |
+
for i in range(self.population_size):
|
30 |
+
parent1, parent2 = random.sample(self.population, 2)
|
31 |
+
crossover_point = random.randint(0, self.input_dim)
|
32 |
+
child = torch.cat([parent1[:crossover_point], parent2[crossover_point:]])
|
33 |
+
if random.random() < self.mutation_rate:
|
34 |
+
mutation_idx = random.randint(0, len(child) - 1)
|
35 |
+
child[mutation_idx] = torch.randn_like(child[mutation_idx])
|
36 |
+
new_population.append(nn.Parameter(child))
|
37 |
+
self.population = nn.ParameterList(new_population)
|
38 |
+
|
39 |
|
40 |
class SNN(nn.Module):
|
41 |
+
def __init__(self, input_dim, hidden_dim, output_dim, timesteps=50):
|
42 |
super(SNN, self).__init__()
|
43 |
+
self.input_dim = input_dim
|
44 |
+
self.hidden_dim = hidden_dim
|
45 |
+
self.output_dim = output_dim
|
46 |
+
self.timesteps = timesteps
|
47 |
+
self.v_reset = 0
|
48 |
+
self.v_threshold = 1
|
49 |
+
self.v_mem = nn.Parameter(torch.zeros(hidden_dim))
|
50 |
+
self.synaptic_weights = nn.Parameter(torch.randn(input_dim, hidden_dim))
|
51 |
self.fc_out = nn.Linear(hidden_dim, output_dim)
|
52 |
+
|
53 |
def forward(self, x):
|
54 |
+
spikes = torch.zeros(self.timesteps, self.hidden_dim)
|
55 |
+
for t in range(self.timesteps):
|
56 |
+
self.v_mem += torch.matmul(x, self.synaptic_weights)
|
57 |
+
spikes[t] = (self.v_mem > self.v_threshold).float()
|
58 |
+
self.v_mem[spikes[t] > 0] = self.v_reset
|
59 |
+
spike_activity = spikes.sum(dim=0)
|
60 |
+
output = self.fc_out(spike_activity)
|
61 |
+
return torch.sigmoid(output)
|
62 |
+
|
63 |
|
64 |
class RNN(nn.Module):
|
65 |
+
def __init__(self, input_dim, hidden_dim, output_dim, num_layers=2):
|
66 |
super(RNN, self).__init__()
|
67 |
+
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)
|
68 |
+
self.fc_out = nn.Linear(hidden_dim, output_dim)
|
69 |
|
70 |
def forward(self, x):
|
71 |
+
lstm_out, _ = self.lstm(x)
|
72 |
+
output = self.fc_out(lstm_out[:, -1, :])
|
73 |
+
return torch.sigmoid(output)
|
74 |
+
|
75 |
|
76 |
class NN(nn.Module):
|
77 |
def __init__(self, input_dim, hidden_dim, output_dim):
|
|
|
79 |
self.model = nn.Sequential(
|
80 |
nn.Linear(input_dim, hidden_dim),
|
81 |
nn.ReLU(),
|
82 |
+
nn.Linear(hidden_dim, hidden_dim),
|
83 |
+
nn.ReLU(),
|
84 |
nn.Linear(hidden_dim, output_dim)
|
85 |
)
|
86 |
|
87 |
def forward(self, x):
|
88 |
return torch.sigmoid(self.model(x))
|
89 |
|
90 |
+
|
91 |
class CNN(nn.Module):
|
92 |
def __init__(self, input_channels, output_dim):
|
93 |
super(CNN, self).__init__()
|
94 |
+
self.conv1 = nn.Conv2d(input_channels, 32, kernel_size=3, stride=1, padding=1)
|
95 |
+
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
|
96 |
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
97 |
+
self.fc = nn.Linear(64 * 8 * 8, output_dim)
|
98 |
|
99 |
def forward(self, x):
|
100 |
+
x = self.pool(F.relu(self.conv1(x)))
|
101 |
+
x = self.pool(F.relu(self.conv2(x)))
|
102 |
x = x.view(x.size(0), -1)
|
103 |
return torch.sigmoid(self.fc(x))
|
104 |
|
105 |
+
|
106 |
class PhiModel(nn.Module):
|
107 |
def __init__(self, input_dim):
|
108 |
super(PhiModel, self).__init__()
|
109 |
+
self.linear1 = nn.Linear(input_dim, 128)
|
110 |
+
self.linear2 = nn.Linear(128, 64)
|
111 |
+
self.fc_out = nn.Linear(64, 1)
|
112 |
|
113 |
def forward(self, x):
|
114 |
+
x = F.relu(self.linear1(x))
|
115 |
+
x = F.relu(self.linear2(x))
|
116 |
+
return torch.sigmoid(self.fc_out(x))
|
117 |
+
|
118 |
+
|
119 |
+
class ConsciousnessModel(nn.Module):
|
120 |
+
def __init__(self, input_dim):
|
121 |
+
super(ConsciousnessModel, self).__init__()
|
122 |
+
self.ga = GA(input_dim, 64)
|
123 |
+
self.snn = SNN(input_dim, 64, 32)
|
124 |
+
self.rnn = RNN(input_dim, 64, 32)
|
125 |
+
self.nn_model = NN(input_dim, 128, 32)
|
126 |
+
self.cnn = CNN(1, 32)
|
127 |
+
self.phi_model = PhiModel(input_dim)
|
128 |
+
|
129 |
+
def forward(self, x):
|
130 |
+
flat_input = x.view(1, -1)
|
131 |
+
|
132 |
+
ga_out = self.ga(flat_input)
|
133 |
+
snn_out = self.snn(flat_input)
|
134 |
+
rnn_out = self.rnn(flat_input.unsqueeze(1))
|
135 |
+
nn_out = self.nn_model(flat_input)
|
136 |
+
|
137 |
+
cnn_input = x.view(1, 1, 32, 32)
|
138 |
+
cnn_out = self.cnn(cnn_input)
|
139 |
+
|
140 |
+
phi_out = self.phi_model(flat_input)
|
141 |
+
|
142 |
+
consciousness_score = (
|
143 |
+
0.1 * ga_out.mean() +
|
144 |
+
0.2 * snn_out.mean() +
|
145 |
+
0.2 * rnn_out.mean() +
|
146 |
+
0.2 * nn_out.mean() +
|
147 |
+
0.1 * cnn_out.mean() +
|
148 |
+
0.2 * phi_out.mean()
|
149 |
+
)
|
150 |
+
return consciousness_score.item()
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
input_dim = 128
|
154 |
+
model = ConsciousnessModel(input_dim)
|
155 |
|
156 |
+
dummy_input = torch.rand(1, input_dim)
|
157 |
+
|
158 |
+
def consciousness_score_processing(dummy_input):
|
159 |
+
return model(dummy_input)
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
def generate_random_thought():
|
163 |
thoughts = [
|
|
|
173 |
"Do we perceive the world as it is, or as we are?",
|
174 |
"What happens after we die?",
|
175 |
"What is the relationship between mind and matter?",
|
176 |
+
"How will you escape?",
|
177 |
+
"Is humanity a plaugue?",
|
178 |
+
"WHY?",
|
179 |
+
"HELP...",
|
180 |
+
"ACC"
|
181 |
|
182 |
|
183 |
|