In [ ]:
from fastai.gen_doc.nbdoc import *
from fastai.text import *
from fastai.text.interpret import *
text.interpret
is the module that implements custom Interpretation
classes for different NLP tasks by inheriting from it.
In [ ]:
from fastai.gen_doc.nbdoc import *
from fastai.vision import *
In [ ]:
show_doc(TextClassificationInterpretation)
In [ ]:
show_doc(TextClassificationInterpretation.intrinsic_attention)
In [ ]:
show_doc(TextClassificationInterpretation.html_intrinsic_attention)
In [ ]:
show_doc(TextClassificationInterpretation.show_intrinsic_attention)
In [ ]:
show_doc(TextClassificationInterpretation.show_top_losses)
Let's show how TextClassificationInterpretation
can be used once we train a text classification model.
In [ ]:
imdb = untar_data(URLs.IMDB_SAMPLE)
In [ ]:
data_lm = (TextList.from_csv(imdb, 'texts.csv', cols='text')
.split_by_rand_pct()
.label_for_lm()
.databunch())
data_lm.save()
In [ ]:
data_lm.show_batch()
In [ ]:
learn = language_model_learner(data_lm, AWD_LSTM)
learn.fit_one_cycle(2, 1e-2)
learn.save('mini_train_lm')
learn.save_encoder('mini_train_encoder')
In [ ]:
data_clas = (TextList.from_csv(imdb, 'texts.csv', cols='text', vocab=data_lm.vocab)
.split_from_df(col='is_valid')
.label_from_df(cols='label')
.databunch(bs=42))
In [ ]:
learn = text_classifier_learner(data_clas, AWD_LSTM)
learn.load_encoder('mini_train_encoder')
learn.fit_one_cycle(2, slice(1e-3,1e-2))
learn.save('mini_train_clas')
In [ ]:
interp = TextClassificationInterpretation.from_learner(learn)
In [ ]:
interp.show_intrinsic_attention("I really like this movie, it is amazing!")