|
15 | 15 | "metadata": {},
|
16 | 16 | "source": [
|
17 | 17 | "# BERT in TransformerLens\n",
|
18 |
| - "This demo shows how to use BERT in TransformerLens for the Masked Language Modelling task." |
| 18 | + "This demo shows how to use BERT in TransformerLens for the Masked Language Modelling and Next Sentence Prediction task." |
19 | 19 | ]
|
20 | 20 | },
|
21 | 21 | {
|
|
29 | 29 | },
|
30 | 30 | {
|
31 | 31 | "cell_type": "code",
|
32 |
| - "execution_count": 15, |
| 32 | + "execution_count": 1, |
33 | 33 | "metadata": {},
|
34 | 34 | "outputs": [
|
35 | 35 | {
|
36 | 36 | "name": "stdout",
|
37 | 37 | "output_type": "stream",
|
38 | 38 | "text": [
|
39 |
| - "Running as a Jupyter notebook - intended for development only!\n", |
40 |
| - "The autoreload extension is already loaded. To reload it, use:\n", |
41 |
| - " %reload_ext autoreload\n" |
| 39 | + "Running as a Jupyter notebook - intended for development only!\n" |
42 | 40 | ]
|
43 | 41 | },
|
44 | 42 | {
|
|
92 | 90 | },
|
93 | 91 | {
|
94 | 92 | "cell_type": "code",
|
95 |
| - "execution_count": 3, |
| 93 | + "execution_count": 2, |
96 | 94 | "metadata": {},
|
97 | 95 | "outputs": [
|
98 | 96 | {
|
|
116 | 114 | },
|
117 | 115 | {
|
118 | 116 | "cell_type": "code",
|
119 |
| - "execution_count": 4, |
| 117 | + "execution_count": 3, |
120 | 118 | "metadata": {},
|
121 | 119 | "outputs": [
|
122 | 120 | {
|
|
136 | 134 | "<circuitsvis.utils.render.RenderedHTML at 0x13a9760d0>"
|
137 | 135 | ]
|
138 | 136 | },
|
139 |
| - "execution_count": 4, |
| 137 | + "execution_count": 3, |
140 | 138 | "metadata": {},
|
141 | 139 | "output_type": "execute_result"
|
142 | 140 | }
|
|
150 | 148 | },
|
151 | 149 | {
|
152 | 150 | "cell_type": "code",
|
153 |
| - "execution_count": 5, |
| 151 | + "execution_count": 4, |
154 | 152 | "metadata": {},
|
155 | 153 | "outputs": [],
|
156 | 154 | "source": [
|
|
159 | 157 | "\n",
|
160 | 158 | "from transformers import AutoTokenizer\n",
|
161 | 159 | "\n",
|
162 |
| - "from transformer_lens import HookedEncoder" |
| 160 | + "from transformer_lens import HookedEncoder, BertNextSentencePrediction" |
163 | 161 | ]
|
164 | 162 | },
|
165 | 163 | {
|
166 | 164 | "cell_type": "code",
|
167 |
| - "execution_count": 6, |
| 165 | + "execution_count": 5, |
168 | 166 | "metadata": {},
|
169 | 167 | "outputs": [
|
170 | 168 | {
|
|
173 | 171 | "<torch.autograd.grad_mode.set_grad_enabled at 0x2a285a790>"
|
174 | 172 | ]
|
175 | 173 | },
|
176 |
| - "execution_count": 6, |
| 174 | + "execution_count": 5, |
177 | 175 | "metadata": {},
|
178 | 176 | "output_type": "execute_result"
|
179 | 177 | }
|
|
189 | 187 | "source": [
|
190 | 188 | "# BERT\n",
|
191 | 189 | "\n",
|
192 |
| - "In this section, we will load a pretrained BERT model and use it for the Masked Language Modelling task" |
| 190 | + "In this section, we will load a pretrained BERT model and use it for the Masked Language Modelling and Next Sentence Prediction task" |
193 | 191 | ]
|
194 | 192 | },
|
195 | 193 | {
|
196 | 194 | "cell_type": "code",
|
197 |
| - "execution_count": 14, |
| 195 | + "execution_count": 6, |
198 | 196 | "metadata": {},
|
199 | 197 | "outputs": [
|
200 | 198 | {
|
|
225 | 223 | "cell_type": "markdown",
|
226 | 224 | "metadata": {},
|
227 | 225 | "source": [
|
228 |
| - "Use the \"[MASK]\" token to mask any tokens which you would like the model to predict." |
| 226 | + "## Masked Language Modelling\n", |
| 227 | + "Use the \"[MASK]\" token to mask any tokens which you would like the model to predict. \n", |
| 228 | + "When specifying return_type=\"predictions\" the prediction of the model is returned, alternatively (and by default) the function returns logits. \n", |
| 229 | + "You can also specify None as return type for which nothing is returned" |
229 | 230 | ]
|
230 | 231 | },
|
231 | 232 | {
|
232 | 233 | "cell_type": "code",
|
233 |
| - "execution_count": 11, |
| 234 | + "execution_count": 7, |
234 | 235 | "metadata": {},
|
235 |
| - "outputs": [], |
| 236 | + "outputs": [ |
| 237 | + { |
| 238 | + "name": "stdout", |
| 239 | + "output_type": "stream", |
| 240 | + "text": [ |
| 241 | + "Prompt: The [MASK] is bright today.\n", |
| 242 | + "Prediction: \"sun\"\n" |
| 243 | + ] |
| 244 | + } |
| 245 | + ], |
| 246 | + "source": [ |
| 247 | + "prompt = \"The [MASK] is bright today.\"\n", |
| 248 | + "\n", |
| 249 | + "prediction = bert(prompt, return_type=\"predictions\")\n", |
| 250 | + "\n", |
| 251 | + "print(f\"Prompt: {prompt}\")\n", |
| 252 | + "print(f'Prediction: \"{prediction}\"')" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "markdown", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "You can also input a list of prompts:" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": 8, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [ |
| 267 | + { |
| 268 | + "name": "stdout", |
| 269 | + "output_type": "stream", |
| 270 | + "text": [ |
| 271 | + "Prompt: ['The [MASK] is bright today.', 'She [MASK] to the store.', 'The dog [MASK] the ball.']\n", |
| 272 | + "Prediction: \"['Prediction 0: sun', 'Prediction 1: went', 'Prediction 2: caught']\"\n" |
| 273 | + ] |
| 274 | + } |
| 275 | + ], |
| 276 | + "source": [ |
| 277 | + "prompts = [\"The [MASK] is bright today.\", \"She [MASK] to the store.\", \"The dog [MASK] the ball.\"]\n", |
| 278 | + "\n", |
| 279 | + "predictions = bert(prompts, return_type=\"predictions\")\n", |
| 280 | + "\n", |
| 281 | + "print(f\"Prompt: {prompts}\")\n", |
| 282 | + "print(f'Prediction: \"{predictions}\"')" |
| 283 | + ] |
| 284 | + }, |
| 285 | + { |
| 286 | + "cell_type": "markdown", |
| 287 | + "metadata": {}, |
| 288 | + "source": [ |
| 289 | + "## Next Sentence Prediction\n", |
| 290 | + "To carry out Next Sentence Prediction, you have to use the class BertNextSentencePrediction, and pass a HookedEncoder in its constructor. \n", |
| 291 | + "Then, create a list with the two sentences you want to perform NSP on as elements and use that as input to the forward function. \n", |
| 292 | + "The model will then predict the probability of the sentence at position 1 following (i.e. being the next sentence) to the sentence at position 0." |
| 293 | + ] |
| 294 | + }, |
| 295 | + { |
| 296 | + "cell_type": "code", |
| 297 | + "execution_count": 9, |
| 298 | + "metadata": {}, |
| 299 | + "outputs": [ |
| 300 | + { |
| 301 | + "name": "stdout", |
| 302 | + "output_type": "stream", |
| 303 | + "text": [ |
| 304 | + "Sentence A: A man walked into a grocery store.\n", |
| 305 | + "Sentence B: He bought an apple.\n", |
| 306 | + "Prediction: \"The sentences are sequential\"\n" |
| 307 | + ] |
| 308 | + } |
| 309 | + ], |
236 | 310 | "source": [
|
237 |
| - "prompt = \"BERT: Pre-training of Deep Bidirectional [MASK] for Language Understanding\"\n", |
| 311 | + "nsp = BertNextSentencePrediction(bert)\n", |
| 312 | + "sentence_a = \"A man walked into a grocery store.\"\n", |
| 313 | + "sentence_b = \"He bought an apple.\"\n", |
238 | 314 | "\n",
|
239 |
| - "input_ids = tokenizer(prompt, return_tensors=\"pt\")[\"input_ids\"]\n", |
240 |
| - "mask_index = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item()" |
| 315 | + "input = [sentence_a, sentence_b]\n", |
| 316 | + "\n", |
| 317 | + "predictions = nsp(input, return_type=\"predictions\")\n", |
| 318 | + "\n", |
| 319 | + "print(f\"Sentence A: {sentence_a}\")\n", |
| 320 | + "print(f\"Sentence B: {sentence_b}\")\n", |
| 321 | + "print(f'Prediction: \"{predictions}\"')" |
| 322 | + ] |
| 323 | + }, |
| 324 | + { |
| 325 | + "cell_type": "markdown", |
| 326 | + "metadata": {}, |
| 327 | + "source": [ |
| 328 | + "# Inputting tokens directly\n", |
| 329 | + "You can also input tokens instead of a string or a list of strings into the model, which could look something like this" |
241 | 330 | ]
|
242 | 331 | },
|
243 | 332 | {
|
244 | 333 | "cell_type": "code",
|
245 |
| - "execution_count": 12, |
| 334 | + "execution_count": 10, |
246 | 335 | "metadata": {},
|
247 | 336 | "outputs": [
|
248 | 337 | {
|
249 | 338 | "name": "stdout",
|
250 | 339 | "output_type": "stream",
|
251 | 340 | "text": [
|
252 |
| - "Prompt: BERT: Pre-training of Deep Bidirectional [MASK] for Language Understanding\n", |
253 |
| - "Prediction: \"Systems\"\n" |
| 341 | + "Prompt: The [MASK] is bright today.\n", |
| 342 | + "Prediction: \"sun\"\n" |
254 | 343 | ]
|
255 | 344 | }
|
256 | 345 | ],
|
257 | 346 | "source": [
|
258 |
| - "logprobs = bert(input_ids)[input_ids == tokenizer.mask_token_id].log_softmax(dim=-1)\n", |
| 347 | + "prompt = \"The [MASK] is bright today.\"\n", |
| 348 | + "\n", |
| 349 | + "tokens = tokenizer(prompt, return_tensors=\"pt\")[\"input_ids\"]\n", |
| 350 | + "logits = bert(tokens) # Since we are not specifying return_type, we get the logits\n", |
| 351 | + "logprobs = logits[tokens == tokenizer.mask_token_id].log_softmax(dim=-1)\n", |
259 | 352 | "prediction = tokenizer.decode(logprobs.argmax(dim=-1).item())\n",
|
260 | 353 | "\n",
|
261 | 354 | "print(f\"Prompt: {prompt}\")\n",
|
|
267 | 360 | "cell_type": "markdown",
|
268 | 361 | "metadata": {},
|
269 | 362 | "source": [
|
270 |
| - "Better luck next time, BERT." |
| 363 | + "Well done, BERT!" |
271 | 364 | ]
|
272 | 365 | }
|
273 | 366 | ],
|
274 | 367 | "metadata": {
|
275 | 368 | "kernelspec": {
|
276 |
| - "display_name": ".venv", |
| 369 | + "display_name": "Python 3", |
277 | 370 | "language": "python",
|
278 | 371 | "name": "python3"
|
279 | 372 | },
|
|
287 | 380 | "name": "python",
|
288 | 381 | "nbconvert_exporter": "python",
|
289 | 382 | "pygments_lexer": "ipython3",
|
290 |
| - "version": "3.11.8" |
| 383 | + "version": "3.10.15" |
291 | 384 | },
|
292 | 385 | "orig_nbformat": 4
|
293 | 386 | },
|
|
0 commit comments