-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhrm_comp.py
More file actions
330 lines (253 loc) · 10.6 KB
/
Copy pathhrm_comp.py
File metadata and controls
330 lines (253 loc) · 10.6 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import random
class SimpleExpandedDataset:
"""Take the working simple dataset and add just a few more patterns"""
def create_batch(self, batch_size=32):
inputs = []
targets = []
pattern_types = []
for i in range(batch_size):
pattern_choice = random.random()
if pattern_choice < 0.4: # 40% arithmetic (what worked)
start = random.randint(1, 5)
diff = random.randint(1, 3)
seq = [start + j * diff for j in range(7)]
next_val = start + 7 * diff
pattern_type = "arithmetic"
elif pattern_choice < 0.7: # 30% fibonacci (what worked)
a, b = random.randint(1, 3), random.randint(1, 3)
seq = [a, b]
for j in range(5):
seq.append(seq[-1] + seq[-2])
next_val = seq[-1] + seq[-2]
pattern_type = "fibonacci"
elif pattern_choice < 0.85: # 15% simple geometric (×2 only)
start = 1 # Always start with 1 to keep numbers small
seq = [start * (2 ** j) for j in range(7)] # 1,2,4,8,16,32,64
next_val = start * (2 ** 7) # 128
pattern_type = "geometric"
else: # 15% simple squares
start = 1 # Always start with 1^2
seq = [(start + j) ** 2 for j in range(7)] # 1,4,9,16,25,36,49
next_val = (start + 7) ** 2 # 64
pattern_type = "squares"
inputs.append(seq)
targets.append(next_val)
pattern_types.append(pattern_type)
return torch.tensor(inputs, dtype=torch.float32), torch.tensor(targets, dtype=torch.float32), pattern_types
class WorkingHRMPlus(nn.Module):
"""Keep the EXACT same architecture that worked, just better training"""
def __init__(self):
super().__init__()
# EXACT same architecture that worked before
self.encoder = nn.Sequential(
nn.Linear(7, 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU()
)
# Engineer (low-level) - SAME
self.engineer = nn.Sequential(
nn.Linear(128, 64), # state + strategy
nn.ReLU(),
nn.Linear(64, 64)
)
# Architect (high-level) - SAME
self.architect = nn.Sequential(
nn.Linear(128, 64), # prev + curr
nn.ReLU(),
nn.Linear(64, 64)
)
# Halt predictor - SAME
self.halt = nn.Linear(64, 1)
# Simple decoder - SAME
self.decoder = nn.Sequential(
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 1)
)
# Strategy init - SAME
self.strategy = nn.Parameter(torch.zeros(1, 64))
print(f"Working HRM Plus: {sum(p.numel() for p in self.parameters()):,} parameters")
def forward(self, x, verbose=False):
batch_size = x.size(0)
if verbose:
print(f"\n🧠 Working HRM Plus Forward Pass")
print(f"Input: {x[0].int().tolist()}")
# Encode input - SAME
state = self.encoder(x)
strategy = self.strategy.expand(batch_size, -1)
halt_probs = []
prev_state = state.clone()
# Simple reasoning loop - SAME
for cycle in range(6): # Fixed cycles like before
if verbose:
print(f"\n🔄 Cycle {cycle+1}")
pre_norm = torch.norm(state).item()
# Engineer: process with strategy - SAME
engineer_input = torch.cat([state, strategy], dim=-1)
state = self.engineer(engineer_input)
if verbose:
post_norm = torch.norm(state).item()
print(f" 🔧 Engineer: {pre_norm:.2f} -> {post_norm:.2f}")
# Architect: update strategy - SAME
architect_input = torch.cat([prev_state, state], dim=-1)
strategy = self.architect(architect_input)
if verbose:
strat_norm = torch.norm(strategy).item()
print(f" 🏛️ Architect: strategy norm {strat_norm:.2f}")
# Halt decision - SAME
halt_prob = torch.sigmoid(self.halt(strategy))
halt_probs.append(halt_prob)
if verbose:
print(f" 🎯 Halt prob: {halt_prob.mean().item():.3f}")
prev_state = state.clone()
# Decode to prediction - SAME
prediction = self.decoder(state)
if verbose:
print(f"📤 Prediction: {prediction[0].item():.1f}")
return {
'prediction': prediction,
'halt_probs': torch.stack(halt_probs),
'num_cycles': 6
}
def train_working_plus():
"""Train with the EXACT same method that worked"""
print("🎯 Training Working HRM Plus")
print("=" * 40)
model = WorkingHRMPlus()
dataset = SimpleExpandedDataset()
# EXACT same training setup that worked
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01) # Same LR
print("Training on expanded patterns...")
# Show examples
inputs, targets, types = dataset.create_batch(8)
print("Training examples:")
for i in range(8):
seq = inputs[i].int().tolist()
target = int(targets[i].item())
pattern = types[i]
print(f" {pattern.upper()}: {seq} -> {target}")
print("\nTraining progress:")
for epoch in range(1500): # More epochs for more patterns
inputs, targets, _ = dataset.create_batch(32)
optimizer.zero_grad()
results = model(inputs)
loss = criterion(results['prediction'].squeeze(), targets)
loss.backward()
optimizer.step()
if epoch % 300 == 0:
print(f"Epoch {epoch}: Loss {loss.item():.4f}")
print("Training done!")
return model, dataset
def test_working_plus(model, dataset):
"""Test on expanded patterns"""
print("\n🧮 Testing Working HRM Plus")
print("=" * 40)
test_cases = [
# Original working patterns
([2, 4, 6, 8, 10, 12, 14], 16, "Arithmetic +2"),
([1, 3, 5, 7, 9, 11, 13], 15, "Arithmetic +2"),
([1, 1, 2, 3, 5, 8, 13], 21, "Fibonacci"),
([2, 3, 5, 8, 13, 21, 34], 55, "Fibonacci"),
# New patterns
([1, 2, 4, 8, 16, 32, 64], 128, "Geometric ×2"),
([1, 4, 9, 16, 25, 36, 49], 64, "Squares"),
]
model.eval()
correct = 0
arithmetic_cycles = []
fibonacci_cycles = []
geometric_cycles = []
square_cycles = []
with torch.no_grad():
for i, (seq, expected, desc) in enumerate(test_cases):
input_tensor = torch.tensor([seq], dtype=torch.float32)
result = model(input_tensor, verbose=(i==0)) # Verbose for first one
predicted = result['prediction'].item()
error = abs(predicted - expected)
cycles = result['num_cycles']
print(f"\nTest {i+1}: {desc}")
print(f"Input: {seq}")
print(f"Expected: {expected}, Predicted: {predicted:.1f}")
print(f"Error: {error:.1f}, Cycles: {cycles}")
if error <= 3: # Allow some error
correct += 1
print("✅ PASS")
else:
print("❌ FAIL")
# Track cycles by pattern type
if "Arithmetic" in desc:
arithmetic_cycles.append(cycles)
elif "Fibonacci" in desc:
fibonacci_cycles.append(cycles)
elif "Geometric" in desc:
geometric_cycles.append(cycles)
elif "Squares" in desc:
square_cycles.append(cycles)
accuracy = correct / len(test_cases) * 100
print(f"\nOverall Accuracy: {correct}/{len(test_cases)} ({accuracy:.1f}%)")
# Pattern-specific analysis
pattern_cycles = {
"Arithmetic": arithmetic_cycles,
"Fibonacci": fibonacci_cycles,
"Geometric": geometric_cycles,
"Squares": square_cycles
}
print(f"\n📊 Cycles by Pattern Type:")
for pattern, cycles in pattern_cycles.items():
if cycles:
avg_cycles = np.mean(cycles)
print(f" {pattern}: {avg_cycles:.1f} cycles average")
return accuracy
def demonstrate_expanded_reasoning(model):
"""Show reasoning on different pattern types"""
print(f"\n🔍 Expanded Reasoning Demonstration")
print("=" * 50)
examples = [
([1, 1, 2, 3, 5, 8, 13], 21, "Fibonacci"),
([1, 2, 4, 8, 16, 32, 64], 128, "Geometric ×2"),
([1, 4, 9, 16, 25, 36, 49], 64, "Perfect Squares"),
]
model.eval()
for seq, expected, desc in examples:
print(f"\n🧮 {desc}: {seq} -> ?")
print(f"Expected: {expected}")
input_tensor = torch.tensor([seq], dtype=torch.float32)
with torch.no_grad():
result = model(input_tensor, verbose=True)
predicted = result['prediction'].item()
accuracy = "🎯 CORRECT!" if abs(predicted - expected) <= 3 else "❌ WRONG"
print(f"Result: {accuracy}")
def main():
"""Run expanded pattern learning"""
print("🌟 WORKING HRM PLUS - EXPANDED PATTERNS")
print("=" * 60)
# Train on expanded patterns
model, dataset = train_working_plus()
# Test expanded patterns
accuracy = test_working_plus(model, dataset)
# Show reasoning on different patterns
demonstrate_expanded_reasoning(model)
# Summary
total_params = sum(p.numel() for p in model.parameters())
print(f"\n🏆 SUMMARY:")
print(f"Accuracy: {accuracy:.1f}%")
print(f"Parameters: {total_params:,}")
print(f"Hierarchical Reasoning: ✅")
print(f"Pattern Variety: ✅")
if accuracy >= 80:
print("🎉 EXCELLENT! Expanded patterns working!")
elif accuracy >= 60:
print("✅ GOOD! Solid pattern recognition!")
elif accuracy >= 40:
print("📈 PROGRESS! Some patterns learned!")
else:
print("🔧 FOUNDATION! Architecture solid, needs tuning!")
return model
if __name__ == "__main__":
model = main()