from fastai import * # Quick access to most common functionality
from fastai.text import * # Quick access to NLP functionality
An example of creating a language model and then transfering to a classifier.
path = untar_data(URLs.IMDB_SAMPLE)
path
PosixPath('/home/ubuntu/.fastai/data/imdb_sample')
Open and view the independent and dependent variables:
df = pd.read_csv(path/'texts.csv', header=None)
df.head()
0 | 1 | 2 | |
---|---|---|---|
0 | label | text | is_valid |
1 | 0 | Un-bleeping-believable! Meg Ryan doesn't even ... | False |
2 | 1 | This is a extremely well-made film. The acting... | False |
3 | 0 | Every once in a long while a movie will come a... | False |
4 | 1 | Name just says it all. I watched this movie wi... | False |
Create a DataBunch
for each of the language model and the classifier:
data_lm = TextLMDataBunch.from_csv(path, 'texts.csv')
data_clas = TextClasDataBunch.from_csv(path, 'texts.csv', vocab=data_lm.train_ds.vocab, bs=42)
We'll fine-tune the language model. fast.ai has a pre-trained English model available that we can download, we jsut have to specify it like this:
learn = language_model_learner(data_lm, pretrained_model=URLs.WT103)
learn.unfreeze()
learn.fit(2, slice(1e-4,1e-2))
Total time: 00:48 epoch train_loss valid_loss accuracy 1 4.898340 4.119840 0.254581 (00:24) 2 4.610869 4.052252 0.260789 (00:23)
Save our language model's encoder:
learn.save_encoder('enc')
Fine tune it to create a classifier:
learn = text_classifier_learner(data_clas)
learn.load_encoder('enc')
learn.fit(3, 1e-3)