forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.learner.html
640 lines (464 loc) · 45.2 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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
---
title: text.learner
keywords: fastai
sidebar: home_sidebar
summary: "Easy access of language models and ULMFiT"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/text.learner.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="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#L201" 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>Tests found for <code>language_model_learner</code>:</p><ul><li><code>pytest -sv tests/test_text_train.py::test_qrnn_works_if_split_fn_provided</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_train.py#L73" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_train.py::test_qrnn_works_with_no_split</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_train.py#L61" 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 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.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.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.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 and Tranformer 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>
<p>If your <a href="/text.data.html#text.data"><code>data</code></a> is backward, the pretrained model downloaded will also be a backard one (only available for <a href="/text.models.awd_lstm.html#AWD_LSTM"><code>AWD_LSTM</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 ">
<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#L287" 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#L81" 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 code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="TextClassificationInterpretation" class="doc_header"><code>class</code> <code>TextClassificationInterpretation</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L208" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextClassificationInterpretation-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TextClassificationInterpretation</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>preds</code></strong>:<code>Tensor</code>, <strong><code>y_true</code></strong>:<code>Tensor</code>, <strong><code>losses</code></strong>:<code>Tensor</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>) :: <a href="/train.html#ClassificationInterpretation"><code>ClassificationInterpretation</code></a></p>
</blockquote>
<div class="collapse" id="TextClassificationInterpretation-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextClassificationInterpretation-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>TextClassificationInterpretation</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>Provides an interpretation of classification based on input sensitivity. This was designed for AWD-LSTM only for the moment, because Transformer already has its own attentional model.</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 darker the word-shading in the below example, the more it contributes to the classification. Results here are without any fitting. After fitting to acceptable accuracy, this class can show you what is being used to produce the classification of a particular case.</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">import</span> <span class="nn">matplotlib.cm</span> <span class="k">as</span> <span class="nn">cm</span>
<span class="n">txt_ci</span> <span class="o">=</span> <span class="n">TextClassificationInterpretation</span><span class="o">.</span><span class="n">from_learner</span><span class="p">(</span><span class="n">learn</span><span class="p">)</span>
<span class="n">test_text</span> <span class="o">=</span> <span class="s2">"Zombiegeddon was perhaps the GREATEST movie i have ever seen!"</span>
<span class="n">txt_ci</span><span class="o">.</span><span class="n">show_intrinsic_attention</span><span class="p">(</span><span class="n">test_text</span><span class="p">,</span><span class="n">cmap</span><span class="o">=</span><span class="n">cm</span><span class="o">.</span><span class="n">Purples</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 ">
<span style="font-family: monospace;"><span title="0.608" style="background-color: rgba(132, 128, 187, 0.5);">xxbos</span> <span title="0.496" style="background-color: rgba(159, 155, 200, 0.5);">xxmaj</span> <span title="0.471" style="background-color: rgba(165, 162, 204, 0.5);">xxunk</span> <span title="0.495" style="background-color: rgba(159, 155, 200, 0.5);">was</span> <span title="0.523" style="background-color: rgba(152, 148, 197, 0.5);">perhaps</span> <span title="0.539" style="background-color: rgba(148, 144, 195, 0.5);">the</span> <span title="0.566" style="background-color: rgba(142, 138, 192, 0.5);">xxup</span> <span title="0.615" style="background-color: rgba(130, 127, 187, 0.5);">greatest</span> <span title="0.689" style="background-color: rgba(116, 102, 174, 0.5);">movie</span> <span title="0.805" style="background-color: rgba(95, 61, 153, 0.5);">i</span> <span title="0.933" style="background-color: rgba(74, 20, 134, 0.5);">have</span> <span title="1.000" style="background-color: rgba(63, 0, 125, 0.5);">ever</span> <span title="0.908" style="background-color: rgba(78, 28, 137, 0.5);">seen</span> <span title="0.579" style="background-color: rgba(138, 135, 190, 0.5);">!</span></span>
</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>You can also view the raw attention values with <code>.intrinsic_attention(text)</code></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">txt_ci</span><span class="o">.</span><span class="n">intrinsic_attention</span><span class="p">(</span><span class="n">test_text</span><span class="p">)[</span><span class="mi">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_text output_subarea output_execute_result">
<pre>tensor([0.6078, 0.4961, 0.4707, 0.4946, 0.5228, 0.5393, 0.5656, 0.6153, 0.6893,
0.8047, 0.9329, 1.0000, 0.9080, 0.5786], device='cuda:0')</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>Create a tabulation showing the first <code>k</code> texts in top_losses along with their prediction, actual,loss, and probability of actual class. <code>max_len</code> is the maximum number of tokens displayed. If <code>max_len=None</code>, it will display all tokens.</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">txt_ci</span><span class="o">.</span><span class="n">show_top_losses</span><span class="p">(</span><span class="mi">5</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 ">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>Text</th>
<th>Prediction</th>
<th>Actual</th>
<th>Loss</th>
<th>Probability</th>
</tr>
</thead>
<tbody>
<tr>
<td>xxbos i have to agree with what many of the other reviewers concluded . a subject which could have been thought - provoking and shed light on a reversed double - standard , failed miserably . \n \n xxmaj rape being a crime of violence and forced abusive control , the scenes here were for the most part pathetic . xxmaj it would have been a better idea to</td>
<td>pos</td>
<td>neg</td>
<td>8.25</td>
<td>0.00</td>
</tr>
<tr>
<td>xxbos xxmaj betty xxmaj sizemore ( xxmaj renee xxmaj zellweger ) lives her life through soap xxmaj opera " a xxmaj reason to xxmaj love " as a way to escape her slob husband and dull life . xxmaj after a shocking incident involving two hit men ( xxmaj morgan xxmaj freeman and xxmaj chris xxmaj rock ) , xxmaj betty goes into shock and travels to xxup la ,</td>
<td>pos</td>
<td>pos</td>
<td>7.71</td>
<td>1.00</td>
</tr>
<tr>
<td>xxbos xxmaj when people harp on about how " they do n't make 'em like they used to " then just point them towards this fantastically entertaining , and quaint - looking , comedy horror from writer - director xxmaj glenn mcquaid . \n \n xxmaj it 's a tale of graverobbers ( played by xxmaj dominic xxmaj monaghan and xxmaj larry xxmaj fessenden ) who end up digging</td>
<td>pos</td>
<td>pos</td>
<td>7.47</td>
<td>1.00</td>
</tr>
<tr>
<td>xxbos i have to agree with all the previous xxunk -- this is simply the best of all frothy comedies , with xxmaj bardot as sexy as xxmaj marilyn xxmaj monroe ever was , and definitely with a prettier face ( maybe there 's less mystique , but look how xxmaj marilyn paid for that . ) i do n't think i 've ever seen such a succulent - looking</td>
<td>pos</td>
<td>pos</td>
<td>6.55</td>
<td>1.00</td>
</tr>
<tr>
<td>xxbos i will freely admit that i have n't seen the original movie , but i 've read the play , so i 've some background with the " original . " xxmaj if you shuck off the fact that this is a remake of an old classic , this movie is smart , witty , fresh , and hilarious . xxmaj yes , the casting decisions may seem strange</td>
<td>pos</td>
<td>pos</td>
<td>6.38</td>
<td>1.00</td>
</tr>
</tbody>
</table>
</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">
<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#L64" 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>, <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>)</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#L72" 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#L113" 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#L116" 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#L137" 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#L187" 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#L270" 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>