|
13 | 13 | "cell_type": "markdown", |
14 | 14 | "metadata": {}, |
15 | 15 | "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", |
17 | 17 | "\n", |
18 | 18 | "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", |
19 | 19 | "\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", |
21 | 21 | "- model: save the entire model instead of just model.state_dict()\n", |
22 | 22 | "\n", |
23 | 23 | "**Note:** Before running this tutorial, please install the spacy package, and its NLP modules for English language." |
|
81 | 81 | "cell_type": "markdown", |
82 | 82 | "metadata": {}, |
83 | 83 | "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", |
85 | 85 | "\n", |
86 | 86 | "Redefining the model in order to be able to load it.\n" |
87 | 87 | ] |
|
113 | 113 | " \n", |
114 | 114 | " def forward(self, text):\n", |
115 | 115 | " \n", |
116 | | - " #text = [sent len, batch size]\n", |
| 116 | + " # text = [sent len, batch size]\n", |
117 | 117 | " \n", |
118 | | - " #text = text.permute(1, 0)\n", |
| 118 | + " # text = text.permute(1, 0)\n", |
119 | 119 | " \n", |
120 | | - " #text = [batch size, sent len]\n", |
| 120 | + " # text = [batch size, sent len]\n", |
121 | 121 | " \n", |
122 | 122 | " embedded = self.embedding(text)\n", |
123 | 123 | "\n", |
124 | | - " #embedded = [batch size, sent len, emb dim]\n", |
| 124 | + " # embedded = [batch size, sent len, emb dim]\n", |
125 | 125 | " \n", |
126 | 126 | " embedded = embedded.unsqueeze(1)\n", |
127 | 127 | " \n", |
128 | | - " #embedded = [batch size, 1, sent len, emb dim]\n", |
| 128 | + " # embedded = [batch size, 1, sent len, emb dim]\n", |
129 | 129 | " \n", |
130 | 130 | " conved = [F.relu(conv(embedded)).squeeze(3) for conv in self.convs]\n", |
131 | 131 | " \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", |
133 | 133 | " \n", |
134 | 134 | " pooled = [F.max_pool1d(conv, conv.shape[2]).squeeze(2) for conv in conved]\n", |
135 | 135 | " \n", |
136 | | - " #pooled_n = [batch size, n_filters]\n", |
| 136 | + " # pooled_n = [batch size, n_filters]\n", |
137 | 137 | " \n", |
138 | 138 | " cat = self.dropout(torch.cat(pooled, dim = 1))\n", |
139 | 139 | "\n", |
140 | | - " #cat = [batch size, n_filters * len(filter_sizes)]\n", |
| 140 | + " # cat = [batch size, n_filters * len(filter_sizes)]\n", |
141 | 141 | " \n", |
142 | 142 | " return self.fc(cat)" |
143 | 143 | ] |
|
148 | 148 | "source": [ |
149 | 149 | "Loads pretrained model and sets the model to eval mode.\n", |
150 | 150 | "\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." |
152 | 152 | ] |
153 | 153 | }, |
154 | 154 | { |
|
166 | 166 | "cell_type": "markdown", |
167 | 167 | "metadata": {}, |
168 | 168 | "source": [ |
169 | | - "Forward function that supports sigmoid" |
| 169 | + "Forward function that supports sigmoid." |
170 | 170 | ] |
171 | 171 | }, |
172 | 172 | { |
|
290 | 290 | "metadata": {}, |
291 | 291 | "source": [ |
292 | 292 | "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", |
294 | 294 | "\n", |
295 | 295 | "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", |
296 | 296 | "\n", |
|
319 | 319 | "metadata": {}, |
320 | 320 | "outputs": [], |
321 | 321 | "source": [ |
322 | | - "# accumalate couple samples in this array for visualization purposes\n", |
| 322 | + "# accumulate couple samples in this array for visualization purposes\n", |
323 | 323 | "vis_data_records_ig = []\n", |
324 | 324 | "\n", |
325 | 325 | "def interpret_sentence(model, sentence, min_len = 7, label = 0):\n", |
|
0 commit comments