-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor scalers.py and add tests for periodic and ple
- Loading branch information
Artem Sakhno
committed
Jan 27, 2025
1 parent
9561b6d
commit 2c1673c
Showing
2 changed files
with
101 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import torch | ||
|
||
from ptls.data_load.padded_batch import PaddedBatch | ||
from ptls.nn.trx_encoder import TrxEncoder | ||
|
||
from ptls.nn.trx_encoder.scalers import Periodic, PeriodicMLP, PLE, PLE_MLP | ||
|
||
|
||
def test_periodic(): | ||
B, T = 5, 20 | ||
num_periods = 4 | ||
scaler = Periodic(num_periods = num_periods, param_dist_sigma = 3) | ||
trx_encoder = TrxEncoder( | ||
numeric_values={'amount': scaler}, | ||
) | ||
x = PaddedBatch( | ||
payload={ | ||
'amount': torch.randn(B, T), | ||
}, | ||
length=torch.randint(10, 20, (B,)), | ||
) | ||
z = trx_encoder(x) | ||
assert z.payload.shape == (5, 20, 2 * num_periods) # B, T, H | ||
assert trx_encoder.output_size == 2 * num_periods | ||
|
||
|
||
def test_periodic_mlp(): | ||
B, T = 5, 20 | ||
num_periods = 4 | ||
mlp_output_size = 32 | ||
scaler = PeriodicMLP(num_periods = num_periods, param_dist_sigma = 3, mlp_output_size = mlp_output_size) | ||
trx_encoder = TrxEncoder( | ||
numeric_values={'amount': scaler}, | ||
) | ||
x = PaddedBatch( | ||
payload={ | ||
'amount': torch.randn(B, T), | ||
}, | ||
length=torch.randint(10, 20, (B,)), | ||
) | ||
z = trx_encoder(x) | ||
assert z.payload.shape == (5, 20, mlp_output_size) # B, T, H | ||
assert trx_encoder.output_size == mlp_output_size | ||
|
||
|
||
|
||
def test_ple(): | ||
B, T = 5, 20 | ||
bins = [-1, 0, 1] | ||
scaler = PLE(bins = bins) | ||
trx_encoder = TrxEncoder( | ||
numeric_values={'amount': scaler}, | ||
) | ||
x = PaddedBatch( | ||
payload={ | ||
'amount': torch.randn(B, T), | ||
}, | ||
length=torch.randint(10, 20, (B,)), | ||
) | ||
z = trx_encoder(x) | ||
assert z.payload.shape == (5, 20, len(bins) - 1) # B, T, H | ||
assert trx_encoder.output_size == len(bins) - 1 | ||
|
||
|
||
def test_ple_mlp(): | ||
B, T = 5, 20 | ||
bins = [-1, 0, 1] | ||
mlp_output_size = 64 | ||
scaler = PLE_MLP(bins = bins, mlp_output_size = mlp_output_size) | ||
trx_encoder = TrxEncoder( | ||
numeric_values={'amount': scaler}, | ||
) | ||
x = PaddedBatch( | ||
payload={ | ||
'amount': torch.randn(B, T), | ||
}, | ||
length=torch.randint(10, 20, (B,)), | ||
) | ||
z = trx_encoder(x) | ||
assert z.payload.shape == (5, 20, mlp_output_size) # B, T, H | ||
assert trx_encoder.output_size == mlp_output_size | ||
|