# ULMFiT


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from fastai.text.all import *
from nbdev.showdoc import show_doc
```

## Finetune a pretrained Language Model

First we get our data and tokenize it.

``` python
path = untar_data(URLs.IMDB)
```

``` python
texts = get_files(path, extensions=['.txt'], folders=['unsup', 'train', 'test'])
len(texts)
```

    100000

Then we put it in a
[`Datasets`](https://docs.fast.ai/data.core.html#datasets). For a
language model, we don’t have targets, so there is only one transform to
numericalize the texts. Note that
[`tokenize_df`](https://docs.fast.ai/text.core.html#tokenize_df) returns
the count of the words in the corpus to make it easy to create a
vocabulary.

``` python
def read_file(f): return L(f.read_text().split(' '))
```

``` python
splits = RandomSplitter(valid_pct=0.1)(texts)
tfms = [Tokenizer.from_folder(path), Numericalize()]
dsets = Datasets(texts, [tfms], splits=splits, dl_type=LMDataLoader)
```

Then we use that
[`Datasets`](https://docs.fast.ai/data.core.html#datasets) to create a
[`DataLoaders`](https://docs.fast.ai/data.core.html#dataloaders). Here
the class of [`TfmdDL`](https://docs.fast.ai/data.core.html#tfmddl) we
need to use is
[`LMDataLoader`](https://docs.fast.ai/text.data.html#lmdataloader) which
will concatenate all the texts in a source (with a shuffle at each epoch
for the training set), split it in `bs` chunks then read continuously
through it.

``` python
bs,sl=256,80
dbunch_lm = dsets.dataloaders(bs=bs, seq_len=sl, val_bs=bs)
```

``` python
dbunch_lm.show_batch()
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">text</th>
<th data-quarto-table-cell-role="th">text_</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>▁xxbos ▁xxmaj ▁this ▁is ▁an ▁xxmaj ▁emperor ' s ▁xxmaj ▁new ▁xxmaj
▁clothes ▁situation . ▁xxmaj ▁someone ▁needs ▁to ▁say ▁" that ' s ▁not
▁a ▁funny ▁and ▁original , ▁( etc . , ▁etc . ) ▁film ; ▁that ▁is ▁an
▁inferior ▁film . ▁xxmaj ▁don ' t ▁waste ▁your ▁money ▁on ▁it ." ▁xxmaj
▁the ▁film ▁is ▁trashy , ▁and ▁the ▁people ▁in ▁it ▁are ▁embarrassingly
▁inferior ▁trailer ▁trash . ▁xxmaj ▁they ▁are ▁all - too -
realistic</td>
<td>▁xxmaj ▁this ▁is ▁an ▁xxmaj ▁emperor ' s ▁xxmaj ▁new ▁xxmaj ▁clothes
▁situation . ▁xxmaj ▁someone ▁needs ▁to ▁say ▁" that ' s ▁not ▁a ▁funny
▁and ▁original , ▁( etc . , ▁etc . ) ▁film ; ▁that ▁is ▁an ▁inferior
▁film . ▁xxmaj ▁don ' t ▁waste ▁your ▁money ▁on ▁it ." ▁xxmaj ▁the ▁film
▁is ▁trashy , ▁and ▁the ▁people ▁in ▁it ▁are ▁embarrassingly ▁inferior
▁trailer ▁trash . ▁xxmaj ▁they ▁are ▁all - too - realistic ally</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>▁xxmaj ▁listener ▁is ▁without ▁doubt ▁one ▁of ▁the ▁dullest ▁films
▁i ▁have ▁ever ▁seen . ▁xxmaj ▁there ▁was ▁nothing ▁happening ▁in ▁this
▁film ▁what ▁so ▁ever ▁- ▁i ▁didn ' t ▁care ▁for ▁any ▁of ▁the
▁characters , ▁didn ' t ▁buy ▁in ▁to ▁the ▁whole ▁mystery ▁type ▁plot ,
▁didn ' t ▁care ▁how ▁it ▁ended ▁.... nothing . ▁xxmaj ▁there ▁is ▁no
▁comedy , ▁no ▁action , ▁no ▁thrills , ▁no ▁suspense , ▁nothing . ▁xxmaj
▁the ▁highlights</td>
<td>▁listener ▁is ▁without ▁doubt ▁one ▁of ▁the ▁dullest ▁films ▁i ▁have
▁ever ▁seen . ▁xxmaj ▁there ▁was ▁nothing ▁happening ▁in ▁this ▁film
▁what ▁so ▁ever ▁- ▁i ▁didn ' t ▁care ▁for ▁any ▁of ▁the ▁characters ,
▁didn ' t ▁buy ▁in ▁to ▁the ▁whole ▁mystery ▁type ▁plot , ▁didn ' t
▁care ▁how ▁it ▁ended ▁.... nothing . ▁xxmaj ▁there ▁is ▁no ▁comedy ,
▁no ▁action , ▁no ▁thrills , ▁no ▁suspense , ▁nothing . ▁xxmaj ▁the
▁highlights ▁include</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>▁mutated ▁humans ▁who ▁have ▁been ▁outcast ▁by ▁society . ▁xxmaj
▁eventually , ▁they ▁receive ▁a ▁special ▁en vo y ▁from ▁xxmaj ▁earth
▁with ▁an ▁unexpected ▁message . ▁xxmaj ▁the ▁basic ▁problem ▁is ▁that
▁this ▁whole ▁movie ▁could ▁have ▁been ▁summarized ▁into ▁a ▁sentence
▁and ▁making ▁a ▁1 ▁hour ▁movie ▁out ▁of ▁it ▁added ▁nothing . ▁xxmaj
▁what ▁you ▁essentially ▁get ▁is ▁some ▁effectively ▁gross - looking
▁characters ▁with ▁dialog ▁that ▁is ▁so ▁boring ▁you ▁want ▁to ▁blow
▁their ▁ship ▁up</td>
<td>▁humans ▁who ▁have ▁been ▁outcast ▁by ▁society . ▁xxmaj ▁eventually
, ▁they ▁receive ▁a ▁special ▁en vo y ▁from ▁xxmaj ▁earth ▁with ▁an
▁unexpected ▁message . ▁xxmaj ▁the ▁basic ▁problem ▁is ▁that ▁this
▁whole ▁movie ▁could ▁have ▁been ▁summarized ▁into ▁a ▁sentence ▁and
▁making ▁a ▁1 ▁hour ▁movie ▁out ▁of ▁it ▁added ▁nothing . ▁xxmaj ▁what
▁you ▁essentially ▁get ▁is ▁some ▁effectively ▁gross - looking
▁characters ▁with ▁dialog ▁that ▁is ▁so ▁boring ▁you ▁want ▁to ▁blow
▁their ▁ship ▁up ▁every</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">3</td>
<td>▁soul ▁of ▁man ▁can ▁be ▁distorted ▁in ▁such ▁a ▁way ▁that , ▁pain
▁and ▁suffering ▁being ▁brought ▁to ▁bear ▁on ▁a ▁fellow ▁human ▁being
▁is ▁in ▁some ▁way ▁satisfying . ▁xxmaj ▁be ▁it ▁mental ▁or ▁physical .
▁i ▁found ▁the ▁film ▁very ▁thought ▁provoking . ▁xxbos ▁i ▁have ▁been
▁hooked ▁on ▁" gg " ▁since ▁midway ▁through ▁2001 - 2002 ▁(2 nd ▁season
), ▁when ▁i ▁tuned ▁in ▁to ▁see ▁" small ville " ▁10 ▁minutes ▁early .
▁xxmaj ▁thanks</td>
<td>▁of ▁man ▁can ▁be ▁distorted ▁in ▁such ▁a ▁way ▁that , ▁pain ▁and
▁suffering ▁being ▁brought ▁to ▁bear ▁on ▁a ▁fellow ▁human ▁being ▁is
▁in ▁some ▁way ▁satisfying . ▁xxmaj ▁be ▁it ▁mental ▁or ▁physical . ▁i
▁found ▁the ▁film ▁very ▁thought ▁provoking . ▁xxbos ▁i ▁have ▁been
▁hooked ▁on ▁" gg " ▁since ▁midway ▁through ▁2001 - 2002 ▁(2 nd ▁season
), ▁when ▁i ▁tuned ▁in ▁to ▁see ▁" small ville " ▁10 ▁minutes ▁early .
▁xxmaj ▁thanks ▁to</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">4</td>
<td>▁came ▁to ▁the ▁cinemas , ▁and ▁brought ▁my ▁entire ▁family ▁along .
▁i ▁had ▁already ▁seen ▁xxmaj ▁jim ▁xxmaj ▁carrey ▁in ▁the ▁xxmaj ▁mask
, ▁and ▁xxmaj ▁ace ▁xxmaj ▁ventura , ▁and ▁loved ▁him ▁in ▁those ▁films
. ▁xxmaj ▁the ▁review ▁of ▁the ▁film ▁was ▁quite ▁good , ▁so ▁i ▁looked
▁forward ▁to ▁this . ▁xxmaj ▁my ▁father ▁wondered ▁if ▁it ▁really ▁was
▁a ▁movie ▁in ▁his ▁taste ▁... ▁and ▁then ▁the ▁movie ▁started . ▁i
▁have ▁never ▁in ▁my</td>
<td>▁to ▁the ▁cinemas , ▁and ▁brought ▁my ▁entire ▁family ▁along . ▁i
▁had ▁already ▁seen ▁xxmaj ▁jim ▁xxmaj ▁carrey ▁in ▁the ▁xxmaj ▁mask ,
▁and ▁xxmaj ▁ace ▁xxmaj ▁ventura , ▁and ▁loved ▁him ▁in ▁those ▁films .
▁xxmaj ▁the ▁review ▁of ▁the ▁film ▁was ▁quite ▁good , ▁so ▁i ▁looked
▁forward ▁to ▁this . ▁xxmaj ▁my ▁father ▁wondered ▁if ▁it ▁really ▁was
▁a ▁movie ▁in ▁his ▁taste ▁... ▁and ▁then ▁the ▁movie ▁started . ▁i
▁have ▁never ▁in ▁my ▁movie</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">5</td>
<td>▁or ▁xxmaj ▁ladd ▁and ▁xxmaj ▁lake ) to ▁soften ▁the ▁violent
▁elements . ▁" kiss ▁xxmaj ▁tomorrow ▁xxmaj ▁goodbye " ▁( a ▁gem ▁of ▁a
▁noir ▁title ▁if ▁there ▁ever ▁was ▁one , ▁be speaking ▁a ▁bleak ,
▁fatalistic ▁vision ) ▁contains ▁no ▁romantic ▁subplot , ▁unless ▁you
▁count ▁the ▁xxmaj ▁cagney ▁character ' s ▁involvement ▁with ▁the ▁rich
▁woman , ▁which ▁is ▁more ▁about ▁greed ▁and ▁rebellion ▁than ▁love .
▁xxmaj ▁this ▁film ▁is ▁a ▁great ▁of ▁example ▁of</td>
<td>▁xxmaj ▁ladd ▁and ▁xxmaj ▁lake ) to ▁soften ▁the ▁violent ▁elements
. ▁" kiss ▁xxmaj ▁tomorrow ▁xxmaj ▁goodbye " ▁( a ▁gem ▁of ▁a ▁noir
▁title ▁if ▁there ▁ever ▁was ▁one , ▁be speaking ▁a ▁bleak , ▁fatalistic
▁vision ) ▁contains ▁no ▁romantic ▁subplot , ▁unless ▁you ▁count ▁the
▁xxmaj ▁cagney ▁character ' s ▁involvement ▁with ▁the ▁rich ▁woman ,
▁which ▁is ▁more ▁about ▁greed ▁and ▁rebellion ▁than ▁love . ▁xxmaj
▁this ▁film ▁is ▁a ▁great ▁of ▁example ▁of ▁pure</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">6</td>
<td>▁xxmaj ▁swann ▁( o ' toole ) ▁out ▁of ▁trouble ▁ . ▁xxmaj ▁by ▁no
▁means ▁an ▁easy ▁task . ▁xxmaj ▁the ▁things ▁they ▁do ▁together ▁are
▁quite ▁out ▁of ▁the ▁ordinary . ▁xxmaj ▁though ▁he ▁is ▁in ▁charge ▁of
▁keeping ▁xxmaj ▁swann ▁out ▁of ▁trouble , ▁he ▁really ▁gets ▁to ▁enjoy
▁his ▁job . ▁xxmaj ▁this ▁film ▁also ▁show ▁the ▁side ▁of ▁the
▁entertainment ▁history ▁we ▁don ' t ▁see , ▁xxmaj ▁swann ▁( o ' toole ▁
)</td>
<td>▁swann ▁( o ' toole ) ▁out ▁of ▁trouble ▁ . ▁xxmaj ▁by ▁no ▁means
▁an ▁easy ▁task . ▁xxmaj ▁the ▁things ▁they ▁do ▁together ▁are ▁quite
▁out ▁of ▁the ▁ordinary . ▁xxmaj ▁though ▁he ▁is ▁in ▁charge ▁of
▁keeping ▁xxmaj ▁swann ▁out ▁of ▁trouble , ▁he ▁really ▁gets ▁to ▁enjoy
▁his ▁job . ▁xxmaj ▁this ▁film ▁also ▁show ▁the ▁side ▁of ▁the
▁entertainment ▁history ▁we ▁don ' t ▁see , ▁xxmaj ▁swann ▁( o ' toole ▁
) ▁confesses</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">7</td>
<td>, ▁it ▁just ▁keeps ▁plodding ▁on . ▁xxmaj ▁christopher ▁xxmaj
▁walken ▁has ▁a ▁part , ▁but ▁it ▁is ▁completely ▁senseless , ▁as ▁is
▁most ▁of ▁the ▁movie . ▁xxmaj ▁this ▁movie ▁had ▁potential , ▁but ▁it
▁looks ▁like ▁some ▁really ▁bad ▁made ▁for ▁xxup ▁tv ▁movie . ▁i ▁would
▁avoid ▁this ▁movie . ▁xxbos ▁" men ace " ▁is ▁not ▁funny . ▁xxmaj ▁it
▁tries ▁hard ▁- ▁too ▁hard . ▁but ▁rarely ▁brings ▁a ▁smile . ▁xxmaj
▁there ▁is ▁no</td>
<td>▁it ▁just ▁keeps ▁plodding ▁on . ▁xxmaj ▁christopher ▁xxmaj ▁walken
▁has ▁a ▁part , ▁but ▁it ▁is ▁completely ▁senseless , ▁as ▁is ▁most ▁of
▁the ▁movie . ▁xxmaj ▁this ▁movie ▁had ▁potential , ▁but ▁it ▁looks
▁like ▁some ▁really ▁bad ▁made ▁for ▁xxup ▁tv ▁movie . ▁i ▁would ▁avoid
▁this ▁movie . ▁xxbos ▁" men ace " ▁is ▁not ▁funny . ▁xxmaj ▁it ▁tries
▁hard ▁- ▁too ▁hard . ▁but ▁rarely ▁brings ▁a ▁smile . ▁xxmaj ▁there ▁is
▁no ▁acting</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">8</td>
<td>▁pink ▁xxmaj ▁panther . ▁xxmaj ▁this ▁was ▁an ▁xxup ▁rko ▁movie ▁but
▁it ▁did ▁not ▁have ▁the ▁nice ▁airplane ▁logo ▁that ▁xxup ▁rko ▁used
▁to ▁use . ▁i ▁liked ▁xxmaj ▁victor ▁xxmaj ▁mature ▁in ▁xxmaj ▁one
▁xxmaj ▁million , ▁xxup ▁b . c . , ▁and ▁xxmaj ▁sam p son ▁and ▁xxmaj
▁delilah ▁and ▁especially ▁in ▁xxmaj ▁violent ▁xxmaj ▁saturday . ▁xxmaj
▁see ▁if ▁you ▁can ▁find ▁that ▁one . ▁xxmaj ▁he ▁was ▁wonderful ▁in
▁the ▁comedy ▁with ▁xxmaj ▁peter</td>
<td>▁xxmaj ▁panther . ▁xxmaj ▁this ▁was ▁an ▁xxup ▁rko ▁movie ▁but ▁it
▁did ▁not ▁have ▁the ▁nice ▁airplane ▁logo ▁that ▁xxup ▁rko ▁used ▁to
▁use . ▁i ▁liked ▁xxmaj ▁victor ▁xxmaj ▁mature ▁in ▁xxmaj ▁one ▁xxmaj
▁million , ▁xxup ▁b . c . , ▁and ▁xxmaj ▁sam p son ▁and ▁xxmaj ▁delilah
▁and ▁especially ▁in ▁xxmaj ▁violent ▁xxmaj ▁saturday . ▁xxmaj ▁see ▁if
▁you ▁can ▁find ▁that ▁one . ▁xxmaj ▁he ▁was ▁wonderful ▁in ▁the ▁comedy
▁with ▁xxmaj ▁peter ▁xxmaj</td>
</tr>
</tbody>
</table>

