File size: 4,719 Bytes
551d375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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


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)