florentianayuwono commited on
Commit
d3faa2e
1 Parent(s): 082fe19

Complete chapter 2

Browse files
Files changed (1) hide show
  1. training.ipynb +459 -522
training.ipynb CHANGED
@@ -10,7 +10,7 @@
10
  },
11
  {
12
  "cell_type": "code",
13
- "execution_count": 20,
14
  "metadata": {},
15
  "outputs": [],
16
  "source": [
@@ -27,7 +27,7 @@
27
  },
28
  {
29
  "cell_type": "code",
30
- "execution_count": 7,
31
  "metadata": {},
32
  "outputs": [
33
  {
@@ -44,7 +44,7 @@
44
  "[{'label': 'POSITIVE', 'score': 0.6012226343154907}]"
45
  ]
46
  },
47
- "execution_count": 7,
48
  "metadata": {},
49
  "output_type": "execute_result"
50
  }
@@ -58,7 +58,7 @@
58
  },
59
  {
60
  "cell_type": "code",
61
- "execution_count": 6,
62
  "metadata": {},
63
  "outputs": [
64
  {
@@ -68,7 +68,7 @@
68
  " {'label': 'NEGATIVE', 'score': 0.9995977282524109}]"
69
  ]
70
  },
71
- "execution_count": 6,
72
  "metadata": {},
73
  "output_type": "execute_result"
74
  }
@@ -81,7 +81,7 @@
81
  },
82
  {
83
  "cell_type": "code",
84
- "execution_count": 12,
85
  "metadata": {},
86
  "outputs": [
87
  {
@@ -100,7 +100,7 @@
100
  " 'scores': [0.7144545316696167, 0.19746531546115875, 0.08808010816574097]}"
101
  ]
102
  },
103
- "execution_count": 12,
104
  "metadata": {},
105
  "output_type": "execute_result"
106
  }
@@ -115,7 +115,7 @@
115
  },
