Skip to content

Commit e860bf3

Browse files
j-t-1facebook-github-bot
authored andcommitted
Tweak IMDB TorchText interpretation tutorial (meta-pytorch#1516)
Summary: Pull Request resolved: meta-pytorch#1516 Reviewed By: cyrjano Differential Revision: D74943084 Pulled By: jjuncho fbshipit-source-id: a4549e34a0c57f24173bacfc55b4bda838081837
1 parent b0d5350 commit e860bf3

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

tutorials/IMDB_TorchText_Interpret.ipynb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"cell_type": "markdown",
1414
"metadata": {},
1515
"source": [
16-
"This notebook loads pretrained CNN model for sentiment analysis on IMDB dataset. It makes predictions on test samples and interprets those predictions using integrated gradients method.\n",
16+
"This notebook loads a pretrained CNN model for sentiment analysis on an IMDB dataset. It makes predictions on test samples and interprets those predictions using the Integrated Gradients method.\n",
1717
"\n",
1818
"The model was trained using an open source sentiment analysis tutorials described in: https://github.com/bentrevett/pytorch-sentiment-analysis/blob/master/4%20-%20Convolutional%20Sentiment%20Analysis.ipynb with the following changes:\n",
1919
"\n",
20-
"- TEXT: set lower=True at initialization and call build_vocab() on the entire training data including validation to avoid mismatched indices\n",
20+
"- TEXT: set lower=True at initialization and call build_vocab() on the entire training data including validation, to avoid mismatched indices\n",
2121
"- model: save the entire model instead of just model.state_dict()\n",
2222
"\n",
2323
"**Note:** Before running this tutorial, please install the spacy package, and its NLP modules for English language."
@@ -81,7 +81,7 @@
8181
"cell_type": "markdown",
8282
"metadata": {},
8383
"source": [
84-
"The dataset used for training this model can be found in: https://ai.stanford.edu/~amaas/data/sentiment/\n",
84+
"The dataset used for training this model can be found in: https://ai.stanford.edu/~amaas/data/sentiment/.\n",
8585
"\n",
8686
"Redefining the model in order to be able to load it.\n"
8787
]
@@ -113,31 +113,31 @@
113113
" \n",
114114
" def forward(self, text):\n",
115115
" \n",
116-
" #text = [sent len, batch size]\n",
116+
" # text = [sent len, batch size]\n",
117117
" \n",
118-
" #text = text.permute(1, 0)\n",
118+
" # text = text.permute(1, 0)\n",
119119
" \n",
120-
" #text = [batch size, sent len]\n",
120+
" # text = [batch size, sent len]\n",
121121
" \n",
122122
" embedded = self.embedding(text)\n",
123123
"\n",
124-
" #embedded = [batch size, sent len, emb dim]\n",
124+
" # embedded = [batch size, sent len, emb dim]\n",
125125
" \n",
126126
" embedded = embedded.unsqueeze(1)\n",
127127
" \n",
128-
" #embedded = [batch size, 1, sent len, emb dim]\n",
128+
" # embedded = [batch size, 1, sent len, emb dim]\n",
129129
" \n",
130130
" conved = [F.relu(conv(embedded)).squeeze(3) for conv in self.convs]\n",
131131
" \n",
132-
" #conved_n = [batch size, n_filters, sent len - filter_sizes[n] + 1]\n",
132+
" # conved_n = [batch size, n_filters, sent len - filter_sizes[n] + 1]\n",
133133
" \n",
134134
" pooled = [F.max_pool1d(conv, conv.shape[2]).squeeze(2) for conv in conved]\n",
135135
" \n",
136-
" #pooled_n = [batch size, n_filters]\n",
136+
" # pooled_n = [batch size, n_filters]\n",
137137
" \n",
138138
" cat = self.dropout(torch.cat(pooled, dim = 1))\n",
139139
"\n",
140-
" #cat = [batch size, n_filters * len(filter_sizes)]\n",
140+
" # cat = [batch size, n_filters * len(filter_sizes)]\n",
141141
" \n",
142142
" return self.fc(cat)"
143143
]
@@ -148,7 +148,7 @@
148148
"source": [
149149
"Loads pretrained model and sets the model to eval mode.\n",
150150
"\n",
151-
"The model can be downloaded here: https://github.com/pytorch/captum/blob/master/tutorials/models/imdb-model-cnn-large.pt"
151+
"The model can be downloaded here: https://github.com/pytorch/captum/blob/master/tutorials/models/imdb-model-cnn-large.pt."
152152
]
153153
},
154154
{
@@ -166,7 +166,7 @@
166166
"cell_type": "markdown",
167167
"metadata": {},
168168
"source": [
169-
"Forward function that supports sigmoid"
169+
"Forward function that supports sigmoid."
170170
]
171171
},
172172
{
@@ -290,7 +290,7 @@
290290
"metadata": {},
291291
"source": [
292292
"Let's create an instance of `LayerIntegratedGradients` using forward function of our model and the embedding layer.\n",
293-
"This instance of layer integrated gradients will be used to interpret movie rating review.\n",
293+
"This instance of Layer Integrated Gradients will be used to interpret movie rating review.\n",
294294
"\n",
295295
"Layer Integrated Gradients will allow us to assign an attribution score to each word/token embedding tensor in the movie review text. We will ultimately sum the attribution scores across all embedding dimensions for each word/token in order to attain a word/token level attribution score.\n",
296296
"\n",
@@ -319,7 +319,7 @@
319319
"metadata": {},
320320
"outputs": [],
321321
"source": [
322-
"# accumalate couple samples in this array for visualization purposes\n",
322+
"# accumulate couple samples in this array for visualization purposes\n",
323323
"vis_data_records_ig = []\n",
324324
"\n",
325325
"def interpret_sentence(model, sentence, min_len = 7, label = 0):\n",

0 commit comments

Comments
 (0)