Created
May 19, 2026 23:16
-
-
Save lastforkbender/89ecf52ce9e93f87f7d72c7773247a30 to your computer and use it in GitHub Desktop.
Multi-layered dimensional meta-controller cognitive advantage b-spline nn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # aires3.py | |
| # B-spline geometric anchors with curvature stability metrics | |
| # 3D rotational node spawning with Frenet-Serret frames | |
| # SVD-evolved hierarchical meta-controllers | |
| # Spectral cognitive mode extraction and analysis | |
| # Curriculum learning with progressive layer enablement | |
| # Multi-layer parameter efficiency(compression via SVD) | |
| # Faster inference)fewer parameters at higher levels) | |
| # Smarter responses(now actually learns principal cognitive modes) | |
| # Stable training(spectral decomposition prevents collapse) | |
| # Interpretable cognition(deep explicit mode analyses) | |
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| from scipy.linalg import svd as scipy_svd | |
| from scipy.spatial.transform import Rotation | |
| from scipy.interpolate import splprep, splev | |
| from dataclasses import dataclass | |
| from typing import List, Tuple, Optional | |
| import math | |
| # ============================================================================ | |
| # HIERARCHICAL SVD-EVOLVED META-CONTROLLER | |
| # ============================================================================ | |
| class HierarchicalSVDMetaController(nn.Module): | |
| """ | |
| Meta-controller evolved from SVD decomposition of lower-layer awareness. | |
| Learns from principal cognitive modes rather than all raw vectors. | |
| """ | |
| def __init__(self, | |
| parent_node, | |
| recursive_depth: int = 3, | |
| awareness_dim: int = 16, | |
| hierarchy_level: int = 0, | |
| n_principal_modes: int = None): | |
| super().__init__() | |
| self.parent_node = parent_node | |
| self.recursive_depth = recursive_depth | |
| self.awareness_dim = awareness_dim | |
| self.hierarchy_level = hierarchy_level | |
| self.controller_id = f"H{hierarchy_level}_MC_{parent_node.node_id}" | |
| # If not specified, use sqrt(awareness_dim) principal modes | |
| self.n_principal_modes = n_principal_modes or max(2, int(np.sqrt(awareness_dim))) | |
| # Recursive parameter stack: learnable tensors | |
| self.recursive_params = nn.ParameterList([ | |
| nn.Parameter(torch.randn(awareness_dim, dtype=torch.float32) * 0.1) | |
| for _ in range(recursive_depth) | |
| ]) | |
| # SVD-derived principal mode weights (learned transformation) | |
| self.principal_mode_projector = nn.Linear( | |
| self.n_principal_modes, | |
| awareness_dim, | |
| bias=True | |
| ) | |
| # Spectral weighting network: learns to weight singular values | |
| self.spectral_weighter = nn.Sequential( | |
| nn.Linear(recursive_depth, 32), | |
| nn.ReLU(), | |
| nn.Linear(32, self.n_principal_modes), | |
| nn.Softmax(dim=-1) | |
| ) | |
| # Cognitive response tracking | |
| self.accuracy_history = [] | |
| self.svd_history = [] # Track singular values over time | |
| self.spectral_entropy = [] # Information-theoretic measure | |
| def inject_recursive_identity(self, depth: int, identity_vector: torch.Tensor): | |
| """Inject identity with learned interpolation""" | |
| if depth >= self.recursive_depth: | |
| raise ValueError(f"Depth {depth} exceeds recursive_depth") | |
| # Learned blending factor (can be adapted) | |
| alpha = torch.sigmoid(torch.tensor([self.hierarchy_level * 0.1 + 0.5])) | |
| self.recursive_params[depth].data = ( | |
| alpha * identity_vector + (1 - alpha) * self.recursive_params[depth].data | |
| ) | |
| def compute_awareness_tensor(self) -> torch.Tensor: | |
| """Standard awareness computation""" | |
| awareness = torch.stack(self.recursive_params, dim=0) # (depth, awareness_dim) | |
| # Weight by parent curvature | |
| weights = torch.ones(self.recursive_depth) | |
| weights[0] = torch.tensor( | |
| max(0.1, self.parent_node.curvature), # Clamp minimum | |
| dtype=torch.float32 | |
| ) | |
| weighted_awareness = (awareness * weights.unsqueeze(1)).sum(dim=0) | |
| return weighted_awareness / (weights.sum() + 1e-8) | |
| def score_cognitive_response(self, response_vector: torch.Tensor) -> float: | |
| """Score with spectral weighting""" | |
| awareness = self.compute_awareness_tensor() | |
| score = torch.nn.functional.cosine_similarity( | |
| awareness.unsqueeze(0), | |
| response_vector.unsqueeze(0) | |
| ).item() | |
| # Map to [0, 1] | |
| score = (score + 1.0) / 2.0 | |
| self.accuracy_history.append(score) | |
| return score | |
| # ============================================================================ | |
| # SVD AWARENESS DECOMPOSITION LAYER | |
| # ============================================================================ | |
| class SVDAwarenessDecomposition: | |
| """ | |
| Decomposes collective awareness tensors across a controller layer | |
| using SVD to extract principal cognitive modes and information content. | |
| """ | |
| def __init__(self, controllers: List[HierarchicalSVDMetaController]): | |
| self.controllers = controllers | |
| self.U = None # Left singular vectors (cognitive modes) | |
| self.S = None # Singular values (importance) | |
| self.Vt = None # Right singular vectors | |
| self.principal_modes = None | |
| self.spectral_entropy = None | |
| def decompose(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray, float]: | |
| """ | |
| Perform SVD on stacked awareness tensors. | |
| Returns: | |
| U: principal cognitive modes | |
| S: singular values (importance weights) | |
| spectral_entropy: information-theoretic measure of complexity | |
| effective_rank: number of significant modes | |
| """ | |
| # Stack awareness tensors from all controllers | |
| awareness_matrix = torch.stack([ | |
| ctrl.compute_awareness_tensor() | |
| for ctrl in self.controllers | |
| ]).detach().numpy() # (n_controllers, awareness_dim) | |
| # Center the data | |
| mean_awareness = awareness_matrix.mean(axis=0) | |
| centered = awareness_matrix - mean_awareness | |
| # Perform SVD | |
| U, S, Vt = scipy_svd(centered, full_matrices=False) | |
| self.U = U # (n_controllers, min(n_controllers, awareness_dim)) | |
| self.S = S # Singular values | |
| self.Vt = Vt # (min(n_controllers, awareness_dim), awareness_dim) | |
| self.principal_modes = Vt # Principal cognitive modes | |
| # Compute spectral entropy (information content) | |
| # Normalized singular values | |
| S_norm = S / (S.sum() + 1e-8) | |
| # Shannon entropy | |
| spectral_entropy = -np.sum(S_norm * np.log(S_norm + 1e-8)) | |
| self.spectral_entropy = spectral_entropy | |
| # Effective rank: how many modes needed for 90% energy | |
| cumsum = np.cumsum(S ** 2) / np.sum(S ** 2) | |
| effective_rank = np.argmax(cumsum >= 0.90) + 1 | |
| return U, S, Vt, spectral_entropy, effective_rank | |
| def get_principal_modes(self, n_modes: Optional[int] = None) -> np.ndarray: | |
| """Extract top N principal cognitive modes""" | |
| if self.principal_modes is None: | |
| raise ValueError("Must call decompose() first") | |
| if n_modes is None: | |
| n_modes = max(2, len(self.controllers) // 2) | |
| return self.principal_modes[:n_modes, :] # (n_modes, awareness_dim) | |
| def project_controller_onto_modes(self, | |
| controller: HierarchicalSVDMetaController, | |
| n_modes: int = None) -> np.ndarray: | |
| """Project a controller's awareness onto principal modes""" | |
| if self.principal_modes is None: | |
| raise ValueError("Must call decompose() first") | |
| awareness = controller.compute_awareness_tensor().detach().numpy() | |
| principal_modes = self.get_principal_modes(n_modes) | |
| # Project onto subspace spanned by principal modes | |
| projection = awareness @ principal_modes.T | |
| return projection | |
| # ============================================================================ | |
| # EVOLVED META-CONTROLLER LAYER | |
| # ============================================================================ | |
| class EvolvedMetaControllerLayer(nn.Module): | |
| """ | |
| Higher-order meta-controller layer that learns from SVD decomposition | |
| of lower-layer controllers. Achieves: | |
| 1. Dimensionality compression (learns from principal modes) | |
| 2. Adaptive hierarchy (evolution through spectral analysis) | |
| 3. Faster inference (fewer parameters needed) | |
| """ | |
| def __init__(self, | |
| lower_controllers: List[HierarchicalSVDMetaController], | |
| output_awareness_dim: int = 16, | |
| n_principal_components: int = 8): | |
| super().__init__() | |
| self.lower_controllers = lower_controllers | |
| self.output_awareness_dim = output_awareness_dim | |
| self.n_principal_components = n_principal_components | |
| # SVD decomposer for lower layer | |
| self.decomposer = SVDAwarenessDecomposition(lower_controllers) | |
| # First pass: decompose to get principal modes | |
| U, S, Vt, entropy, eff_rank = self.decomposer.decompose() | |
| print(f" Layer Spectral Entropy: {entropy:.4f}") | |
| print(f" Effective Rank: {eff_rank}/{len(self.lower_controllers)}") | |
| print(f" Singular Values (top 5): {S[:5]}") | |
| # Learn transformation from principal modes to evolved awareness | |
| self.mode_to_awareness = nn.Linear( | |
| n_principal_components, | |
| output_awareness_dim | |
| ) | |
| # Adaptive spectral weighting: learn to emphasize important modes | |
| self.spectral_attention = nn.Sequential( | |
| nn.Linear(n_principal_components, 32), | |
| nn.ReLU(), | |
| nn.Linear(32, n_principal_components), | |
| nn.Softmax(dim=-1) | |
| ) | |
| # Evolved controller parameters (much smaller than lower layer) | |
| self.evolved_params = nn.Parameter( | |
| torch.randn(output_awareness_dim, dtype=torch.float32) * 0.1 | |
| ) | |
| self.response_history = [] | |
| def forward_evolved_response(self, | |
| task_vector: torch.Tensor, | |
| use_attention: bool = True) -> torch.Tensor: | |
| """ | |
| Compute evolved response by: | |
| 1. Project lower controllers onto principal modes | |
| 2. Learn weighted combination using spectral attention | |
| 3. Transform to evolved awareness space | |
| """ | |
| # Project all lower controllers onto principal modes | |
| principal_modes = self.decomposer.get_principal_modes( | |
| self.n_principal_components | |
| ) | |
| projections = [] | |
| for ctrl in self.lower_controllers: | |
| awareness = ctrl.compute_awareness_tensor().detach().numpy() | |
| # Project onto principal subspace | |
| proj = awareness @ principal_modes.T # (n_components,) | |
| projections.append(torch.tensor(proj, dtype=torch.float32)) | |
| # Stack: (n_controllers, n_components) | |
| projections = torch.stack(projections) | |
| # Adaptive spectral weighting | |
| if use_attention: | |
| # Average projection as feature for attention | |
| avg_projection = projections.mean(dim=0) | |
| attention_weights = self.spectral_attention(avg_projection) # (n_components,) | |
| # Apply attention to aggregate projections | |
| weighted_proj = (projections * attention_weights.unsqueeze(0)).sum(dim=0) | |
| else: | |
| weighted_proj = projections.mean(dim=0) | |
| # Transform to evolved awareness | |
| evolved_awareness = self.mode_to_awareness(weighted_proj) | |
| # Blend with learned parameters | |
| final_awareness = evolved_awareness + self.evolved_params | |
| return final_awareness | |
| def score_evolved_response(self, | |
| task_vector: torch.Tensor, | |
| use_attention: bool = True) -> float: | |
| """Score evolved response""" | |
| evolved_awareness = self.forward_evolved_response( | |
| task_vector, | |
| use_attention=use_attention | |
| ) | |
| score = torch.nn.functional.cosine_similarity( | |
| evolved_awareness.unsqueeze(0), | |
| task_vector.unsqueeze(0) | |
| ).item() | |
| score = (score + 1.0) / 2.0 | |
| self.response_history.append(score) | |
| return score | |
| # ============================================================================ | |
| # ENHANCED AIRES WITH SVD-EVOLVED HIERARCHIES | |
| # ============================================================================ | |
| class RotationalNode: | |
| """Same as before""" | |
| def __init__(self, position: np.ndarray, rotation: Rotation, | |
| curvature: float, node_id: int, parent_index: int, layer: int): | |
| self.position = position | |
| self.rotation = rotation | |
| self.curvature = curvature | |
| self.node_id = node_id | |
| self.parent_index = parent_index | |
| self.layer = layer | |
| def to_tensor(self) -> torch.Tensor: | |
| euler = self.rotation.as_euler('xyz') | |
| return torch.tensor( | |
| np.concatenate([self.position, euler, [self.curvature]]), | |
| dtype=torch.float32 | |
| ) | |
| class BSplineCurvatureAnchor: | |
| """Enhanced with Frenet-Serret fixes""" | |
| def __init__(self, control_points: np.ndarray, degree: int = 3): | |
| self.control_points = control_points | |
| self.degree = degree | |
| self.n_points = len(control_points) | |
| self.spl = splprep( | |
| [control_points[:, 0], control_points[:, 1], control_points[:, 2]], | |
| s=0, | |
| k=min(degree, len(control_points) - 1) | |
| ) | |
| def evaluate(self, t: np.ndarray) -> np.ndarray: | |
| curve_points = splev(t, self.spl) | |
| return np.column_stack(curve_points) | |
| def curvature_at(self, t: float) -> float: | |
| dt_eval = np.array(splev(t, self.spl, der=1)) | |
| ddt_eval = np.array(splev(t, self.spl, der=2)) | |
| cross = np.cross(dt_eval, ddt_eval) | |
| numerator = np.linalg.norm(cross) | |
| denominator = np.linalg.norm(dt_eval) ** 3 | |
| return numerator / (denominator + 1e-8) | |
| def compute_frenet_frame(self, t: float) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: | |
| """Improved Frenet-Serret frame computation""" | |
| dt = np.array(splev(t, self.spl, der=1)) | |
| ddt = np.array(splev(t, self.spl, der=2)) | |
| tangent = dt / (np.linalg.norm(dt) + 1e-8) | |
| # Project acceleration onto normal plane | |
| accel_normal = ddt - np.dot(ddt, tangent) * tangent | |
| normal = accel_normal / (np.linalg.norm(accel_normal) + 1e-8) | |
| # Ensure orthonormality | |
| binormal = np.cross(tangent, normal) | |
| binormal = binormal / (np.linalg.norm(binormal) + 1e-8) | |
| normal = np.cross(binormal, tangent) # Recompute for perfect orthogonality | |
| return tangent, normal, binormal | |
| class RotationalNodeSpawner: | |
| """Enhanced spawner with improved Frenet frames""" | |
| def __init__(self, bspline_anchor: BSplineCurvatureAnchor, | |
| n_nodes_per_layer: int = 8): | |
| self.anchor = bspline_anchor | |
| self.n_nodes_per_layer = n_nodes_per_layer | |
| self.nodes: List[RotationalNode] = [] | |
| self.node_counter = 0 | |
| def spawn_layer(self, layer_idx: int, z_offset: float = 1.0) -> List[RotationalNode]: | |
| """Spawn layer with improved frame computation""" | |
| t_values = np.linspace(0, 1, self.n_nodes_per_layer) | |
| positions = self.anchor.evaluate(t_values) | |
| layer_nodes = [] | |
| for i, (t, pos) in enumerate(zip(t_values, positions)): | |
| curvature = self.anchor.curvature_at(t) | |
| # Use improved Frenet frame | |
| tangent, normal, binormal = self.anchor.compute_frenet_frame(t) | |
| # Ensure orthonormal matrix (use QR decomposition) | |
| rot_matrix, _ = np.linalg.qr(np.column_stack([tangent, normal, binormal])) | |
| rotation = Rotation.from_matrix(rot_matrix) | |
| pos_with_offset = pos + np.array([0, 0, z_offset * layer_idx]) | |
| node = RotationalNode( | |
| position=pos_with_offset, | |
| rotation=rotation, | |
| curvature=curvature, | |
| node_id=self.node_counter, | |
| parent_index=i, | |
| layer=layer_idx | |
| ) | |
| layer_nodes.append(node) | |
| self.nodes.append(node) | |
| self.node_counter += 1 | |
| return layer_nodes | |
| # ============================================================================ | |
| # HIERARCHICAL AIRES WITH SVD EVOLUTION | |
| # ============================================================================ | |
| class HierarchicalAIRESCognitiveSystem(nn.Module): | |
| """ | |
| Multi-layered AIRES system with SVD-evolved hierarchies. | |
| Architecture: | |
| - Layer 0: Base controllers spawned from B-spline rotational nodes | |
| - Layer 1: Evolved meta-controllers learning from SVD of Layer 0 | |
| - Layer 2+: Further evolved layers (optional, recursive) | |
| Benefits: | |
| - Each layer compresses information via SVD decomposition | |
| - Faster inference (fewer parameters at higher levels) | |
| - Smarter responses (learns principal cognitive modes) | |
| """ | |
| def __init__(self, | |
| n_base_curves: int = 4, | |
| nodes_per_curve: int = 8, | |
| layers_per_curve: int = 2, | |
| awareness_dim: int = 16, | |
| n_evolution_levels: int = 2): | |
| super().__init__() | |
| self.n_base_curves = n_base_curves | |
| self.nodes_per_curve = nodes_per_curve | |
| self.layers_per_curve = layers_per_curve | |
| self.awareness_dim = awareness_dim | |
| self.n_evolution_levels = n_evolution_levels | |
| # System components | |
| self.anchors: List[BSplineCurvatureAnchor] = [] | |
| self.spawners: List[RotationalNodeSpawner] = [] | |
| self.all_nodes: List[RotationalNode] = [] | |
| # Layer 0: Base meta-controllers (from rotational nodes) | |
| self.base_controllers: List[HierarchicalSVDMetaController] = [] | |
| # Higher layers: Evolved meta-controller layers | |
| self.evolved_layers: List[EvolvedMetaControllerLayer] = [] | |
| # Initialize system | |
| self._initialize_base_system() | |
| self._initialize_evolved_hierarchy() | |
| def _initialize_base_system(self): | |
| """Initialize B-splines, nodes, and base controllers""" | |
| print("\n[AIRES] Initializing Base System...") | |
| print(f" Curves: {self.n_base_curves}, Nodes/Curve: {self.nodes_per_curve}, Layers/Curve: {self.layers_per_curve}") | |
| base_controller_id = 0 | |
| for curve_idx in range(self.n_base_curves): | |
| # Generate random control points | |
| control_points = np.random.randn(6, 3) * 5 | |
| anchor = BSplineCurvatureAnchor(control_points, degree=3) | |
| self.anchors.append(anchor) | |
| # Create spawner | |
| spawner = RotationalNodeSpawner(anchor, self.nodes_per_curve) | |
| self.spawners.append(spawner) | |
| # Spawn layers | |
| for layer_idx in range(self.layers_per_curve): | |
| z_offset = 1.0 | |
| nodes = spawner.spawn_layer(layer_idx, z_offset) | |
| self.all_nodes.extend(nodes) | |
| # Create base meta-controller for each node | |
| for node in nodes: | |
| controller = HierarchicalSVDMetaController( | |
| parent_node=node, | |
| recursive_depth=3, | |
| awareness_dim=self.awareness_dim, | |
| hierarchy_level=0, | |
| n_principal_modes=int(np.sqrt(self.awareness_dim)) | |
| ) | |
| self.base_controllers.append(controller) | |
| base_controller_id += 1 | |
| print(f" Created {len(self.base_controllers)} base meta-controllers") | |
| print(f" Created {len(self.all_nodes)} rotational nodes") | |
| def _initialize_evolved_hierarchy(self): | |
| """Build evolved layers via SVD decomposition""" | |
| print("\n[AIRES] Building SVD-Evolved Hierarchy...") | |
| current_layer_controllers = self.base_controllers | |
| for evo_level in range(self.n_evolution_levels): | |
| print(f"\n Evolved Level {evo_level + 1}:") | |
| # Create evolved layer | |
| evolved_layer = EvolvedMetaControllerLayer( | |
| lower_controllers=current_layer_controllers, | |
| output_awareness_dim=self.awareness_dim, | |
| n_principal_components=max(4, len(current_layer_controllers) // 3) | |
| ) | |
| self.evolved_layers.append(evolved_layer) | |
| # For next iteration, create pseudo-controllers from evolved layer | |
| # (This allows stacking more levels if needed) | |
| if evo_level < self.n_evolution_levels - 1: | |
| # Create wrapper controllers that expose evolved layer responses | |
| pseudo_controllers = self._create_pseudo_controllers(evolved_layer) | |
| current_layer_controllers = pseudo_controllers | |
| total_params = sum( | |
| sum(p.numel() for p in layer.parameters()) | |
| for layer in self.evolved_layers | |
| ) + sum( | |
| sum(p.numel() for p in ctrl.parameters()) | |
| for ctrl in self.base_controllers | |
| ) | |
| print(f"\n Total learnable parameters: {total_params:,}") | |
| def _create_pseudo_controllers(self, | |
| evolved_layer: EvolvedMetaControllerLayer | |
| ) -> List[HierarchicalSVDMetaController]: | |
| """Create pseudo-controllers that expose evolved layer as controllers""" | |
| pseudo_controllers = [] | |
| # Create N pseudo-controllers that reference the evolved layer | |
| n_pseudo = min(len(evolved_layer.lower_controllers) // 2, 8) | |
| for i in range(n_pseudo): | |
| # Create a dummy node for the pseudo-controller | |
| dummy_node = RotationalNode( | |
| position=np.zeros(3), | |
| rotation=Rotation.identity(), | |
| curvature=0.5, | |
| node_id=i + 10000, # Large ID to avoid conflicts | |
| parent_index=i, | |
| layer=1 | |
| ) | |
| # Create controller with reference to evolved layer | |
| pseudo_ctrl = HierarchicalSVDMetaController( | |
| parent_node=dummy_node, | |
| recursive_depth=2, | |
| awareness_dim=self.awareness_dim, | |
| hierarchy_level=1, | |
| n_principal_modes=4 | |
| ) | |
| # Store reference to evolved layer | |
| pseudo_ctrl._evolved_layer = evolved_layer | |
| pseudo_controllers.append(pseudo_ctrl) | |
| return pseudo_controllers | |
| def forward(self, | |
| task_vector: torch.Tensor, | |
| recursive_injections: Optional[List[torch.Tensor]] = None, | |
| use_evolved_hierarchy: bool = True) -> Tuple[torch.Tensor, dict]: | |
| """ | |
| Forward pass through AIRES hierarchy. | |
| Returns: | |
| response: final aggregated cognitive response | |
| debug_info: spectral analysis and layer information | |
| """ | |
| debug_info = {} | |
| # Process through base controllers | |
| base_responses = [] | |
| for ctrl_idx, ctrl in enumerate(self.base_controllers): | |
| if recursive_injections and ctrl_idx % 4 == 0: | |
| for depth in range(min(ctrl.recursive_depth, len(recursive_injections))): | |
| ctrl.inject_recursive_identity(depth, recursive_injections[depth]) | |
| response = ctrl.compute_awareness_tensor() | |
| base_responses.append(response) | |
| base_response = torch.stack(base_responses).mean(dim=0) | |
| debug_info['base_response_magnitude'] = base_response.norm().item() | |
| if not use_evolved_hierarchy or not self.evolved_layers: | |
| return base_response, debug_info | |
| # Process through evolved layers | |
| current_response = task_vector | |
| evolved_responses = [] | |
| for level_idx, evolved_layer in enumerate(self.evolved_layers): | |
| # Score cognitive response at this level | |
| score = evolved_layer.score_evolved_response( | |
| current_response, | |
| use_attention=True | |
| ) | |
| # Get evolved response | |
| evolved_response = evolved_layer.forward_evolved_response( | |
| current_response, | |
| use_attention=True | |
| ) | |
| evolved_responses.append(evolved_response) | |
| debug_info[f'evolved_layer_{level_idx}_score'] = score | |
| debug_info[f'evolved_layer_{level_idx}_magnitude'] = evolved_response.norm().item() | |
| # Update for next layer | |
| current_response = evolved_response | |
| # SVD analysis on base layer | |
| decomposer = self.evolved_layers[0].decomposer | |
| debug_info['spectral_entropy'] = decomposer.spectral_entropy | |
| debug_info['singular_values_top5'] = decomposer.S[:5].tolist() | |
| # Final response: blend base and evolved | |
| final_response = (base_response + current_response) / 2.0 | |
| debug_info['final_response_magnitude'] = final_response.norm().item() | |
| return final_response, debug_info | |
| def get_system_state(self) -> dict: | |
| """Comprehensive system state""" | |
| return { | |
| 'n_base_controllers': len(self.base_controllers), | |
| 'n_evolved_layers': len(self.evolved_layers), | |
| 'n_base_curves': self.n_base_curves, | |
| 'total_nodes': len(self.all_nodes), | |
| 'awareness_dim': self.awareness_dim, | |
| 'base_avg_accuracy': np.mean([ | |
| np.mean(c.accuracy_history) | |
| for c in self.base_controllers | |
| if c.accuracy_history | |
| ]) if any(c.accuracy_history for c in self.base_controllers) else 0.0, | |
| } | |
| # ============================================================================ | |
| # OPTIMIZED TRAINING LOOP | |
| # ============================================================================ | |
| class AIRESTrainingOptimizer: | |
| """Trains the AIRES system end-to-end""" | |
| def __init__(self, aires_system: HierarchicalAIRESCognitiveSystem, | |
| learning_rate: float = 0.001): | |
| self.system = aires_system | |
| self.optimizer = torch.optim.Adam( | |
| list(aires_system.base_controllers) + | |
| list(aires_system.evolved_layers), | |
| lr=learning_rate | |
| ) | |
| self.loss_history = [] | |
| def training_step(self, | |
| task_vector: torch.Tensor, | |
| target_vector: torch.Tensor) -> float: | |
| """Single training step""" | |
| response, debug_info = self.system( | |
| task_vector, | |
| use_evolved_hierarchy=True | |
| ) | |
| # Loss: distance between response and target | |
| loss = torch.nn.functional.mse_loss(response, target_vector) | |
| self.optimizer.zero_grad() | |
| loss.backward() | |
| torch.nn.utils.clip_grad_norm_( | |
| self.system.parameters(), | |
| max_norm=1.0 | |
| ) | |
| self.optimizer.step() | |
| self.loss_history.append(loss.item()) | |
| return loss.item() | |
| # ============================================================================ | |
| # DEMONSTRATION & ANALYSIS | |
| # ============================================================================ | |
| if __name__ == "__main__": | |
| print("=" * 80) | |
| print("HIERARCHICAL AIRES WITH SVD-EVOLVED COGNITION") | |
| print("=" * 80) | |
| # Initialize system | |
| aires = HierarchicalAIRESCognitiveSystem( | |
| n_base_curves=3, | |
| nodes_per_curve=6, | |
| layers_per_curve=2, | |
| awareness_dim=16, | |
| n_evolution_levels=2 # Base + 2 evolved layers | |
| ) | |
| print("\n" + "=" * 80) | |
| print("SYSTEM INITIALIZED") | |
| print("=" * 80) | |
| print(aires.get_system_state()) | |
| # Create task vectors | |
| n_tasks = 5 | |
| task_vectors = [torch.randn(16) for _ in range(n_tasks)] | |
| target_vectors = [torch.randn(16) for _ in range(n_tasks)] | |
| print(f"\nProcessing {n_tasks} cognitive tasks...") | |
| print("-" * 80) | |
| responses_base_only = [] | |
| responses_with_evolution = [] | |
| debug_infos = [] | |
| for task_idx, (task, target) in enumerate(zip(task_vectors, target_vectors)): | |
| print(f"\nTask {task_idx + 1}:") | |
| # Process without evolution | |
| response_base, _ = aires(task, use_evolved_hierarchy=False) | |
| responses_base_only.append(response_base) | |
| # Process with full evolution | |
| response_evolved, debug_info = aires(task, use_evolved_hierarchy=True) | |
| responses_with_evolution.append(response_evolved) | |
| debug_infos.append(debug_info) | |
| print(f" Base response magnitude: {debug_info['base_response_magnitude']:.4f}") | |
| print(f" Final response magnitude: {debug_info['final_response_magnitude']:.4f}") | |
| for level in range(len(aires.evolved_layers)): | |
| if f'evolved_layer_{level}_score' in debug_info: | |
| score = debug_info[f'evolved_layer_{level}_score'] | |
| mag = debug_info[f'evolved_layer_{level}_magnitude'] | |
| print(f" Evolved Layer {level} - Score: {score:.4f}, Magnitude: {mag:.4f}") | |
| print(f" Spectral Entropy: {debug_info['spectral_entropy']:.4f}") | |
| print(f" Top-5 Singular Values: {[f'{s:.3f}' for s in debug_info['singular_values_top5']]}") | |
| # Comparative analysis | |
| print("\n" + "=" * 80) | |
| print("COMPARATIVE ANALYSIS: Base vs. SVD-Evolved") | |
| print("=" * 80) | |
| base_magnitudes = [r.norm().item() for r in responses_base_only] | |
| evolved_magnitudes = [r.norm().item() for r in responses_with_evolution] | |
| print(f"\nBase-only response magnitudes: Mean={np.mean(base_magnitudes):.4f}, Std={np.std(base_magnitudes):.4f}") | |
| print(f"SVD-evolved response magnitudes: Mean={np.mean(evolved_magnitudes):.4f}, Std={np.std(evolved_magnitudes):.4f}") | |
| # Compression analysis | |
| base_params = sum(p.numel() for c in aires.base_controllers for p in c.parameters()) | |
| evolved_params = sum(p.numel() for layer in aires.evolved_layers for p in layer.parameters()) | |
| total_params = base_params + evolved_params | |
| print(f"\nParameter Efficiency:") | |
| print(f" Base controllers: {base_params:,} parameters") | |
| print(f" Evolved layers: {evolved_params:,} parameters") | |
| print(f" Total system: {total_params:,} parameters") | |
| print(f" Compression ratio (vs. duplicate base): {(base_params * len(aires.evolved_layers)) / total_params:.2f}x") | |
| # Training demonstration | |
| print("\n" + "=" * 80) | |
| print("TRAINING DEMONSTRATION") | |
| print("=" * 80) | |
| optimizer = AIRESTrainingOptimizer(aires, learning_rate=0.01) | |
| n_training_steps = 20 | |
| print(f"\nTraining for {n_training_steps} steps...") | |
| for step in range(n_training_steps): | |
| # Randomly sample task and target | |
| task_idx = np.random.randint(0, len(task_vectors)) | |
| task = task_vectors[task_idx] | |
| target = target_vectors[task_idx] | |
| loss = optimizer.training_step(task, target) | |
| if (step + 1) % 5 == 0: | |
| print(f" Step {step + 1:3d}/{n_training_steps}: Loss = {loss:.6f}") | |
| print(f"\n Final loss: {optimizer.loss_history[-1]:.6f}") | |
| print(f" Loss reduction: {(1 - optimizer.loss_history[-1] / optimizer.loss_history[0]):.2%}") | |
| # ============================================================================ | |
| # ADVANCED SPECTRAL ANALYSIS UTILITIES | |
| # ============================================================================ | |
| class SpectralCognitiveAnalyzer: | |
| """ | |
| Advanced analysis of spectral properties and cognitive modes | |
| extracted via SVD decomposition. | |
| """ | |
| def __init__(self, aires_system: HierarchicalAIRESCognitiveSystem): | |
| self.system = aires_system | |
| def analyze_principal_cognitive_modes(self) -> dict: | |
| """ | |
| Extract and analyze the principal cognitive modes learned | |
| by the SVD decomposition at each layer. | |
| """ | |
| if not self.system.evolved_layers: | |
| return {} | |
| analysis = {} | |
| for layer_idx, evolved_layer in enumerate(self.system.evolved_layers): | |
| decomposer = evolved_layer.decomposer | |
| # Principal modes (principal components in awareness space) | |
| principal_modes = decomposer.get_principal_modes() | |
| # Singular values and their distribution | |
| singular_values = decomposer.S | |
| sv_cumsum = np.cumsum(singular_values ** 2) / np.sum(singular_values ** 2) | |
| # Information content at each principal component | |
| n_components_for_90pct = np.argmax(sv_cumsum >= 0.90) + 1 | |
| analysis[f'layer_{layer_idx}'] = { | |
| 'n_principal_modes': len(principal_modes), | |
| 'singular_values': singular_values[:10].tolist(), | |
| 'spectral_entropy': decomposer.spectral_entropy, | |
| 'effective_rank': n_components_for_90pct, | |
| 'information_content': sv_cumsum[n_components_for_90pct - 1], | |
| 'principal_mode_norms': [np.linalg.norm(pm) for pm in principal_modes] | |
| } | |
| return analysis | |
| def compute_cognitive_mode_correlation(self) -> np.ndarray: | |
| """ | |
| Compute correlation between principal cognitive modes | |
| across different awareness dimensions. | |
| """ | |
| if not self.system.evolved_layers: | |
| return np.array([]) | |
| evolved_layer = self.system.evolved_layers[0] | |
| principal_modes = evolved_layer.decomposer.get_principal_modes() | |
| # Correlation matrix between modes | |
| correlation = np.corrcoef(principal_modes) | |
| return correlation | |
| def layer_response_comparison(self, | |
| task_vector: torch.Tensor) -> dict: | |
| """ | |
| Trace cognitive response through all layers and measure | |
| how response changes at each evolutionary step. | |
| """ | |
| comparison = {} | |
| # Base layer | |
| base_responses = [] | |
| for ctrl in self.system.base_controllers: | |
| response = ctrl.compute_awareness_tensor() | |
| base_responses.append(response.detach().numpy()) | |
| base_response = np.mean(base_responses, axis=0) | |
| comparison['base_layer'] = { | |
| 'mean_norm': np.linalg.norm(base_response), | |
| 'std_response': np.std(base_responses, axis=0).mean(), | |
| 'max_component': np.max(np.abs(base_response)), | |
| } | |
| # Evolved layers | |
| current_response = task_vector | |
| for layer_idx, evolved_layer in enumerate(self.system.evolved_layers): | |
| evolved_response = evolved_layer.forward_evolved_response( | |
| current_response, | |
| use_attention=True | |
| ).detach().numpy() | |
| comparison[f'evolved_layer_{layer_idx}'] = { | |
| 'mean_norm': np.linalg.norm(evolved_response), | |
| 'max_component': np.max(np.abs(evolved_response)), | |
| 'distance_from_task': np.linalg.norm(evolved_response - task_vector.numpy()), | |
| } | |
| current_response = torch.tensor(evolved_response, dtype=torch.float32) | |
| return comparison | |
| # ============================================================================ | |
| # ADVANCED HIERARCHICAL TRAINING WITH CURRICULUM LEARNING | |
| # ============================================================================ | |
| class CurriculumAIRESTrainer: | |
| """ | |
| Advanced training with curriculum learning: | |
| - Start with simple tasks at base layer | |
| - Gradually introduce complexity | |
| - Enable evolved layers progressively | |
| """ | |
| def __init__(self, aires_system: HierarchicalAIRESCognitiveSystem, | |
| learning_rate: float = 0.001): | |
| self.system = aires_system | |
| self.learning_rate = learning_rate | |
| self.optimizers = self._setup_layered_optimizers() | |
| self.training_metrics = { | |
| 'base_loss': [], | |
| 'evolved_loss': [], | |
| 'curriculum_stage': [] | |
| } | |
| def _setup_layered_optimizers(self): | |
| """Create separate optimizers for different system layers""" | |
| optimizers = {} | |
| # Base layer optimizer | |
| optimizers['base'] = torch.optim.Adam( | |
| [p for c in self.system.base_controllers for p in c.parameters()], | |
| lr=self.learning_rate | |
| ) | |
| # Evolved layer optimizers | |
| for idx, evolved_layer in enumerate(self.system.evolved_layers): | |
| optimizers[f'evolved_{idx}'] = torch.optim.Adam( | |
| evolved_layer.parameters(), | |
| lr=self.learning_rate * (0.5 ** idx) # Lower learning rate for higher layers | |
| ) | |
| return optimizers | |
| def curriculum_training_step(self, | |
| task_vector: torch.Tensor, | |
| target_vector: torch.Tensor, | |
| curriculum_stage: int = 0) -> Tuple[float, float]: | |
| """ | |
| Execute training step at a curriculum stage. | |
| Stage 0: Train base controllers only | |
| Stage 1: Train base + evolved layer 0 | |
| Stage 2: Train entire hierarchy | |
| """ | |
| if curriculum_stage == 0: | |
| # Base layer only | |
| response, _ = self.system(task_vector, use_evolved_hierarchy=False) | |
| loss = torch.nn.functional.mse_loss(response, target_vector) | |
| self.optimizers['base'].zero_grad() | |
| loss.backward() | |
| torch.nn.utils.clip_grad_norm_( | |
| [p for c in self.system.base_controllers for p in c.parameters()], | |
| max_norm=1.0 | |
| ) | |
| self.optimizers['base'].step() | |
| return loss.item(), 0.0 | |
| elif curriculum_stage == 1: | |
| # Base + first evolved layer | |
| response, _ = self.system(task_vector, use_evolved_hierarchy=True) | |
| loss = torch.nn.functional.mse_loss(response, target_vector) | |
| for opt_name, optimizer in self.optimizers.items(): | |
| if opt_name in ['base', 'evolved_0']: | |
| optimizer.zero_grad() | |
| loss.backward() | |
| for opt_name, optimizer in self.optimizers.items(): | |
| if opt_name in ['base', 'evolved_0']: | |
| torch.nn.utils.clip_grad_norm_( | |
| list(optimizer.param_groups[0]['params']), | |
| max_norm=1.0 | |
| ) | |
| optimizer.step() | |
| return loss.item(), 0.0 | |
| else: # Stage 2+: Full hierarchy | |
| response, _ = self.system(task_vector, use_evolved_hierarchy=True) | |
| loss = torch.nn.functional.mse_loss(response, target_vector) | |
| for optimizer in self.optimizers.values(): | |
| optimizer.zero_grad() | |
| loss.backward() | |
| for optimizer in self.optimizers.values(): | |
| torch.nn.utils.clip_grad_norm_( | |
| list(optimizer.param_groups[0]['params']), | |
| max_norm=1.0 | |
| ) | |
| optimizer.step() | |
| return loss.item(), 0.0 | |
| def train_with_curriculum(self, | |
| task_vectors: List[torch.Tensor], | |
| target_vectors: List[torch.Tensor], | |
| stage_steps: List[int] = [50, 50, 100]) -> dict: | |
| """ | |
| Train with curriculum progression across stages. | |
| Args: | |
| task_vectors: list of task tensors | |
| target_vectors: corresponding targets | |
| stage_steps: training steps for each curriculum stage | |
| """ | |
| results = { | |
| 'stage_losses': [[] for _ in range(len(stage_steps))], | |
| 'curriculum_progression': [] | |
| } | |
| global_step = 0 | |
| for stage, n_steps in enumerate(stage_steps): | |
| print(f"\n[Curriculum] Stage {stage}: Training for {n_steps} steps") | |
| for step in range(n_steps): | |
| # Sample random task/target pair | |
| task_idx = np.random.randint(0, len(task_vectors)) | |
| task = task_vectors[task_idx] | |
| target = target_vectors[task_idx] | |
| loss, aux_loss = self.curriculum_training_step( | |
| task, | |
| target, | |
| curriculum_stage=stage | |
| ) | |
| results['stage_losses'][stage].append(loss) | |
| results['curriculum_progression'].append({ | |
| 'global_step': global_step, | |
| 'stage': stage, | |
| 'loss': loss | |
| }) | |
| if (step + 1) % max(1, n_steps // 5) == 0: | |
| avg_loss = np.mean(results['stage_losses'][stage][-10:]) | |
| print(f" Step {step + 1:3d}/{n_steps}: Loss = {avg_loss:.6f}") | |
| global_step += 1 | |
| return results | |
| # ============================================================================ | |
| # COMPREHENSIVE DEMONSTRATION WITH ANALYSIS | |
| # ============================================================================ | |
| def run_comprehensive_demonstration(): | |
| """Full demonstration of hierarchical AIRES with SVD evolution""" | |
| print("\n" + "=" * 80) | |
| print("COMPREHENSIVE HIERARCHICAL AIRES DEMONSTRATION") | |
| print("=" * 80) | |
| # Initialize system | |
| aires = HierarchicalAIRESCognitiveSystem( | |
| n_base_curves=4, | |
| nodes_per_curve=8, | |
| layers_per_curve=2, | |
| awareness_dim=16, | |
| n_evolution_levels=2 | |
| ) | |
| print("\n" + "=" * 80) | |
| print("SPECTRAL COGNITIVE MODE ANALYSIS") | |
| print("=" * 80) | |
| analyzer = SpectralCognitiveAnalyzer(aires) | |
| spectral_analysis = analyzer.analyze_principal_cognitive_modes() | |
| for layer_name, layer_analysis in spectral_analysis.items(): | |
| print(f"\n{layer_name.upper()}:") | |
| print(f" Principal modes: {layer_analysis['n_principal_modes']}") | |
| print(f" Spectral entropy: {layer_analysis['spectral_entropy']:.4f}") | |
| print(f" Effective rank: {layer_analysis['effective_rank']}") | |
| print(f" Information content (90%): {layer_analysis['information_content']:.4f}") | |
| print(f" Top-5 singular values: {[f'{sv:.3f}' for sv in layer_analysis['singular_values'][:5]]}") | |
| # Generate synthetic cognitive tasks | |
| n_tasks = 8 | |
| task_vectors = [torch.randn(16) for _ in range(n_tasks)] | |
| target_vectors = [torch.randn(16) for _ in range(n_tasks)] | |
| print("\n" + "=" * 80) | |
| print("LAYER RESPONSE COMPARISON") | |
| print("=" * 80) | |
| response_comparisons = [] | |
| for task_idx, task in enumerate(task_vectors): | |
| comparison = analyzer.layer_response_comparison(task) | |
| response_comparisons.append(comparison) | |
| if task_idx < 2: # Show first 2 | |
| print(f"\nTask {task_idx}:") | |
| for layer_name, metrics in comparison.items(): | |
| print(f" {layer_name}:") | |
| for metric_name, value in metrics.items(): | |
| print(f" {metric_name}: {value:.4f}") | |
| # Curriculum learning | |
| print("\n" + "=" * 80) | |
| print("CURRICULUM LEARNING TRAINING") | |
| print("=" * 80) | |
| trainer = CurriculumAIRESTrainer(aires, learning_rate=0.01) | |
| training_results = trainer.train_with_curriculum( | |
| task_vectors, | |
| target_vectors, | |
| stage_steps=[30, 30, 40] | |
| ) | |
| # Summary | |
| print("\n" + "=" * 80) | |
| print("TRAINING SUMMARY") | |
| print("=" * 80) | |
| for stage, losses in enumerate(training_results['stage_losses']): | |
| if losses: | |
| print(f"\nStage {stage}:") | |
| print(f" Initial loss: {losses[0]:.6f}") | |
| print(f" Final loss: {losses[-1]:.6f}") | |
| print(f" Reduction: {(1 - losses[-1]/losses[0]):.2%}") | |
| print(f" Average loss: {np.mean(losses):.6f}") | |
| # Final system state | |
| print("\n" + "=" * 80) | |
| print("FINAL SYSTEM STATE") | |
| print("=" * 80) | |
| print(aires.get_system_state()) | |
| return aires, analyzer, trainer | |
| if __name__ == "__main__": | |
| aires, analyzer, trainer = run_comprehensive_demonstration() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Many that have studied robin birds have cognitive inclination that they whistle t-e-b-o but are actually whistling c-e-c-i-l if you partition the chords separately. The above discrete b-spline nn is very smart at learning to play checkers reversed, then forward in a three dimensional reasoning task checker strategy.