-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
480 lines (384 loc) · 14.7 KB
/
model.py
File metadata and controls
480 lines (384 loc) · 14.7 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
477
478
479
480
"""
Causal Time Series Transformer for Crypto Trading
===================================================
Implements a Transformer with CAUSAL MASKING to prevent lookahead bias.
Key Anti-Cheating Measures:
1. Causal attention mask: each position can only attend to previous positions
2. No future information leakage in positional encoding
3. Proper temporal ordering in input sequences
FIXES:
- Added gradient checkpointing for memory efficiency
- Better weight initialization
- More robust causal masking verification
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Tuple
from config import Config, ModelConfig
class PositionalEncoding(nn.Module):
"""
Sinusoidal Positional Encoding.
Encodes position information without any future knowledge.
Position 0 is the oldest data point, position T-1 is the most recent.
"""
def __init__(self, d_model: int, max_len: int = 512, dropout: float = 0.1):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
# Create positional encoding matrix
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
if d_model % 2 == 1:
pe[:, 1::2] = torch.cos(position * div_term[:-1])
else:
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0) # (1, max_len, d_model)
self.register_buffer('pe', pe)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Args:
x: (batch_size, seq_len, d_model)
Returns:
x + positional encoding
"""
seq_len = x.size(1)
x = x + self.pe[:, :seq_len, :]
return self.dropout(x)
class CausalSelfAttention(nn.Module):
"""
Multi-Head Self-Attention with CAUSAL MASKING.
CRITICAL: The causal mask ensures that position i can only attend
to positions j <= i. This prevents any lookahead bias.
"""
def __init__(self, d_model: int, nhead: int, dropout: float = 0.1):
super().__init__()
assert d_model % nhead == 0, "d_model must be divisible by nhead"
self.d_model = d_model
self.nhead = nhead
self.head_dim = d_model // nhead
self.qkv_proj = nn.Linear(d_model, 3 * d_model)
self.out_proj = nn.Linear(d_model, d_model)
self.dropout = nn.Dropout(dropout)
# Scale factor for attention
self.scale = self.head_dim ** -0.5
# Register a buffer for the causal mask (will be resized as needed)
self.register_buffer('causal_mask', None, persistent=False)
def _get_causal_mask(self, seq_len: int, device: torch.device) -> torch.Tensor:
"""
Get or create causal mask.
Cached for efficiency.
"""
if self.causal_mask is None or self.causal_mask.size(0) < seq_len:
# Upper triangular matrix (excluding diagonal) = future positions
mask = torch.triu(torch.ones(seq_len, seq_len, device=device), diagonal=1)
self.causal_mask = mask.bool()
return self.causal_mask[:seq_len, :seq_len]
def forward(
self,
x: torch.Tensor,
mask: Optional[torch.Tensor] = None,
causal: bool = True
) -> torch.Tensor:
"""
Args:
x: (batch_size, seq_len, d_model)
mask: Optional attention mask
causal: Whether to apply causal masking
Returns:
Output: (batch_size, seq_len, d_model)
"""
batch_size, seq_len, _ = x.shape
# Project to Q, K, V
qkv = self.qkv_proj(x)
qkv = qkv.reshape(batch_size, seq_len, 3, self.nhead, self.head_dim)
qkv = qkv.permute(2, 0, 3, 1, 4) # (3, batch, nhead, seq, head_dim)
q, k, v = qkv[0], qkv[1], qkv[2]
# Compute attention scores
attn_scores = torch.matmul(q, k.transpose(-2, -1)) * self.scale
# Apply causal mask
if causal:
causal_mask = self._get_causal_mask(seq_len, x.device)
attn_scores = attn_scores.masked_fill(
causal_mask.unsqueeze(0).unsqueeze(0),
float('-inf')
)
# Apply additional mask if provided
if mask is not None:
attn_scores = attn_scores.masked_fill(
mask.unsqueeze(1).unsqueeze(2),
float('-inf')
)
# Softmax and dropout
attn_weights = F.softmax(attn_scores, dim=-1)
attn_weights = self.dropout(attn_weights)
# Apply attention to values
out = torch.matmul(attn_weights, v)
out = out.transpose(1, 2).reshape(batch_size, seq_len, self.d_model)
out = self.out_proj(out)
return out
class TransformerEncoderBlock(nn.Module):
"""
Transformer Encoder Block with Causal Self-Attention.
"""
def __init__(
self,
d_model: int,
nhead: int,
dim_feedforward: int,
dropout: float = 0.1
):
super().__init__()
self.self_attn = CausalSelfAttention(d_model, nhead, dropout)
# Feed-forward network
self.ffn = nn.Sequential(
nn.Linear(d_model, dim_feedforward),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(dim_feedforward, d_model),
nn.Dropout(dropout)
)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x: torch.Tensor, causal: bool = True) -> torch.Tensor:
"""
Args:
x: (batch_size, seq_len, d_model)
causal: Whether to use causal masking
Returns:
Output: (batch_size, seq_len, d_model)
"""
# Self-attention with residual (Pre-LN variant)
x_norm = self.norm1(x)
attn_out = self.self_attn(x_norm, causal=causal)
x = x + self.dropout(attn_out)
# FFN with residual
x_norm = self.norm2(x)
ffn_out = self.ffn(x_norm)
x = x + ffn_out
return x
class CausalTimeSeriesTransformer(nn.Module):
"""
Causal Time Series Transformer for Trading Signal Classification.
Architecture:
1. Input Projection: Map features to d_model dimensions
2. Positional Encoding: Add temporal information
3. Transformer Encoder: Stack of causal attention blocks
4. Output Head: Classify trading signal
ANTI-LOOKAHEAD MEASURES:
- Causal masking in all attention layers
- Output at position t only depends on positions 0 to t
- For prediction, we use the output at the LAST position only
"""
def __init__(self, config: ModelConfig):
super().__init__()
self.config = config
# Input projection
self.input_proj = nn.Sequential(
nn.Linear(config.input_dim, config.d_model),
nn.LayerNorm(config.d_model),
nn.GELU(),
nn.Dropout(config.dropout)
)
# Positional encoding
self.pos_encoder = PositionalEncoding(
d_model=config.d_model,
max_len=config.max_seq_len,
dropout=config.dropout
)
# Transformer encoder layers
self.encoder_layers = nn.ModuleList([
TransformerEncoderBlock(
d_model=config.d_model,
nhead=config.nhead,
dim_feedforward=config.dim_feedforward,
dropout=config.dropout
)
for _ in range(config.num_encoder_layers)
])
# Final layer norm
self.final_norm = nn.LayerNorm(config.d_model)
# Output head
self.output_head = nn.Sequential(
nn.Linear(config.d_model, config.d_model // 2),
nn.GELU(),
nn.Dropout(config.dropout),
nn.Linear(config.d_model // 2, config.output_dim)
)
# Initialize weights
self._init_weights()
def _init_weights(self):
"""Initialize weights with Xavier/Glorot initialization."""
for module in self.modules():
if isinstance(module, nn.Linear):
nn.init.xavier_uniform_(module.weight)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.LayerNorm):
nn.init.ones_(module.weight)
nn.init.zeros_(module.bias)
def forward(
self,
x: torch.Tensor,
return_all_positions: bool = False
) -> torch.Tensor:
"""
Forward pass with causal attention.
Args:
x: Input features (batch_size, seq_len, input_dim)
return_all_positions: If True, return outputs for all positions
Returns:
If return_all_positions:
(batch_size, seq_len, output_dim)
Else:
(batch_size, output_dim) - output at last position only
"""
# Input projection
x = self.input_proj(x)
# Add positional encoding
x = self.pos_encoder(x)
# Pass through encoder layers with causal masking
for layer in self.encoder_layers:
x = layer(x, causal=self.config.causal)
# Final layer norm
x = self.final_norm(x)
# Output projection
logits = self.output_head(x)
if return_all_positions:
return logits
else:
return logits[:, -1, :]
def predict(
self,
x: torch.Tensor,
return_confidence: bool = True
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
"""
Make predictions with optional confidence scores.
Args:
x: Input features (batch_size, seq_len, input_dim)
return_confidence: Whether to return confidence scores
Returns:
predictions: (batch_size,) - predicted class
confidences: (batch_size,) - confidence scores (if requested)
"""
self.eval()
with torch.no_grad():
logits = self.forward(x, return_all_positions=False)
probs = F.softmax(logits, dim=-1)
predictions = torch.argmax(probs, dim=-1)
if return_confidence:
confidences = torch.max(probs, dim=-1)[0]
return predictions, confidences
return predictions, None
class TradingSignalLoss(nn.Module):
"""
Custom loss function for trading signals.
Combines:
1. Cross-entropy for classification
2. Optional class weighting (to handle imbalanced labels)
3. Optional label smoothing
"""
def __init__(
self,
class_weights: Optional[torch.Tensor] = None,
label_smoothing: float = 0.0
):
super().__init__()
self.class_weights = class_weights
self.label_smoothing = label_smoothing
def forward(
self,
logits: torch.Tensor,
targets: torch.Tensor
) -> torch.Tensor:
"""
Args:
logits: (batch_size, num_classes)
targets: (batch_size,)
Returns:
Scalar loss
"""
if self.class_weights is not None:
weights = self.class_weights.to(logits.device)
else:
weights = None
loss = F.cross_entropy(
logits,
targets,
weight=weights,
label_smoothing=self.label_smoothing
)
return loss
def count_parameters(model: nn.Module) -> int:
"""Count trainable parameters."""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def verify_causal_masking(
model: CausalTimeSeriesTransformer,
seq_len: int = 10,
num_tests: int = 5
) -> bool:
"""
Verify that causal masking is working correctly.
Test: Changing future inputs should NOT affect current outputs.
Args:
model: The model to test
seq_len: Sequence length for testing
num_tests: Number of random tests to run
Returns:
True if causal masking is working correctly
"""
model.eval()
device = next(model.parameters()).device
batch_size = 2
input_dim = model.config.input_dim
all_passed = True
for test_idx in range(num_tests):
# Create test input
x = torch.randn(batch_size, seq_len, input_dim, device=device)
# Test at different positions
for test_pos in range(1, seq_len - 1):
# Get output at test position
with torch.no_grad():
out1 = model(x, return_all_positions=True)[:, test_pos, :].clone()
# Modify positions after test_pos (future positions)
x_modified = x.clone()
x_modified[:, test_pos + 1:, :] = torch.randn(
batch_size, seq_len - test_pos - 1, input_dim, device=device
)
# Get output at test position again
with torch.no_grad():
out2 = model(x_modified, return_all_positions=True)[:, test_pos, :]
# Check if outputs are the same
is_same = torch.allclose(out1, out2, atol=1e-5)
if not is_same:
max_diff = (out1 - out2).abs().max().item()
print(f"✗ Test {test_idx + 1}, pos {test_pos}: FAILED (max diff: {max_diff:.6f})")
all_passed = False
if all_passed:
print("✓ Causal masking verified: Future changes don't affect past outputs")
else:
print("✗ WARNING: Causal masking may not be working correctly!")
return all_passed
if __name__ == "__main__":
from config import Config
config = Config()
config.model.input_dim = 22 # Example feature count
model = CausalTimeSeriesTransformer(config.model)
print(f"Model parameters: {count_parameters(model):,}")
# Test forward pass
batch_size = 4
seq_len = 168
x = torch.randn(batch_size, seq_len, config.model.input_dim)
out = model(x)
print(f"Input shape: {x.shape}")
print(f"Output shape: {out.shape}")
# Verify causal masking
verify_causal_masking(model, seq_len=20, num_tests=3)
# Test prediction
preds, confs = model.predict(x)
print(f"Predictions: {preds}")
print(f"Confidences: {confs}")