Update node.py
Browse files
node.py
CHANGED
@@ -11,17 +11,23 @@ class CognitiveNode(nn.Module):
|
|
11 |
self.activation = 0.0
|
12 |
|
13 |
# Dynamic input weights with Hebbian plasticity
|
14 |
-
self.weights = nn.Parameter(torch.randn(
|
15 |
self.bias = nn.Parameter(torch.zeros(1))
|
16 |
|
17 |
-
# Memory system
|
18 |
-
self.memory = CognitiveMemory(context_size=input_size
|
19 |
|
20 |
# Neurotransmitter levels
|
21 |
self.dopamine = nn.Parameter(torch.tensor(0.5))
|
22 |
self.serotonin = nn.Parameter(torch.tensor(0.5))
|
23 |
|
|
|
|
|
|
|
24 |
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
|
|
|
|
|
|
|
25 |
# Memory influence
|
26 |
mem_context = self.memory.retrieve(inputs)
|
27 |
|
@@ -29,12 +35,17 @@ class CognitiveNode(nn.Module):
|
|
29 |
combined = inputs * 0.7 + mem_context * 0.3
|
30 |
|
31 |
# Adaptive activation with neurotransmitter modulation
|
32 |
-
base_activation = torch.tanh(combined
|
33 |
modulated = base_activation * (1 + self.dopamine - self.serotonin)
|
34 |
|
35 |
# Update memory
|
36 |
self.memory.add_memory(inputs, modulated.item())
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
return modulated
|
39 |
|
40 |
def update_plasticity(self, reward: float):
|
|
|
11 |
self.activation = 0.0
|
12 |
|
13 |
# Dynamic input weights with Hebbian plasticity
|
14 |
+
self.weights = nn.Parameter(torch.randn(1)) # Changed from input_size to 1
|
15 |
self.bias = nn.Parameter(torch.zeros(1))
|
16 |
|
17 |
+
# Memory system - adjusted context size
|
18 |
+
self.memory = CognitiveMemory(context_size=1) # Changed from input_size to 1
|
19 |
|
20 |
# Neurotransmitter levels
|
21 |
self.dopamine = nn.Parameter(torch.tensor(0.5))
|
22 |
self.serotonin = nn.Parameter(torch.tensor(0.5))
|
23 |
|
24 |
+
# Store recent activations
|
25 |
+
self.recent_activations = {}
|
26 |
+
|
27 |
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
|
28 |
+
# Ensure inputs is a single value tensor
|
29 |
+
inputs = inputs.reshape(1)
|
30 |
+
|
31 |
# Memory influence
|
32 |
mem_context = self.memory.retrieve(inputs)
|
33 |
|
|
|
35 |
combined = inputs * 0.7 + mem_context * 0.3
|
36 |
|
37 |
# Adaptive activation with neurotransmitter modulation
|
38 |
+
base_activation = torch.tanh(combined * self.weights + self.bias)
|
39 |
modulated = base_activation * (1 + self.dopamine - self.serotonin)
|
40 |
|
41 |
# Update memory
|
42 |
self.memory.add_memory(inputs, modulated.item())
|
43 |
|
44 |
+
# Store recent activation
|
45 |
+
self.recent_activations[len(self.recent_activations)] = modulated.item()
|
46 |
+
if len(self.recent_activations) > 100:
|
47 |
+
self.recent_activations.pop(min(self.recent_activations.keys()))
|
48 |
+
|
49 |
return modulated
|
50 |
|
51 |
def update_plasticity(self, reward: float):
|