In [ ]:
from fastai.text import * # Quick access to NLP functionality
An example of creating a language model and then transfering to a classifier.
In [ ]:
path = untar_data(URLs.IMDB_SAMPLE)
path
Out[ ]:
Open and view the independent and dependent variables:
In [ ]:
df = pd.read_csv(path/'texts.csv')
df.head()
Out[ ]:
Create a DataBunch
for each of the language model and the classifier:
In [ ]:
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 just have to specify it like this:
In [ ]:
moms = (0.8,0.7)
In [ ]:
learn = language_model_learner(data_lm, AWD_LSTM)
learn.unfreeze()
learn.fit_one_cycle(4, slice(1e-2), moms=moms)
Save our language model's encoder:
In [ ]:
learn.save_encoder('enc')
Fine tune it to create a classifier:
In [ ]:
learn = text_classifier_learner(data_clas, AWD_LSTM)
learn.load_encoder('enc')
learn.fit_one_cycle(4, moms=moms)
In [ ]:
learn.save('stage1-clas')
In [ ]:
learn.unfreeze()
learn.fit_one_cycle(8, slice(1e-5,1e-3), moms=moms)
In [ ]:
learn.predict("I really liked this movie!")
Out[ ]:
In [ ]: