|
| 1 | +from pathlib import Path |
| 2 | +from typing import Tuple, Dict |
| 3 | + |
| 4 | +import optax |
| 5 | +from flax import nnx |
| 6 | +from flax.training.early_stopping import EarlyStopping |
| 7 | +from jax import Array |
| 8 | +from torch.utils.data import DataLoader |
| 9 | + |
| 10 | +from clax import PositionBasedModel |
| 11 | +from clax.datasets import ( |
| 12 | + BaiduUltrFeatureClickDataset, |
| 13 | + BaiduUltrFeatureAnnotationDataset, |
| 14 | +) |
| 15 | +from clax.trainer import Trainer |
| 16 | + |
| 17 | + |
| 18 | +def get_baidu_click_loader( |
| 19 | + dataset_dir: Path, |
| 20 | + session_range: Tuple[int, int], |
| 21 | +): |
| 22 | + dataset = BaiduUltrFeatureClickDataset( |
| 23 | + dataset_dir=dataset_dir, |
| 24 | + session_range=session_range, |
| 25 | + ) |
| 26 | + |
| 27 | + return DataLoader( |
| 28 | + dataset, |
| 29 | + batch_size=256, |
| 30 | + collate_fn=dataset.collate_fn, |
| 31 | + num_workers=2, |
| 32 | + persistent_workers=True, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def get_baidu_annotation_loader( |
| 37 | + dataset_dir: Path, |
| 38 | + session_range: Tuple[int, int], |
| 39 | +): |
| 40 | + dataset = BaiduUltrFeatureAnnotationDataset( |
| 41 | + dataset_dir=dataset_dir, |
| 42 | + session_range=session_range, |
| 43 | + ) |
| 44 | + |
| 45 | + return DataLoader( |
| 46 | + dataset, |
| 47 | + batch_size=256, |
| 48 | + collate_fn=dataset.collate_fn, |
| 49 | + num_workers=2, |
| 50 | + persistent_workers=True, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class CustomAttraction(nnx.Module): |
| 55 | + """ |
| 56 | + Example of a custom flax module with attention, |
| 57 | + every module needs to specify how to compute a logit, |
| 58 | + log probability and probability for a given batch. |
| 59 | +
|
| 60 | + In the simplest case, the logit layer can be re-used for probability |
| 61 | + and log probability computation. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, query_doc_features, rngs): |
| 65 | + super().__init__() |
| 66 | + self.attention = nnx.MultiHeadAttention( |
| 67 | + num_heads=1, |
| 68 | + in_features=query_doc_features, |
| 69 | + qkv_features=8, |
| 70 | + decode=False, |
| 71 | + rngs=rngs, |
| 72 | + ) |
| 73 | + self.projection = nnx.Linear(query_doc_features, 1, rngs=rngs) |
| 74 | + |
| 75 | + def logit(self, batch: Dict) -> Array: |
| 76 | + return self.projection(self.attention(batch["query_doc_features"])).squeeze() |
| 77 | + |
| 78 | + def prob(self, batch: Dict) -> Array: |
| 79 | + return nnx.sigmoid(self.logit(batch)) |
| 80 | + |
| 81 | + def log_prob(self, batch: Dict) -> Array: |
| 82 | + return nnx.log_sigmoid(self.logit(batch)) |
| 83 | + |
| 84 | + |
| 85 | +def main(): |
| 86 | + # Load sessions from a subset of the Baidu-ULTR dataset with pre-processed query-doc-features: |
| 87 | + dataset_dir = Path("../../clax-datasets/baidu-ultr-uva") |
| 88 | + query_doc_features = 768 |
| 89 | + |
| 90 | + train_loader = get_baidu_click_loader( |
| 91 | + dataset_dir, |
| 92 | + session_range=(0, 100_000), |
| 93 | + ) |
| 94 | + val_loader = get_baidu_click_loader( |
| 95 | + dataset_dir, |
| 96 | + session_range=(1_000_000, 1_500_000), |
| 97 | + ) |
| 98 | + test_loader = get_baidu_click_loader( |
| 99 | + dataset_dir, |
| 100 | + session_range=(1_500_000, 2_000_000), |
| 101 | + ) |
| 102 | + annotation_loader = get_baidu_annotation_loader( |
| 103 | + dataset_dir, |
| 104 | + session_range=(0, 400_000), |
| 105 | + ) |
| 106 | + |
| 107 | + # Instantiate a PBM with a custom module for document attraction, |
| 108 | + # note might be slow on CPU: |
| 109 | + rngs = nnx.Rngs(42) |
| 110 | + |
| 111 | + model = PositionBasedModel( |
| 112 | + attraction=CustomAttraction(query_doc_features, rngs), |
| 113 | + positions=10, |
| 114 | + rngs=rngs, |
| 115 | + ) |
| 116 | + trainer = Trainer( |
| 117 | + optax.adamw(0.0003), |
| 118 | + epochs=3, |
| 119 | + early_stopping=EarlyStopping(patience=0), |
| 120 | + ) |
| 121 | + train_df = trainer.train(model, train_loader, val_loader) |
| 122 | + click_df = trainer.test_clicks(model, test_loader) |
| 123 | + ranking_df = trainer.test_ranking(model, annotation_loader) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments