Update network.py
Browse files- network.py +5 -9
network.py
CHANGED
|
@@ -19,13 +19,11 @@ class DynamicCognitiveNet(nn.Module):
|
|
| 19 |
f'input_{i}': CognitiveNode(i, 1) for i in range(input_size)
|
| 20 |
})
|
| 21 |
self.output_nodes = nn.ModuleList([
|
| 22 |
-
CognitiveNode(input_size + i,
|
| 23 |
-
for i in range(output_size)
|
| 24 |
])
|
| 25 |
|
| 26 |
# Structure learning parameters
|
| 27 |
self.connection_strength = nn.ParameterDict()
|
| 28 |
-
self.recent_activations = {}
|
| 29 |
self.init_connections()
|
| 30 |
|
| 31 |
# Emotional context
|
|
@@ -63,7 +61,7 @@ class DynamicCognitiveNet(nn.Module):
|
|
| 63 |
|
| 64 |
if input_acts:
|
| 65 |
combined = sum(input_acts) / math.sqrt(len(input_acts))
|
| 66 |
-
out_act = out_node(combined)
|
| 67 |
outputs.append(out_act)
|
| 68 |
|
| 69 |
return torch.cat(outputs)
|
|
@@ -76,9 +74,7 @@ class DynamicCognitiveNet(nn.Module):
|
|
| 76 |
new_strength = weight + self.learning_rate * reward
|
| 77 |
else:
|
| 78 |
new_strength = weight * 0.9
|
| 79 |
-
self.connection_strength[conn_id] =
|
| 80 |
-
torch.clamp(new_strength, -1, 1)
|
| 81 |
-
)
|
| 82 |
|
| 83 |
# Add new connections if performance is poor
|
| 84 |
if reward < -0.5 and torch.rand(1).item() < 0.3:
|
|
@@ -92,9 +88,9 @@ class DynamicCognitiveNet(nn.Module):
|
|
| 92 |
"""Create new random connection between underutilized nodes"""
|
| 93 |
# Find least active nodes
|
| 94 |
node_activations = {
|
| 95 |
-
node_id:
|
| 96 |
for node_id, node in self.nodes.items()
|
| 97 |
-
if
|
| 98 |
}
|
| 99 |
|
| 100 |
if not node_activations:
|
|
|
|
| 19 |
f'input_{i}': CognitiveNode(i, 1) for i in range(input_size)
|
| 20 |
})
|
| 21 |
self.output_nodes = nn.ModuleList([
|
| 22 |
+
CognitiveNode(input_size + i, 1) for i in range(output_size)
|
|
|
|
| 23 |
])
|
| 24 |
|
| 25 |
# Structure learning parameters
|
| 26 |
self.connection_strength = nn.ParameterDict()
|
|
|
|
| 27 |
self.init_connections()
|
| 28 |
|
| 29 |
# Emotional context
|
|
|
|
| 61 |
|
| 62 |
if input_acts:
|
| 63 |
combined = sum(input_acts) / math.sqrt(len(input_acts))
|
| 64 |
+
out_act = out_node(combined.unsqueeze(0))
|
| 65 |
outputs.append(out_act)
|
| 66 |
|
| 67 |
return torch.cat(outputs)
|
|
|
|
| 74 |
new_strength = weight + self.learning_rate * reward
|
| 75 |
else:
|
| 76 |
new_strength = weight * 0.9
|
| 77 |
+
self.connection_strength[conn_id].data = torch.clamp(new_strength, -1, 1)
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Add new connections if performance is poor
|
| 80 |
if reward < -0.5 and torch.rand(1).item() < 0.3:
|
|
|
|
| 88 |
"""Create new random connection between underutilized nodes"""
|
| 89 |
# Find least active nodes
|
| 90 |
node_activations = {
|
| 91 |
+
node_id: sum(node.recent_activations.values()) / len(node.recent_activations)
|
| 92 |
for node_id, node in self.nodes.items()
|
| 93 |
+
if node.recent_activations
|
| 94 |
}
|
| 95 |
|
| 96 |
if not node_activations:
|