This module adds to the tools from fastai collab to use transfer learning by loading embeddings needed for collaborative filtering from a pretrained model. Additionally, it also adds the capability of saving the vocabulary the collab model was trained on. The most important function in this module is collab_learner.
Loading users/items embeddings from a pretrained model
In a collab model, to load a pretrained vocabulary, we need to adapt the embeddings of the vocabulary used for the pre-training to the vocabulary of our current collab corpus.
Convert the users and items (possibly saved as 0.module.encoder.weight and 1.attn.lbs_weight.weight respectively) embedding in old_wgts to go from old_vocab to new_vocab
Type
Details
old_wgts
dict
Embedding weights of the pretrained model
old_vocab
list
Vocabulary (tokens and labels) of the corpus used for pretraining
new_vocab
dict
Current collab corpus vocabulary (users and items)
Create a Learner for collaborative filtering on dls.
Type
Default
Details
dls
DataLoaders
DataLoaders containing fastai or PyTorch DataLoaders
n_factors
int
50
use_nn
bool
False
emb_szs
NoneType
None
layers
NoneType
None
config
NoneType
None
y_range
NoneType
None
loss_func
callable | None
None
Loss function. Defaults to dls loss
pretrained
bool
False
opt_func
Optimizer | OptimWrapper
Adam
Optimization function for training
lr
float | slice
0.001
Default learning rate
splitter
callable
trainable_params
Split model into parameter groups. Defaults to one parameter group
cbs
Callback | MutableSequence | None
None
Callbacks to add to Learner
metrics
callable | MutableSequence | None
None
Metrics to calculate on validation set
path
str | Path | None
None
Parent directory to save, load, and export models. Defaults to dlspath
model_dir
str | Path
models
Subdirectory to save and load models
wd
float | int | None
None
Default weight decay
wd_bn_bias
bool
False
Apply weight decay to normalization and bias parameters
train_bn
bool
True
Train frozen normalization layers
moms
tuple
(0.95, 0.85, 0.95)
Default momentum for schedulers
default_cbs
bool
True
Include default Callbacks
If use_nn=False, the model used is an EmbeddingDotBias with n_factors and y_range. Otherwise, it’s a EmbeddingNN for which you can pass emb_szs (will be inferred from the dls with get_emb_sz if you don’t provide any), layers (defaults to [n_factors]) y_range, and a config that you can create with tabular_config to customize your model.
loss_func will default to MSELossFlat and all the other arguments are passed to Learner.
with tempfile.TemporaryDirectory() as d: learn = collab_learner(dls, y_range=(0,5), path=d) learn.fit(1)# Test save created a file learn.save('tmp')assert (Path(d)/'models/tmp.pth').exists()assert (Path(d)/'models/tmp_vocab.pkl').exists()