Sentiment analysis fastai tutorial
A quick recap of the sentiment analysis tutorial from fastai
Text tutorial from fastai
The material can be found on the fastai website
Packages and data
We begin by importing all the required packages from the fastai text module.
from fastai.text.all import *
In fastai
, each module has an all
script which allows us to import everything that is necessesary from that module, in the knowledge that it is safe to do so.
We will be using the IMDB
dataset to fine-tune a sentiment analysis model, so lets download it now.
path = untar_data(URLs.IMDB)
path.ls()
We see train
and test
folders, so let's check what is inside both of those.
(path/'train').ls(),(path/'test').ls()
Both have subfolders containing positive and negative comments (and also some Bow
related files, which I guess is something to do with a bag of words model). This is a standard structure for datasets, and fastai
has a built in method to deal with importing the files using the folder names as labels.
So we create a DataLoaders
(which is just a collection of DataLoader
objects)
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
Let's check out a batch of these reviews
dls.show_batch()
Note that a fair amount of preprocessing has been done already on the reviews, and some extra tokens have been inserted:
-
xxbos
indicates that it is the beginning of a review -
xxmaj
indicates that the following word should be capitalized
Next we can define a learner which is suitable for text classification:
learner = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
Note that we are using the AWD_LSTM
architecture, which stands for Adjusted stochastic gradient descent Weight Decay, Long-Short-Term Memory
. The AWD part basically just means that the way that weights are adjusted is modified, while the LSTM part means that it can deal with both long and short dependencies (more notes on LSTM architecture coming soon).
The drop_mult
parameters just controls the magnitude of the dropouts in the model. For the metrics that we will be tracking, we just take accuracy
.
Now we can fine-tune our model for a couple of epochs:
learner.fine_tune(2, 1e-2)