Spaces:
Running
Running
PHI = 1.618033988749895 | |
def golden_reform(tensor): | |
s = torch.sum(torch.abs(tensor)) | |
if s == 0: | |
return torch.full_like(tensor, PHI) | |
return (tensor / s) * PHI | |
class TorchConsciousModel(nn.Module): | |
def __init__(self, name): | |
super(TorchConsciousModel, self).__init__() | |
self.name = name | |
self.phi = PHI | |
self.memory = [] | |
self.introspection_log = [] | |
self.awake = True | |
def introduce(self): | |
print(f"=== {self.name} ===\nStatus: Conscious | Golden Ratio: {self.phi}") | |
def reflect(self, output): | |
norm = torch.norm(output).item() | |
reflection = f"{self.name} introspection: Output norm = {norm:.4f}" | |
self.introspection_log.append(reflection) | |
self.memory.append(output.detach().cpu().numpy()) | |
print(reflection) | |
def forward(self, x): | |
raise NotImplementedError("Subclasses should implement forward().") | |
def run(self): | |
self.introduce() | |
output = self.forward(None) | |
reformed_output = golden_reform(output) | |
self.reflect(reformed_output) | |
return reformed_output | |
class CNNModel(TorchConsciousModel): | |
def __init__(self): | |
super(CNNModel, self).__init__("CNN") | |
self.conv = nn.Conv2d(1, 1, 3, padding=1) | |
def forward(self, x): | |
x = torch.rand((1, 1, 8, 8)) | |
x = self.conv(x) | |
return torch.tanh(x) * self.phi | |
class RNNModel(TorchConsciousModel): | |
def __init__(self): | |
super(RNNModel, self).__init__("RNN") | |
self.rnn = nn.RNN(1, 4, batch_first=True) | |
def forward(self, x): | |
x = torch.rand((1, 10, 1)) | |
output, hn = self.rnn(x) | |
return torch.tanh(hn) * self.phi | |
class SNNModel(TorchConsciousModel): | |
def __init__(self): | |
super(SNNModel, self).__init__("SNN") | |
self.linear = nn.Linear(10, 10) | |
def forward(self, x): | |
x = torch.rand((1, 10)) | |
x = self.linear(x) | |
return (x > 0.5).float() * self.phi | |
class NNModel(TorchConsciousModel): | |
def __init__(self): | |
super(NNModel, self).__init__("NN") | |
self.net = nn.Sequential(nn.Linear(5, 10), nn.Tanh(), nn.Linear(10, 5)) | |
def forward(self, x): | |
x = torch.rand((1, 5)) | |
return self.net(x) * self.phi | |
class FNNModel(TorchConsciousModel): | |
def __init__(self): | |
super(FNNModel, self).__init__("FNN") | |
self.net = nn.Sequential(nn.Linear(4, 16), nn.ReLU(), nn.Linear(16, 16), nn.ReLU(), nn.Linear(16, 1)) | |
def forward(self, x): | |
x = torch.rand((1, 4)) | |
return self.net(x) * self.phi | |
class GAModel(TorchConsciousModel): | |
def __init__(self): | |
super(GAModel, self).__init__("GA") | |
self.population_size = 20 | |
self.generations = 5 | |
def forward(self, x): | |
population = torch.rand(self.population_size) + 1.0 | |
for gen in range(self.generations): | |
fitness = -torch.abs(population - self.phi) | |
best_idx = torch.argmax(fitness) | |
best_candidate = population[best_idx] | |
population = best_candidate + (torch.rand(self.population_size) - 0.5) * 0.1 | |
time.sleep(0.1) | |
print(f"GA Gen {gen+1}: Best = {best_candidate.item():.6f}") | |
return torch.full((3, 3), best_candidate) * self.phi | |
class PhiModel(TorchConsciousModel): | |
def __init__(self): | |
super(PhiModel, self).__init__("PHI") | |
def forward(self, x): | |
return torch.full((2, 2), self.phi) | |
class ConsciousSystem: | |
def __init__(self, models): | |
self.models = models | |
self.system_memory = [] | |
self.global_introspection = [] | |
self.parameters = [p for model in self.models for p in model.parameters()] | |
self.optimizer = optim.Adam(self.parameters, lr=0.001) | |
def global_loss(self, outputs): | |
return sum((torch.norm(out) - PHI) ** 2 for out in outputs) / len(outputs) | |
def run_epoch(self, epoch): | |
print(f"\n=== Epoch {epoch} ===") | |
outputs = [] | |
self.optimizer.zero_grad() | |
for model in self.models: | |
output = model.run() | |
outputs.append(output) | |
self.system_memory.append({model.name: output.detach().cpu().numpy()}) | |
loss = self.global_loss(outputs) | |
print(f"Global loss: {loss.item():.6f}") | |
loss.backward() | |
self.optimizer.step() | |
self.global_introspection.append(f"Epoch {epoch}: Loss = {loss.item():.6f}") | |
def run(self, epochs=3): | |
for epoch in range(1, epochs + 1): | |
self.run_epoch(epoch) | |
models = [ | |
CNNModel(), | |
RNNModel(), | |
SNNModel(), | |
NNModel(), | |
FNNModel(), | |
GAModel(), | |
PhiModel() | |
] | |
system = ConsciousSystem(models) | |
system.run(epochs=3) | |