#!/usr/bin/env python # coding: utf-8 # ## NLP Interpret # In[1]: from fastai.gen_doc.nbdoc import * from fastai.text import * from fastai.text.interpret import * # [`text.interpret`](/text.interpret.html#text.interpret) is the module that implements custom [`Interpretation`](/train.html#Interpretation) classes for different NLP tasks by inheriting from it. # In[2]: from fastai.gen_doc.nbdoc import * from fastai.vision import * # In[3]: show_doc(TextClassificationInterpretation) # In[4]: show_doc(TextClassificationInterpretation.intrinsic_attention) # In[5]: show_doc(TextClassificationInterpretation.html_intrinsic_attention) # In[6]: show_doc(TextClassificationInterpretation.show_intrinsic_attention) # In[7]: show_doc(TextClassificationInterpretation.show_top_losses) # Let's show how [`TextClassificationInterpretation`](/text.interpret.html#TextClassificationInterpretation) can be used once we train a text classification model. # ### train # 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') # ### interpret # In[ ]: interp = TextClassificationInterpretation.from_learner(learn) # In[ ]: interp.show_intrinsic_attention("I really like this movie, it is amazing!") # ## Undocumented Methods - Methods moved below this line will intentionally be hidden # ## New Methods - Please document or move to the undocumented section