Then we have a convenience method to directly grab a
[`Learner`](https://docs.fast.ai/learner.html#learner) from it, using
the [`AWD_LSTM`](https://docs.fast.ai/text.models.awdlstm.html#awd_lstm)
architecture.

``` python
opt_func = partial(Adam, wd=0.1)
learn = language_model_learner(dbunch_lm, AWD_LSTM, opt_func=opt_func, metrics=[accuracy, Perplexity()], path=path)
learn = learn.to_fp16()
```

``` python
learn.fit_one_cycle(1, 2e-2, moms=(0.8,0.7,0.8))
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">perplexity</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>4.426135</td>
<td>3.984901</td>
<td>0.292371</td>
<td>53.779987</td>
<td>07:00</td>
</tr>
</tbody>
</table>

``` python
learn.save('stage1')
```

``` python
learn.load('stage1');
```

``` python
learn.unfreeze()
learn.fit_one_cycle(10, 2e-3, moms=(0.8,0.7,0.8))
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">perplexity</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>4.163227</td>
<td>3.870354</td>
<td>0.306840</td>
<td>47.959347</td>
<td>07:24</td>
</tr>
<tr>
<td>1</td>
<td>4.055693</td>
<td>3.790802</td>
<td>0.316436</td>
<td>44.291908</td>
<td>07:41</td>
</tr>
<tr>
<td>2</td>
<td>3.979279</td>
<td>3.729021</td>
<td>0.323357</td>
<td>41.638317</td>
<td>07:22</td>
</tr>
<tr>
<td>3</td>
<td>3.919654</td>
<td>3.688891</td>
<td>0.327770</td>
<td>40.000469</td>
<td>07:22</td>
</tr>
<tr>
<td>4</td>
<td>3.889432</td>
<td>3.660633</td>
<td>0.330762</td>
<td>38.885933</td>
<td>07:22</td>
</tr>
<tr>
<td>5</td>
<td>3.842923</td>
<td>3.637397</td>
<td>0.333315</td>
<td>37.992798</td>
<td>07:26</td>
</tr>
<tr>
<td>6</td>
<td>3.813823</td>
<td>3.619074</td>
<td>0.335308</td>
<td>37.303013</td>
<td>07:25</td>
</tr>
<tr>
<td>7</td>
<td>3.793213</td>
<td>3.608010</td>
<td>0.336566</td>
<td>36.892574</td>
<td>07:20</td>
</tr>
<tr>
<td>8</td>
<td>3.766456</td>
<td>3.602140</td>
<td>0.337257</td>
<td>36.676647</td>
<td>07:22</td>
</tr>
<tr>
<td>9</td>
<td>3.759768</td>
<td>3.600955</td>
<td>0.337450</td>
<td>36.633202</td>
<td>07:23</td>
</tr>
</tbody>
</table>

Once we have fine-tuned the pretrained language model to this corpus, we
save the encoder since we will use it for the classifier.

``` python
learn.save_encoder('finetuned1')
```

## Use it to train a classifier

``` python
texts = get_files(path, extensions=['.txt'], folders=['train', 'test'])
```

``` python
splits = GrandparentSplitter(valid_name='test')(texts)
```

For classification, we need to use two set of transforms: one to
numericalize the texts and the other to encode the labels as categories.

``` python
x_tfms = [Tokenizer.from_folder(path), Numericalize(vocab=dbunch_lm.vocab)]
dsets = Datasets(texts, [x_tfms, [parent_label, Categorize()]], splits=splits, dl_type=SortedDL)
```

``` python
bs = 64
```

``` python
dls = dsets.dataloaders(before_batch=pad_input_chunk, bs=bs)
```

``` python
dls.show_batch(max_n=2)
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">text</th>
<th data-quarto-table-cell-role="th">category</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>xxbos * * attention xxmaj spoilers * * \n\n xxmaj first of all , let
me say that xxmaj rob xxmaj roy is one of the best films of the 90 's .
xxmaj it was an amazing achievement for all those involved , especially
the acting of xxmaj liam xxmaj neeson , xxmaj jessica xxmaj lange ,
xxmaj john xxmaj hurt , xxmaj brian xxmaj cox , and xxmaj tim xxmaj roth
. xxmaj michael xxmaj canton xxmaj jones painted a wonderful portrait of
the honor and dishonor that men can represent in themselves . xxmaj but
alas … \n\n it constantly , and unfairly gets compared to " braveheart "
. xxmaj these are two entirely different films , probably only similar
in the fact that they are both about xxmaj scots in historical xxmaj
scotland . xxmaj yet , this comparison frequently bothers me because it
seems</td>
<td>pos</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>xxbos xxmaj by now you 've probably heard a bit about the new xxmaj
disney dub of xxmaj miyazaki 's classic film , xxmaj laputa : xxmaj
castle xxmaj in xxmaj the xxmaj sky . xxmaj during late summer of 1998 ,
xxmaj disney released " kiki 's xxmaj delivery xxmaj service " on video
which included a preview of the xxmaj laputa dub saying it was due out
in " 1 xxrep 3 9 " . xxmaj it 's obviously way past that year now , but
the dub has been finally completed . xxmaj and it 's not " laputa :
xxmaj castle xxmaj in xxmaj the xxmaj sky " , just " castle xxmaj in
xxmaj the xxmaj sky " for the dub , since xxmaj laputa is not such a
nice word in xxmaj spanish ( even though they use the word xxmaj laputa
many times</td>
<td>pos</td>
</tr>
</tbody>
</table>

Then we once again have a convenience function to create a classifier
from this
[`DataLoaders`](https://docs.fast.ai/data.core.html#dataloaders) with
the [`AWD_LSTM`](https://docs.fast.ai/text.models.awdlstm.html#awd_lstm)
architecture.

``` python
opt_func = partial(Adam, wd=0.1)
learn = text_classifier_learner(dls, AWD_LSTM, metrics=[accuracy], path=path, drop_mult=0.5, opt_func=opt_func)
```

We load our pretrained encoder.

``` python
learn = learn.load_encoder('finetuned1')
learn = learn.to_fp16(clip=0.1)
```

Then we can train with gradual unfreezing and differential learning
rates.

``` python
lr = 1e-1 * bs/128
```

``` python
learn.fit_one_cycle(1, lr, moms=(0.8,0.7,0.8), wd=0.1)
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0.328318</td>
<td>0.200650</td>
<td>0.922120</td>
<td>01:08</td>
</tr>
</tbody>
</table>

``` python
learn.freeze_to(-2)
lr /= 2
learn.fit_one_cycle(1, slice(lr/(2.6**4),lr), moms=(0.8,0.7,0.8), wd=0.1)
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0.208120</td>
<td>0.166004</td>
<td>0.937440</td>
<td>01:15</td>
</tr>
</tbody>
</table>

``` python
learn.freeze_to(-3)
lr /= 2
learn.fit_one_cycle(1, slice(lr/(2.6**4),lr), moms=(0.8,0.7,0.8), wd=0.1)
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0.162498</td>
<td>0.154959</td>
<td>0.942400</td>
<td>01:35</td>
</tr>
</tbody>
</table>

``` python
learn.unfreeze()
lr /= 5
learn.fit_one_cycle(2, slice(lr/(2.6**4),lr), moms=(0.8,0.7,0.8), wd=0.1)
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0.133800</td>
<td>0.163456</td>
<td>0.940560</td>
<td>01:34</td>
</tr>
<tr>
<td>1</td>
<td>0.095326</td>
<td>0.154301</td>
<td>0.945120</td>
<td>01:34</td>
</tr>
</tbody>
</table>
