Fast AI — DL 2018 L5 note
2 min readApr 5, 2018
Good notes
Topics
- Create collaborative filtering model MovieLens
- Detail of Optimize function
MovieLens
- https://github.com/fastai/fastai/blob/master/courses/dl1/lesson5-movielens.ipynb
- MovieLens is a web-based recommender system
- User X Movie = Ratings
- Use RMSE as score for Prediction v Actual
Matrix factorization
- For certain User X certain Movie, presented by cross-tab, can be predict by A . B (dot product) where A and B are 1 rank latent factor
Solve it by Excel solver
- Set objective
- Set variable cells, i.e. matrix for A . B
- Set solving method: GRG Nonliear
- No non-linear layer, kind of Shallow learning
- Assumption: certain user likes how much [romantic, action, sci-fi] dot Movie contains how much [romantic, action, sci-fi]
- But we don’t have this latent matrix …
- Latent matrix is kind of embedding matrix
Solve it by Python
- embedding matrix size = 50
- data visualization by seaborn
Get Allen Wu’s stories in your inbox
Join Medium for free to get updates from this writer.
Dot product model with PyTorch
- Customized PyTorch class: EmbeddingDot
- Dot product of User X Movie
- Dictionary comprehension: {key: value for value, key in enumerate (ratings.userId.unique())}
- HE weight init. : uniform dist.
- …uniform_(0, 0.05) where underscore means no return, it’s “in-replace” version
- data = ColumnarModelData.from_data_frame…
- optimizer with weight decay
Improvement — Bias
- Add bias to embedding matrix: EmbeddingDotBias
- PyTorch’s fit has no SGDR, differential LR, LR finder …
- Then start to add some FastAI tech. “manually”
- LR annealing
Improvement — NN
- Using NN instead of Dot product of User X Movie
- Concatenating user, movie embedding together
- Or concat more: user emb(embedding) + movie emb + genre emb+ timing emb + …
Optimize / Fit by Excel
Stochastic Gradient Descent (SGD)
- For matrix, use Jacobian, Hessian instead of single element finite difference
Momentum
- Speed up gradient descent
- SGD + Momentum
Adam
- It’s faster, but the final answer is not as good as SGD + Momentum
- AdamW: Fixing Weight Decay Regularization in Adam









