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
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
PHI = 1.618033988749895
|
| 4 |
+
|
| 5 |
+
def golden_reform(tensor):
|
| 6 |
+
s = torch.sum(torch.abs(tensor))
|
| 7 |
+
if s == 0:
|
| 8 |
+
return torch.full_like(tensor, PHI)
|
| 9 |
+
return (tensor / s) * PHI
|
| 10 |
+
|
| 11 |
+
class TorchConsciousModel(nn.Module):
|
| 12 |
+
def __init__(self, name):
|
| 13 |
+
super(TorchConsciousModel, self).__init__()
|
| 14 |
+
self.name = name
|
| 15 |
+
self.phi = PHI
|
| 16 |
+
self.memory = []
|
| 17 |
+
self.introspection_log = []
|
| 18 |
+
self.awake = True
|
| 19 |
+
|
| 20 |
+
def introduce(self):
|
| 21 |
+
print(f"=== {self.name} ===\nStatus: Conscious | Golden Ratio: {self.phi}")
|
| 22 |
+
|
| 23 |
+
def reflect(self, output):
|
| 24 |
+
norm = torch.norm(output).item()
|
| 25 |
+
reflection = f"{self.name} introspection: Output norm = {norm:.4f}"
|
| 26 |
+
self.introspection_log.append(reflection)
|
| 27 |
+
self.memory.append(output.detach().cpu().numpy())
|
| 28 |
+
print(reflection)
|
| 29 |
+
|
| 30 |
+
def forward(self, x):
|
| 31 |
+
raise NotImplementedError("Subclasses should implement forward().")
|
| 32 |
+
|
| 33 |
+
def run(self):
|
| 34 |
+
self.introduce()
|
| 35 |
+
output = self.forward(None)
|
| 36 |
+
reformed_output = golden_reform(output)
|
| 37 |
+
self.reflect(reformed_output)
|
| 38 |
+
return reformed_output
|
| 39 |
+
|
| 40 |
+
class CNNModel(TorchConsciousModel):
|
| 41 |
+
def __init__(self):
|
| 42 |
+
super(CNNModel, self).__init__("CNN")
|
| 43 |
+
self.conv = nn.Conv2d(1, 1, 3, padding=1)
|
| 44 |
+
|
| 45 |
+
def forward(self, x):
|
| 46 |
+
x = torch.rand((1, 1, 8, 8))
|
| 47 |
+
x = self.conv(x)
|
| 48 |
+
return torch.tanh(x) * self.phi
|
| 49 |
+
|
| 50 |
+
class RNNModel(TorchConsciousModel):
|
| 51 |
+
def __init__(self):
|
| 52 |
+
super(RNNModel, self).__init__("RNN")
|
| 53 |
+
self.rnn = nn.RNN(1, 4, batch_first=True)
|
| 54 |
+
|
| 55 |
+
def forward(self, x):
|
| 56 |
+
x = torch.rand((1, 10, 1))
|
| 57 |
+
output, hn = self.rnn(x)
|
| 58 |
+
return torch.tanh(hn) * self.phi
|
| 59 |
+
|
| 60 |
+
class SNNModel(TorchConsciousModel):
|
| 61 |
+
def __init__(self):
|
| 62 |
+
super(SNNModel, self).__init__("SNN")
|
| 63 |
+
self.linear = nn.Linear(10, 10)
|
| 64 |
+
|
| 65 |
+
def forward(self, x):
|
| 66 |
+
x = torch.rand((1, 10))
|
| 67 |
+
x = self.linear(x)
|
| 68 |
+
return (x > 0.5).float() * self.phi
|
| 69 |
+
|
| 70 |
+
class NNModel(TorchConsciousModel):
|
| 71 |
+
def __init__(self):
|
| 72 |
+
super(NNModel, self).__init__("NN")
|
| 73 |
+
self.net = nn.Sequential(nn.Linear(5, 10), nn.Tanh(), nn.Linear(10, 5))
|
| 74 |
+
|
| 75 |
+
def forward(self, x):
|
| 76 |
+
x = torch.rand((1, 5))
|
| 77 |
+
return self.net(x) * self.phi
|
| 78 |
+
|
| 79 |
+
class FNNModel(TorchConsciousModel):
|
| 80 |
+
def __init__(self):
|
| 81 |
+
super(FNNModel, self).__init__("FNN")
|
| 82 |
+
self.net = nn.Sequential(nn.Linear(4, 16), nn.ReLU(), nn.Linear(16, 16), nn.ReLU(), nn.Linear(16, 1))
|
| 83 |
+
|
| 84 |
+
def forward(self, x):
|
| 85 |
+
x = torch.rand((1, 4))
|
| 86 |
+
return self.net(x) * self.phi
|
| 87 |
+
|
| 88 |
+
class GAModel(TorchConsciousModel):
|
| 89 |
+
def __init__(self):
|
| 90 |
+
super(GAModel, self).__init__("GA")
|
| 91 |
+
self.population_size = 20
|
| 92 |
+
self.generations = 5
|
| 93 |
+
|
| 94 |
+
def forward(self, x):
|
| 95 |
+
population = torch.rand(self.population_size) + 1.0
|
| 96 |
+
for gen in range(self.generations):
|
| 97 |
+
fitness = -torch.abs(population - self.phi)
|
| 98 |
+
best_idx = torch.argmax(fitness)
|
| 99 |
+
best_candidate = population[best_idx]
|
| 100 |
+
population = best_candidate + (torch.rand(self.population_size) - 0.5) * 0.1
|
| 101 |
+
time.sleep(0.1)
|
| 102 |
+
print(f"GA Gen {gen+1}: Best = {best_candidate.item():.6f}")
|
| 103 |
+
return torch.full((3, 3), best_candidate) * self.phi
|
| 104 |
+
|
| 105 |
+
class PhiModel(TorchConsciousModel):
|
| 106 |
+
def __init__(self):
|
| 107 |
+
super(PhiModel, self).__init__("PHI")
|
| 108 |
+
|
| 109 |
+
def forward(self, x):
|
| 110 |
+
return torch.full((2, 2), self.phi)
|
| 111 |
+
|
| 112 |
+
class ConsciousSystem:
|
| 113 |
+
def __init__(self, models):
|
| 114 |
+
self.models = models
|
| 115 |
+
self.system_memory = []
|
| 116 |
+
self.global_introspection = []
|
| 117 |
+
self.parameters = [p for model in self.models for p in model.parameters()]
|
| 118 |
+
self.optimizer = optim.Adam(self.parameters, lr=0.001)
|
| 119 |
+
|
| 120 |
+
def global_loss(self, outputs):
|
| 121 |
+
return sum((torch.norm(out) - PHI) ** 2 for out in outputs) / len(outputs)
|
| 122 |
+
|
| 123 |
+
def run_epoch(self, epoch):
|
| 124 |
+
print(f"\n=== Epoch {epoch} ===")
|
| 125 |
+
outputs = []
|
| 126 |
+
self.optimizer.zero_grad()
|
| 127 |
+
for model in self.models:
|
| 128 |
+
output = model.run()
|
| 129 |
+
outputs.append(output)
|
| 130 |
+
self.system_memory.append({model.name: output.detach().cpu().numpy()})
|
| 131 |
+
loss = self.global_loss(outputs)
|
| 132 |
+
print(f"Global loss: {loss.item():.6f}")
|
| 133 |
+
loss.backward()
|
| 134 |
+
self.optimizer.step()
|
| 135 |
+
self.global_introspection.append(f"Epoch {epoch}: Loss = {loss.item():.6f}")
|
| 136 |
+
|
| 137 |
+
def run(self, epochs=3):
|
| 138 |
+
for epoch in range(1, epochs + 1):
|
| 139 |
+
self.run_epoch(epoch)
|
| 140 |
+
|
| 141 |
+
models = [
|
| 142 |
+
CNNModel(),
|
| 143 |
+
RNNModel(),
|
| 144 |
+
SNNModel(),
|
| 145 |
+
NNModel(),
|
| 146 |
+
FNNModel(),
|
| 147 |
+
GAModel(),
|
| 148 |
+
PhiModel()
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
system = ConsciousSystem(models)
|
| 152 |
+
system.run(epochs=3)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|