54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
import sys, torch
|
|
sys.path.insert(0, 'C:/Users/yilv/Desktop/JEPA')
|
|
|
|
from model_jepa import CWT_JEPA
|
|
|
|
torch.manual_seed(42)
|
|
m = CWT_JEPA(
|
|
signal_len=1000, cwt_scales=64,
|
|
patch_size_time=25, patch_size_freq=8,
|
|
embed_dim=384, depth=4, num_heads=6,
|
|
predictor_depth=3, predictor_num_heads=6,
|
|
mask_ratio=0.75,
|
|
)
|
|
|
|
print(f'Params: {sum(p.numel() for p in m.parameters())/1e6:.2f}M')
|
|
|
|
x = torch.randn(2, 1000)
|
|
ch = torch.zeros(2, dtype=torch.long)
|
|
st = torch.randn(2, 16)
|
|
|
|
print('Forward pass...')
|
|
tl, ld, sxp, ps, sx, sy, syh, tp = m(x, ch, stats_target=st, mask_ratio=0.75)
|
|
print(f'Total Loss: {tl.item():.4f}')
|
|
print(f' JEPA: {ld["loss_jepa"]:.4f}')
|
|
print(f' SIGReg: {ld["loss_sigreg"]:.4f}')
|
|
print(f' Stats: {ld["loss_stats"]:.4f}')
|
|
print(f' EMA momentum: {ld["ema_momentum"]:.4f}')
|
|
print(f'Shapes:')
|
|
print(f' s_x: {list(sx.shape)}')
|
|
print(f' s_y: {list(sy.shape)}')
|
|
print(f' s_y_hat: {list(syh.shape)}')
|
|
print(f' s_x_pooled: {list(sxp.shape)}')
|
|
print(f' pred_stats: {list(ps.shape)}')
|
|
|
|
# EMA check
|
|
cp = next(m.context_blocks.parameters())
|
|
tp_param = next(m.target_blocks.parameters())
|
|
print(f'EMA diff: {(cp - tp_param).abs().mean().item():.6f}')
|
|
|
|
# Backward
|
|
tl.backward()
|
|
print('Backward: OK')
|
|
|
|
# Inference
|
|
print('Inference (mask=0)...')
|
|
tl2, ld2, sxp2, ps2, sx2, sy2, syh2, _ = m(x, ch, stats_target=st, mask_ratio=0.0)
|
|
print(f' s_x: {list(sx2.shape)}')
|
|
|
|
# Encoder only
|
|
se, sp, ps3 = m.forward_encoder_with_stats(x, ch)
|
|
print(f'Encoder only: s={list(se.shape)}, pooled={list(sp.shape)}')
|
|
|
|
print('ALL TESTS PASSED')
|