Distillers
Distillers define how teacher knowledge is transferred to the student model. RecDistillery currently includes embedding-based, ranking-based, and composite distillation strategies.
Available Distillers
Distiller defaults are stored in:
Distiller Base
Distiller
Bases: Module, ABC
Source code in recdistill/distillers/base.py
on_train_start(teacher_state: TeacherState, dataset: InteractionDataset) -> None
on_epoch_start() -> None
build_aux_batch(batch: InteractionBatch, device: torch.device) -> object | None
compute_loss(student: nn.Module, batch: InteractionBatch, aux_batch: object | None = None) -> torch.Tensor
DE
Expert
Bases: Module
Source code in recdistill/distillers/de.py
mlp = nn.Sequential(nn.Linear(dims[0], dims[1]), nn.ReLU(), nn.Linear(dims[1], dims[2]))
instance-attribute
__init__(dims: list[int])
DEDistiller
Bases: Distiller
Source code in recdistill/distillers/de.py
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 | |
teacher_dim = teacher_dim
instance-attribute
student_dim = student_dim
instance-attribute
num_experts = num_experts
instance-attribute
lambda_de = lambda_de
instance-attribute
temperature = temperature
instance-attribute
user_experts = nn.ModuleList((Expert(dims)) for _ in (range(num_experts)))
instance-attribute
item_experts = nn.ModuleList((Expert(dims)) for _ in (range(num_experts)))
instance-attribute
user_gate = nn.Sequential(nn.Linear(teacher_dim, num_experts), nn.Softmax(dim=1))
instance-attribute
item_gate = nn.Sequential(nn.Linear(teacher_dim, num_experts), nn.Softmax(dim=1))
instance-attribute
softmax = nn.Softmax(dim=1)
instance-attribute
__init__(teacher_dim: int, student_dim: int, num_experts: int, lambda_de: float, temperature: float = 0.01)
Source code in recdistill/distillers/de.py
on_train_start(teacher_state: TeacherState, dataset) -> None
Source code in recdistill/distillers/de.py
set_temperature(temperature: float) -> None
compute_loss(student: nn.Module, batch: InteractionBatch, aux_batch: object | None = None) -> torch.Tensor
Source code in recdistill/distillers/de.py
RRD
RRDDistiller
Bases: Distiller
Source code in recdistill/distillers/rrd.py
sampler = sampler
instance-attribute
lambda_rrd = lambda_rrd
instance-attribute
__init__(sampler: RRDSampler, lambda_rrd: float)
on_train_start(teacher_state, dataset) -> None
on_epoch_start() -> None
build_aux_batch(batch: InteractionBatch, device: torch.device) -> RRDAuxBatch
compute_loss(student: torch.nn.Module, batch: InteractionBatch, aux_batch: RRDAuxBatch | None = None) -> torch.Tensor
Source code in recdistill/distillers/rrd.py
relaxed_ranking_loss(interesting_scores: torch.Tensor, uninteresting_scores: torch.Tensor) -> torch.Tensor
Source code in recdistill/distillers/rrd.py
UnKD
UnKDDistiller
Bases: Distiller
UnKD distillation from the standalone implementation in UnKD/sample.py.
The distiller samples teacher-ranked item pairs inside popularity groups, then asks the student to preserve the teacher preference ordering with a BPR-style loss. Popularity groups and ratios are built from the recdistill InteractionDataset so the original UnKD loader is not required.
Source code in recdistill/distillers/unkd.py
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 | |
lambda_unkd = float(lambda_unkd)
instance-attribute
sample_num = int(sample_num)
instance-attribute
group_count = int(group_count)
instance-attribute
popularity_lambda = float(popularity_lambda)
instance-attribute
rank_top_k = int(rank_top_k)
instance-attribute
rank_temperature = float(rank_temperature)
instance-attribute
dataset: InteractionDataset | None = None
instance-attribute
teacher_state: TeacherState | None = None
instance-attribute
__init__(lambda_unkd: float = 1.0, sample_num: int = 30, group_count: int = 2, popularity_lambda: float = 1.0, rank_top_k: int = 1000, rank_temperature: float = 20.0)
Source code in recdistill/distillers/unkd.py
on_train_start(teacher_state: TeacherState, dataset: InteractionDataset) -> None
Source code in recdistill/distillers/unkd.py
on_epoch_start() -> None
build_aux_batch(batch: InteractionBatch, device: torch.device) -> UnKDAuxBatch
Source code in recdistill/distillers/unkd.py
compute_loss(student: torch.nn.Module, batch: InteractionBatch, aux_batch: UnKDAuxBatch | None = None) -> torch.Tensor
Source code in recdistill/distillers/unkd.py
refresh() -> None
Source code in recdistill/distillers/unkd.py
HTD
GroupMLP
Bases: Module
Source code in recdistill/distillers/htd.py
num_groups = num_groups
instance-attribute
hidden_dim = hidden_dim
instance-attribute
fc1 = nn.Linear(in_dim, num_groups * hidden_dim)
instance-attribute
fc2 = nn.Conv1d(in_channels=(num_groups * hidden_dim), out_channels=(num_groups * out_dim), kernel_size=1, groups=num_groups)
instance-attribute
relu = nn.ReLU()
instance-attribute
__init__(in_dim: int, hidden_dim: int, out_dim: int, num_groups: int)
Source code in recdistill/distillers/htd.py
HTDistiller
Bases: Distiller
Hierarchical Topology Distillation (adapted).
Uses TeacherState for teacher embeddings and the student's
get_all_*_embeddings() API to index student embeddings.
Source code in recdistill/distillers/htd.py
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 | |
lambda_td = lambda_td
instance-attribute
alpha = alpha
instance-attribute
K = num_groups
instance-attribute
topology_mode = topology_mode
instance-attribute
initial_tau = initial_tau
instance-attribute
min_tau = min_tau
instance-attribute
decay_epochs = decay_epochs
instance-attribute
entity_sample_size = int(entity_sample_size)
instance-attribute
tau = initial_tau
instance-attribute
v_user = None
instance-attribute
v_item = None
instance-attribute
f_user = None
instance-attribute
f_item = None
instance-attribute
__init__(lambda_td: float = 0.001, alpha: float = 0.5, num_groups: int = 40, topology_mode: str = 'group_pe', initial_tau: float = 1.0, min_tau: float = 1e-10, decay_epochs: int = 100, entity_sample_size: int = 0)
Source code in recdistill/distillers/htd.py
on_train_start(teacher_state: TeacherState, dataset) -> None
Source code in recdistill/distillers/htd.py
on_epoch_start() -> None
compute_loss(student: nn.Module, batch: InteractionBatch, aux_batch: object | None = None) -> Tensor
Source code in recdistill/distillers/htd.py
FTD
FTDistiller
Bases: Distiller
Source code in recdistill/distillers/ftd.py
lambda_td = lambda_td
instance-attribute
entity_sample_size = int(entity_sample_size)
instance-attribute
__init__(lambda_td: float = 0.001, entity_sample_size: int = 0)
Source code in recdistill/distillers/ftd.py
on_train_start(teacher_state: TeacherState, dataset) -> None
Source code in recdistill/distillers/ftd.py
compute_loss(student: nn.Module, batch: InteractionBatch, aux_batch: object | None = None) -> Tensor
Source code in recdistill/distillers/ftd.py
Composite Distillation
CompositeDistiller
Bases: Distiller
Source code in recdistill/distillers/composite.py
distillers = torch.nn.ModuleList(distillers)
instance-attribute
__init__(distillers: list[Distiller])
on_train_start(teacher_state: TeacherState, dataset: InteractionDataset) -> None
on_epoch_start() -> None
build_aux_batch(batch: InteractionBatch, device: torch.device) -> dict[str, object]
Source code in recdistill/distillers/composite.py
compute_loss(student: torch.nn.Module, batch: InteractionBatch, aux_batch: dict[str, object] | None = None) -> torch.Tensor
Source code in recdistill/distillers/composite.py
Distillation Samplers
AuxiliarySampler
Bases: ABC
Source code in recdistill/samplers/base.py
initialize(dataset, teacher_state) -> None
refresh() -> None
BPRNegativeSampler
Source code in recdistill/samplers/negative.py
dataset = dataset
instance-attribute
__init__(dataset: InteractionDataset)
RRDSampler
Bases: AuxiliarySampler
Source code in recdistill/samplers/rrd.py
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 | |