forked from fastai/fastai
/
text.data.html
1402 lines (1099 loc) · 90.8 KB
/
text.data.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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: text.data
keywords: fastai
sidebar: home_sidebar
summary: "Basic dataset for NLP tasks and helper functions to create a DataBunch"
---
<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-datasets">NLP datasets<a class="anchor-link" href="#NLP-datasets">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This module contains the <a href="/text.data.html#TextDataset"><code>TextDataset</code></a> class, which is the main dataset you should use for your NLP tasks. It automatically does the preprocessing steps described in <a href="/text.transform.html#text.transform"><code>text.transform</code></a>. It also contains all the functions to quickly get a <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> ready.</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-assemble-your-data">Quickly assemble your data<a class="anchor-link" href="#Quickly-assemble-your-data">¶</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>You should get your data in one of the following formats to make the most of the fastai library and use one of the factory methods of one of the <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> classes:</p>
<ul>
<li>raw text files in folders train, valid, test in an ImageNet style,</li>
<li>a csv where some column(s) gives the label(s) and the following one the associated text,</li>
<li>a dataframe structured the same way,</li>
<li>tokens and labels arrays,</li>
<li>ids, vocabulary (correspondence id to word) and labels.</li>
</ul>
<p>If you are assembling the data for a language model, you should define your labels as always 0 to respect those formats. The first time you create a <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> with one of those functions, your data will be preprocessed automatically. You can save it, so that the next time you call it is almost instantaneous.</p>
<p>Below are the classes that help assembling the raw data in a <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> suitable for 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="TextLMDataBunch" class="doc_header"><code>class</code> <code>TextLMDataBunch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L235" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextLMDataBunch-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TextLMDataBunch</code>(<strong><code>train_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>valid_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>fix_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>test_dl</code></strong>:<code>Optional</code>[<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>]=<strong><em><code>None</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>dl_tfms</code></strong>:<code>Optional</code>[<code>Collection</code>[<code>Callable</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>collate_fn</code></strong>:<code>Callable</code>=<strong><em><code>'data_collate'</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextLMDataBunch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextLMDataBunch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>TextLMDataBunch</code>:</p><p>Related tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_should_load_backwards_lm_1</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L83" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_should_load_backwards_lm_2</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L99" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_from_csv_and_from_df</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L57" 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="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> suitable for training 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>All the texts in the <a href="/datasets.html#datasets"><code>datasets</code></a> are concatenated and the labels are ignored. Instead, the target is the next word in the sentence.</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="TextLMDataBunch.create" class="doc_header"><code>create</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L237" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextLMDataBunch-create-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>create</code>(<strong><code>train_ds</code></strong>, <strong><code>valid_ds</code></strong>, <strong><code>test_ds</code></strong>=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>bs</code></strong>=<strong><em><code>64</code></em></strong>, <strong><code>val_bs</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>, <strong><code>num_workers</code></strong>:<code>int</code>=<strong><em><code>0</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>collate_fn</code></strong>:<code>Callable</code>=<strong><em><code>'data_collate'</code></em></strong>, <strong><code>dl_tfms</code></strong>:<code>Optional</code>[<code>Collection</code>[<code>Callable</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>bptt</code></strong>:<code>int</code>=<strong><em><code>70</code></em></strong>, <strong><code>backwards</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong>**<code>dl_kwargs</code></strong>) → <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextLMDataBunch-create-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextLMDataBunch-create-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>create</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create a <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> in <code>path</code> from the <code>datasets</code> for language modelling. Passes <code>**dl_kwargs</code> on to <code>DataLoader()</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 ">
<h3 id="TextClasDataBunch" class="doc_header"><code>class</code> <code>TextClasDataBunch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L250" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextClasDataBunch-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TextClasDataBunch</code>(<strong><code>train_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>valid_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>fix_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>test_dl</code></strong>:<code>Optional</code>[<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>]=<strong><em><code>None</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>dl_tfms</code></strong>:<code>Optional</code>[<code>Collection</code>[<code>Callable</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>collate_fn</code></strong>:<code>Callable</code>=<strong><em><code>'data_collate'</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextClasDataBunch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextClasDataBunch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>TextClasDataBunch</code>:</p><p>Related tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_load_and_save_test</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L129" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_backwards_cls_databunch</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L110" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_from_ids_works_for_equally_length_sentences</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L158" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_from_ids_works_for_variable_length_sentences</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L166" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_from_csv_and_from_df</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L57" 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="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> suitable for training an RNN classifier.</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="TextClasDataBunch.create" class="doc_header"><code>create</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L252" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextClasDataBunch-create-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>create</code>(<strong><code>train_ds</code></strong>, <strong><code>valid_ds</code></strong>, <strong><code>test_ds</code></strong>=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>bs</code></strong>:<code>int</code>=<strong><em><code>32</code></em></strong>, <strong><code>val_bs</code></strong>:<code>int</code>=<strong><em><code>None</code></em></strong>, <strong><code>pad_idx</code></strong>=<strong><em><code>1</code></em></strong>, <strong><code>pad_first</code></strong>=<strong><em><code>True</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>backwards</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong>**<code>dl_kwargs</code></strong>) → <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextClasDataBunch-create-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextClasDataBunch-create-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>create</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>Function that transform the <code>datasets</code> in a <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> for classification. Passes <code>**dl_kwargs</code> on to <code>DataLoader()</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>All the texts are grouped by length (with a bit of randomness for the training set) then padded so that the samples have the same length to get in a batch.</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="TextDataBunch" class="doc_header"><code>class</code> <code>TextDataBunch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L147" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TextDataBunch</code>(<strong><code>train_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>valid_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>, <strong><code>fix_dl</code></strong>:<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>test_dl</code></strong>:<code>Optional</code>[<a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader"><code>DataLoader</code></a>]=<strong><em><code>None</code></em></strong>, <strong><code>device</code></strong>:<a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-device"><code>device</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>dl_tfms</code></strong>:<code>Optional</code>[<code>Collection</code>[<code>Callable</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>collate_fn</code></strong>:<code>Callable</code>=<strong><em><code>'data_collate'</code></em></strong>, <strong><code>no_check</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextDataBunch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>TextDataBunch</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>General class to get a <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> for NLP. Subclassed by <a href="/text.data.html#TextLMDataBunch"><code>TextLMDataBunch</code></a> and <a href="/text.data.html#TextClasDataBunch"><code>TextClasDataBunch</code></a>.</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 ">
<div markdown="span" class="alert alert-danger" role="alert"><i class="fa fa-danger-circle"></i> <b>Warning: </b>This class can only work directly if all the texts have the same length.</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">
<h3 id="Factory-methods-(TextDataBunch)">Factory methods (TextDataBunch)<a class="anchor-link" href="#Factory-methods-(TextDataBunch)">¶</a></h3>
</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>All those classes have the following factory methods.</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="TextDataBunch.from_folder" class="doc_header"><code>from_folder</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L221" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-from_folder-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_folder</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>, <strong><code>train</code></strong>:<code>str</code>=<strong><em><code>'train'</code></em></strong>, <strong><code>valid</code></strong>:<code>str</code>=<strong><em><code>'valid'</code></em></strong>, <strong><code>test</code></strong>:<code>Optional</code>[<code>str</code>]=<strong><em><code>None</code></em></strong>, <strong><code>classes</code></strong>:<code>ArgStar</code>=<strong><em><code>None</code></em></strong>, <strong><code>tokenizer</code></strong>:<a href="/text.transform.html#Tokenizer"><code>Tokenizer</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>chunksize</code></strong>:<code>int</code>=<strong><em><code>10000</code></em></strong>, <strong><code>max_vocab</code></strong>:<code>int</code>=<strong><em><code>60000</code></em></strong>, <strong><code>min_freq</code></strong>:<code>int</code>=<strong><em><code>2</code></em></strong>, <strong><code>mark_fields</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="TextDataBunch-from_folder-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-from_folder-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>from_folder</code>:</p><p>Direct tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_from_folder</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L30" class="source_link" style="float:right">[source]</a></li></ul><p>Related tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_filter_classes</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L42" 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="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> from text files in folders.</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 floders are scanned in <code>path</code> with a <code>train</code>, <code>valid</code> and maybe <code>test</code> folders. Text files in the <code>train</code> and <code>valid</code> folders should be places in subdirectories according to their classes (not applicable for a language model). <code>tokenizer</code> will be used to parse those texts into tokens.</p>
<p>You can pass a specific <code>vocab</code> for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). kwargs will be split between the <a href="/text.data.html#TextDataset"><code>TextDataset</code></a> function and to the class initialization, you can precise there parameters such as <code>max_vocab</code>, <code>chunksize</code>, <code>min_freq</code>, <code>n_labels</code> (see the <a href="/text.data.html#TextDataset"><code>TextDataset</code></a> documentation) or <code>bs</code>, <code>bptt</code> and <code>pad_idx</code> (see the sections LM data and classifier data).</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="TextDataBunch.from_csv" class="doc_header"><code>from_csv</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L206" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-from_csv-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_csv</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>, <strong><code>csv_name</code></strong>, <strong><code>valid_pct</code></strong>:<code>float</code>=<strong><em><code>0.2</code></em></strong>, <strong><code>test</code></strong>:<code>Optional</code>[<code>str</code>]=<strong><em><code>None</code></em></strong>, <strong><code>tokenizer</code></strong>:<a href="/text.transform.html#Tokenizer"><code>Tokenizer</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>classes</code></strong>:<code>StrList</code>=<strong><em><code>None</code></em></strong>, <strong><code>delimiter</code></strong>:<code>str</code>=<strong><em><code>None</code></em></strong>, <strong><code>header</code></strong>=<strong><em><code>'infer'</code></em></strong>, <strong><code>text_cols</code></strong>:<code>IntsOrStrs</code>=<strong><em><code>1</code></em></strong>, <strong><code>label_cols</code></strong>:<code>IntsOrStrs</code>=<strong><em><code>0</code></em></strong>, <strong><code>label_delim</code></strong>:<code>str</code>=<strong><em><code>None</code></em></strong>, <strong><code>chunksize</code></strong>:<code>int</code>=<strong><em><code>10000</code></em></strong>, <strong><code>max_vocab</code></strong>:<code>int</code>=<strong><em><code>60000</code></em></strong>, <strong><code>min_freq</code></strong>:<code>int</code>=<strong><em><code>2</code></em></strong>, <strong><code>mark_fields</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong>**<code>kwargs</code></strong>) → <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextDataBunch-from_csv-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-from_csv-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>from_csv</code>:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_from_csv_and_from_df</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L57" 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="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> from texts in csv files. <code>kwargs</code> are passed to the dataloader creation.</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 method will look for <code>csv_name</code>, and optionally a <code>test</code> csv file, in <code>path</code>. These will be opened with <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html#pandas-read-csv"><code>header</code></a>, using <code>delimiter</code>. You can specify which are the <code>text_cols</code> and <code>label_cols</code>; by default a single label column is assumed to come before a single text column. If your csv has no header, you must specify these as indices. If you're training a language model and don't have labels, you must specify the <code>text_cols</code>. If there are several <code>text_cols</code>, the texts will be concatenated together with an optional field token. If there are several <code>label_cols</code>, the labels will be assumed to be one-hot encoded and <code>classes</code> will default to <code>label_cols</code> (you can ignore that argument for a language model). <code>label_delim</code> can be used to specify the separator between multiple labels in a column.</p>
<p>You can pass a <code>tokenizer</code> to be used to parse the texts into tokens and/or a specific <code>vocab</code> for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). Otherwise you can specify parameters such as <code>max_vocab</code>, <code>min_freq</code>, <code>chunksize</code> for the Tokenizer and Numericalizer (processors). Other parameters (e.g. <code>bs</code>, <code>val_bs</code> and <code>num_workers</code>, etc.) will be passed to <a href="/data_block.html#LabelLists.databunch"><code>LabelLists.databunch()</code></a> documentation) (see the LM data and classifier data sections for more info).</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="TextDataBunch.from_df" class="doc_header"><code>from_df</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L190" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-from_df-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_df</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>, <strong><code>train_df</code></strong>:<code>DataFrame</code>, <strong><code>valid_df</code></strong>:<code>DataFrame</code>, <strong><code>test_df</code></strong>:<code>OptDataFrame</code>=<strong><em><code>None</code></em></strong>, <strong><code>tokenizer</code></strong>:<a href="/text.transform.html#Tokenizer"><code>Tokenizer</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>classes</code></strong>:<code>StrList</code>=<strong><em><code>None</code></em></strong>, <strong><code>text_cols</code></strong>:<code>IntsOrStrs</code>=<strong><em><code>1</code></em></strong>, <strong><code>label_cols</code></strong>:<code>IntsOrStrs</code>=<strong><em><code>0</code></em></strong>, <strong><code>label_delim</code></strong>:<code>str</code>=<strong><em><code>None</code></em></strong>, <strong><code>chunksize</code></strong>:<code>int</code>=<strong><em><code>10000</code></em></strong>, <strong><code>max_vocab</code></strong>:<code>int</code>=<strong><em><code>60000</code></em></strong>, <strong><code>min_freq</code></strong>:<code>int</code>=<strong><em><code>2</code></em></strong>, <strong><code>mark_fields</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong>**<code>kwargs</code></strong>) → <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextDataBunch-from_df-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-from_df-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>from_df</code>:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_from_csv_and_from_df</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L57" class="source_link" style="float:right">[source]</a></li></ul><p>Related tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_load_and_save_test</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L129" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_should_load_backwards_lm_1</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L83" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_backwards_cls_databunch</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L110" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_should_load_backwards_lm_2</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L99" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_regression</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L174" 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="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> from DataFrames. <code>kwargs</code> are passed to the dataloader creation.</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 method will use <code>train_df</code>, <code>valid_df</code> and optionally <code>test_df</code> to build the <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> in <code>path</code>. You can specify <code>text_cols</code> and <code>label_cols</code>; by default a single label column comes before a single text column. If you're training a language model and don't have labels, you must specify the <code>text_cols</code>. If there are several <code>text_cols</code>, the texts will be concatenated together with an optional field token. If there are several <code>label_cols</code>, the labels will be assumed to be one-hot encoded and <code>classes</code> will default to <code>label_cols</code> (you can ignore that argument for a language model).</p>
<p>You can pass a <code>tokenizer</code> to be used to parse the texts into tokens and/or a specific <code>vocab</code> for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). Otherwise you can specify parameters such as <code>max_vocab</code>, <code>min_freq</code>, <code>chunksize</code> for the default Tokenizer and Numericalizer (processors). Other parameters (e.g. <code>bs</code>, <code>val_bs</code> and <code>num_workers</code>, etc.) will be passed to <a href="/data_block.html#LabelLists.databunch"><code>LabelLists.databunch()</code></a> documentation) (see the LM data and classifier data sections for more info).</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="TextDataBunch.from_tokens" class="doc_header"><code>from_tokens</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L177" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-from_tokens-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_tokens</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>, <strong><code>trn_tok</code></strong>:<code>Tokens</code>, <strong><code>trn_lbls</code></strong>:<code>Collection</code>[<code>Union</code>[<code>int</code>, <code>float</code>]], <strong><code>val_tok</code></strong>:<code>Tokens</code>, <strong><code>val_lbls</code></strong>:<code>Collection</code>[<code>Union</code>[<code>int</code>, <code>float</code>]], <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>tst_tok</code></strong>:<code>Tokens</code>=<strong><em><code>None</code></em></strong>, <strong><code>classes</code></strong>:<code>ArgStar</code>=<strong><em><code>None</code></em></strong>, <strong><code>max_vocab</code></strong>:<code>int</code>=<strong><em><code>60000</code></em></strong>, <strong><code>min_freq</code></strong>:<code>int</code>=<strong><em><code>3</code></em></strong>, <strong>**<code>kwargs</code></strong>) → <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextDataBunch-from_tokens-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-from_tokens-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>from_tokens</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create a <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> from tokens and labels. <code>kwargs</code> are passed to the dataloader creation.</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 function will create a <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> from <code>trn_tok</code>, <code>trn_lbls</code>, <code>val_tok</code>, <code>val_lbls</code> and maybe <code>tst_tok</code>.</p>
<p>You can pass a specific <code>vocab</code> for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). kwargs will be split between the <a href="/text.data.html#TextDataset"><code>TextDataset</code></a> function and to the class initialization, you can precise there parameters such as <code>max_vocab</code>, <code>chunksize</code>, <code>min_freq</code>, <code>n_labels</code>, <code>tok_suff</code> and <code>lbl_suff</code> (see the <a href="/text.data.html#TextDataset"><code>TextDataset</code></a> documentation) or <code>bs</code>, <code>bptt</code> and <code>pad_idx</code> (see the sections LM data and classifier data).</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="TextDataBunch.from_ids" class="doc_header"><code>from_ids</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L150" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-from_ids-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_ids</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>, <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>, <strong><code>train_ids</code></strong>:<code>Collection</code>[<code>Collection</code>[<code>int</code>]], <strong><code>valid_ids</code></strong>:<code>Collection</code>[<code>Collection</code>[<code>int</code>]], <strong><code>test_ids</code></strong>:<code>Collection</code>[<code>Collection</code>[<code>int</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>train_lbls</code></strong>:<code>Collection</code>[<code>Union</code>[<code>int</code>, <code>float</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>valid_lbls</code></strong>:<code>Collection</code>[<code>Union</code>[<code>int</code>, <code>float</code>]]=<strong><em><code>None</code></em></strong>, <strong><code>classes</code></strong>:<code>ArgStar</code>=<strong><em><code>None</code></em></strong>, <strong><code>processor</code></strong>:<a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a>=<strong><em><code>None</code></em></strong>, <strong>**<code>kwargs</code></strong>) → <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a></p>
</blockquote>
<div class="collapse" id="TextDataBunch-from_ids-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-from_ids-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>from_ids</code>:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_from_ids_works_for_equally_length_sentences</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L158" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_from_ids_works_for_variable_length_sentences</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L166" 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="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> from ids, labels and a <code>vocab</code>. <code>kwargs</code> are passed to the dataloader creation.</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>Texts are already preprocessed into <code>train_ids</code>, <code>train_lbls</code>, <code>valid_ids</code>, <code>valid_lbls</code> and maybe <code>test_ids</code>. You can specify the corresponding <code>classes</code> if applicable. You must specify a <code>path</code> and the <code>vocab</code> so that the <a href="/text.learner.html#RNNLearner"><code>RNNLearner</code></a> class can later infer the corresponding sizes in the model it will create. kwargs will be passed to the class initialization.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Load-and-save">Load and save<a class="anchor-link" href="#Load-and-save">¶</a></h3>
</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>To avoid losing time preprocessing the text data more than once, you should save and load your <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> using <a href="/basic_data.html#DataBunch.save"><code>DataBunch.save</code></a> and <a href="/basic_data.html#load_data"><code>load_data</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="TextDataBunch.load" class="doc_header"><code>load</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L164" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextDataBunch-load-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>load</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>, <strong><code>cache_name</code></strong>:<code>PathOrStr</code>=<strong><em><code>'tmp'</code></em></strong>, <strong><code>processor</code></strong>:<a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a>=<strong><em><code>None</code></em></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="TextDataBunch-load-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextDataBunch-load-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>load</code>:</p><p>Direct tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_load_and_save_test</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L129" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_should_load_backwards_lm_1</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L83" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_should_load_backwards_lm_2</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L99" 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>Load a <a href="/text.data.html#TextDataBunch"><code>TextDataBunch</code></a> from <code>path/cache_name</code>. <code>kwargs</code> are passed to the dataloader creation.</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 ">
<div markdown="span" class="alert alert-danger" role="alert"><i class="fa fa-danger-circle"></i> <b>Warning: </b>This method should only be used to load back `TextDataBunch` saved in v1.0.43 or before, it is now deprecated.</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">
<h3 id="Example">Example<a class="anchor-link" href="#Example">¶</a></h3>
</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>Untar the IMDB sample dataset if not already done:</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">path</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>PosixPath('/home/ubuntu/.fastai/data/imdb_sample')</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>Since it comes in the form of csv files, we will use the corresponding <code>text_data</code> method. Here is an overview of what your file you should look like:</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">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="o">/</span><span class="s1">'texts.csv'</span><span class="p">)</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>label</th>
<th>text</th>
<th>is_valid</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>negative</td>
<td>Un-bleeping-believable! Meg Ryan doesn't even ...</td>
<td>False</td>
</tr>
<tr>
<th>1</th>
<td>positive</td>
<td>This is a extremely well-made film. The acting...</td>
<td>False</td>
</tr>
<tr>
<th>2</th>
<td>negative</td>
<td>Every once in a long while a movie will come a...</td>
<td>False</td>
</tr>
<tr>
<th>3</th>
<td>positive</td>
<td>Name just says it all. I watched this movie wi...</td>
<td>False</td>
</tr>
<tr>
<th>4</th>
<td>negative</td>
<td>This movie succeeds at being one of the most u...</td>
<td>False</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>And here is a simple way of creating your <a href="/basic_data.html#DataBunch"><code>DataBunch</code></a> for language modelling or classification.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">data_lm</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="n">path</span><span class="p">),</span> <span class="s1">'texts.csv'</span><span class="p">)</span>
<span class="n">data_clas</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="n">path</span><span class="p">),</span> <span class="s1">'texts.csv'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="The-TextList-input-classes">The TextList input classes<a class="anchor-link" href="#The-TextList-input-classes">¶</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>Behind the scenes, the previous functions will create a training, validation and maybe test <a href="/text.data.html#TextList"><code>TextList</code></a> that will be tokenized and numericalized (if needed) using <a href="/data_block.html#PreProcessor"><code>PreProcessor</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="Text" class="doc_header"><code>class</code> <code>Text</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L272" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#Text-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>Text</code>(<strong><code>ids</code></strong>, <strong><code>text</code></strong>) :: <a href="/core.html#ItemBase"><code>ItemBase</code></a></p>
</blockquote>
<div class="collapse" id="Text-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#Text-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>Text</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 item for <code>text</code> data in numericalized <code>ids</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 ">
<h3 id="TextList" class="doc_header"><code>class</code> <code>TextList</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L309" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextList-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TextList</code>(<strong><code>items</code></strong>:<code>Iterator</code>[<code>T_co</code>], <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>pad_idx</code></strong>:<code>int</code>=<strong><em><code>1</code></em></strong>, <strong>**<code>kwargs</code></strong>) :: <a href="/data_block.html#ItemList"><code>ItemList</code></a></p>
</blockquote>
<div class="collapse" id="TextList-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextList-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>TextList</code>:</p><p>Related tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_filter_classes</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L42" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_from_folder</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L30" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_text_data.py::test_regression</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L174" 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>Basic <a href="/data_block.html#ItemList"><code>ItemList</code></a> for text data.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><code>vocab</code> contains the correspondence between ids and tokens, <code>pad_idx</code> is the id used for padding. You can pass a custom <code>processor</code> in the <code>kwargs</code> to change the defaults for tokenization or numericalization. It should have the following form:</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">processor</span> <span class="o">=</span> <span class="p">[</span><span class="n">TokenizeProcessor</span><span class="p">(</span><span class="n">tokenizer</span><span class="o">=</span><span class="n">SpacyTokenizer</span><span class="p">(</span><span class="s1">'en'</span><span class="p">)),</span> <span class="n">NumericalizeProcessor</span><span class="p">(</span><span class="n">max_vocab</span><span class="o">=</span><span class="mi">30000</span><span class="p">)]</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>See below for all the arguments those tokenizers can take.</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="TextList.label_for_lm" class="doc_header"><code>label_for_lm</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L324" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextList-label_for_lm-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>label_for_lm</code>(<strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="TextList-label_for_lm-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextList-label_for_lm-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>label_for_lm</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 special labelling method for language models.</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="TextList.from_folder" class="doc_header"><code>from_folder</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L334" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextList-from_folder-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_folder</code>(<strong><code>path</code></strong>:<code>PathOrStr</code>=<strong><em><code>'.'</code></em></strong>, <strong><code>extensions</code></strong>:<code>StrList</code>=<strong><em><code>{'.txt'}</code></em></strong>, <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>processor</code></strong>:<a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a>=<strong><em><code>None</code></em></strong>, <strong>**<code>kwargs</code></strong>) → <code>TextList</code></p>
</blockquote>
<div class="collapse" id="TextList-from_folder-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextList-from_folder-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>from_folder</code>:</p><p>Direct tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_from_folder</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L30" class="source_link" style="float:right">[source]</a></li></ul><p>Related tests:</p><ul><li><code>pytest -sv tests/test_text_data.py::test_filter_classes</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_text_data.py#L42" 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>Get the list of files in <code>path</code> that have a text suffix. <code>recurse</code> determines if we search subfolders.</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="TextList.show_xys" class="doc_header"><code>show_xys</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L341" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextList-show_xys-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>show_xys</code>(<strong><code>xs</code></strong>, <strong><code>ys</code></strong>, <strong><code>max_len</code></strong>:<code>int</code>=<strong><em><code>70</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="TextList-show_xys-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextList-show_xys-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>show_xys</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>Show the <code>xs</code> (inputs) and <code>ys</code> (targets). <code>max_len</code> is the maximum number of tokens displayed.</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="TextList.show_xyzs" class="doc_header"><code>show_xyzs</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L354" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TextList-show_xyzs-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>show_xyzs</code>(<strong><code>xs</code></strong>, <strong><code>ys</code></strong>, <strong><code>zs</code></strong>, <strong><code>max_len</code></strong>:<code>int</code>=<strong><em><code>70</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="TextList-show_xyzs-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TextList-show_xyzs-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>show_xyzs</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>Show <code>xs</code> (inputs), <code>ys</code> (targets) and <code>zs</code> (predictions). <code>max_len</code> is the maximum number of tokens displayed.</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="OpenFileProcessor" class="doc_header"><code>class</code> <code>OpenFileProcessor</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L304" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#OpenFileProcessor-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>OpenFileProcessor</code>(<strong><code>ds</code></strong>:<code>Collection</code>[<code>T_co</code>]=<strong><em><code>None</code></em></strong>) :: <a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a></p>
</blockquote>
<div class="collapse" id="OpenFileProcessor-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#OpenFileProcessor-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>OpenFileProcessor</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p><a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a> that opens the filenames and read the texts.</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="open_text" class="doc_header"><code>open_text</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L268" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#open_text-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>open_text</code>(<strong><code>fn</code></strong>:<code>PathOrStr</code>, <strong><code>enc</code></strong>=<strong><em><code>'utf-8'</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="open_text-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#open_text-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>open_text</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>Read the text in <code>fn</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 ">
<h3 id="TokenizeProcessor" class="doc_header"><code>class</code> <code>TokenizeProcessor</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L277" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#TokenizeProcessor-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>TokenizeProcessor</code>(<strong><code>ds</code></strong>:<a href="/data_block.html#ItemList"><code>ItemList</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>tokenizer</code></strong>:<a href="/text.transform.html#Tokenizer"><code>Tokenizer</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>chunksize</code></strong>:<code>int</code>=<strong><em><code>10000</code></em></strong>, <strong><code>mark_fields</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a></p>
</blockquote>
<div class="collapse" id="TokenizeProcessor-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#TokenizeProcessor-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>TokenizeProcessor</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p><a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a> that tokenizes the texts in <code>ds</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><code>tokenizer</code> is used on bits of <code>chunksize</code>. If <code>mark_fields=True</code>, add field tokens between each parts of the texts (given when the texts are read in several columns of a dataframe). See more about tokenizers in the <a href="/text.transform.html">transform documentation</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="NumericalizeProcessor" class="doc_header"><code>class</code> <code>NumericalizeProcessor</code><a href="https://github.com/fastai/fastai/blob/master/fastai/text/data.py#L292" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#NumericalizeProcessor-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>NumericalizeProcessor</code>(<strong><code>ds</code></strong>:<a href="/data_block.html#ItemList"><code>ItemList</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>vocab</code></strong>:<a href="/text.transform.html#Vocab"><code>Vocab</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>max_vocab</code></strong>:<code>int</code>=<strong><em><code>60000</code></em></strong>, <strong><code>min_freq</code></strong>:<code>int</code>=<strong><em><code>3</code></em></strong>) :: <a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a></p>
</blockquote>
<div class="collapse" id="NumericalizeProcessor-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#NumericalizeProcessor-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>NumericalizeProcessor</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p><a href="/data_block.html#PreProcessor"><code>PreProcessor</code></a> that numericalizes the tokens in <code>ds</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>Uses <code>vocab</code> for this (if not None), otherwise create one with <code>max_vocab</code> and <code>min_freq</code> from tokens.</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-data">Language Model data<a class="anchor-link" href="#Language-Model-data">¶</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>A language model is trained to guess what the next word is inside a flow of words. We don't feed it the different texts separately but concatenate them all together in a big array. To create the batches, we split this array into <code>bs</code> chunks of continuous texts. Note that in all NLP tasks, we don't use the usual convention of sequence length being the first dimension so batch size is the first dimension and sequence length is the second. Here you can read the chunks of texts in lines.</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">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">x</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">train_dl</span><span class="p">))</span>
<span class="n">example</span> <span class="o">=</span> <span class="n">x</span><span class="p">[:</span><span class="mi">15</span><span class="p">,:</span><span class="mi">15</span><span class="p">]</span><span class="o">.</span><span class="n">cpu</span><span class="p">()</span>
<span class="n">texts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="n">data</span><span class="o">.</span><span class="n">train_ds</span><span class="o">.</span><span class="n">vocab</span><span class="o">.</span><span class="n">textify</span><span class="p">(</span><span class="n">l</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">example</span><span class="p">])</span>
<span class="n">texts</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>xxbos</td>
<td>xxmaj</td>
<td>name</td>
<td>just</td>
<td>says</td>
<td>it</td>
<td>all</td>
<td>.</td>
<td>i</td>
<td>watched</td>
<td>this</td>
<td>movie</td>
<td>with</td>
<td>my</td>
<td>dad</td>
</tr>
<tr>
<th>1</th>
<td>going</td>
<td>to</td>
<td>see</td>
<td>anything</td>
<td>you</td>
<td>'ll</td>
<td>remember</td>
<td>.</td>
<td>xxbos</td>
<td>xxmaj</td>
<td>evidently</td>
<td>when</td>
<td>you</td>
<td>offer</td>
<td>a</td>
</tr>
<tr>
<th>2</th>
<td>of</td>
<td>the</td>
<td>xxunk</td>
<td>xxunk</td>
<td>xxmaj</td>
<td>superbly</td>
<td>shot</td>
<td>,</td>
<td>this</td>
<td>thrilling</td>
<td>adult</td>
<td>adventure</td>
<td>certainly</td>
<td>contains</td>
<td>some</td>
</tr>
<tr>
<th>3</th>
<td>\n\n</td>
<td>xxmaj</td>
<td>one</td>
<td>bright</td>
<td>light</td>
<td>in</td>
<td>the</td>
<td>midst</td>
<td>of</td>
<td>this</td>
<td>is</td>
<td>xxmaj</td>
<td>fred</td>
<td>xxmaj</td>
<td>xxunk</td>
</tr>
<tr>
<th>4</th>
<td>leave</td>
<td>you</td>
<td>xxunk</td>
<td>that</td>
<td>you</td>
<td>watched</td>
<td>it</td>
<td>.</td>
<td>i</td>
<td>feel</td>
<td>really</td>
<td>bad</td>
<td>for</td>
<td>those</td>
<td>xxmaj</td>
</tr>
<tr>
<th>5</th>
<td>xxmaj</td>
<td>ginger</td>
<td>'s</td>
<td>blonde</td>
<td>hair</td>
<td>)</td>
<td>has</td>
<td>a</td>
<td>couple</td>
<td>of</td>
<td>emotional</td>
<td>solo</td>
<td>numbers</td>
<td>,</td>
<td>including</td>
</tr>
<tr>
<th>6</th>
<td>.</td>
<td>xxmaj</td>
<td>this</td>