<jupyter_start><jupyter_text>Training a text classifier model with fastai- this notebook assumes you have already run text_model_training.ipynb notebook- In this notebook, the IMDB dataset is ingested- the first section<jupyter_code>#hide
!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
#hide
from fastbook import *
from fastai.text.all import *
# switch to control whether direct TDL or DataBlocks definition used 
tdl = True
# ensure the modifier value matches the value set for modifier in text_model_training notebook
modifier = "july4_2021"<jupyter_output><empty_output><jupyter_text>Ingest the dataset- define the path for the dataset- create a TextDataLoaders object<jupyter_code>%%time
# create dataloaders object
path = untar_data(URLs.IMDB)
path.ls()<jupyter_output>CPU times: user 2.86 ms, sys: 822 µs, total: 3.68 ms
Wall time: 11.8 ms<jupyter_text>Define the text classifier<jupyter_code>%%time
# define TextDataLoaders object
dls_clas = TextDataLoaders.from_folder(path, valid='test')

# directory structure of the IMDB curated dataset
'''
├── test
│   ├── neg
│   └── pos
├── tmp_clas
├── tmp_lm
├── train
│   ├── neg
│   └── pos
└── unsup
'''
dls_clas.path
dls_clas.show_batch(max_n=3)
# save the current path
keep_path = path
print("keep_path is: ",str(keep_path))
%%time
# define a text_classifier_learner object
learn_clas = text_classifier_learner(dls_clas, AWD_LSTM, 
                                metrics=accuracy).to_fp16()<jupyter_output>CPU times: user 6.89 s, sys: 1.16 s, total: 8.05 s
Wall time: 5.06 s<jupyter_text>Fine-tune the text classifierUse the encoder created as part of training the language model to fine tune the text classifier<jupyter_code># Path('/storage/data/imdb')
learn_clas.path
%%time
# set the path to the location of the encoder
learn_clas.path = Path('/notebooks/temp')
# load the encoder that was saved when the language model was trained
learn_clas = learn_clas.load_encoder('ft_'+modifier)
learn_clas.path
# set the path back to the original path
learn_clas.path = keep_path
# ch 10 style Path('/storage/data/imdb')
learn_clas.path
%%time
# fine tune the model
learn_clas.fit_one_cycle(5, 2e-2)
x, y = first(dls_clas.train)
x.shape, y.shape, len(dls_clas.train)
learn_clas.summary()<jupyter_output><empty_output><jupyter_text>Exercise the text classifierApply the fine-tuned text classifier on some text samples.<jupyter_code>preds = learn_clas.predict("this film shows incredibly bad writing and is a complete disaster")
preds
preds = learn_clas.predict("this film shows incredible talent and is a complete triumph")
preds
# save the classifier model
learn_clas.path = Path('/notebooks/temp')
learn_clas.save('classifier_single_epoch_'+modifier+'d')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09586056644880174, 'fraction_numerical': 0.013435003631082063, 'mean_word_length': 5.606714628297362, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '160593', 'license': 'MIT', 'n_tokens_mistral': 966, 'n_tokens_neox': 940, 'n_words': 291, 'path': 'ch4/text_classifier_model.ipynb', 'repo_name': 'Vega95/Deep-Learning-with-fastai-Cookbook', 'size': '2754'}