forked from fastai/fastai
/
text.learner.html
450 lines (317 loc) · 34.9 KB
/
text.learner.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
---
title: text.learner
keywords: fastai
sidebar: home_sidebar
summary: "Easy access of language models and ULMFiT"
---
<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="NLP-model-creation-and-training">NLP model creation and training<a class="anchor-link" href="#NLP-model-creation-and-training">¶</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>The main thing here is <a href="/text.learner.html#RNNLearner"><code>RNNLearner</code></a>. There are also some utility functions to help create and update text models.</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="Quickly-get-a-learner">Quickly get a learner<a class="anchor-link" href="#Quickly-get-a-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 ">
<h4 id="language_model_learner" class="doc_header"><code>language_model_learner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L197" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#language_model_learner-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>language_model_learner</code>(<strong><code>data</code></strong>:<a href="/basic_data.html#DataBunch"><code>DataBunch</code></a>, <strong><code>arch</code></strong>, <strong><code>config</code></strong>:<code>dict</code>=<strong><em><code>None</code></em></strong>, <strong><code>drop_mult</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>pretrained</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>pretrained_fnames</code></strong>:<code>OptStrTuple</code>=<strong><em><code>None</code></em></strong>, <strong>**<code>learn_kwargs</code></strong>) → <code>LanguageLearner</code></p>
</blockquote>
<div class="collapse" id="language_model_learner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#language_model_learner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>language_model_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 <a href="/basic_train.html#Learner"><code>Learner</code></a> with a language model from <code>data</code> and <code>arch</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>The model used is given by <code>arch</code> and <code>config</code>. It can be:</p>
<ul>
<li>an <a href="/text.models.awd_lstm.html#AWD_LSTM"><code>AWD_LSTM</code></a>(<a href="https://arxiv.org/abs/1708.02182">Merity et al.</a>)</li>
<li>a <a href="/text.models.transformer.html#Transformer"><code>Transformer</code></a> decoder (<a href="https://arxiv.org/abs/1706.03762">Vaswani et al.</a>)</li>
<li>a <a href="/text.models.transformer.html#TransformerXL"><code>TransformerXL</code></a> (<a href="https://arxiv.org/abs/1901.02860">Dai et al.</a>)</li>
</ul>
<p>They each have a default config for language modelling that is in <code>{lower_case_class_name}_lm_config</code> if you want to change the default parameter. At this stage, only the AWD LSTM support <code>pretrained=True</code> but we hope to add more pretrained models soon. <code>drop_mult</code> is applied to all the dropouts weights of the <code>config</code>, <code>learn_kwargs</code> are passed to the <a href="/basic_train.html#Learner"><code>Learner</code></a> initialization.</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 ">
<div markdown="span" class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note: </b>Using QRNN (change the flag in the config of the AWD LSTM) requires to have cuda installed (same version as pytorch is using).</div>
</div>
</div>
</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">IMDB_SAMPLE</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">TextLMDataBunch</span><span class="o">.</span><span class="n">from_csv</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">'texts.csv'</span><span class="p">)</span>
<span class="n">learn</span> <span class="o">=</span> <span class="n">language_model_learner</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">AWD_LSTM</span><span class="p">,</span> <span class="n">drop_mult</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
</pre></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="text_classifier_learner" class="doc_header"><code>text_classifier_learner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L280" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#text_classifier_learner-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>text_classifier_learner</code>(<strong><code>data</code></strong>:<a href="/basic_data.html#DataBunch"><code>DataBunch</code></a>, <strong><code>arch</code></strong>:<code>Callable</code>, <strong><code>bptt</code></strong>:<code>int</code>=<strong><em><code>70</code></em></strong>, <strong><code>max_len</code></strong>:<code>int</code>=<strong><em><code>1400</code></em></strong>, <strong><code>config</code></strong>:<code>dict</code>=<strong><em><code>None</code></em></strong>, <strong><code>pretrained</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>drop_mult</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>lin_ftrs</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>learn_kwargs</code></strong>) → <code>TextClassifierLearner</code></p>
</blockquote>
<div class="collapse" id="text_classifier_learner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#text_classifier_learner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>text_classifier_learner</code>:</p><ul><li><code>pytest -sv tests/test_text_train.py::test_classifier</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_train.py#L100" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_train.py::test_order_preds</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_train.py#L139" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Create a <a href="/basic_train.html#Learner"><code>Learner</code></a> with a text classifier from <code>data</code> and <code>arch</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>Here again, the backbone of the model is determined by <code>arch</code> and <code>config</code>. The input texts are fed into that model by bunch of <code>bptt</code> and only the last <code>max_len</code> activations are considered. This gives us the backbone of our model. The head then consists of:</p>
<ul>
<li>a layer that concatenates the final outputs of the RNN with the maximum and average of all the intermediate outputs (on the sequence length dimension),</li>
<li>blocks of (<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.BatchNorm1d"><code>nn.BatchNorm1d</code></a>, <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Dropout"><code>nn.Dropout</code></a>, <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Linear"><code>nn.Linear</code></a>, <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.ReLU"><code>nn.ReLU</code></a>) layers.</li>
</ul>
<p>The blocks are defined by the <code>lin_ftrs</code> and <code>drops</code> arguments. Specifically, the first block will have a number of inputs inferred from the backbone arch and the last one will have a number of outputs equal to data.c (which contains the number of classes of the data) and the intermediate blocks have a number of inputs/outputs determined by <code>lin_ftrs</code> (of course a block has a number of inputs equal to the number of outputs of the previous block). The dropouts all have a the same value ps if you pass a float, or the corresponding values if you pass a list. Default is to have an intermediate hidden size of 50 (which makes two blocks model_activation -> 50 -> n_classes) with a dropout of 0.1.</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">IMDB_SAMPLE</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">TextClasDataBunch</span><span class="o">.</span><span class="n">from_csv</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">'texts.csv'</span><span class="p">)</span>
<span class="n">learn</span> <span class="o">=</span> <span class="n">text_classifier_learner</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">AWD_LSTM</span><span class="p">,</span> <span class="n">drop_mult</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
</pre></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="RNNLearner" class="doc_header"><code>class</code> <code>RNNLearner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L45" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#RNNLearner-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>RNNLearner</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>split_func</code></strong>:<code>OptSplitFunc</code>=<strong><em><code>None</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>alpha</code></strong>:<code>float</code>=<strong><em><code>2.0</code></em></strong>, <strong><code>beta</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>metrics</code></strong>=<strong><em><code>None</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="RNNLearner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#RNNLearner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>RNNLearner</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>Basic class for a <a href="/basic_train.html#Learner"><code>Learner</code></a> in NLP.</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>Handles the whole creation from <code>data</code> and a <code>model</code> with a text data using a certain <code>bptt</code>. The <code>split_func</code> is used to properly split the model in different groups for gradual unfreezing and differential learning rates. Gradient clipping of <code>clip</code> is optionally applied. <code>alpha</code> and <code>beta</code> are all passed to create an instance of <a href="/callbacks.rnn.html#RNNTrainer"><code>RNNTrainer</code></a>. Can be used for a language model or an RNN classifier. It also handles the conversion of weights from a pretrained model as well as saving or loading the encoder.</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="RNNLearner.get_preds" class="doc_header"><code>get_preds</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L79" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#RNNLearner-get_preds-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>get_preds</code>(<strong><code>ds_type</code></strong>:<a href="/basic_data.html#DatasetType"><code>DatasetType</code></a>=<strong><em><code><DatasetType.Valid: 2></code></em></strong>, <strong><code>with_loss</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>n_batch</code></strong>:<code>Optional</code>[<code>int</code>]=<strong><em><code>None</code></em></strong>, <strong><code>pbar</code></strong>:<code>Union</code>[<code>MasterBar</code>, <code>ProgressBar</code>, <code>NoneType</code>]=<strong><em><code>None</code></em></strong>, <strong><code>ordered</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) → <code>List</code>[<code>Tensor</code>]</p>
</blockquote>
<div class="collapse" id="RNNLearner-get_preds-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#RNNLearner-get_preds-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>get_preds</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>Return predictions and targets on the valid, train, or test set, depending on <code>ds_type</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>If <code>ordered=True</code>, returns the predictions in the order of the dataset, otherwise they will be ordered by the sampler (from the longest text to the shortest). The other arguments are passed <a href="/basic_train.html#Learner.get_preds"><code>Learner.get_preds</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">
<h3 id="Loading-and-saving">Loading and saving<a class="anchor-link" href="#Loading-and-saving">¶</a></h3>
</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="RNNLearner.load_encoder" class="doc_header"><code>load_encoder</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L63" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#RNNLearner-load_encoder-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>load_encoder</code>(<strong><code>name</code></strong>:<code>str</code>)</p>
</blockquote>
<div class="collapse" id="RNNLearner-load_encoder-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#RNNLearner-load_encoder-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>load_encoder</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>Load the encoder <code>name</code> from the model directory.</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="RNNLearner.save_encoder" class="doc_header"><code>save_encoder</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L57" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#RNNLearner-save_encoder-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>save_encoder</code>(<strong><code>name</code></strong>:<code>str</code>)</p>
</blockquote>
<div class="collapse" id="RNNLearner-save_encoder-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#RNNLearner-save_encoder-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>save_encoder</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>Save the encoder to <code>name</code> inside the model directory.</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="RNNLearner.load_pretrained" class="doc_header"><code>load_pretrained</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L70" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#RNNLearner-load_pretrained-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>load_pretrained</code>(<strong><code>wgts_fname</code></strong>:<code>str</code>, <strong><code>itos_fname</code></strong>:<code>str</code>, <strong><code>strict</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="RNNLearner-load_pretrained-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#RNNLearner-load_pretrained-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>load_pretrained</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>Load a pretrained model and adapts it to the data vocabulary.</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>Opens the weights in the <code>wgts_fname</code> of <code>self.model_dir</code> and the dictionary in <code>itos_fname</code> then adapts the pretrained weights to the vocabulary of the <code>data</code>. The two files should be in the models directory of the <code>learner.path</code>.</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="Utility-functions">Utility functions<a class="anchor-link" href="#Utility-functions">¶</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 ">
<h4 id="convert_weights" class="doc_header"><code>convert_weights</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L28" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#convert_weights-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>convert_weights</code>(<strong><code>wgts</code></strong>:<code>Weights</code>, <strong><code>stoi_wgts</code></strong>:<code>Dict</code>[<code>str</code>, <code>int</code>], <strong><code>itos_new</code></strong>:<code>StrList</code>) → <code>Weights</code></p>
</blockquote>
<div class="collapse" id="convert_weights-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#convert_weights-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>convert_weights</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>Convert the model <code>wgts</code> to go with a new vocabulary.</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>Uses the dictionary <code>stoi_wgts</code> (mapping of word to id) of the weights to map them to a new dictionary <code>itos_new</code> (mapping id to word).</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="Get-predictions">Get predictions<a class="anchor-link" href="#Get-predictions">¶</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="LanguageLearner" class="doc_header"><code>class</code> <code>LanguageLearner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L109" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#LanguageLearner-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>LanguageLearner</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>split_func</code></strong>:<code>OptSplitFunc</code>=<strong><em><code>None</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>alpha</code></strong>:<code>float</code>=<strong><em><code>2.0</code></em></strong>, <strong><code>beta</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>metrics</code></strong>=<strong><em><code>None</code></em></strong>, <strong>**<code>learn_kwargs</code></strong>) :: <a href="/text.learner.html#RNNLearner"><code>RNNLearner</code></a></p>
</blockquote>
<div class="collapse" id="LanguageLearner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#LanguageLearner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>LanguageLearner</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 of RNNLearner for predictions.</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="LanguageLearner.predict" class="doc_header"><code>predict</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L112" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#LanguageLearner-predict-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>predict</code>(<strong><code>text</code></strong>:<code>str</code>, <strong><code>n_words</code></strong>:<code>int</code>=<strong><em><code>1</code></em></strong>, <strong><code>no_unk</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>temperature</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>min_p</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>sep</code></strong>:<code>str</code>=<strong><em><code>' '</code></em></strong>, <strong><code>decoder</code></strong>=<strong><em><code>'decode_spec_tokens'</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="LanguageLearner-predict-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#LanguageLearner-predict-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>predict</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>Return the <code>n_words</code> that come after <code>text</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>If <code>no_unk=True</code> the unknown token is never picked. Words are taken randomly with the distribution of probabilities returned by the model. If <code>min_p</code> is not <code>None</code>, that value is the minimum probability to be considered in the pool of words. Lowering <code>temperature</code> will make the texts less randomized.</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="LanguageLearner.beam_search" class="doc_header"><code>beam_search</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L133" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#LanguageLearner-beam_search-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>beam_search</code>(<strong><code>text</code></strong>:<code>str</code>, <strong><code>n_words</code></strong>:<code>int</code>, <strong><code>no_unk</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>top_k</code></strong>:<code>int</code>=<strong><em><code>10</code></em></strong>, <strong><code>beam_sz</code></strong>:<code>int</code>=<strong><em><code>1000</code></em></strong>, <strong><code>temperature</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>sep</code></strong>:<code>str</code>=<strong><em><code>' '</code></em></strong>, <strong><code>decoder</code></strong>=<strong><em><code>'decode_spec_tokens'</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="LanguageLearner-beam_search-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#LanguageLearner-beam_search-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>beam_search</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>Return the <code>n_words</code> that come after <code>text</code> using beam search.</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">
<h2 id="Basic-functions-to-get-a-model">Basic functions to get a model<a class="anchor-link" href="#Basic-functions-to-get-a-model">¶</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 ">
<h4 id="get_language_model" class="doc_header"><code>get_language_model</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L183" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#get_language_model-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>get_language_model</code>(<strong><code>arch</code></strong>:<code>Callable</code>, <strong><code>vocab_sz</code></strong>:<code>int</code>, <strong><code>config</code></strong>:<code>dict</code>=<strong><em><code>None</code></em></strong>, <strong><code>drop_mult</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="get_language_model-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#get_language_model-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>get_language_model</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 language model from <code>arch</code> and its <code>config</code>, maybe <code>pretrained</code>.</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="get_text_classifier" class="doc_header"><code>get_text_classifier</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L263" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#get_text_classifier-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>get_text_classifier</code>(<strong><code>arch</code></strong>:<code>Callable</code>, <strong><code>vocab_sz</code></strong>:<code>int</code>, <strong><code>n_class</code></strong>:<code>int</code>, <strong><code>bptt</code></strong>:<code>int</code>=<strong><em><code>70</code></em></strong>, <strong><code>max_len</code></strong>:<code>int</code>=<strong><em><code>1400</code></em></strong>, <strong><code>config</code></strong>:<code>dict</code>=<strong><em><code>None</code></em></strong>, <strong><code>drop_mult</code></strong>:<code>float</code>=<strong><em><code>1.0</code></em></strong>, <strong><code>lin_ftrs</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>pad_idx</code></strong>:<code>int</code>=<strong><em><code>1</code></em></strong>) → <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a></p>
</blockquote>
<div class="collapse" id="get_text_classifier-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#get_text_classifier-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>get_text_classifier</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 text classifier from <code>arch</code> and its <code>config</code>, maybe <code>pretrained</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>This model uses an encoder taken from the <code>arch</code> on <code>config</code>. This encoder is fed the sequence by successive bits of size <code>bptt</code> and we only keep the last <code>max_seq</code> outputs for the pooling layers.</p>
<p>The decoder use a concatenation of the last outputs, a <code>MaxPooling</code> of all the outputs and an <code>AveragePooling</code> of all the outputs. It then uses a list of <code>BatchNorm</code>, <code>Dropout</code>, <code>Linear</code>, <code>ReLU</code> blocks (with no <code>ReLU</code> in the last one), using a first layer size of <code>3*emb_sz</code> then following the numbers in <code>n_layers</code>. The dropouts probabilities are read in <code>drops</code>.</p>
<p>Note that the model returns a list of three things, the actual output being the first, the two others being the intermediate hidden states before and after dropout (used by the <a href="/callbacks.rnn.html#RNNTrainer"><code>RNNTrainer</code></a>). Most loss functions expect one output, so you should use a Callback to remove the other two if you're not using <a href="/callbacks.rnn.html#RNNTrainer"><code>RNNTrainer</code></a>.</p>
</div>
</div>
</div>
</div>