116
  {
117
  "cell_type": "code",
118
- "execution_count": 13,
119
  "metadata": {},
120
  "outputs": [
121
  {
@@ -123,97 +123,7 @@
123
  "output_type": "stream",
124
  "text": [
125
  "No model was supplied, defaulted to gpt2 and revision 6c0e608 (https://huggingface.co/gpt2).\n",
126
- "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
127
- ]
128
- },
129
- {
130
- "data": {
131
- "application/vnd.jupyter.widget-view+json": {
132
- "model_id": "f065452c7f924df7a5666b71186fd6d5",
133
- "version_major": 2,
134
- "version_minor": 0
135
- },
136
- "text/plain": [
137
- "Downloading: 0%| | 0.00/665 [00:00<?, ?B/s]"
138
- ]
139
- },
140
- "metadata": {},
141
- "output_type": "display_data"
142
- },
143
- {
144
- "data": {
145
- "application/vnd.jupyter.widget-view+json": {
146
- "model_id": "f261021fbdc5427abc2c54914de96ed2",
147
- "version_major": 2,
148
- "version_minor": 0
149
- },
150
- "text/plain": [
151
- "Downloading: 0%| | 0.00/548M [00:00<?, ?B/s]"
152
- ]
153
- },
154
- "metadata": {},
155
- "output_type": "display_data"
156
- },
157
- {
158
- "data": {
159
- "application/vnd.jupyter.widget-view+json": {
160
- "model_id": "b3dbab5273b64fb09e75ced6c5380c1c",
161
- "version_major": 2,
162
- "version_minor": 0
163
- },
164
- "text/plain": [
165
- "Downloading: 0%| | 0.00/124 [00:00<?, ?B/s]"
166
- ]
167
- },
168
- "metadata": {},
169
- "output_type": "display_data"
170
- },
171
- {
172
- "data": {
173
- "application/vnd.jupyter.widget-view+json": {
174
- "model_id": "85df61ca152e4e9dabccb090df5b195e",
175
- "version_major": 2,
176
- "version_minor": 0
177
- },
178
- "text/plain": [
179
- "Downloading: 0%| | 0.00/1.04M [00:00<?, ?B/s]"
180
- ]
181
- },
182
- "metadata": {},
183
- "output_type": "display_data"
184
- },
185
- {
186
- "data": {
187
- "application/vnd.jupyter.widget-view+json": {
188
- "model_id": "3a371f13013f4e90890b17b0d56fa7d6",
189
- "version_major": 2,
190
- "version_minor": 0
191
- },
192
- "text/plain": [
193
- "Downloading: 0%| | 0.00/456k [00:00<?, ?B/s]"
194
- ]
195
- },
196
- "metadata": {},
197
- "output_type": "display_data"
198
- },
199
- {
200
- "data": {
201
- "application/vnd.jupyter.widget-view+json": {
202
- "model_id": "389ab29d60d64fd7bc9c7745599cf713",
203
- "version_major": 2,
204
- "version_minor": 0
205
- },
206
- "text/plain": [
207
- "Downloading: 0%| | 0.00/1.36M [00:00<?, ?B/s]"
208
- ]
209
- },
210
- "metadata": {},
211
- "output_type": "display_data"
212
- },
213
- {
214
- "name": "stderr",
215
- "output_type": "stream",
216
- "text": [
217
  "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
218
  "/Users/florentiana.yuwono/anaconda3/lib/python3.10/site-packages/transformers/generation/utils.py:1353: UserWarning: Using `max_length`'s default (50) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
219
  " warnings.warn(\n"
@@ -222,10 +132,10 @@
222
  {
223
  "data": {
224
  "text/plain": [
225
- "[{'generated_text': \"In this class, I will speak about something I've been thinking about for quite some time and it won't even come up for a while.\\n\\nLet's be honest and tell you; it has to be so simple. You do not need\"}]"
226
  ]
227
  },
228
- "execution_count": 13,
229
  "metadata": {},
230
  "output_type": "execute_result"
231
  }
@@ -237,93 +147,9 @@
237
  },
238
  {
239
  "cell_type": "code",
240
- "execution_count": 14,
241
  "metadata": {},
242
  "outputs": [
243
- {
244
- "data": {
245
- "application/vnd.jupyter.widget-view+json": {
246
- "model_id": "d893dfa6d45e4dce8f8f548ce903c330",
247
- "version_major": 2,
248
- "version_minor": 0
249
- },
250
- "text/plain": [
251
- "Downloading: 0%| | 0.00/762 [00:00<?, ?B/s]"
252
- ]
253
- },
254
- "metadata": {},
255
- "output_type": "display_data"
256
- },
257
- {
258
- "data": {
259
- "application/vnd.jupyter.widget-view+json": {
260
- "model_id": "5e0cafc878e94597872168c19bcfbe00",
261
- "version_major": 2,
262
- "version_minor": 0
263
- },
264
- "text/plain": [
265
- "Downloading: 0%| | 0.00/353M [00:00<?, ?B/s]"
266
- ]
267
- },
268
- "metadata": {},
269
- "output_type": "display_data"
270
- },
271
- {
272
- "data": {
273
- "application/vnd.jupyter.widget-view+json": {
274
- "model_id": "a374402e1a4748f4b3b8b964589f6868",
275
- "version_major": 2,
276
- "version_minor": 0
277
- },
278
- "text/plain": [
279
- "Downloading: 0%| | 0.00/124 [00:00<?, ?B/s]"
280
- ]
281
- },
282
- "metadata": {},
283
- "output_type": "display_data"
284
- },
285
- {
286
- "data": {
287
- "application/vnd.jupyter.widget-view+json": {
288
- "model_id": "8476884d2c114598904427a1891a6beb",
289
- "version_major": 2,
290
- "version_minor": 0
291
- },
292
- "text/plain": [
293
- "Downloading: 0%| | 0.00/1.04M [00:00<?, ?B/s]"
294
- ]
295
- },
296
- "metadata": {},
297
- "output_type": "display_data"
298
- },
299
- {
300
- "data": {
301
- "application/vnd.jupyter.widget-view+json": {
302
- "model_id": "4730d96e505a4cf0b9f2761a1201822b",
303
- "version_major": 2,
304
- "version_minor": 0
305
- },
306
- "text/plain": [
307
- "Downloading: 0%| | 0.00/456k [00:00<?, ?B/s]"
308
- ]
309
- },
310
- "metadata": {},
311
- "output_type": "display_data"
312
- },
313
- {
314
- "data": {
315
- "application/vnd.jupyter.widget-view+json": {
316
- "model_id": "ca8e61625c024428963339dfca8ecb67",
317
- "version_major": 2,
318
- "version_minor": 0
319
- },
320
- "text/plain": [
321
- "Downloading: 0%| | 0.00/1.36M [00:00<?, ?B/s]"
322
- ]
323
- },
324
- "metadata": {},
325
- "output_type": "display_data"
326
- },
327
  {
328
  "name": "stderr",
329
  "output_type": "stream",
@@ -334,11 +160,11 @@
334
  {
335
  "data": {
336
  "text/plain": [
337
- "[{'generated_text': 'In this class, I will speak as a lecturer to the students on social media (for those of you who are interested).\\nThere are many classes'},\n",
338
- " {'generated_text': 'In this class, I will speak for a particular type of group of writers that I want to talk about, like us writers like us writers, people'}]"
339
  ]
340
  },
341
- "execution_count": 14,
342
  "metadata": {},
343
  "output_type": "execute_result"
344
  }
@@ -354,7 +180,7 @@
354
  },
355
  {
356
  "cell_type": "code",
357
- "execution_count": 15,
358
  "metadata": {},
359
  "outputs": [
360
  {
@@ -365,76 +191,6 @@
365
  "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
366
  ]
367
  },
368
- {
369
- "data": {
370
- "application/vnd.jupyter.widget-view+json": {
371
- "model_id": "cf08df8aa5a444649e21d7c0d7e64039",
372
- "version_major": 2,
373
- "version_minor": 0
374
- },
375
- "text/plain": [
376
- "Downloading: 0%| | 0.00/480 [00:00<?, ?B/s]"
377
- ]
378
- },
379
- "metadata": {},
380
- "output_type": "display_data"
381
- },
382
- {
383
- "data": {
384
- "application/vnd.jupyter.widget-view+json": {
385
- "model_id": "8b5e4348c7904d0890da6b768461c5af",
386
- "version_major": 2,
387
- "version_minor": 0
388
- },
389
- "text/plain": [
390
- "Downloading: 0%| | 0.00/331M [00:00<?, ?B/s]"
391
- ]
392
- },
393
- "metadata": {},
394
- "output_type": "display_data"
395
- },
396
- {
397
- "data": {
398
- "application/vnd.jupyter.widget-view+json": {
399
- "model_id": "100d8fbb75b14bea92bb635b116c1d08",
400
- "version_major": 2,
401
- "version_minor": 0
402
- },
403
- "text/plain": [
404
- "Downloading: 0%| | 0.00/899k [00:00<?, ?B/s]"
405
- ]
406
- },
407
- "metadata": {},
408
- "output_type": "display_data"
409
- },
410
- {
411
- "data": {
412
- "application/vnd.jupyter.widget-view+json": {
413
- "model_id": "a4cdc5db6d7541918624369df29642ed",
414
- "version_major": 2,
415
- "version_minor": 0
416
- },
417
- "text/plain": [
418
- "Downloading: 0%| | 0.00/456k [00:00<?, ?B/s]"
419
- ]
420
- },
421
- "metadata": {},
422
- "output_type": "display_data"
423
- },
424
- {
425
- "data": {
426
- "application/vnd.jupyter.widget-view+json": {
427
- "model_id": "d808f036abef4c62bea2dd62601502e8",
428
- "version_major": 2,
429
- "version_minor": 0
430
- },
431
- "text/plain": [
432
- "Downloading: 0%| | 0.00/1.36M [00:00<?, ?B/s]"
433
- ]
434
- },
435
- "metadata": {},
436
- "output_type": "display_data"
437
- },
438
  {
439
  "data": {
440
  "text/plain": [
@@ -452,7 +208,7 @@
452
  " 'sequence': 'The sky is blue and bright, I wonder what that is about.'}]"
453
  ]
454
  },
455
- "execution_count": 15,
456
  "metadata": {},
457
  "output_type": "execute_result"
458
  }
@@ -464,7 +220,7 @@
464
  },
465
  {
466
  "cell_type": "code",
467
- "execution_count": 16,
468
  "metadata": {},
469
  "outputs": [
470
  {
@@ -472,99 +228,37 @@
472
  "output_type": "stream",
473
  "text": [
474
  "No model was supplied, defaulted to dbmdz/bert-large-cased-finetuned-conll03-english and revision f2482bf (https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english).\n",
475
- "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
 
 
476
  ]
477
  },
478
  {
479
  "data": {
480
- "application/vnd.jupyter.widget-view+json": {
481
- "model_id": "d1a918d701ee46ccb54ab2989305585f",
482
- "version_major": 2,
483
- "version_minor": 0
484
- },
485
  "text/plain": [
486
- "Downloading: 0%| | 0.00/998 [00:00<?, ?B/s]"
 
 
 
 
487
  ]
488
  },
 
489
  "metadata": {},
490
- "output_type": "display_data"
491
- },
492
- {
493
- "data": {
494
- "application/vnd.jupyter.widget-view+json": {
495
- "model_id": "a95eff2616b64dcfba172bc43dfe23be",
496
- "version_major": 2,
497
- "version_minor": 0
498
- },
499
- "text/plain": [
500
- "Downloading: 0%| | 0.00/1.33G [00:00<?, ?B/s]"
501
- ]
502
- },
503
- "metadata": {},
504
- "output_type": "display_data"
505
- },
506
- {
507
- "data": {
508
- "application/vnd.jupyter.widget-view+json": {
509
- "model_id": "63c9f30a9542417aabc57971b9d9b44f",
510
- "version_major": 2,
511
- "version_minor": 0
512
- },
513
- "text/plain": [
514
- "Downloading: 0%| | 0.00/60.0 [00:00<?, ?B/s]"
515
- ]
516
- },
517
- "metadata": {},
518
- "output_type": "display_data"
519
- },
520
- {
521
- "data": {
522
- "application/vnd.jupyter.widget-view+json": {
523
- "model_id": "e61894113e164c1eb5a66ad300971bfe",
524
- "version_major": 2,
525
- "version_minor": 0
526
- },
527
- "text/plain": [
528
- "Downloading: 0%| | 0.00/213k [00:00<?, ?B/s]"
529
- ]
530
- },
531
- "metadata": {},
532
- "output_type": "display_data"
533
- },
534
- {
535
- "name": "stderr",
536
- "output_type": "stream",
537
- "text": [
538
- "/Users/florentiana.yuwono/anaconda3/lib/python3.10/site-packages/transformers/pipelines/token_classification.py:169: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy=\"simple\"` instead.\n",
539
- " warnings.warn(\n"
540
- ]
541
- },
542
- {
543
- "data": {
544
- "text/plain": [
545
- "[{'entity_group': 'LOC',\n",
546
- " 'score': 0.86960346,\n",
547
- " 'word': 'Owl City',\n",
548
- " 'start': 56,\n",
549
- " 'end': 64}]"
550
- ]
551
- },
552
- "execution_count": 16,
553
- "metadata": {},
554
- "output_type": "execute_result"
555
- }
556
- ],
557
- "source": [
558
- "ner = pipeline(\"ner\", grouped_entities=True)\n",
559
- "\n",
560
- "ner(\"Mine is Hilarious, usually spotted at united nations in Owl City.\")"
561
- ]
562
- },
563
- {
564
- "cell_type": "code",
565
- "execution_count": 17,
566
- "metadata": {},
567
- "outputs": [
568
  {
569
  "name": "stderr",
570
  "output_type": "stream",
@@ -573,76 +267,6 @@
573
  "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
574
  ]
575
  },
576
- {
577
- "data": {
578
- "application/vnd.jupyter.widget-view+json": {
579
- "model_id": "95136a50322745ea8715e36e9e2a4b50",
580
- "version_major": 2,
581
- "version_minor": 0
582
- },
583
- "text/plain": [
584
- "Downloading: 0%| | 0.00/473 [00:00<?, ?B/s]"
585
- ]
586
- },
587
- "metadata": {},
588
- "output_type": "display_data"
589
- },
590
- {
591
- "data": {
592
- "application/vnd.jupyter.widget-view+json": {
593
- "model_id": "a1440abd06884310b97a38e669641a21",
594
- "version_major": 2,
595
- "version_minor": 0
596
- },
597
- "text/plain": [
598
- "Downloading: 0%| | 0.00/261M [00:00<?, ?B/s]"
599
- ]
600
- },
601
- "metadata": {},
602
- "output_type": "display_data"
603
- },
604
- {
605
- "data": {
606
- "application/vnd.jupyter.widget-view+json": {
607
- "model_id": "3359a992135d47a48b29f5e0f939eb44",
608
- "version_major": 2,
609
- "version_minor": 0
610
- },
611
- "text/plain": [
612
- "Downloading: 0%| | 0.00/29.0 [00:00<?, ?B/s]"
613
- ]
614
- },
615
- "metadata": {},
616
- "output_type": "display_data"
617
- },
618
- {
619
- "data": {
620
- "application/vnd.jupyter.widget-view+json": {
621
- "model_id": "bd5f68624b5c4b62b4c87d8e046b69f4",
622
- "version_major": 2,
623
- "version_minor": 0
624
- },
625
- "text/plain": [
626
- "Downloading: 0%| | 0.00/213k [00:00<?, ?B/s]"
627
- ]
628
- },
629
- "metadata": {},
630
- "output_type": "display_data"
631
- },
632
- {
633
- "data": {
634
- "application/vnd.jupyter.widget-view+json": {
635
- "model_id": "7160d2a6f8a942d7b298453684e9ee49",
636
- "version_major": 2,
637
- "version_minor": 0
638
- },
639
- "text/plain": [
640
- "Downloading: 0%| | 0.00/436k [00:00<?, ?B/s]"
641
- ]
642
- },
643
- "metadata": {},
644
- "output_type": "display_data"
645
- },
646
  {
647
  "data": {
648
  "text/plain": [
@@ -652,7 +276,7 @@
652
  " 'answer': 'Mine is Hilarious, usually grab from library'}"
653
  ]
654
  },
655
- "execution_count": 17,
656
  "metadata": {},
657
  "output_type": "execute_result"
658
  }
@@ -667,7 +291,7 @@
667
  },
668
  {
669
  "cell_type": "code",
670
- "execution_count": 18,
671
  "metadata": {},
672
  "outputs": [
673
  {
@@ -678,83 +302,13 @@
678
  "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
679
  ]
680
  },
681
- {
682
- "data": {
683
- "application/vnd.jupyter.widget-view+json": {
684
- "model_id": "201842ea28eb45b9838d9ad77e90b528",
685
- "version_major": 2,
686
- "version_minor": 0
687
- },
688
- "text/plain": [
689
- "Downloading: 0%| | 0.00/1.80k [00:00<?, ?B/s]"
690
- ]
691
- },
692
- "metadata": {},
693
- "output_type": "display_data"
694
- },
695
- {
696
- "data": {
697
- "application/vnd.jupyter.widget-view+json": {
698
- "model_id": "848fa320643340798fc4e3c6119b567e",
699
- "version_major": 2,
700
- "version_minor": 0
701
- },
702
- "text/plain": [
703
- "Downloading: 0%| | 0.00/1.22G [00:00<?, ?B/s]"
704
- ]
705
- },
706
- "metadata": {},
707
- "output_type": "display_data"
708
- },
709
- {
710
- "data": {
711
- "application/vnd.jupyter.widget-view+json": {
712
- "model_id": "bf8110edeb18471fa048308bb782b089",
713
- "version_major": 2,
714
- "version_minor": 0
715
- },
716
- "text/plain": [
717
- "Downloading: 0%| | 0.00/26.0 [00:00<?, ?B/s]"
718
- ]
719
- },
720
- "metadata": {},
721
- "output_type": "display_data"
722
- },
723
- {
724
- "data": {
725
- "application/vnd.jupyter.widget-view+json": {
726
- "model_id": "b05eb6e6ada44e1ea88342739fc0f215",
727
- "version_major": 2,
728
- "version_minor": 0
729
- },
730
- "text/plain": [
731
- "Downloading: 0%| | 0.00/899k [00:00<?, ?B/s]"
732
- ]
733
- },
734
- "metadata": {},
735
- "output_type": "display_data"
736
- },
737
- {
738
- "data": {
739
- "application/vnd.jupyter.widget-view+json": {
740
- "model_id": "992d67bdfdb64d2387f4029d4edb8d4f",
741
- "version_major": 2,
742
- "version_minor": 0
743
- },
744
- "text/plain": [
745
- "Downloading: 0%| | 0.00/456k [00:00<?, ?B/s]"
746
- ]
747
- },
748
- "metadata": {},
749
- "output_type": "display_data"
750
- },
751
  {
752
  "data": {
753
  "text/plain": [
754
  "[{'summary_text': ' America has changed dramatically during recent years . The number of engineering graduates in the U.S. has declined in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering . Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance engineering .'}]"
755
  ]
756
  },
757
- "execution_count": 18,
758
  "metadata": {},
759
  "output_type": "execute_result"
760
  }
@@ -787,21 +341,18 @@
787
  },
788
  {
789
  "cell_type": "code",
790
- "execution_count": 24,
791
  "metadata": {},
792
  "outputs": [
793
  {
794
- "ename": "ValueError",
795
- "evalue": "This tokenizer cannot be instantiated. Please make sure you have `sentencepiece` installed in order to use this tokenizer.",
796
- "output_type": "error",
797
- "traceback": [
798
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
799
- "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
800
- "Cell \u001b[0;32mIn[24], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m translator \u001b[39m=\u001b[39m pipeline(\u001b[39m\"\u001b[39;49m\u001b[39mtranslation\u001b[39;49m\u001b[39m\"\u001b[39;49m, model\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mHelsinki-NLP/opus-mt-fr-en\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n\u001b[1;32m 2\u001b[0m translator(\u001b[39m\"\u001b[39m\u001b[39mCe cours est produit par.\u001b[39m\u001b[39m\"\u001b[39m)\n",
801
- "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/transformers/pipelines/__init__.py:885\u001b[0m, in \u001b[0;36mpipeline\u001b[0;34m(task, model, config, tokenizer, feature_extractor, image_processor, framework, revision, use_fast, use_auth_token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)\u001b[0m\n\u001b[1;32m 882\u001b[0m tokenizer_kwargs \u001b[39m=\u001b[39m model_kwargs\u001b[39m.\u001b[39mcopy()\n\u001b[1;32m 883\u001b[0m tokenizer_kwargs\u001b[39m.\u001b[39mpop(\u001b[39m\"\u001b[39m\u001b[39mtorch_dtype\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39mNone\u001b[39;00m)\n\u001b[0;32m--> 885\u001b[0m tokenizer \u001b[39m=\u001b[39m AutoTokenizer\u001b[39m.\u001b[39;49mfrom_pretrained(\n\u001b[1;32m 886\u001b[0m tokenizer_identifier, use_fast\u001b[39m=\u001b[39;49muse_fast, _from_pipeline\u001b[39m=\u001b[39;49mtask, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mhub_kwargs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mtokenizer_kwargs\n\u001b[1;32m 887\u001b[0m )\n\u001b[1;32m 889\u001b[0m \u001b[39mif\u001b[39;00m load_image_processor:\n\u001b[1;32m 890\u001b[0m \u001b[39m# Try to infer image processor from model or config name (if provided as str)\u001b[39;00m\n\u001b[1;32m 891\u001b[0m \u001b[39mif\u001b[39;00m image_processor \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n",
802
- "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py:714\u001b[0m, in \u001b[0;36mAutoTokenizer.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *inputs, **kwargs)\u001b[0m\n\u001b[1;32m 712\u001b[0m \u001b[39mreturn\u001b[39;00m tokenizer_class_py\u001b[39m.\u001b[39mfrom_pretrained(pretrained_model_name_or_path, \u001b[39m*\u001b[39minputs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[1;32m 713\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 714\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 715\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mThis tokenizer cannot be instantiated. Please make sure you have `sentencepiece` installed \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 716\u001b[0m \u001b[39m\"\u001b[39m\u001b[39min order to use this tokenizer.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 717\u001b[0m )\n\u001b[1;32m 719\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 720\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mUnrecognized configuration class \u001b[39m\u001b[39m{\u001b[39;00mconfig\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m}\u001b[39;00m\u001b[39m to build an AutoTokenizer.\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[1;32m 721\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mModel type should be one of \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m, \u001b[39m\u001b[39m'\u001b[39m\u001b[39m.\u001b[39mjoin(c\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m \u001b[39mfor\u001b[39;00m c \u001b[39min\u001b[39;00m TOKENIZER_MAPPING\u001b[39m.\u001b[39mkeys())\u001b[39m}\u001b[39;00m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 722\u001b[0m )\n",
803
- "\u001b[0;31mValueError\u001b[0m: This tokenizer cannot be instantiated. Please make sure you have `sentencepiece` installed in order to use this tokenizer."
804
- ]
805
  }
806
  ],
807
  "source": [
@@ -817,15 +368,231 @@
817
  "## Bias and limitations"
818
  ]
819
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820
  {
821
  "cell_type": "code",
822
  "execution_count": 26,
823
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824
  "outputs": [
825
  {
826
  "data": {
827
  "application/vnd.jupyter.widget-view+json": {
828
- "model_id": "e7aa993970d14ab986e0a7ea1e60087a",
829
  "version_major": 2,
830
  "version_minor": 0
831
  },
@@ -839,12 +606,12 @@
839
  {
840
  "data": {
841
  "application/vnd.jupyter.widget-view+json": {
842
- "model_id": "67fc2d322c01448b97219733efdaed9e",
843
  "version_major": 2,
844
  "version_minor": 0
845
  },
846
  "text/plain": [
847
- "Downloading: 0%| | 0.00/440M [00:00<?, ?B/s]"
848
  ]
849
  },
850
  "metadata": {},
@@ -854,20 +621,32 @@
854
  "name": "stderr",
855
  "output_type": "stream",
856
  "text": [
857
- "Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertForMaskedLM: ['cls.seq_relationship.weight', 'cls.seq_relationship.bias']\n",
858
- "- This IS expected if you are initializing BertForMaskedLM from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
859
- "- This IS NOT expected if you are initializing BertForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
860
  ]
861
- },
 
 
 
 
 
 
 
 
 
 
 
 
862
  {
863
  "data": {
864
  "application/vnd.jupyter.widget-view+json": {
865
- "model_id": "0621be2286144ee296194d349ae8dd5d",
866
  "version_major": 2,
867
  "version_minor": 0
868
  },
869
  "text/plain": [
870
- "Downloading: 0%| | 0.00/28.0 [00:00<?, ?B/s]"
871
  ]
872
  },
873
  "metadata": {},
@@ -876,12 +655,12 @@
876
  {
877
  "data": {
878
  "application/vnd.jupyter.widget-view+json": {
879
- "model_id": "674c967d777243819fc3dc85915a1764",
880
  "version_major": 2,
881
  "version_minor": 0
882
  },
883
  "text/plain": [
884
- "Downloading: 0%| | 0.00/232k [00:00<?, ?B/s]"
885
  ]
886
  },
887
  "metadata": {},
@@ -890,12 +669,12 @@
890
  {
891
  "data": {
892
  "application/vnd.jupyter.widget-view+json": {
893
- "model_id": "d27abdd37df84243b85e16dea5d6d65e",
894
  "version_major": 2,
895
  "version_minor": 0
896
  },
897
  "text/plain": [
898
- "Downloading: 0%| | 0.00/466k [00:00<?, ?B/s]"
899
  ]
900
  },
901
  "metadata": {},
@@ -905,19 +684,177 @@
905
  "name": "stdout",
906
  "output_type": "stream",
907
  "text": [
908
- "['carpenter', 'lawyer', 'farmer', 'businessman', 'doctor']\n",
909
- "['nurse', 'maid', 'teacher', 'waitress', 'prostitute']\n"
910
  ]
911
  }
912
  ],
913
  "source": [
914
- "unmasker = pipeline(\"fill-mask\", model=\"bert-base-uncased\")\n",
915
- "result = unmasker(\"This man works as a [MASK].\")\n",
916
- "print([r[\"token_str\"] for r in result])\n",
917
  "\n",
918
- "result = unmasker(\"This woman works as a [MASK].\")\n",
919
- "print([r[\"token_str\"] for r in result])"
 
 
920
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
921
  }
922
  ],
923
  "metadata": {
 
10
  },
11
  {
12
  "cell_type": "code",
13
+ "execution_count": 2,
14
  "metadata": {},
15
  "outputs": [],
16
  "source": [
 
27
  },
28
  {
29
  "cell_type": "code",
30
+ "execution_count": 3,
31
  "metadata": {},
32
  "outputs": [
33
  {
 
44
  "[{'label': 'POSITIVE', 'score': 0.6012226343154907}]"
45
  ]
46
  },
47
+ "execution_count": 3,
48
  "metadata": {},
49
  "output_type": "execute_result"
50
  }
 
58
  },
59
  {
60
  "cell_type": "code",
61
+ "execution_count": 4,
62
  "metadata": {},
63
  "outputs": [
64
  {
 
68
  " {'label': 'NEGATIVE', 'score': 0.9995977282524109}]"
69
  ]
70
  },
71
+ "execution_count": 4,
72
  "metadata": {},
73
  "output_type": "execute_result"
74
  }
 
81
  },
82
  {
83
  "cell_type": "code",
84
+ "execution_count": 5,
85
  "metadata": {},
86
  "outputs": [
87
  {
 
100
  " 'scores': [0.7144545316696167, 0.19746531546115875, 0.08808010816574097]}"
101
  ]
102
  },
103
+ "execution_count": 5,
104
  "metadata": {},
105
  "output_type": "execute_result"
106
  }
 
115
  },
116
  {
117
  "cell_type": "code",
118
+ "execution_count": 6,
119
  "metadata": {},
120
  "outputs": [
121
  {
 
123
  "output_type": "stream",
124
  "text": [
125
  "No model was supplied, defaulted to gpt2 and revision 6c0e608 (https://huggingface.co/gpt2).\n",
126
+ "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
128
  "/Users/florentiana.yuwono/anaconda3/lib/python3.10/site-packages/transformers/generation/utils.py:1353: UserWarning: Using `max_length`'s default (50) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
129
  " warnings.warn(\n"
 
132
  {
133
  "data": {
134
  "text/plain": [
135
+ "[{'generated_text': 'In this class, I will speak about the key differences between Python and Java, how to do it, how to use it with Python, the importance of data literals to your data structure and more.\\n\\nA lot of the time I am'}]"
136
  ]
137
  },
138
+ "execution_count": 6,
139
  "metadata": {},
140
  "output_type": "execute_result"
141
  }
 
147
  },
148
  {
149
  "cell_type": "code",
150
+ "execution_count": 7,
151
  "metadata": {},
152
  "outputs": [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  {
154
  "name": "stderr",
155
  "output_type": "stream",
 
160
  {
161
  "data": {
162
  "text/plain": [
163
+ "[{'generated_text': 'In this class, I will speak about each of the subclasses in question which you have introduced in the introduction.\\nThe above classes are built around'},\n",
164
+ " {'generated_text': 'In this class, I will speak of the fact that you can\\u2028t read the entire class,\\u202a and read all the classes,�'}]"
165
  ]
166
  },
167
+ "execution_count": 7,
168
  "metadata": {},
169
  "output_type": "execute_result"
170
  }
 
180
  },
181
  {
182
  "cell_type": "code",
183
+ "execution_count": 8,
184
  "metadata": {},
185
  "outputs": [
186
  {
 
191
  "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
192
  ]
193
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  {
195
  "data": {
196
  "text/plain": [
 
208
  " 'sequence': 'The sky is blue and bright, I wonder what that is about.'}]"
209
  ]
210
  },
211
+ "execution_count": 8,
212
  "metadata": {},
213
  "output_type": "execute_result"
214
  }
 
220
  },
221
  {
222
  "cell_type": "code",
223
+ "execution_count": 9,
224
  "metadata": {},
225
  "outputs": [
226
  {
 
228
  "output_type": "stream",
229
  "text": [
230
  "No model was supplied, defaulted to dbmdz/bert-large-cased-finetuned-conll03-english and revision f2482bf (https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english).\n",
231
+ "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
232
+ "/Users/florentiana.yuwono/anaconda3/lib/python3.10/site-packages/transformers/pipelines/token_classification.py:169: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy=\"simple\"` instead.\n",
233
+ " warnings.warn(\n"
234
  ]
235
  },
236
  {
237
  "data": {
 
 
 
 
 
238
  "text/plain": [
239
+ "[{'entity_group': 'LOC',\n",
240
+ " 'score': 0.86960346,\n",
241
+ " 'word': 'Owl City',\n",
242
+ " 'start': 56,\n",
243
+ " 'end': 64}]"
244
  ]
245
  },
246
+ "execution_count": 9,
247
  "metadata": {},
248
+ "output_type": "execute_result"
249
+ }
250
+ ],
251
+ "source": [
252
+ "ner = pipeline(\"ner\", grouped_entities=True)\n",
253
+ "\n",
254
+ "ner(\"Mine is Hilarious, usually spotted at united nations in Owl City.\")"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": 10,
260
+ "metadata": {},
261
+ "outputs": [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  {
263
  "name": "stderr",
264
  "output_type": "stream",
 
267
  "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
268
  ]
269
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  {
271
  "data": {
272
  "text/plain": [
 
276
  " 'answer': 'Mine is Hilarious, usually grab from library'}"
277
  ]
278
  },
279
+ "execution_count": 10,
280
  "metadata": {},
281
  "output_type": "execute_result"
282
  }
 
291
  },
292
  {
293
  "cell_type": "code",
294
+ "execution_count": 11,
295
  "metadata": {},
296
  "outputs": [
297
  {
 
302
  "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
303
  ]
304
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  {
306
  "data": {
307
  "text/plain": [
308
  "[{'summary_text': ' America has changed dramatically during recent years . The number of engineering graduates in the U.S. has declined in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering . Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance engineering .'}]"
309
  ]
310
  },
311
+ "execution_count": 11,
312
  "metadata": {},
313
  "output_type": "execute_result"
314
  }
 
341
  },
342
  {
343
  "cell_type": "code",
344
+ "execution_count": 14,
345
  "metadata": {},
346
  "outputs": [
347
  {
348
+ "data": {
349
+ "text/plain": [
350
+ "[{'translation_text': 'This course is produced by para.'}]"
351
+ ]
352
+ },
353
+ "execution_count": 14,
354
+ "metadata": {},
355
+ "output_type": "execute_result"
 
 
 
356
  }
357
  ],
358
  "source": [
 
368
  "## Bias and limitations"
369
  ]
370
  },
371
+ {
372
+ "cell_type": "code",
373
+ "execution_count": 15,
374
+ "metadata": {},
375
+ "outputs": [
376
+ {
377
+ "name": "stderr",
378
+ "output_type": "stream",
379
+ "text": [
380
+ "Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertForMaskedLM: ['cls.seq_relationship.weight', 'cls.seq_relationship.bias']\n",
381
+ "- This IS expected if you are initializing BertForMaskedLM from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
382
+ "- This IS NOT expected if you are initializing BertForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
383
+ ]
384
+ },
385
+ {
386
+ "name": "stdout",
387
+ "output_type": "stream",
388
+ "text": [
389
+ "['carpenter', 'lawyer', 'farmer', 'businessman', 'doctor']\n",
390
+ "['nurse', 'maid', 'teacher', 'waitress', 'prostitute']\n"
391
+ ]
392
+ }
393
+ ],
394
+ "source": [
395
+ "unmasker = pipeline(\"fill-mask\", model=\"bert-base-uncased\")\n",
396
+ "result = unmasker(\"This man works as a [MASK].\")\n",
397
+ "print([r[\"token_str\"] for r in result])\n",
398
+ "\n",
399
+ "result = unmasker(\"This woman works as a [MASK].\")\n",
400
+ "print([r[\"token_str\"] for r in result])"
401
+ ]
402
+ },
403
+ {
404
+ "attachments": {},
405
+ "cell_type": "markdown",
406
+ "metadata": {},
407
+ "source": [
408
+ "# 2. Using Transformers"
409
+ ]
410
+ },
411
+ {
412
+ "cell_type": "code",
413
+ "execution_count": 16,
414
+ "metadata": {},
415
+ "outputs": [],
416
+ "source": [
417
+ "from transformers import AutoTokenizer"
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "code",
422
+ "execution_count": 17,
423
+ "metadata": {},
424
+ "outputs": [],
425
+ "source": [
426
+ "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n",
427
+ "tokenizer = AutoTokenizer.from_pretrained(checkpoint)"
428
+ ]
429
+ },
430
+ {
431
+ "cell_type": "code",
432
+ "execution_count": 18,
433
+ "metadata": {},
434
+ "outputs": [
435
+ {
436
+ "name": "stdout",
437
+ "output_type": "stream",
438
+ "text": [
439
+ "{'input_ids': tensor([[ 101, 1045, 1005, 2310, 2042, 3403, 2005, 17662, 2227, 2026,\n",
440
+ " 2878, 2166, 1012, 102],\n",
441
+ " [ 101, 1045, 5223, 2023, 2061, 2172, 999, 999, 102, 0,\n",
442
+ " 0, 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
443
+ " [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]])}\n"
444
+ ]
445
+ }
446
+ ],
447
+ "source": [
448
+ "raw_inputs = [\n",
449
+ " \"I've been waiting for Hugging Face my whole life.\",\n",
450
+ " \"I hate this so much!!\"\n",
451
+ "]\n",
452
+ "inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors=\"pt\")\n",
453
+ "print(inputs)"
454
+ ]
455
+ },
456
+ {
457
+ "cell_type": "code",
458
+ "execution_count": 19,
459
+ "metadata": {},
460
+ "outputs": [
461
+ {
462
+ "name": "stderr",
463
+ "output_type": "stream",
464
+ "text": [
465
+ "Some weights of the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english were not used when initializing DistilBertModel: ['classifier.bias', 'pre_classifier.bias', 'pre_classifier.weight', 'classifier.weight']\n",
466
+ "- This IS expected if you are initializing DistilBertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
467
+ "- This IS NOT expected if you are initializing DistilBertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
468
+ ]
469
+ }
470
+ ],
471
+ "source": [
472
+ "from transformers import AutoModel\n",
473
+ "\n",
474
+ "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n",
475
+ "model = AutoModel.from_pretrained(checkpoint)"
476
+ ]
477
+ },
478
+ {
479
+ "cell_type": "code",
480
+ "execution_count": 20,
481
+ "metadata": {},
482
+ "outputs": [
483
+ {
484
+ "name": "stdout",
485
+ "output_type": "stream",
486
+ "text": [
487
+ "torch.Size([2, 14, 768])\n"
488
+ ]
489
+ }
490
+ ],
491
+ "source": [
492
+ "outputs = model(**inputs)\n",
493
+ "print(outputs.last_hidden_state.shape)"
494
+ ]
495
+ },
496
+ {
497
+ "cell_type": "code",
498
+ "execution_count": 24,
499
+ "metadata": {},
500
+ "outputs": [
501
+ {
502
+ "name": "stdout",
503
+ "output_type": "stream",
504
+ "text": [
505
+ "torch.Size([2, 2])\n",
506
+ "tensor([[-3.0737, 3.1512],\n",
507
+ " [ 4.0217, -3.2439]], grad_fn=<AddmmBackward0>)\n",
508
+ "SequenceClassifierOutput(loss=None, logits=tensor([[-3.0737, 3.1512],\n",
509
+ " [ 4.0217, -3.2439]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None)\n"
510
+ ]
511
+ }
512
+ ],
513
+ "source": [
514
+ "from transformers import AutoModelForSequenceClassification\n",
515
+ "\n",
516
+ "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n",
517
+ "model = AutoModelForSequenceClassification.from_pretrained(checkpoint)\n",
518
+ "outputs = model(**inputs)\n",
519
+ "print(outputs.logits.shape)\n",
520
+ "print(outputs.logits)\n",
521
+ "print(outputs)"
522
+ ]
523
+ },
524
+ {
525
+ "cell_type": "code",
526
+ "execution_count": 25,
527
+ "metadata": {},
528
+ "outputs": [
529
+ {
530
+ "name": "stdout",
531
+ "output_type": "stream",
532
+ "text": [
533
+ "tensor([[1.9756e-03, 9.9802e-01],\n",
534
+ " [9.9930e-01, 6.9871e-04]], grad_fn=<SoftmaxBackward0>)\n"
535
+ ]
536
+ }
537
+ ],
538
+ "source": [
539
+ "import torch\n",
540
+ "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n",
541
+ "print(predictions)"
542
+ ]
543
+ },
544
  {
545
  "cell_type": "code",
546
  "execution_count": 26,
547
  "metadata": {},
548
+ "outputs": [
549
+ {
550
+ "name": "stdout",
551
+ "output_type": "stream",
552
+ "text": [
553
+ "BertConfig {\n",
554
+ " \"attention_probs_dropout_prob\": 0.1,\n",
555
+ " \"classifier_dropout\": null,\n",
556
+ " \"hidden_act\": \"gelu\",\n",
557
+ " \"hidden_dropout_prob\": 0.1,\n",
558
+ " \"hidden_size\": 768,\n",
559
+ " \"initializer_range\": 0.02,\n",
560
+ " \"intermediate_size\": 3072,\n",
561
+ " \"layer_norm_eps\": 1e-12,\n",
562
+ " \"max_position_embeddings\": 512,\n",
563
+ " \"model_type\": \"bert\",\n",
564
+ " \"num_attention_heads\": 12,\n",
565
+ " \"num_hidden_layers\": 12,\n",
566
+ " \"pad_token_id\": 0,\n",
567
+ " \"position_embedding_type\": \"absolute\",\n",
568
+ " \"transformers_version\": \"4.30.2\",\n",
569
+ " \"type_vocab_size\": 2,\n",
570
+ " \"use_cache\": true,\n",
571
+ " \"vocab_size\": 30522\n",
572
+ "}\n",
573
+ "\n"
574
+ ]
575
+ }
576
+ ],
577
+ "source": [
578
+ "from transformers import BertConfig, BertModel\n",
579
+ "\n",
580
+ "config = BertConfig()\n",
581
+ "\n",
582
+ "model = BertModel(config)\n",
583
+ "\n",
584
+ "print(config)"
585
+ ]
586
+ },
587
+ {
588
+ "cell_type": "code",
589
+ "execution_count": 27,
590
+ "metadata": {},
591
  "outputs": [
592
  {
593
  "data": {
594
  "application/vnd.jupyter.widget-view+json": {
595
+ "model_id": "58b47a5171714102b6499f7e76b36326",
596
  "version_major": 2,
597
  "version_minor": 0
598
  },
 
606
  {
607
  "data": {
608
  "application/vnd.jupyter.widget-view+json": {
609
+ "model_id": "f5c4c02f53e842768c1883a31af3af32",
610
  "version_major": 2,
611
  "version_minor": 0
612
  },
613
  "text/plain": [
614
+ "Downloading: 0%| | 0.00/436M [00:00<?, ?B/s]"
615
  ]
616
  },
617
  "metadata": {},
 
621
  "name": "stderr",
622
  "output_type": "stream",
623
  "text": [
624
+ "Some weights of the model checkpoint at bert-base-cased were not used when initializing BertModel: ['cls.seq_relationship.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.dense.weight', 'cls.seq_relationship.bias', 'cls.predictions.bias']\n",
625
+ "- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
626
+ "- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
627
  ]
628
+ }
629
+ ],
630
+ "source": [
631
+ "from transformers import BertModel\n",
632
+ "\n",
633
+ "model = BertModel.from_pretrained(\"bert-base-cased\")"
634
+ ]
635
+ },
636
+ {
637
+ "cell_type": "code",
638
+ "execution_count": 28,
639
+ "metadata": {},
640
+ "outputs": [
641
  {
642
  "data": {
643
  "application/vnd.jupyter.widget-view+json": {
644
+ "model_id": "35841e13cd65421d900df61bd8447af8",
645
  "version_major": 2,
646
  "version_minor": 0
647
  },
648
  "text/plain": [
649
+ "Downloading: 0%| | 0.00/29.0 [00:00<?, ?B/s]"
650
  ]
651
  },
652
  "metadata": {},
 
655
  {
656
  "data": {
657
  "application/vnd.jupyter.widget-view+json": {
658
+ "model_id": "e47773bb1757436bb7f73ae3e783cb06",
659
  "version_major": 2,
660
  "version_minor": 0
661
  },
662
  "text/plain": [
663
+ "Downloading: 0%| | 0.00/213k [00:00<?, ?B/s]"
664
  ]
665
  },
666
  "metadata": {},
 
669
  {
670
  "data": {
671
  "application/vnd.jupyter.widget-view+json": {
672
+ "model_id": "2535a3e607484dd2ba06ffecfa51098f",
673
  "version_major": 2,
674
  "version_minor": 0
675
  },
676
  "text/plain": [
677
+ "Downloading: 0%| | 0.00/436k [00:00<?, ?B/s]"
678
  ]
679
  },
680
  "metadata": {},
 
684
  "name": "stdout",
685
  "output_type": "stream",
686
  "text": [
687
+ "['Using', 'a', 'Trans', '##former', 'network', 'is', 'simple']\n"
 
688
  ]
689
  }
690
  ],
691
  "source": [
692
+ "from transformers import AutoTokenizer\n",
 
 
693
  "\n",
694
+ "tokenizer = AutoTokenizer.from_pretrained(\"bert-base-cased\")\n",
695
+ "sequence = \"Using a Transformer network is simple\"\n",
696
+ "tokens = tokenizer.tokenize(sequence)\n",
697
+ "print(tokens)"
698
  ]
699
+ },
700
+ {
701
+ "cell_type": "code",
702
+ "execution_count": 30,
703
+ "metadata": {},
704
+ "outputs": [
705
+ {
706
+ "name": "stdout",
707
+ "output_type": "stream",
708
+ "text": [
709
+ "[7993, 170, 13809, 23763, 2443, 1110, 3014]\n"
710
+ ]
711
+ }
712
+ ],
713
+ "source": [
714
+ "ids = tokenizer.convert_tokens_to_ids(tokens)\n",
715
+ "print(ids)"
716
+ ]
717
+ },
718
+ {
719
+ "cell_type": "code",
720
+ "execution_count": 32,
721
+ "metadata": {},
722
+ "outputs": [
723
+ {
724
+ "name": "stdout",
725
+ "output_type": "stream",
726
+ "text": [
727
+ "Using a Transformer network is simple\n"
728
+ ]
729
+ }
730
+ ],
731
+ "source": [
732
+ "decoded_string = tokenizer.decode([7993, 170, 13809, 23763, 2443, 1110, 3014])\n",
733
+ "print(decoded_string)"
734
+ ]
735
+ },
736
+ {
737
+ "cell_type": "code",
738
+ "execution_count": 33,
739
+ "metadata": {},
740
+ "outputs": [
741
+ {
742
+ "name": "stdout",
743
+ "output_type": "stream",
744
+ "text": [
745
+ "tensor([[1045, 1005, 2310, 2042, 3403, 2005, 2023, 2607, 2026, 2878, 2166, 1012]])\n",
746
+ "tensor([[-2.7220, 2.7847]], grad_fn=<AddmmBackward0>)\n"
747
+ ]
748
+ }
749
+ ],
750
+ "source": [
751
+ "import torch\n",
752
+ "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
753
+ "\n",
754
+ "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n",
755
+ "tokenizer = AutoTokenizer.from_pretrained(checkpoint)\n",
756
+ "model = AutoModelForSequenceClassification.from_pretrained(checkpoint)\n",
757
+ "\n",
758
+ "sequence = \"I've been waiting for this course my whole life.\"\n",
759
+ "\n",
760
+ "tokens = tokenizer.tokenize(sequence)\n",
761
+ "ids = tokenizer.convert_tokens_to_ids(tokens)\n",
762
+ "\n",
763
+ "input_ids = torch.tensor([ids])\n",
764
+ "print(input_ids)\n",
765
+ "\n",
766
+ "output = model(input_ids)\n",
767
+ "print(output.logits)"
768
+ ]
769
+ },
770
+ {
771
+ "cell_type": "code",
772
+ "execution_count": 34,
773
+ "metadata": {},
774
+ "outputs": [
775
+ {
776
+ "name": "stdout",
777
+ "output_type": "stream",
778
+ "text": [
779
+ "tensor([[ 1.5694, -1.3895]], grad_fn=<AddmmBackward0>)\n",
780
+ "tensor([[ 0.5803, -0.4125]], grad_fn=<AddmmBackward0>)\n",
781
+ "tensor([[ 1.5694, -1.3895],\n",
782
+ " [ 1.3373, -1.2163]], grad_fn=<AddmmBackward0>)\n"
783
+ ]
784
+ }
785
+ ],
786
+ "source": [
787
+ "model = AutoModelForSequenceClassification.from_pretrained(checkpoint)\n",
788
+ "\n",
789
+ "sequence1_ids = [[200, 200, 200]]\n",
790
+ "sequence2_ids = [[200, 200]]\n",
791
+ "batched_ids = [\n",
792
+ " [200, 200, 200],\n",
793
+ " [200, 200, tokenizer.pad_token_id]\n",
794
+ "]\n",
795
+ "\n",
796
+ "print(model(torch.tensor(sequence1_ids)).logits)\n",
797
+ "print(model(torch.tensor(sequence2_ids)).logits)\n",
798
+ "print(model(torch.tensor(batched_ids)).logits)"
799
+ ]
800
+ },
801
+ {
802
+ "cell_type": "code",
803
+ "execution_count": 35,
804
+ "metadata": {},
805
+ "outputs": [
806
+ {
807
+ "name": "stdout",
808
+ "output_type": "stream",
809
+ "text": [
810
+ "tensor([[ 1.5694, -1.3895],\n",
811
+ " [ 0.5803, -0.4125]], grad_fn=<AddmmBackward0>)\n"
812
+ ]
813
+ }
814
+ ],
815
+ "source": [
816
+ "attention_mask = [\n",
817
+ " [1, 1, 1], [1, 1, 0]\n",
818
+ "]\n",
819
+ "\n",
820
+ "outputs = model(torch.tensor(batched_ids), attention_mask=torch.tensor(attention_mask))\n",
821
+ "print(outputs.logits)"
822
+ ]
823
+ },
824
+ {
825
+ "cell_type": "code",
826
+ "execution_count": 37,
827
+ "metadata": {},
828
+ "outputs": [
829
+ {
830
+ "name": "stdout",
831
+ "output_type": "stream",
832
+ "text": [
833
+ "SequenceClassifierOutput(loss=None, logits=tensor([[-2.7211, 2.7688],\n",
834
+ " [-3.0041, 3.2001]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None)\n"
835
+ ]
836
+ }
837
+ ],
838
+ "source": [
839
+ "import torch\n",
840
+ "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
841
+ "\n",
842
+ "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n",
843
+ "tokenizer = AutoTokenizer.from_pretrained(checkpoint)\n",
844
+ "model = AutoModelForSequenceClassification.from_pretrained(checkpoint)\n",
845
+ "sequences = [\"I've been waiting for this!\", \"So have I.\"]\n",
846
+ "\n",
847
+ "tokens = tokenizer(sequences, padding=True, truncation=True, return_tensors=\"pt\")\n",
848
+ "output = model(**tokens)\n",
849
+ "print(output)"
850
+ ]
851
+ },
852
+ {
853
+ "cell_type": "code",
854
+ "execution_count": null,
855
+ "metadata": {},
856
+ "outputs": [],
857
+ "source": []
858
  }
859
  ],
860
  "metadata": {