forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.models.html
785 lines (555 loc) · 53.1 KB
/
text.models.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
---
title: text.models
keywords: fastai
sidebar: home_sidebar
summary: "Implementation of the AWD-LSTM and the RNN models"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/text.models.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="Implementation-of-the-language-models">Implementation of the language models<a class="anchor-link" href="#Implementation-of-the-language-models">¶</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><a href="/text.models.html#text.models"><code>text.models</code></a> module fully implements the encoder for an <a href="https://arxiv.org/pdf/1708.02182.pdf">AWD-LSTM</a>, the <a href="https://arxiv.org/abs/1706.03762">transformer model</a> and the <a href="https://arxiv.org/abs/1901.02860">transformer XL model</a>. They can then plugged in with a decoder to make a language model, or some classifying layers to make a text classifier.</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="Language-model-modules">Language model modules<a class="anchor-link" href="#Language-model-modules">¶</a></h2>
</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">show_doc</span><span class="p">(</span><span class="n">AWD_LSTM</span><span class="p">,</span> <span class="n">title_level</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="AWD_LSTM" class="doc_header"><code>class</code> <code>AWD_LSTM</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L75" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#AWD_LSTM-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>AWD_LSTM</code>(<strong><code>vocab_sz</code></strong>:<code>int</code>, <strong><code>emb_sz</code></strong>:<code>int</code>, <strong><code>n_hid</code></strong>:<code>int</code>, <strong><code>n_layers</code></strong>:<code>int</code>, <strong><code>pad_token</code></strong>:<code>int</code>=<strong><em><code>1</code></em></strong>, <strong><code>hidden_p</code></strong>:<code>float</code>=<strong><em><code>0.2</code></em></strong>, <strong><code>input_p</code></strong>:<code>float</code>=<strong><em><code>0.6</code></em></strong>, <strong><code>embed_p</code></strong>:<code>float</code>=<strong><em><code>0.1</code></em></strong>, <strong><code>weight_p</code></strong>:<code>float</code>=<strong><em><code>0.5</code></em></strong>, <strong><code>qrnn</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>bidir</code></strong>:<code>bool</code>=<strong><em><code>False</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="AWD_LSTM-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#AWD_LSTM-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>AWD_LSTM</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>AWD-LSTM/QRNN inspired by <a href="https://arxiv.org/abs/1708.02182">https://arxiv.org/abs/1708.02182</a>.</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 main idea of the article is to use a <a href="http://www.pnas.org/content/79/8/2554">RNN</a> with dropout everywhere, but in an intelligent way. There is a difference with the usual dropout, which is why you’ll see a <a href="/text.models.awd_lstm.html#RNNDropout"><code>RNNDropout</code></a> module: we zero things, as is usual in dropout, but we always zero the same thing according to the sequence dimension (which is the first dimension in pytorch). This ensures consistency when updating the hidden state through the whole sentences/articles.</p>
<p>This being given, there are a total four different dropouts in the encoder of the AWD-LSTM:</p>
<ul>
<li>the first one, embedding dropout, is applied when we look the ids of our tokens inside the embedding matrix (to transform them from numbers to a vector of float). We zero some lines of it, so random ids are sent to a vector of zeros instead of being sent to their embedding vector. This is the <code>embed_p</code> parameter.</li>
<li>the second one, input dropout, is applied to the result of the embedding with dropout. We forget random pieces of the embedding matrix (but as stated in the last paragraph, the same ones in the sequence dimension). This is the <code>input_p</code> parameter.</li>
<li>the third one is the weight dropout. It’s the trickiest to implement as we randomly replace by 0s some weights of the hidden-to-hidden matrix inside the RNN: this needs to be done in a way that ensure the gradients are still computed and the initial weights still updated. This is the <code>weight_p</code> parameter.</li>
<li>the fourth one is the hidden dropout. It’s applied to the output of one of the layers of the RNN before it’s used as input of the next layer (again same coordinates are zeroed in the sequence dimension). It isn’t applied to the last output (which will get its own dropout in the decoder).This is the <code>hidden_p</code> parameter.</li>
</ul>
<p>The other attributes are <code>vocab_sz</code> for the number of tokens in your vocabulary, <code>emb_sz</code> for the embedding size, <code>n_hid</code> for the hidden size of your inner LSTMs (or QRNNs), <code>n_layers</code> the number of layers and <code>pad_token</code> for the index of an eventual padding token (1 by default in fastai).</p>
<p>The flag <code>qrnn=True</code> replace the inner LSTMs by <a href="https://arxiv.org/abs/1611.01576">QRNNs</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="AWD_LSTM.reset" class="doc_header"><code>reset</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L130" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#AWD_LSTM-reset-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>reset</code>()</p>
</blockquote>
<div class="collapse" id="AWD_LSTM-reset-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#AWD_LSTM-reset-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>reset</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>Reset the hidden states.</p>
</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">show_doc</span><span class="p">(</span><span class="n">Transformer</span><span class="p">,</span> <span class="n">title_level</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="Transformer" class="doc_header"><code>class</code> <code>Transformer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L149" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Transformer-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>Transformer</code>(<strong><code>vocab_sz</code></strong>:<code>int</code>, <strong><code>ctx_len</code></strong>:<code>int</code>, <strong><code>n_layers</code></strong>:<code>int</code>, <strong><code>n_heads</code></strong>:<code>int</code>, <strong><code>d_model</code></strong>:<code>int</code>, <strong><code>d_head</code></strong>:<code>int</code>, <strong><code>d_inner</code></strong>:<code>int</code>, <strong><code>resid_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>attn_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>ff_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>embed_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>bias</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>scale</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>act</code></strong>:<a href="/text.models.transformer.html#Activation"><code>Activation</code></a>=<strong><em><code><Activation.ReLU: 1></code></em></strong>, <strong><code>double_drop</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>attn_cls</code></strong>:<code>Callable</code>=<strong><em><code>'MultiHeadAttention'</code></em></strong>, <strong><code>learned_pos_enc</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>mask</code></strong>:<code>bool</code>=<strong><em><code>True</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="Transformer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Transformer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>Transformer</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>Transformer model: <a href="https://arxiv.org/abs/1706.03762">https://arxiv.org/abs/1706.03762</a>.</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 main idea of this article is to use regular neural net for NLP instead of an RNN, but with lots of attention layers. Intuitively, those attention layers tell the model to pay more interest to this or that world when trying to predict its output.</p>
<p>It starts from embeddings from <code>vocab_sz</code> (number of tokens) to <code>d_model</code> (which is basically the hidden size throughout the model), and it will look at inputs of size batch_size by <code>ctx_len</code> (for context length). We add a positional encoding to the embeddings (since a regular neural net has no idea of the order of words), either learned or coming from <a href="/text.models.transformer.html#PositionalEncoding"><code>PositionalEncoding</code></a> depending on <code>learned_pos_enc</code>. We then have a dropout of <code>embed_p</code> followed by <code>n_layers</code> blocks of <a href="/text.models.transformer.html#MultiHeadAttention"><code>MultiHeadAttention</code></a> followed by <a href="/text.models.transformer.html#feed_forward"><code>feed_forward</code></a>.</p>
<p>In the attention we use <code>n_heads</code> with each a hidden state of <code>d_head</code> (will default to <code>d_model//n_heads</code>). If <code>mask=True</code>, a mask will make sure no attention is paid to future tokens (which would be cheating when training a language model). If <code>scale=True</code>, the attention scores are scaled by a factor <code>1 / math.sqrt(d_head)</code>. A dropout of <code>attn_p</code> is applied to the attention scores, then the final result get applied a dropout of <code>resid_p</code> before being summed to the original input (residual connection before the layer norm).</p>
<p>In feed forward, we have two linear layers from <code>d_model</code> to <code>d_inner</code> and then back. Those have <code>bias</code> if that flag is <code>True</code> and a dropout of <code>ff_p</code> is applied, after each if <code>double_drop=True</code>, or just at the end otherwise. <code>act</code> is used in the middle as a non-linearity.</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">show_doc</span><span class="p">(</span><span class="n">TransformerXL</span><span class="p">,</span> <span class="n">title_level</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="TransformerXL" class="doc_header"><code>class</code> <code>TransformerXL</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L174" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TransformerXL-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TransformerXL</code>(<strong><code>vocab_sz</code></strong>:<code>int</code>, <strong><code>ctx_len</code></strong>:<code>int</code>, <strong><code>n_layers</code></strong>:<code>int</code>, <strong><code>n_heads</code></strong>:<code>int</code>, <strong><code>d_model</code></strong>:<code>int</code>, <strong><code>d_head</code></strong>:<code>int</code>, <strong><code>d_inner</code></strong>:<code>int</code>, <strong><code>resid_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>attn_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>ff_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>embed_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>bias</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>scale</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>act</code></strong>:<a href="/text.models.transformer.html#Activation"><code>Activation</code></a>=<strong><em><code><Activation.ReLU: 1></code></em></strong>, <strong><code>double_drop</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>attn_cls</code></strong>:<code>Callable</code>=<strong><em><code>'MultiHeadRelativeAttention'</code></em></strong>, <strong><code>learned_pos_enc</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>mask</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>mem_len</code></strong>:<code>int</code>=<strong><em><code>0</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="TransformerXL-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TransformerXL-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>TransformerXL</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>TransformerXL model: <a href="https://arxiv.org/abs/1901.02860">https://arxiv.org/abs/1901.02860</a>.</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>TransformerXL is a transformer architecture with a sort of hidden state formed by the results of the intermediate layers on previous tokens. Its size is determined by <code>mem_len</code>. By using this context, those models are capable of learning longer dependencies and can also be used for faster text generation at inference: a regular transformer model would have to reexamine the whole of sequence of indexes generated so far, whereas we can feed the new tokens one by one to a transformer XL (like we do with a regular RNN).</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">show_doc</span><span class="p">(</span><span class="n">TransformerXL</span><span class="o">.</span><span class="n">reset</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="TransformerXL.reset" class="doc_header"><code>reset</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L191" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TransformerXL-reset-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>reset</code>()</p>
</blockquote>
<div class="collapse" id="TransformerXL-reset-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TransformerXL-reset-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>reset</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>Reset the internal memory.</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="Decoders">Decoders<a class="anchor-link" href="#Decoders">¶</a></h2>
</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">show_doc</span><span class="p">(</span><span class="n">LinearDecoder</span><span class="p">,</span> <span class="n">title_level</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="LinearDecoder" class="doc_header"><code>class</code> <code>LinearDecoder</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L136" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#LinearDecoder-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>LinearDecoder</code>(<strong><code>n_out</code></strong>:<code>int</code>, <strong><code>n_hid</code></strong>:<code>int</code>, <strong><code>output_p</code></strong>:<code>float</code>, <strong><code>tie_encoder</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>bias</code></strong>:<code>bool</code>=<strong><em><code>True</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="LinearDecoder-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#LinearDecoder-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>LinearDecoder</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>To go on top of a RNNCore module and create a Language 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>Create a the decoder to go on top of an <a href="/text.models.awd_lstm.html#RNNCore"><code>RNNCore</code></a> encoder and create a language model. <code>n_hid</code> is the dimension of the last hidden state of the encoder, <code>n_out</code> the size of the output. Dropout of <code>output_p</code> is applied. If a <code>tie_encoder</code> is passed, it will be used for the weights of the linear layer, that will have <code>bias</code> or not.</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">show_doc</span><span class="p">(</span><span class="n">PoolingLinearClassifier</span><span class="p">,</span> <span class="n">title_level</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="PoolingLinearClassifier" class="doc_header"><code>class</code> <code>PoolingLinearClassifier</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/learner.py#L230" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#PoolingLinearClassifier-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>PoolingLinearClassifier</code>(<strong><code>layers</code></strong>:<code>Collection</code>[<code>int</code>], <strong><code>drops</code></strong>:<code>Collection</code>[<code>float</code>]) :: <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="PoolingLinearClassifier-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#PoolingLinearClassifier-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>PoolingLinearClassifier</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 linear classifier with pooling.</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 last output, <code>MaxPooling</code> of all the outputs and <code>AvgPooling</code> of all the outputs are concatenated, then blocks of <a href="/layers.html#bn_drop_lin"><code>bn_drop_lin</code></a> are stacked, according to the values in <a href="/layers.html#layers"><code>layers</code></a> and <code>drops</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="Basic-NLP-modules">Basic NLP modules<a class="anchor-link" href="#Basic-NLP-modules">¶</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>On top of the pytorch or the fastai <a href="/layers.html#layers"><code>layers</code></a>, the language models use some custom layers specific to NLP.</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="EmbeddingDropout" class="doc_header"><code>class</code> <code>EmbeddingDropout</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L57" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#EmbeddingDropout-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>EmbeddingDropout</code>(<strong><code>emb</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>embed_p</code></strong>:<code>float</code>) :: <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="EmbeddingDropout-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#EmbeddingDropout-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>EmbeddingDropout</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>Apply dropout with probabily <code>embed_p</code> to an embedding layer <code>emb</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>Each row of the embedding matrix has a probability <code>embed_p</code> of being replaced by zeros while the others are rescaled accordingly.</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">enc</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Embedding</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="n">padding_idx</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">enc_dp</span> <span class="o">=</span> <span class="n">EmbeddingDropout</span><span class="p">(</span><span class="n">enc</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
<span class="n">tst_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">100</span><span class="p">,(</span><span class="mi">8</span><span class="p">,))</span>
<span class="n">enc_dp</span><span class="p">(</span><span class="n">tst_input</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.7379, -1.3970, -0.4075, -0.1676, 2.0396, 3.2226, 0.7128],
[-0.0000, 0.0000, 0.0000, -0.0000, -0.0000, 0.0000, 0.0000],
[-3.2579, 2.2972, -1.8704, -0.4090, 2.6477, -1.5015, 0.7158],
[ 2.1455, 1.0571, -0.6086, 3.5700, 2.6271, -3.1353, 0.7277],
[-3.7003, -1.8846, 0.2029, -0.6839, 0.2968, -2.0199, 1.3127],
[-0.0000, 0.0000, -0.0000, -0.0000, 0.0000, 0.0000, -0.0000],
[-0.0051, 2.7428, 3.0068, 0.6242, 1.2747, 0.9262, 0.4070],
[ 1.9312, 3.0524, -1.2806, 1.5910, -2.1789, -0.1636, -3.4924]],
grad_fn=<EmbeddingBackward>)</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 ">
<h3 id="RNNDropout" class="doc_header"><code>class</code> <code>RNNDropout</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L17" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#RNNDropout-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>RNNDropout</code>(<strong><code>p</code></strong>:<code>float</code>=<strong><em><code>0.5</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="RNNDropout-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#RNNDropout-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>RNNDropout</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>Dropout with probability <code>p</code> that is consistent on the seq_len dimension.</p>
</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">dp</span> <span class="o">=</span> <span class="n">RNNDropout</span><span class="p">(</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">tst_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">7</span><span class="p">)</span>
<span class="n">tst_input</span><span class="p">,</span> <span class="n">dp</span><span class="p">(</span><span class="n">tst_input</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([[[-2.1156, 0.9734, 0.2428, 0.9396, 0.4072, -0.8197, 0.3718],
[ 0.4838, 1.3077, -0.8239, -0.6557, 1.3938, 0.6086, -0.2622],
[ 0.2372, -0.1627, 0.3117, -0.4811, -1.0841, -0.5207, -0.5131]],
[[-0.6924, 0.4122, 0.2517, -1.0120, 0.6808, 0.8800, -0.7463],
[-0.9498, 0.7655, 0.7471, -0.2767, 1.2155, -0.1042, -2.1443],
[-1.2342, 1.9187, -0.8481, -0.4115, -1.3223, 1.4266, -1.4150]],
[[ 0.1539, 0.3142, 0.2158, 1.1411, 0.1316, 0.6158, -1.5078],
[-1.0177, -0.9230, 0.9994, 0.1140, 0.7432, 0.4353, 0.0096],
[-0.8231, 1.0086, 1.7685, 0.3304, -0.0896, -1.0513, -1.3017]]]),
tensor([[[-3.0223, 1.3905, 0.0000, 0.0000, 0.5818, -0.0000, 0.5312],
[ 0.6911, 1.8681, -0.0000, -0.0000, 1.9911, 0.0000, -0.3745],
[ 0.3389, -0.2324, 0.0000, -0.0000, -1.5487, -0.0000, -0.7331]],
[[-0.9892, 0.5889, 0.3596, -1.4458, 0.9725, 1.2571, -0.0000],
[-1.3569, 1.0936, 1.0673, -0.3953, 1.7364, -0.1489, -0.0000],
[-1.7631, 2.7410, -1.2116, -0.5879, -1.8889, 2.0380, -0.0000]],
[[ 0.0000, 0.4489, 0.0000, 1.6301, 0.1880, 0.8797, -2.1539],
[-0.0000, -1.3186, 0.0000, 0.1628, 1.0617, 0.6218, 0.0137],
[-0.0000, 1.4408, 0.0000, 0.4720, -0.1280, -1.5019, -1.8595]]]))</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 ">
<h3 id="WeightDropout" class="doc_header"><code>class</code> <code>WeightDropout</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L27" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#WeightDropout-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>WeightDropout</code>(<strong><code>module</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>weight_p</code></strong>:<code>float</code>, <strong><code>layer_names</code></strong>:<code>StrList</code>=<strong><em><code>['weight_hh_l0']</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="WeightDropout-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#WeightDropout-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>WeightDropout</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 module that warps another layer in which some weights will be replaced by 0 during training.</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>Applies dropout of probability <code>weight_p</code> to the layers in <code>layer_names</code> of <code>module</code> in training mode. A copy of those weights is kept so that the dropout mask can change at every batch.</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">module</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">LSTM</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">dp_module</span> <span class="o">=</span> <span class="n">WeightDropout</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">)</span>
<span class="nb">getattr</span><span class="p">(</span><span class="n">dp_module</span><span class="o">.</span><span class="n">module</span><span class="p">,</span> <span class="s1">'weight_hh_l0'</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>Parameter containing:
tensor([[-0.0702, 0.5725],
[-0.3910, 0.6512],
[-0.2203, -0.4315],
[ 0.2750, -0.2917],
[-0.4890, -0.3094],
[ 0.4638, -0.3807],
[-0.2290, -0.6964],
[ 0.1224, 0.4043]], requires_grad=True)</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>It's at the beginning of a forward pass that the dropout is applied to the weights.</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">tst_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="n">h</span> <span class="o">=</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="n">x</span><span class="p">,</span><span class="n">h</span> <span class="o">=</span> <span class="n">dp_module</span><span class="p">(</span><span class="n">tst_input</span><span class="p">,</span><span class="n">h</span><span class="p">)</span>
<span class="nb">getattr</span><span class="p">(</span><span class="n">dp_module</span><span class="o">.</span><span class="n">module</span><span class="p">,</span> <span class="s1">'weight_hh_l0'</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.0000, 0.0000],
[-0.6517, 0.0000],
[-0.0000, -0.7191],
[ 0.4583, -0.0000],
[-0.0000, -0.0000],
[ 0.7730, -0.6345],
[-0.0000, -1.1607],
[ 0.2040, 0.6739]], grad_fn=<MulBackward0>)</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 ">
<h3 id="PositionalEncoding" class="doc_header"><code>class</code> <code>PositionalEncoding</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L11" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#PositionalEncoding-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>PositionalEncoding</code>(<strong><code>d</code></strong>:<code>int</code>) :: <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="PositionalEncoding-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#PositionalEncoding-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>PositionalEncoding</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>Encode the position with a sinusoid.</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="DecoderLayer" class="doc_header"><code>class</code> <code>DecoderLayer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L138" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#DecoderLayer-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>DecoderLayer</code>(<strong><code>n_heads</code></strong>:<code>int</code>, <strong><code>d_model</code></strong>:<code>int</code>, <strong><code>d_head</code></strong>:<code>int</code>, <strong><code>d_inner</code></strong>:<code>int</code>, <strong><code>resid_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>attn_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>ff_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>bias</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>scale</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>act</code></strong>:<a href="/text.models.transformer.html#Activation"><code>Activation</code></a>=<strong><em><code><Activation.ReLU: 1></code></em></strong>, <strong><code>double_drop</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>attn_cls</code></strong>:<code>Callable</code>=<strong><em><code>'MultiHeadAttention'</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="DecoderLayer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#DecoderLayer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>DecoderLayer</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 block of a Transformer model.</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="MultiHeadAttention" class="doc_header"><code>class</code> <code>MultiHeadAttention</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L33" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MultiHeadAttention-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>MultiHeadAttention</code>(<strong><code>n_heads</code></strong>:<code>int</code>, <strong><code>d_model</code></strong>:<code>int</code>, <strong><code>d_head</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>, <strong><code>resid_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>attn_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>bias</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>scale</code></strong>:<code>bool</code>=<strong><em><code>True</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="MultiHeadAttention-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MultiHeadAttention-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>MultiHeadAttention</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>MutiHeadAttention.</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="MultiHeadRelativeAttention" class="doc_header"><code>class</code> <code>MultiHeadRelativeAttention</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L89" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MultiHeadRelativeAttention-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>MultiHeadRelativeAttention</code>(<strong><code>n_heads</code></strong>:<code>int</code>, <strong><code>d_model</code></strong>:<code>int</code>, <strong><code>d_head</code></strong>:<code>int</code>, <strong><code>resid_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>attn_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>bias</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>scale</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>) :: <a href="/core.html#PrePostInitMeta"><code>PrePostInitMeta</code></a> :: <a href="/text.models.transformer.html#MultiHeadAttention"><code>MultiHeadAttention</code></a></p>
</blockquote>
<div class="collapse" id="MultiHeadRelativeAttention-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MultiHeadRelativeAttention-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>MultiHeadRelativeAttention</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>MutiHeadAttention with relative positional encoding.</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="SequentialRNN" class="doc_header"><code>class</code> <code>SequentialRNN</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L153" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#SequentialRNN-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>SequentialRNN</code>(<strong>*<code>args</code></strong>) :: <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential"><code>Sequential</code></a></p>
</blockquote>
<div class="collapse" id="SequentialRNN-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#SequentialRNN-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>SequentialRNN</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 sequential module that passes the reset call to its children.</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="SequentialRNN.reset" class="doc_header"><code>reset</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L155" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#SequentialRNN-reset-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>reset</code>()</p>
</blockquote>
<div class="collapse" id="SequentialRNN-reset-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#SequentialRNN-reset-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>reset</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>Call the <code>reset</code> function of <a href="/torch_core.html#children"><code>self.children</code></a> (if they have one).</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="dropout_mask" class="doc_header"><code>dropout_mask</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/awd_lstm.py#L13" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#dropout_mask-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>dropout_mask</code>(<strong><code>x</code></strong>:<code>Tensor</code>, <strong><code>sz</code></strong>:<code>Collection</code>[<code>int</code>], <strong><code>p</code></strong>:<code>float</code>)</p>
</blockquote>
<div class="collapse" id="dropout_mask-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#dropout_mask-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>dropout_mask</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 a dropout mask of the same type as <code>x</code>, size <code>sz</code>, with probability <code>p</code> to cancel an element.</p>
</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">tst_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">7</span><span class="p">)</span>
<span class="n">dropout_mask</span><span class="p">(</span><span class="n">tst_input</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">7</span><span class="p">),</span> <span class="mf">0.3</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.0000, 1.4286, 1.4286, 0.0000, 1.4286, 1.4286, 0.0000],
[1.4286, 1.4286, 1.4286, 0.0000, 1.4286, 0.0000, 0.0000],
[1.4286, 0.0000, 1.4286, 0.0000, 0.0000, 0.0000, 1.4286]])</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>Such a mask is then expanded in the sequence length dimension and multiplied by the input to do an <a href="/text.models.awd_lstm.html#RNNDropout"><code>RNNDropout</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="feed_forward" class="doc_header"><code>feed_forward</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/models/transformer.py#L28" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#feed_forward-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>feed_forward</code>(<strong><code>d_model</code></strong>:<code>int</code>, <strong><code>d_ff</code></strong>:<code>int</code>, <strong><code>ff_p</code></strong>:<code>float</code>=<strong><em><code>0.0</code></em></strong>, <strong><code>act</code></strong>:<a href="/text.models.transformer.html#Activation"><code>Activation</code></a>=<strong><em><code><Activation.ReLU: 1></code></em></strong>, <strong><code>double_drop</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="feed_forward-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#feed_forward-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>feed_forward</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>