forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collab.html
563 lines (424 loc) · 34.1 KB
/
collab.html
1
2
3
4
5
6
7
8
9
10
11
12
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
---
title: collab
keywords: fastai
sidebar: home_sidebar
summary: "Application to collaborative filtering"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/collab.ipynb
# instructions: https://docs.fast.ai/gen_doc_main.html
-->
<div class="container" id="notebook-container">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Collaborative-filtering">Collaborative filtering<a class="anchor-link" href="#Collaborative-filtering">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This package contains all the necessary functions to quickly train a model for a collaborative filtering task. Let's start by importing all we'll need.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">fastai.collab</span> <span class="k">import</span> <span class="o">*</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Overview">Overview<a class="anchor-link" href="#Overview">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Collaborative filtering is when you're tasked to predict how much a user is going to like a certain item. The fastai library contains a <a href="/collab.html#CollabFilteringDataset"><code>CollabFilteringDataset</code></a> class that will help you create datasets suitable for training, and a function <code>get_colab_learner</code> to build a simple model directly from a ratings table. Let's first see how we can get started before delving into the documentation.</p>
<p>For this example, we'll use a small subset of the <a href="https://grouplens.org/datasets/movielens/">MovieLens</a> dataset to predict the rating a user would give a particular movie (from 0 to 5). The dataset comes in the form of a csv file where each line is a rating of a movie by a given person.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">path</span> <span class="o">=</span> <span class="n">untar_data</span><span class="p">(</span><span class="n">URLs</span><span class="o">.</span><span class="n">ML_SAMPLE</span><span class="p">)</span>
<span class="n">ratings</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="o">/</span><span class="s1">'ratings.csv'</span><span class="p">)</span>
<span class="n">ratings</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>userId</th>
<th>movieId</th>
<th>rating</th>
<th>timestamp</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>73</td>
<td>1097</td>
<td>4.0</td>
<td>1255504951</td>
</tr>
<tr>
<th>1</th>
<td>561</td>
<td>924</td>
<td>3.5</td>
<td>1172695223</td>
</tr>
<tr>
<th>2</th>
<td>157</td>
<td>260</td>
<td>3.5</td>
<td>1291598691</td>
</tr>
<tr>
<th>3</th>
<td>358</td>
<td>1210</td>
<td>5.0</td>
<td>957481884</td>
</tr>
<tr>
<th>4</th>
<td>130</td>
<td>316</td>
<td>2.0</td>
<td>1138999234</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We'll first turn the <code>userId</code> and <code>movieId</code> columns in category codes, so that we can replace them with their codes when it's time to feed them to an <code>Embedding</code> layer. This step would be even more important if our csv had names of users, or names of items in it. To do it, we simply have to call a <a href="/collab.html#CollabDataBunch"><code>CollabDataBunch</code></a> factory method.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">CollabDataBunch</span><span class="o">.</span><span class="n">from_df</span><span class="p">(</span><span class="n">ratings</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Now that this step is done, we can directly create a <a href="/basic_train.html#Learner"><code>Learner</code></a> object:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">learn</span> <span class="o">=</span> <span class="n">collab_learner</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">n_factors</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">y_range</span><span class="o">=</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span><span class="mf">5.</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>And then immediately begin training</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">learn</span><span class="o">.</span><span class="n">fit_one_cycle</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mf">5e-3</span><span class="p">,</span> <span class="n">wd</span><span class="o">=</span><span class="mf">0.1</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea ">
Total time: 00:09 <p><table style='width:300px; margin-bottom:10px'>
<tr>
<th>epoch</th>
<th>train_loss</th>
<th>valid_loss</th>
</tr>
<tr>
<th>1</th>
<th>2.427430</th>
<th>1.999472</th>
</tr>
<tr>
<th>2</th>
<th>1.116335</th>
<th>0.663345</th>
</tr>
<tr>
<th>3</th>
<th>0.736155</th>
<th>0.636640</th>
</tr>
<tr>
<th>4</th>
<th>0.612827</th>
<th>0.626773</th>
</tr>
<tr>
<th>5</th>
<th>0.565003</th>
<th>0.626336</th>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="CollabDataBunch" class="doc_header"><code>class</code> <code>CollabDataBunch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L50" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabDataBunch-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>CollabDataBunch</code>(<strong><code>train_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>valid_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>fix_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>test_dl</code></strong>:<code>Optional</code>[<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>]=<strong><em><code>None</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>dl_tfms</code></strong>:<code>Optional</code>[<code>Collection</code>[<code>Callable</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>collate_fn</code></strong>:<code>Callable</code>=<strong><em><code>'data_collate'</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="CollabDataBunch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabDataBunch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>CollabDataBunch</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Base <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> for collaborative filtering.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The init function shouldn't be called directly (as it's the one of a basic <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a>), instead, you'll want to use the following factory method.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CollabDataBunch.from_df" class="doc_header"><code>from_df</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L52" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabDataBunch-from_df-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_df</code>(<strong><code>ratings</code></strong>:<code>DataFrame</code>, <strong><code>valid_pct</code></strong>:<code>float</code>=<strong><em><code>0.2</code></em></strong>, <strong><code>user_name</code></strong>:<code>Optional</code>[<code>str</code>]=<strong><em><code>None</code></em></strong>, <strong><code>item_name</code></strong>:<code>Optional</code>[<code>str</code>]=<strong><em><code>None</code></em></strong>, <strong><code>rating_name</code></strong>:<code>Optional</code>[<code>str</code>]=<strong><em><code>None</code></em></strong>, <strong><code>test</code></strong>:<code>DataFrame</code>=<strong><em><code>None</code></em></strong>, <strong><code>seed</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>bs</code></strong>:<code>int</code>=<strong><em><code>64</code></em></strong>, <strong><code>val_bs</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>, <strong><code>num_workers</code></strong>:<code>int</code>=<strong><em><code>16</code></em></strong>, <strong><code>dl_tfms</code></strong>:<code>Optional</code>[<code>Collection</code>[<code>Callable</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>collate_fn</code></strong>:<code>Callable</code>=<strong><em><code>'data_collate'</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) → <code>CollabDataBunch</code></p>
</blockquote>
<div class="collapse" id="CollabDataBunch-from_df-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabDataBunch-from_df-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>from_df</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create a <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> suitable for collaborative filtering from <code>ratings</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Take a <code>ratings</code> dataframe and splits it randomly for train and test following <code>pct_val</code> (unless it's None). <code>user_name</code>, <code>item_name</code> and <code>rating_name</code> give the names of the corresponding columns (defaults to the first, the second and the third column). Optionally a <code>test</code> dataframe can be passed an a <code>seed</code> for the separation between training and validation set. The <code>kwargs</code> will be passed to <a href="/basic_data.html#DataBunch.create"><code>DataBunch.create</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Model-and-Learner">Model and <a href="/basic_train.html#Learner"><code>Learner</code></a><a class="anchor-link" href="#Model-and-Learner">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="CollabLearner" class="doc_header"><code>class</code> <code>CollabLearner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L68" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabLearner-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>CollabLearner</code>(<strong><code>data</code></strong>:<a href="/basic_data.html#DataBunch"><code>DataBunch</code></a>, <strong><code>model</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>opt_func</code></strong>:<code>Callable</code>=<strong><em><code>'Adam'</code></em></strong>, <strong><code>loss_func</code></strong>:<code>Callable</code>=<strong><em><code>None</code></em></strong>, <strong><code>metrics</code></strong>:<code>Collection</code>[<code>Callable</code>]=<strong><em><code>None</code></em></strong>, <strong><code>true_wd</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>bn_wd</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>wd</code></strong>:<code>Floats</code>=<strong><em><code>0.01</code></em></strong>, <strong><code>train_bn</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>path</code></strong>:<code>str</code>=<strong><em><code>None</code></em></strong>, <strong><code>model_dir</code></strong>:<code>PathOrStr</code>=<strong><em><code>'models'</code></em></strong>, <strong><code>callback_fns</code></strong>:<code>Collection</code>[<code>Callable</code>]=<strong><em><code>None</code></em></strong>, <strong><code>callbacks</code></strong>:<code>Collection</code>[<a href="/callback.html#Callback"><code>Callback</code></a>]=<strong><em><code><factory></code></em></strong>, <strong><code>layer_groups</code></strong>:<code>ModuleList</code>=<strong><em><code>None</code></em></strong>, <strong><code>add_time</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>silent</code></strong>:<code>bool</code>=<strong><em><code>None</code></em></strong>, <strong><code>cb_fns_registered</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/basic_train.html#Learner"><code>Learner</code></a></p>
</blockquote>
<div class="collapse" id="CollabLearner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabLearner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>CollabLearner</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p><a href="/basic_train.html#Learner"><code>Learner</code></a> suitable for collaborative filtering.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This is a subclass of <a href="/basic_train.html#Learner"><code>Learner</code></a> that just introduces helper functions to analyze results, the initialization is the same as a regular <a href="/basic_train.html#Learner"><code>Learner</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CollabLearner.bias" class="doc_header"><code>bias</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L82" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabLearner-bias-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>bias</code>(<strong><code>arr</code></strong>:<code>Collection</code>[<code>T_co</code>], <strong><code>is_item</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="CollabLearner-bias-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabLearner-bias-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>bias</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Bias for item or user (based on <code>is_item</code>) for all in <code>arr</code>. (Set model to <code>cpu</code> and no grad.)</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CollabLearner.get_idx" class="doc_header"><code>get_idx</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L70" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabLearner-get_idx-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>get_idx</code>(<strong><code>arr</code></strong>:<code>Collection</code>[<code>T_co</code>], <strong><code>is_item</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="CollabLearner-get_idx-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabLearner-get_idx-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>get_idx</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Fetch item or user (based on <code>is_item</code>) for all in <code>arr</code>. (Set model to <code>cpu</code> and no grad.)</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CollabLearner.weight" class="doc_header"><code>weight</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L89" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabLearner-weight-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>weight</code>(<strong><code>arr</code></strong>:<code>Collection</code>[<code>T_co</code>], <strong><code>is_item</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="CollabLearner-weight-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabLearner-weight-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>weight</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Bias for item or user (based on <code>is_item</code>) for all in <code>arr</code>. (Set model to <code>cpu</code> and no grad.)</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="EmbeddingDotBias" class="doc_header"><code>class</code> <code>EmbeddingDotBias</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L36" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#EmbeddingDotBias-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>EmbeddingDotBias</code>(<strong><code>n_factors</code></strong>:<code>int</code>, <strong><code>n_users</code></strong>:<code>int</code>, <strong><code>n_items</code></strong>:<code>int</code>, <strong><code>y_range</code></strong>:<code>Point</code>=<strong><em><code>None</code></em></strong>) :: <a href="/core.html#PrePostInitMeta"><code>PrePostInitMeta</code></a> :: <a href="/torch_core.html#Module"><code>Module</code></a></p>
</blockquote>
<div class="collapse" id="EmbeddingDotBias-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#EmbeddingDotBias-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>EmbeddingDotBias</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Base dot model for collaborative filtering.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Creates a simple model with <code>Embedding</code> weights and biases for <code>n_users</code> and <code>n_items</code>, with <code>n_factors</code> latent factors. Takes the dot product of the embeddings and adds the bias, then if <code>y_range</code> is specified, feed the result to a sigmoid rescaled to go from <code>y_range[0]</code> to <code>y_range[1]</code>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="EmbeddingNN" class="doc_header"><code>class</code> <code>EmbeddingNN</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L26" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#EmbeddingNN-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>EmbeddingNN</code>(<strong><code>emb_szs</code></strong>:<code>ListSizes</code>, <strong><code>layers</code></strong>:<code>Collection</code>[<code>int</code>]=<strong><em><code>None</code></em></strong>, <strong><code>ps</code></strong>:<code>Collection</code>[<code>float</code>]=<strong><em><code>None</code></em></strong>, <strong><code>emb_drop</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>y_range</code></strong>:<code>OptRange</code>=<strong><em><code>None</code></em></strong>, <strong><code>use_bn</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>bn_final</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/core.html#PrePostInitMeta"><code>PrePostInitMeta</code></a> :: <a href="/tabular.models.html#TabularModel"><code>TabularModel</code></a></p>
</blockquote>
<div class="collapse" id="EmbeddingNN-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#EmbeddingNN-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>EmbeddingNN</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Subclass <a href="/tabular.models.html#TabularModel"><code>TabularModel</code></a> to create a NN suitable for collaborative filtering.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><code>emb_szs</code> will overwrite the default and <code>kwargs</code> are passed to <a href="/tabular.models.html#TabularModel"><code>TabularModel</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="collab_learner" class="doc_header"><code>collab_learner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L96" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#collab_learner-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>collab_learner</code>(<strong><code>data</code></strong>, <strong><code>n_factors</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>, <strong><code>use_nn</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>emb_szs</code></strong>:<code>Dict</code>[<code>str</code>, <code>int</code>]=<strong><em><code>None</code></em></strong>, <strong><code>layers</code></strong>:<code>Collection</code>[<code>int</code>]=<strong><em><code>None</code></em></strong>, <strong><code>ps</code></strong>:<code>Collection</code>[<code>float</code>]=<strong><em><code>None</code></em></strong>, <strong><code>emb_drop</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>y_range</code></strong>:<code>OptRange</code>=<strong><em><code>None</code></em></strong>, <strong><code>use_bn</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>bn_final</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong>**<code>learn_kwargs</code></strong>) → <a href="/basic_train.html#Learner"><code>Learner</code></a></p>
</blockquote>
<div class="collapse" id="collab_learner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#collab_learner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>collab_learner</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create a Learner for collaborative filtering on <code>data</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>More specifically, binds <a href="/tabular.data.html#tabular.data"><code>data</code></a> with a model that is either an <a href="/collab.html#EmbeddingDotBias"><code>EmbeddingDotBias</code></a> with <code>n_factors</code> if <code>use_nn=False</code> or a <a href="/collab.html#EmbeddingNN"><code>EmbeddingNN</code></a> with <code>emb_szs</code> otherwise. In both cases the numbers of users and items will be inferred from the data, <code>y_range</code> can be specified in the <code>kwargs</code> and you can pass <a href="/metrics.html#metrics"><code>metrics</code></a> or <code>wd</code> to the <a href="/basic_train.html#Learner"><code>Learner</code></a> constructor.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Links-with-the-Data-Block-API">Links with the Data Block API<a class="anchor-link" href="#Links-with-the-Data-Block-API">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="CollabLine" class="doc_header"><code>class</code> <code>CollabLine</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L14" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabLine-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>CollabLine</code>(<strong><code>cats</code></strong>, <strong><code>conts</code></strong>, <strong><code>classes</code></strong>, <strong><code>names</code></strong>) :: <a href="/tabular.data.html#TabularLine"><code>TabularLine</code></a></p>
</blockquote>
<div class="collapse" id="CollabLine-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabLine-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>CollabLine</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Subclass of <a href="/tabular.data.html#TabularLine"><code>TabularLine</code></a> for collaborative filtering.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="CollabList" class="doc_header"><code>class</code> <code>CollabList</code><a href="https://github.com/fastai/fastai/blob/master/fastai/collab.py#L20" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CollabList-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>CollabList</code>(<strong><code>items</code></strong>:<code>Iterator</code>[<code>T_co</code>], <strong><code>cat_names</code></strong>:<code>OptStrList</code>=<strong><em><code>None</code></em></strong>, <strong><code>cont_names</code></strong>:<code>OptStrList</code>=<strong><em><code>None</code></em></strong>, <strong><code>procs</code></strong>=<strong><em><code>None</code></em></strong>, <strong>**<code>kwargs</code></strong>) → <code>TabularList</code> :: <a href="/tabular.data.html#TabularList"><code>TabularList</code></a></p>
</blockquote>
<div class="collapse" id="CollabList-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CollabList-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>CollabList</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Subclass of <a href="/tabular.data.html#TabularList"><code>TabularList</code></a> for collaborative filtering.</p>
</div>
</div>
</div>
</div>