from fastai import * # Quick access to most common functionality
from fastai.collab import * # Quick access to collab filtering functionality
collab models use data in a DataFrame of user, items, and ratings.
path = untar_data(URLs.ML_SAMPLE)
path
PosixPath('/home/nok/.fastai/data/movie_lens_sample')
ratings = pd.read_csv(path/'ratings.csv')
series2cat(ratings, 'userId', 'movieId')
ratings.head()
| userId | movieId | rating | timestamp | |
|---|---|---|---|---|
| 0 | 73 | 1097 | 4.0 | 1255504951 |
| 1 | 561 | 924 | 3.5 | 1172695223 |
| 2 | 157 | 260 | 3.5 | 1291598691 |
| 3 | 358 | 1210 | 5.0 | 957481884 |
| 4 | 130 | 316 | 2.0 | 1138999234 |
data = CollabDataBunch.from_df(ratings, seed=42)
y_range = [0, 5.5]
That's all we need to create and train a model:
learn = collab_learner(data, n_factors=50, y_range=y_range)
learn.fit_one_cycle(4, 5e-3)
Total time: 00:02 epoch train_loss valid_loss 1 1.724424 1.277289 (00:00) 2 0.893744 0.678392 (00:00) 3 0.655527 0.651847 (00:00) 4 0.562305 0.649613 (00:00)