Basic Attention Model
Implementation of an attention-based model for item recommendation.
AttentionBasedContextEmbedding
Bases: BaseBasketModel
Class for the attention-based model.
Wang, Shoujin, Liang Hu, Longbing Cao, Xiaoshui Huang, Defu Lian, and Wei Liu. "Attention-based transactional context embedding for next-item recommendation." In Proceedings of the AAAI conference on artificial intelligence, vol. 32, no. 1. 2018.
Source code in choice_learn/basket_models/basic_attention_model.py
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 |
|
train_iter_method: str
property
Method used to generate sub-baskets from a purchased one.
Available methods are: - 'shopper': randomly orders the purchases and creates the ordered sub-baskets: (1|0); (2|1); (3|1,2); (4|1,2,3); etc... - 'aleacarta': creates all the sub-baskets with N-1 items: (4|1,2,3); (3|1,2,4); (2|1,3,4); (1|2,3,4)
Returns:
Type | Description |
---|---|
str
|
Data generation method. |
trainable_weights
property
Return the trainable weights of the model.
Returns:
Type | Description |
---|---|
list
|
List of trainable weights (Wi, wa, Wo). |
__init__(latent_size=4, n_negative_samples=2, nce_distribution='natural', optimizer='adam', callbacks=None, lr=0.001, epochs=10, batch_size=32, grad_clip_value=None, weight_decay=None, momentum=0.0, **kwargs)
Initialize the model with hyperparameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epochs |
int
|
Number of training epochs. |
10
|
lr |
float
|
Learning rate for the optimizer. |
0.001
|
latent_size |
int
|
Size of the item embeddings. |
4
|
n_negative_samples |
int
|
Number of negative samples to use in training. |
2
|
batch_size |
int
|
Size of the batches for training. Default is 50. |
32
|
optimizer |
str
|
Optimizer to use for training. Default is "Adam". |
'adam'
|
nce_distribution |
Items distribution to be used to compute the NCE Loss Currentlry available: 'natural' to estimate the distribution from the train dataset and 'uniform' where all items have the same disitrbution, 1/n_items. Default is 'natural'. |
'natural'
|
Source code in choice_learn/basket_models/basic_attention_model.py
compute_batch_loss(item_batch, basket_batch, future_batch, store_batch, week_batch, price_batch, available_item_batch)
Compute log-likelihood and loss for one batch of items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item_batch |
ndarray
|
Batch of purchased items ID (integers) Shape must be (batch_size,) |
required |
basket_batch |
ndarray
|
Batch of baskets (ID of items already in the baskets) (arrays) for each purchased item Shape must be (batch_size, max_basket_size) |
required |
future_batch |
ndarray
|
Batch of items to be purchased in the future (ID of items not yet in the basket) (arrays) for each purchased item Shape must be (batch_size, max_basket_size) Here for signature reasons, unused for this model |
required |
store_batch |
ndarray
|
Batch of store IDs (integers) for each purchased item Shape must be (batch_size,) |
required |
week_batch |
ndarray
|
Batch of week numbers (integers) for each purchased item Shape must be (batch_size,) |
required |
price_batch |
ndarray
|
Batch of prices (floats) for each purchased item Shape must be (batch_size,) |
required |
available_item_batch |
ndarray
|
List of availability matrices (indicating the availability (1) or not (0) of the products) (arrays) for each purchased item Shape must be (batch_size, n_items) |
required |
Returns:
Name | Type | Description |
---|---|---|
Variable
|
Value of the loss for the batch (binary cross-entropy), Shape must be (1,) |
|
loglikelihood |
Variable
|
Computed log-likelihood of the batch of items Approximated by difference of utilities between positive and negative samples Shape must be (1,) |
Source code in choice_learn/basket_models/basic_attention_model.py
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 |
|
compute_batch_utility(item_batch, basket_batch, store_batch, week_batch, price_batch, available_item_batch)
Compute the utility of all the items in item_batch given the items in basket_batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item_batch |
Union[ndarray, Tensor]
|
Batch of the purchased items ID (integers) for which to compute the utility Shape must be (batch_size,) (positive and negative samples concatenated together) |
required |
basket_batch |
ndarray
|
Batch of baskets (ID of items already in the baskets) (arrays) for each purchased item Shape must be (batch_size, max_basket_size) |
required |
store_batch |
ndarray
|
Batch of store IDs (integers) for each purchased item Shape must be (batch_size,) |
required |
week_batch |
ndarray
|
Batch of week numbers (integers) for each purchased item Shape must be (batch_size,) |
required |
price_batch |
ndarray
|
Batch of prices (floats) for each purchased item Shape must be (batch_size,) |
required |
available_item_batch |
ndarray
|
Batch of availability matrices (indicating the availability (1) or not (0) of the products) (arrays) for each purchased item Shape must be (batch_size, n_items) |
required |
Returns:
Name | Type | Description |
---|---|---|
item_utilities |
Tensor
|
Utility of all the items in item_batch Shape must be (batch_size,) |
Source code in choice_learn/basket_models/basic_attention_model.py
embed_context(context_items)
Return the context embedding matrix.
Returns:
Type | Description |
---|---|
tf.Tensor
|
[batch_size, latent_size] tf.Tensor Tensor containing the matrix of contexts embeddings. |
Source code in choice_learn/basket_models/basic_attention_model.py
fit(trip_dataset, val_dataset=None, verbose=0)
Trains the model for a specified number of epochs.
Source code in choice_learn/basket_models/basic_attention_model.py
get_negative_samples(available_items, purchased_items, next_item, n_samples)
Sample randomly a set of items.
(set of items not already purchased and not necessarily from the basket)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
available_items |
ndarray
|
Matrix indicating the availability (1) or not (0) of the products Shape must be (n_items,) |
required |
purchased_items |
ndarray
|
List of items already purchased (already in the basket) |
required |
next_item |
int
|
Next item (to be added in the basket) |
required |
n_samples |
int
|
Number of samples to draw |
required |
Returns:
Type | Description |
---|---|
list[int]
|
Random sample of items, each of them distinct from the next item and from the items already in the basket |
Source code in choice_learn/basket_models/basic_attention_model.py
instantiate(n_items)
Initialize the model parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_items |
int
|
Number of unique items in the dataset. |
required |