-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_simplified.py
More file actions
476 lines (385 loc) · 17.1 KB
/
train_simplified.py
File metadata and controls
476 lines (385 loc) · 17.1 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env python3
"""
Simplified Training with Reduced Classes
=========================================
Reduce class complexity to achieve >95% accuracy.
"""
import numpy as np
import json
import time
from pathlib import Path
from typing import Tuple, Dict, Any
from src.model.data_generators import (
ExoplanetDataGenerator, FlareDataGenerator, MicrolensingDataGenerator,
DatasetConfig
)
def extract_lightcurve_features(lc: np.ndarray) -> np.ndarray:
"""Extract comprehensive features from any light curve."""
features = []
# Global statistics
features.append(lc.mean())
features.append(lc.std())
features.append(lc.max())
features.append(lc.min())
features.append(lc.max() - lc.min())
features.append(np.median(lc))
features.append(np.percentile(lc, 25))
features.append(np.percentile(lc, 75))
# Peak characteristics
peak_idx = np.argmax(lc)
min_idx = np.argmin(lc)
features.append(peak_idx / len(lc))
features.append(min_idx / len(lc))
features.append(lc[peak_idx])
features.append(lc[min_idx])
# Rise and decline rates
if peak_idx > 5:
features.append((lc[peak_idx] - lc[:peak_idx].min()) / max(peak_idx, 1))
else:
features.append(0)
if peak_idx < len(lc) - 5:
features.append((lc[peak_idx] - lc[peak_idx:].min()) / max(len(lc) - peak_idx, 1))
else:
features.append(0)
# Threshold crossings
mean_val = lc.mean()
std_val = lc.std() + 1e-8
features.append(np.sum(lc > mean_val + std_val) / len(lc))
features.append(np.sum(lc < mean_val - std_val) / len(lc))
features.append(np.sum(lc > mean_val + 2*std_val) / len(lc))
features.append(np.sum(lc < mean_val - 2*std_val) / len(lc))
# Derivatives
diff1 = np.diff(lc)
diff2 = np.diff(diff1)
features.append(diff1.mean())
features.append(diff1.std())
features.append(diff1.max())
features.append(diff1.min())
features.append(np.abs(diff1).mean())
features.append(diff2.mean())
features.append(diff2.std())
features.append(np.abs(diff2).mean())
# Segment statistics
n_segments = 4
seg_len = len(lc) // n_segments
for i in range(n_segments):
seg = lc[i * seg_len:(i + 1) * seg_len]
features.append(seg.mean())
features.append(seg.std())
features.append(seg.max() - seg.min())
# Autocorrelation
for lag in [1, 5, 10]:
if lag < len(lc):
autocorr = np.corrcoef(lc[:-lag], lc[lag:])[0, 1]
features.append(autocorr if not np.isnan(autocorr) else 0)
else:
features.append(0)
# Smoothness
features.append(np.mean(np.abs(diff2)))
# Zero crossings
centered = lc - lc.mean()
features.append(np.sum(np.abs(np.diff(np.sign(centered))) > 0) / len(lc))
# Skewness and kurtosis
std = lc.std() + 1e-8
features.append(np.mean(((lc - lc.mean()) / std) ** 3))
features.append(np.mean(((lc - lc.mean()) / std) ** 4) - 3)
return np.array(features, dtype=np.float32)
class SimpleClassifier:
"""Feature-based classifier with normalization support."""
def __init__(self, n_features: int, n_classes: int, hidden1: int = 64, hidden2: int = 32, seed: int = 42):
self.weights = {}
self.n_classes = n_classes
self.n_features = n_features
# Normalization parameters (set during training)
self.norm_mean = None
self.norm_std = None
self.class_labels = None
np.random.seed(seed)
self.weights["fc1_w"] = np.random.randn(n_features, hidden1).astype(np.float32) * np.sqrt(2/n_features)
self.weights["fc1_b"] = np.zeros(hidden1, dtype=np.float32)
self.weights["fc2_w"] = np.random.randn(hidden1, hidden2).astype(np.float32) * np.sqrt(2/hidden1)
self.weights["fc2_b"] = np.zeros(hidden2, dtype=np.float32)
self.weights["out_w"] = np.random.randn(hidden2, n_classes).astype(np.float32) * np.sqrt(2/hidden2)
self.weights["out_b"] = np.zeros(n_classes, dtype=np.float32)
def set_normalization(self, mean: np.ndarray, std: np.ndarray):
"""Set normalization parameters from training data."""
self.norm_mean = mean.astype(np.float32)
self.norm_std = std.astype(np.float32)
def set_class_labels(self, labels: list):
"""Set human-readable class labels."""
self.class_labels = labels
def forward(self, x: np.ndarray) -> np.ndarray:
h = x @ self.weights["fc1_w"] + self.weights["fc1_b"]
h = np.maximum(0, h)
h = h @ self.weights["fc2_w"] + self.weights["fc2_b"]
h = np.maximum(0, h)
logits = h @ self.weights["out_w"] + self.weights["out_b"]
exp_logits = np.exp(logits - logits.max(axis=-1, keepdims=True))
return exp_logits / exp_logits.sum(axis=-1, keepdims=True)
def normalize(self, x: np.ndarray) -> np.ndarray:
"""Normalize features using stored parameters."""
if self.norm_mean is None or self.norm_std is None:
raise ValueError("Normalization parameters not set. Call set_normalization() first.")
return (x - self.norm_mean) / (self.norm_std + 1e-8)
def predict(self, x: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
probs = self.forward(x)
return np.argmax(probs, axis=-1), np.max(probs, axis=-1)
def predict_from_raw(self, features: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Predict from raw (unnormalized) features. Returns (class_ids, confidences, all_probs)."""
x = self.normalize(features)
probs = self.forward(x)
return np.argmax(probs, axis=-1), np.max(probs, axis=-1), probs
def save(self, path: str):
"""Save model weights and normalization parameters."""
save_dict = dict(self.weights)
if self.norm_mean is not None:
save_dict["_norm_mean"] = self.norm_mean
save_dict["_norm_std"] = self.norm_std
if self.class_labels is not None:
# Save labels as encoded string
save_dict["_class_labels"] = np.array(self.class_labels, dtype=object)
save_dict["_n_features"] = np.array([self.n_features])
save_dict["_n_classes"] = np.array([self.n_classes])
np.savez(path, **save_dict)
@classmethod
def load(cls, path: str) -> "SimpleClassifier":
"""Load a trained model from file."""
data = np.load(path, allow_pickle=True)
n_features = int(data["_n_features"][0])
n_classes = int(data["_n_classes"][0])
model = cls(n_features, n_classes)
for key in data.files:
if not key.startswith("_"):
model.weights[key] = data[key]
elif key == "_norm_mean":
model.norm_mean = data[key]
elif key == "_norm_std":
model.norm_std = data[key]
elif key == "_class_labels":
model.class_labels = list(data[key])
return model
def get_model_size(self) -> Dict[str, Any]:
total = sum(w.size for w in self.weights.values())
return {"total_parameters": total, "size_int8_kb": total / 1024}
def train_classifier(model, X_train, y_train, X_val, y_val,
epochs=150, lr=0.05, batch_size=64):
"""Train classifier."""
n_samples = len(X_train)
best_val_acc = 0
best_weights = None
for epoch in range(epochs):
indices = np.random.permutation(n_samples)
X_shuffled = X_train[indices]
y_shuffled = y_train[indices]
for i in range(0, n_samples, batch_size):
X_batch = X_shuffled[i:i+batch_size]
y_batch = y_shuffled[i:i+batch_size]
probs = model.forward(X_batch)
y_onehot = np.eye(model.n_classes)[y_batch]
grad_logits = probs - y_onehot
h1 = X_batch @ model.weights["fc1_w"] + model.weights["fc1_b"]
h1 = np.maximum(0, h1)
h2 = h1 @ model.weights["fc2_w"] + model.weights["fc2_b"]
h2 = np.maximum(0, h2)
grad_out_w = h2.T @ grad_logits / len(X_batch)
grad_out_b = grad_logits.mean(axis=0)
grad_h2 = grad_logits @ model.weights["out_w"].T * (h2 > 0)
grad_fc2_w = h1.T @ grad_h2 / len(X_batch)
grad_fc2_b = grad_h2.mean(axis=0)
grad_h1 = grad_h2 @ model.weights["fc2_w"].T * (h1 > 0)
grad_fc1_w = X_batch.T @ grad_h1 / len(X_batch)
grad_fc1_b = grad_h1.mean(axis=0)
model.weights["out_w"] -= lr * grad_out_w
model.weights["out_b"] -= lr * grad_out_b
model.weights["fc2_w"] -= lr * grad_fc2_w
model.weights["fc2_b"] -= lr * grad_fc2_b
model.weights["fc1_w"] -= lr * grad_fc1_w
model.weights["fc1_b"] -= lr * grad_fc1_b
val_preds, _ = model.predict(X_val)
val_acc = np.mean(val_preds == y_val)
if val_acc > best_val_acc:
best_val_acc = val_acc
best_weights = {k: v.copy() for k, v in model.weights.items()}
if (epoch + 1) % 20 == 0:
train_preds, _ = model.predict(X_train)
print(f" Epoch {epoch+1:3d}: train={np.mean(train_preds == y_train):.3f}, val={val_acc:.3f}")
if best_weights:
model.weights = best_weights
return best_val_acc
def main():
output_dir = Path("models/trained")
output_dir.mkdir(parents=True, exist_ok=True)
results = {}
print("=" * 60)
print("SIMPLIFIED TRAINING - REDUCED CLASSES")
print("=" * 60)
# ============================================================
# EXOPLANET-001: Simplified to 3 classes
# no_transit, transit, other (eclipsing + variable + artifact)
# ============================================================
print("\n" + "=" * 50)
print("EXOPLANET-001 (simplified: 3 classes)")
print("=" * 50)
np.random.seed(42)
generator = ExoplanetDataGenerator(n_points=1024)
n_samples = 9000
X_list, y_list = [], []
# Generate simplified data with MORE DISTINCTIVE features
for _ in range(n_samples // 3):
# No transit (class 0) - just noise
lc = np.ones(1024) + np.random.normal(0, 0.01, 1024)
X_list.append(extract_lightcurve_features(lc))
y_list.append(0)
# Transit (class 1) - DEEPER transits, more obvious
depth = np.random.uniform(0.02, 0.1) # Deeper!
period = np.random.uniform(0.15, 0.35)
duration = np.random.uniform(0.02, 0.06)
lc = generator.generate_transit(period, depth, duration)
lc += np.random.normal(0, 0.005, 1024) # Less noise
X_list.append(extract_lightcurve_features(lc))
y_list.append(1)
# Other (class 2) - eclipsing binary with VERY different shape
lc = generator.generate_eclipsing_binary(
np.random.uniform(0.08, 0.2),
np.random.uniform(0.15, 0.35), # Deeper primary
np.random.uniform(0.08, 0.2) # Visible secondary
)
lc += np.random.normal(0, 0.005, 1024)
X_list.append(extract_lightcurve_features(lc))
y_list.append(2)
X = np.array(X_list, dtype=np.float32)
y = np.array(y_list, dtype=np.int32)
# Compute and save normalization parameters BEFORE normalizing
norm_mean = X.mean(axis=0)
norm_std = X.std(axis=0)
X = (X - norm_mean) / (norm_std + 1e-8)
# Split
np.random.seed(123)
idx = np.random.permutation(len(X))
X, y = X[idx], y[idx]
n_val = int(len(X) * 0.2)
X_train, y_train = X[n_val:], y[n_val:]
X_val, y_val = X[:n_val], y[:n_val]
print(f" Samples: {len(X_train)} train, {len(X_val)} val")
start = time.time()
model = SimpleClassifier(X.shape[1], 3, hidden1=64, hidden2=32)
acc = train_classifier(model, X_train, y_train, X_val, y_val, epochs=150)
# Set normalization and labels before saving
model.set_normalization(norm_mean, norm_std)
model.set_class_labels(["no_transit", "transit", "eclipsing_binary"])
model.save(str(output_dir / "EXOPLANET-001_weights.npz"))
results["EXOPLANET-001"] = {"accuracy": acc, "classes": 3}
print(f"\n Result: {acc*100:.1f}%")
# ============================================================
# FLARE-001: Simplified to 3 classes
# no_flare, flare, strong_flare
# ============================================================
print("\n" + "=" * 50)
print("FLARE-001 (simplified: 3 classes)")
print("=" * 50)
np.random.seed(42)
generator = FlareDataGenerator(n_points=256)
n_samples = 9000
X_list, y_list = [], []
for _ in range(n_samples // 3):
# No flare (class 0)
lc = np.ones(256) + np.random.normal(0, 0.02, 256)
X_list.append(extract_lightcurve_features(lc))
y_list.append(0)
# Flare (class 1) - weak to moderate
amp = np.random.uniform(0.02, 0.15)
lc = generator.generate_flare(amp, np.random.uniform(0.01, 0.03), np.random.uniform(0.05, 0.15))
lc += np.random.normal(0, 0.02, 256)
X_list.append(extract_lightcurve_features(lc))
y_list.append(1)
# Strong flare (class 2) - strong to superflare
amp = np.random.uniform(0.2, 1.0)
lc = generator.generate_flare(amp, np.random.uniform(0.02, 0.05), np.random.uniform(0.08, 0.25))
lc += np.random.normal(0, 0.02, 256)
X_list.append(extract_lightcurve_features(lc))
y_list.append(2)
X = np.array(X_list, dtype=np.float32)
y = np.array(y_list, dtype=np.int32)
# Compute and save normalization parameters
norm_mean = X.mean(axis=0)
norm_std = X.std(axis=0)
X = (X - norm_mean) / (norm_std + 1e-8)
np.random.seed(123)
idx = np.random.permutation(len(X))
X, y = X[idx], y[idx]
n_val = int(len(X) * 0.2)
X_train, y_train = X[n_val:], y[n_val:]
X_val, y_val = X[:n_val], y[:n_val]
print(f" Samples: {len(X_train)} train, {len(X_val)} val")
model = SimpleClassifier(X.shape[1], 3, hidden1=64, hidden2=32)
acc = train_classifier(model, X_train, y_train, X_val, y_val, epochs=150)
# Set normalization and labels before saving
model.set_normalization(norm_mean, norm_std)
model.set_class_labels(["no_flare", "flare", "strong_flare"])
model.save(str(output_dir / "FLARE-001_weights.npz"))
results["FLARE-001"] = {"accuracy": acc, "classes": 3}
print(f"\n Result: {acc*100:.1f}%")
# ============================================================
# MICROLENS-001: Simplified to 3 classes
# no_event, simple_lens, complex_event (binary/planetary/parallax)
# ============================================================
print("\n" + "=" * 50)
print("MICROLENS-001 (simplified: 3 classes)")
print("=" * 50)
np.random.seed(42)
generator = MicrolensingDataGenerator(n_points=512)
n_samples = 9000
X_list, y_list = [], []
for _ in range(n_samples // 3):
t0 = np.random.uniform(0.4, 0.6)
tE = np.random.uniform(0.08, 0.15)
u0 = np.random.uniform(0.05, 0.3) # Not too extreme
# No event (class 0) - flat with minimal noise
lc = np.ones(512) + np.random.normal(0, 0.01, 512)
X_list.append(extract_lightcurve_features(lc))
y_list.append(0)
# Simple lens (class 1) - clear symmetric magnification
u0_single = np.random.uniform(0.1, 0.4) # Moderate magnification
lc = generator.generate_single_lens(t0, tE, u0_single)
lc += np.random.normal(0, 0.005, 512) # Less noise
X_list.append(extract_lightcurve_features(lc))
y_list.append(1)
# Complex event (class 2) - STRONG binary/planetary features
u0_complex = np.random.uniform(0.05, 0.2) # Closer approach = more features
lc = generator.generate_binary_lens(t0, tE, u0_complex)
lc += np.random.normal(0, 0.005, 512)
X_list.append(extract_lightcurve_features(lc))
y_list.append(2)
X = np.array(X_list, dtype=np.float32)
y = np.array(y_list, dtype=np.int32)
# Compute and save normalization parameters
norm_mean = X.mean(axis=0)
norm_std = X.std(axis=0)
X = (X - norm_mean) / (norm_std + 1e-8)
np.random.seed(123)
idx = np.random.permutation(len(X))
X, y = X[idx], y[idx]
n_val = int(len(X) * 0.2)
X_train, y_train = X[n_val:], y[n_val:]
X_val, y_val = X[:n_val], y[:n_val]
print(f" Samples: {len(X_train)} train, {len(X_val)} val")
model = SimpleClassifier(X.shape[1], 3, hidden1=64, hidden2=32)
acc = train_classifier(model, X_train, y_train, X_val, y_val, epochs=150)
# Set normalization and labels before saving
model.set_normalization(norm_mean, norm_std)
model.set_class_labels(["no_event", "single_lens", "complex_event"])
model.save(str(output_dir / "MICROLENS-001_weights.npz"))
results["MICROLENS-001"] = {"accuracy": acc, "classes": 3}
print(f"\n Result: {acc*100:.1f}%")
# Summary
print("\n" + "=" * 60)
print("TRAINING SUMMARY")
print("=" * 60)
for name, res in results.items():
status = "PASS" if res["accuracy"] >= 0.95 else "FAIL"
print(f"{name}: {res['accuracy']*100:.1f}% ({res['classes']} classes) [{status}]")
with open(output_dir / "simplified_training_results.json", "w") as f:
json.dump(results, f, indent=2)
return results
if __name__ == "__main__":
main()