-
Notifications
You must be signed in to change notification settings - Fork 0
/
ganesh_kota_Customer_Churn_Prediction_Phase2.html
8613 lines (8224 loc) · 568 KB
/
ganesh_kota_Customer_Churn_Prediction_Phase2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>57dedfc391534c5dbfab25d7b8d7231a</title>
<style>
html {
line-height: 1.5;
font-family: Georgia, serif;
font-size: 20px;
color: #1a1a1a;
background-color: #fdfdfd;
}
body {
margin: 0 auto;
max-width: 36em;
padding-left: 50px;
padding-right: 50px;
padding-top: 50px;
padding-bottom: 50px;
hyphens: auto;
overflow-wrap: break-word;
text-rendering: optimizeLegibility;
font-kerning: normal;
}
@media (max-width: 600px) {
body {
font-size: 0.9em;
padding: 1em;
}
h1 {
font-size: 1.8em;
}
}
@media print {
body {
background-color: transparent;
color: black;
font-size: 12pt;
}
p, h2, h3 {
orphans: 3;
widows: 3;
}
h2, h3, h4 {
page-break-after: avoid;
}
}
p {
margin: 1em 0;
}
a {
color: #1a1a1a;
}
a:visited {
color: #1a1a1a;
}
img {
max-width: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 1.4em;
}
h5, h6 {
font-size: 1em;
font-style: italic;
}
h6 {
font-weight: normal;
}
ol, ul {
padding-left: 1.7em;
margin-top: 1em;
}
li > ol, li > ul {
margin-top: 0;
}
blockquote {
margin: 1em 0 1em 1.7em;
padding-left: 1em;
border-left: 2px solid #e6e6e6;
color: #606060;
}
code {
font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
font-size: 85%;
margin: 0;
}
pre {
margin: 1em 0;
overflow: auto;
}
pre code {
padding: 0;
overflow: visible;
overflow-wrap: normal;
}
.sourceCode {
background-color: transparent;
overflow: visible;
}
hr {
background-color: #1a1a1a;
border: none;
height: 1px;
margin: 1em 0;
}
table {
margin: 1em 0;
border-collapse: collapse;
width: 100%;
overflow-x: auto;
display: block;
font-variant-numeric: lining-nums tabular-nums;
}
table caption {
margin-bottom: 0.75em;
}
tbody {
margin-top: 0.5em;
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
}
th {
border-top: 1px solid #1a1a1a;
padding: 0.25em 0.5em 0.25em 0.5em;
}
td {
padding: 0.125em 0.5em 0.25em 0.5em;
}
header {
margin-bottom: 4em;
text-align: center;
}
#TOC li {
list-style: none;
}
#TOC ul {
padding-left: 1.3em;
}
#TOC > ul {
padding-left: 0;
}
#TOC a:not(:hover) {
text-decoration: none;
}
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<section
id="customer-churn-prediction-a-comparative-analysis-of-models-with-and-without-sentiment-analysis-phase-2"
class="cell markdown" id="lI-TXOPmWBDS">
<h1><strong>Customer Churn Prediction: A Comparative Analysis of Models
with and without Sentiment Analysis (PHASE 2).</strong></h1>
<blockquote>
<p><strong>Overview of the Study:</strong></p>
</blockquote>
<ol>
<li><p>The core purpose of this study is to find the impact of Sentiment
Analysis in predicting customer churn for the e-commerce industry by
employing different predictive models.</p></li>
<li><p>Furthermore, the study is also focused on observing which model
is best in a more accurate prediction for determining the churn rate of
customers.</p></li>
</ol>
<blockquote>
<p><strong>Process involved in the Study:</strong></p>
</blockquote>
<ol>
<li><p>The whole project is divided into two phases. In the first phase,
all the relevant variables that are expected to be causing the customer
churn are selected and then the predictive models are developed. In
this, there will be no feedback from the customer is utilized.</p></li>
<li><p>In the second phase, in addition to all the relevant variables
obtained from EDA, the feedback provided by the customers is also
included in this phase to extract the sentiment scores which are now
added to the data frame. Again, the churn predictive models are
developed with this data.</p></li>
<li><p>Finally, the metrics from both these phases will be reviewed and
interpreted to understand if the inclusion of the sentiment analysis
will be helpful for the organization in better understanding why their
customers are parting away without making any future transactions with
the organization.</p></li>
</ol>
</section>
<section
id="research-question-how-does-sentiment-analysis-impact-in-predicting-the-customer-churn-of-an-organization"
class="cell markdown" id="F24qzdzteBm-">
<h2><strong>Research Question:</strong> "How does sentiment analysis
impact in predicting the customer churn of an organization?"</h2>
</section>
<section id="introduction" class="cell markdown" id="UoHa8cvPcbRl">
<h1><strong>Introduction:</strong></h1>
<ol>
<li><p>In the previous deliverables, both the study phases are
implemented. Nonetheless, before proceeding to the comparison of model
metrics from each phase and among both phases (will be done in next step
of study), a few other additional steps have been implemented in this
file of study.</p></li>
<li><p>In addition to the four models implemented, a new model called
<strong>Naive Bayes</strong> is also implemented in this study in this
file.</p></li>
<li><p>Furthermore, the following additional enhancements are also
performed:</p>
<ul>
<li><p>Cross-Validation is changed to <strong>10 folds</strong> instead
of the previous <strong>5-fold</strong> cross-validation.</p></li>
<li><p><strong>Time required</strong> to train the models and also
<strong>memory consumed</strong> by the models are also
addressed.</p></li>
<li><p>Additional hyperparameters related to kernel are added to the
<strong>SVM</strong> tuning process.</p></li>
<li><p><strong>Sensitivity, Specificity, ROC_AUC</strong> scores are
also calculated for models.</p></li>
<li><p>Plotted the <strong>AUC-ROC Curve</strong> for all the
models.</p></li>
<li><p>Finally, <strong>pickle files</strong> have been created for all
the models.</p></li>
</ul></li>
<li><p>In the next step (later in process), the interpretation and
comparison of results will be done.</p></li>
</ol>
</section>
<section
id="this-file-works-on-the-second-phase-of-the-prediction-study-where-the-churn-prediction-is-done-with-the-inclusion-of-the-customer-feedback-the-below-steps-will-dive-into-the-whole-scenario"
class="cell markdown" id="yEe2RtuExDy2">
<h1><strong>This file works on the "second phase" of the prediction
study where the Churn Prediction is done with the inclusion of the
"customer feedback". The below steps will dive into the whole
scenario.</strong></h1>
</section>
<div class="cell code" id="bIV4USnKlcDT">
<div class="sourceCode" id="cb1"><pre
class="sourceCode python"><code class="sourceCode python"></code></pre></div>
</div>
<section id="beginning-of-the-sentiment-analysis" class="cell markdown"
id="2ZTrHng42gyo">
<h1><strong>BEGINNING OF THE SENTIMENT ANALYSIS.</strong></h1>
</section>
<section
id="logging-in-to-the-hugging-face-account-to-connect-for-my-api"
class="cell markdown" id="GpIcFMapMPuY">
<h1><strong>Logging in to the Hugging Face account to connect for my
API</strong></h1>
<ul>
<li><strong>Need to provide the API Key of HuggingFace
Account.</strong></li>
</ul>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":145,"referenced_widgets":["4f56ce71a80d4fb59c599d2f30e5b730","94dda3f9ffa74718a6e9af91f3c3aa13","6e46956d53f14d09b84ce21444e690cf","e5f1f794539840419cd09ac368da71ad","fa522f5b3253413f80bf5c0ba8268ed4","96c7893bd6a548bc8a72073446c2e104","81f1e6a7503a4e0382d412c2b63c7b1f","9222c776350e4575bea0c7911a3cf452","d8bb0fb7e98040dead2b70322b4cdf0d","6af18a2c231f488d89b1fb69ff29e853","4be6e73fc6584f2490cb1476e5466672","66b1be10073743e6ae78fb974abd326c","12924014266d4eceb5eb7b5404f0a2f4","138189ddf1b3496bab0768bf7b1c2ac4","1700c9725fe3402c888958a7fdb42be8","e5701c1e24714fdd805d0caf1effeeea","cd93fe7356d141a3a1e2ba5cc5fe181f","bbf9a2d6cfde47da80513dba136c2cae","48c979fb1e95404cb170cb7d76dece79","4b053f2410ed4760b5958d4745b1dd38","f20e00dd871c49b6842a32741867b046","3360f104c9794a999136d8816d6106a8","365ff354431f47d5aaf01f1e8b56d766","50ee11fe952b442283f2752fe0abb523","b1a3b806cf0540089a058295fdab33af","d0f9bb9b9d364eeda8f301bdf9500a4a","8dc544bde1f04c5eb542c4ec8563b483","102e7969f1d44666b0d2a9f74ff20c8a","e3b4049091584eb59383cd833841cab1","0fcc2ea39dfc49ba9596ed1989b2c2a8","cebe9295f97f4329a19b5a904022d098","3773b68024d9408695eeda265303f71d"]}"
id="DJLyyTvnZZtN" data-outputId="62d183e2-b5d4-4acd-9ca1-d328a051fc77">
<div class="sourceCode" id="cb2"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> huggingface_hub <span class="im">import</span> notebook_login</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>notebook_login()</span></code></pre></div>
<div class="output display_data">
<div class="sourceCode" id="cb3"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"4f56ce71a80d4fb59c599d2f30e5b730"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
</div>
<section id="installing-required-packages" class="cell markdown"
id="arSCmFyDNIDY">
<h1><strong>Installing required packages</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/"}"
id="vo9nlKdScYL6" data-outputId="828aec9e-bac1-48a1-ccb7-fd113b8fc045">
<div class="sourceCode" id="cb4"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="op">!</span>pip install transformers datasets evaluate accelerate</span></code></pre></div>
<div class="output stream stdout">
<pre><code>Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.38.2)
Collecting datasets
Downloading datasets-2.18.0-py3-none-any.whl (510 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 510.5/510.5 kB 6.8 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 84.1/84.1 kB 9.4 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 297.3/297.3 kB 11.5 MB/s eta 0:00:00
ent already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.13.3)
Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.20.3)
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (1.25.2)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (24.0)
Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (6.0.1)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (2023.12.25)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers) (2.31.0)
Requirement already satisfied: tokenizers<0.19,>=0.14 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.15.2)
Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.4.2)
Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers) (4.66.2)
Requirement already satisfied: pyarrow>=12.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (14.0.2)
Requirement already satisfied: pyarrow-hotfix in /usr/local/lib/python3.10/dist-packages (from datasets) (0.6)
Collecting dill<0.3.9,>=0.3.0 (from datasets)
Downloading dill-0.3.8-py3-none-any.whl (116 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 116.3/116.3 kB 14.1 MB/s eta 0:00:00
ent already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets) (2.0.3)
Collecting xxhash (from datasets)
Downloading xxhash-3.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.1/194.1 kB 19.8 MB/s eta 0:00:00
ultiprocess (from datasets)
Downloading multiprocess-0.70.16-py310-none-any.whl (134 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 134.8/134.8 kB 15.4 MB/s eta 0:00:00
ent already satisfied: fsspec[http]<=2024.2.0,>=2023.1.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (2023.6.0)
Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets) (3.9.3)
Collecting responses<0.19 (from evaluate)
Downloading responses-0.18.0-py3-none-any.whl (38 kB)
Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate) (5.9.5)
Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (2.2.1+cu121)
Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.3.1)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (23.2.0)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.4.1)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (6.0.5)
Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.9.4)
Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (4.0.3)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.19.3->transformers) (4.10.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2024.2.2)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (1.12)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.2.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.1.3)
Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch>=1.10.0->accelerate)
Downloading nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 23.7/23.7 MB 29.1 MB/s eta 0:00:00
e-cu12==12.1.105 (from torch>=1.10.0->accelerate)
Downloading nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 823.6/823.6 kB 34.8 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.1/14.1 MB 42.9 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 731.7/731.7 MB 1.3 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 410.6/410.6 MB 3.7 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.6/121.6 MB 8.7 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.5/56.5 MB 14.6 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124.2/124.2 MB 6.5 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 196.0/196.0 MB 6.8 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_nccl_cu12-2.19.3-py3-none-manylinux1_x86_64.whl (166.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 166.0/166.0 MB 7.9 MB/s eta 0:00:00
torch>=1.10.0->accelerate)
Downloading nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 99.1/99.1 kB 14.9 MB/s eta 0:00:00
ent already satisfied: triton==2.2.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.2.0)
Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.10.0->accelerate)
Downloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.1/21.1 MB 72.0 MB/s eta 0:00:00
ent already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2023.4)
Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.1)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.16.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)
Installing collected packages: xxhash, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, dill, responses, nvidia-cusparse-cu12, nvidia-cudnn-cu12, multiprocess, nvidia-cusolver-cu12, datasets, evaluate, accelerate
Successfully installed accelerate-0.29.1 datasets-2.18.0 dill-0.3.8 evaluate-0.4.1 multiprocess-0.70.16 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.19.3 nvidia-nvjitlink-cu12-12.4.127 nvidia-nvtx-cu12-12.1.105 responses-0.18.0 xxhash-3.4.1
</code></pre>
</div>
</div>
<div class="cell code" id="bWBmnW1kIby7">
<div class="sourceCode" id="cb6"><pre
class="sourceCode python"><code class="sourceCode python"></code></pre></div>
</div>
<div class="cell markdown" id="n_4vaaqCH7Y8">
<p><strong>In order to build the sentiment analysis model, initially
this was trained on the large corpus of textual data. This helps in
training the model on robust textual inputs so that the trained model
can perform well on any of the future sentiment extraction
tasks.</strong></p>
<ul>
<li>To pre-train the model, the data from IMDB has been used. The IMDB
dataset contains large amound of data on the movie reviews. Also, the
reviews are lengthy enough. Subsequently, the model will now be
pre-trained with IMDB dataset.</li>
</ul>
</div>
<section id="importing-the-imdb-dataset-from-the-datasets-package"
class="cell markdown" id="4HMMcobXNReV">
<h1><strong>Importing the IMDB dataset from the datasets
package</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":365,"referenced_widgets":["53ebe2e0f6914f5b8f948eb4caa74150","d4abdbd16a5f43a2bc22176e17723fbc","b4ce1c31c6fa42ef9710401fbf491d5a","462f789451ad48f68669e1db62e71277","806543975c204b27a49e2d45eaee9f94","d0bd9fec4d1848aabfdf1e12faebecb4","0474b6363b5e465ca9e42a9830db01f6","f42bf794457f460fb4ac8b29b3b34e98","b5ad04f046544238982a7345a97a092a","c4950f7c91694338b203b5fc65fc94f7","4956f16eb17a4309b5dfdddf02d2dcde","e1836f150ea04a9ba0f38d292c22cb61","2538b487919449af9f8679d9dd0bd309","ddcb16dfc76344e1b77ce2c3fb6b182e","819f58bbe75d473199103d185c589bf0","d1e602edd9ef4854a017ca89c9fa027c","dd705030a62449a189b44eda1927d46f","f019841ec14441dab8e1e26ee8648a99","e65c8e2a47c3454cbfcb4e1b0bb65714","dcf33694c1064fd9b623dcc16e52ebb9","2d7ff45b2e0145b999101fdf196b221f","a2753a39a9854307bb76c7b44e90e787","535a09ee4a924202a202a51055ce1469","dc2cc065091d4783a59d9f5efaace794","85ca4436d58f4954a29659cec07e4820","2331366fff9d4a17ac02dcd76a445335","be2d8024b0804d87af5008b463c885d3","ffaceebfb35144e58d32a4bd10690f39","7165648d10fc4c08ac0cf619698e0d67","1bd189b2ba8c4669a5d470ccbc04178f","acb0bafa0ba04bf5af40b7e2a2da0047","03d4d8e60e7c4b70b1fb0394ca28bd28","9ed4c3ac897243dbbde70fe6e303ac37","9062851d1cbe403096076dff1f32cdfa","efb4ff6ecbf244628326daaa5ef93344","a773f2079b8140989535b4cf129bd7af","7db2c706f52c45ab9a6d7441e5d8a619","4bd9ee93b1184861a9740188e27ffcf8","97089f1a710d40d096eb7a7fdc18b273","94017d0946544a56a06ac640ba5819d5","25520f15f6db44ecb7f040b4b8982c79","0602382ab64c4e7d805c8e57e1d2c954","6fae8475aea34381a36a3847f565f9d5","e1c1c196100748a2906033a3b47410a0","d9eaa548cb754c11ad5f4476cc77fdca","6df08e00402244328e4027f391393d8b","12344c7c4dbb4a7d9c721681d5167da1","56efe7e201d94dc498c6b6a1967e195b","08f7df0215854b8289897382dce8e61b","76613b547ee7440eaaf6a395028c49a3","2aefaf9544424d33a4aa00b842f101ad","366aaa57507f454c95303bbf3f5c1bd9","2cde5214ca204455967b0138dd2874cb","371f695e2f5c41bdb7dee29e23218e77","562468d2c3e34aaea5e222186ca7ec93","87c3f0cc448843ee993abf28b742fb36","e94c15847c6a4c7fbd372bc7b1297b9b","2b423ede469344afbf8e8ab289905420","c0189fcf2a1f4e0f8b40e4dd94c2d904","9ca1db906ddd49249add673fa3a86842","b7e6496be44545a9a1fd1ad73cbff440","3c2beabf315a4885a61d82449be68a58","c62ef662b34541a6bfabc872bc9374ce","39c84df545a047239ce4976dba86e85a","666d0a84368d4b5ca43e6f35d468d34f","8002d1209823476cb878365b309a5e05","4ef1ce5eb12441c0b3ee46fc8132f756","3b63f6d105e749f8b96ea07a6fdebe24","9ac6287455254ad0aa2a58823de43dfe","8e203f272e674044afe0137687ff2616","749b5eec7ce448a78a35b336cca83326","740fb401623a409187ff470752533ee1","78b132707d304f1eb1d2eadb0743d4b7","c15421964a58402db0632b2488b7f0ad","8548e8dc13dd4899b7e877bf577a05c1","1f504547512e4190bcbc11c41b63592e","b3369b4e41874c95b4d827d17694b8a9"]}"
id="geM7P7DScVtI" data-outputId="e89eef97-3808-42f0-abc1-467656055355">
<div class="sourceCode" id="cb7"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> datasets <span class="im">import</span> load_dataset</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>imdb <span class="op">=</span> load_dataset(<span class="st">"imdb"</span>)</span></code></pre></div>
<div class="output stream stderr">
<pre><code>/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning:
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
warnings.warn(
</code></pre>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb9"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"53ebe2e0f6914f5b8f948eb4caa74150"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb10"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"e1836f150ea04a9ba0f38d292c22cb61"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb11"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"535a09ee4a924202a202a51055ce1469"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb12"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"9062851d1cbe403096076dff1f32cdfa"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb13"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"d9eaa548cb754c11ad5f4476cc77fdca"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb14"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"87c3f0cc448843ee993abf28b742fb36"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb15"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"4ef1ce5eb12441c0b3ee46fc8132f756"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
</div>
<div class="cell code" id="TKRlI-XJcdY-">
<div class="sourceCode" id="cb16"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># from datasets import load_dataset</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> torch.utils.data <span class="im">import</span> DataLoader</span></code></pre></div>
</div>
<section id="process-of-tokenization" class="cell markdown"
id="S-lEwLS5OtE8">
<h1><strong>Process of Tokenization</strong></h1>
</section>
<section
id="importing-the-autotokenizer-from-transformers-to-perform-tokenization"
class="cell markdown" id="wCemJHGbNZlD">
<h1><strong>Importing the AutoTokenizer from Transformers to Perform
Tokenization</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":145,"referenced_widgets":["aee795bcd87b41a8a98a543aab4c549e","75022eb9a8d74f638c4a2deb5dd09de4","ba49ef0aedf541c4b02c3f64d59c480c","65dc131f5f6949b9b318178353013fa6","178331ceeac94e5f97e7ec6bf5b62582","4c40cc2cd5a647f5b08e682fcf97a891","789a070a3c4949d19b5a8bdf636b10ec","6edece18e7454968854474cdd092e210","650e86739e2148f393f50eb0c2dd84ab","f7fb7f05059e47578c8038d93e3e03f7","254fae362e63499e8f4c99c6f708116b","7217129048c745a999bd554ce7602c6c","180c10211b2c49e7b4e36aeb7919799d","e65e3ad0e4594d84874d1fe796916a53","223d386be1984c0da210289bbfe6dca1","7f2f3f93888849768f1076706527407f","ea3a82adbf5c445cabee59522b0bb1a3","e1c37ffbaa41403db425371a8766048a","4d5b0b2fae684946a505b761a60c9635","f4c4b00008304f0797e70a8349421e68","479fff8828be4340aa86974bc996ffcc","3fec3487acf3479a8c095f8571dadb02","876b66bd8a0a412f92dd92530307c551","dff3a4fc9768431d8556593935851cf7","7f33cd02eba744afaf7d845068a4552e","83169973762f43079c9f0d6f83813b5c","6579d534837a41319ecbdea953b194a3","b67f259246064061879dc56c4f464705","51106708d4b448539a5a423c2469f824","81004573772b486bb00b89132cbe226f","9f70aa25242e4f1fbcdcb4f7c7b3978b","ecfccf86258045da8242ae9fa152048f","9e19b71e4c9f4c818cf3cf85b72402bf","32b9ff4f943a45118f3abdf26db82d0f","d7e4fb7131e54cccb819cd516597fdab","c48464d82d71465ca28057a0c2795568","0a663de4e3ab4bd5ba40a18e5c60a677","af1da15603f5484caaede8b13217b22e","521239ec3b2a42d6accd9b1ee76b1282","20003fd692e6456b84b2de51cce8ee41","ba1d3ca23b614a5e99fb809eeefe702a","fdd9342e53e8442db1c3d594a307b114","6f3b63d55fa341398f1ab00bc648e369","1c7ff51ed319430984a34108187a5e23"]}"
id="3P0VO9-Idl-e" data-outputId="f97b9ba1-9671-4d3a-9aa1-ef6ff84d22f0">
<div class="sourceCode" id="cb17"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co">## Initializing the Tokenizer</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> transformers <span class="im">import</span> AutoTokenizer</span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a>tokenizer <span class="op">=</span> AutoTokenizer.from_pretrained(<span class="st">"distilbert-base-uncased"</span>)</span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a>max_length <span class="op">=</span> <span class="dv">512</span> <span class="co"># Set max length</span></span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> preprocess_function(examples):</span>
<span id="cb17-9"><a href="#cb17-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> tokenizer(examples[<span class="st">"text"</span>], truncation<span class="op">=</span><span class="va">True</span>, max_length<span class="op">=</span>max_length)</span></code></pre></div>
<div class="output display_data">
<div class="sourceCode" id="cb18"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"aee795bcd87b41a8a98a543aab4c549e"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb19"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"7217129048c745a999bd554ce7602c6c"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb20"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"876b66bd8a0a412f92dd92530307c551"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb21"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"32b9ff4f943a45118f3abdf26db82d0f"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
</div>
<section id="tokenizing-the-data" class="cell markdown"
id="vvn50kkgUitS">
<h1><strong>Tokenizing the Data</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":49,"referenced_widgets":["f9611561996745c2b4f2057f17d7bca6","1221dc3338b04116a5c00c4c0ea67559","c74c1e9546b74d708fb121ce7ab596fe","41e0145f5a3145dfa2548eb56939985d","2dc794393a7f4f5b8c387aa760068c6b","208b866b004e4d5c927dff8a600fd719","5e7f94599d1647dfaf1429c52921d711","635be5960da64f8c8f3b95b07b0a71cf","7a7384df262f40b9aa15d2e02d8ece90","919420ae933c4ee38b19101f98fc0c2f","7da32491e1a148dba578c7d92293c9db"]}"
id="lZJ_Ph5ilTm5" data-outputId="374b0559-8f6a-40c6-b480-5ba8243ecff3">
<div class="sourceCode" id="cb22"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> transformers <span class="im">import</span> DataCollatorWithPadding</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Tokenize datasets</span></span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a>tokenized_ds <span class="op">=</span> imdb.<span class="bu">map</span>(preprocess_function, batched<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Collate function to handle padding</span></span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a>data_collator <span class="op">=</span> DataCollatorWithPadding(tokenizer, return_tensors<span class="op">=</span><span class="st">'pt'</span>)</span></code></pre></div>
<div class="output display_data">
<div class="sourceCode" id="cb23"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"f9611561996745c2b4f2057f17d7bca6"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
</div>
<section id="creating-the-data-loaders" class="cell markdown"
id="fxklFMU8VPbz">
<h1><strong>Creating the Data Loaders</strong></h1>
</section>
<div class="cell code" id="VRxAzdPeeIV5">
<div class="sourceCode" id="cb24"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Create data loaders</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>train_dataloader <span class="op">=</span> DataLoader(tokenized_ds[<span class="st">'train'</span>], collate_fn<span class="op">=</span>data_collator)</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>val_dataloader <span class="op">=</span> DataLoader(tokenized_ds[<span class="st">'test'</span>], collate_fn<span class="op">=</span>data_collator)</span></code></pre></div>
</div>
<div class="cell code" id="dsmcuqkqgUhs">
<div class="sourceCode" id="cb25"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ## Evaluating the accuracy</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="co"># import evaluate</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="co"># accuracy = evaluate.load("accuracy")</span></span></code></pre></div>
</div>
<div class="cell code" id="0SGHsMMdgW8P">
<div class="sourceCode" id="cb26"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ## Importing the Evaluation Metrics</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="co"># import numpy as np</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a><span class="co"># def compute_metrics(eval_pred):</span></span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a><span class="co"># predictions, labels = eval_pred</span></span>
<span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></a><span class="co"># predictions = np.argmax(predictions, axis=1)</span></span>
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a><span class="co"># return accuracy.compute(predictions=predictions, references=labels)</span></span></code></pre></div>
</div>
<section
id="providing-the-numerical-values-to-the-sentiment-labels-initially"
class="cell markdown" id="3Hh67dGsJUhi">
<h1><strong>Providing the Numerical Values to the Sentiment Labels
initially.</strong></h1>
</section>
<div class="cell code" id="7t5e__s5nJ47">
<div class="sourceCode" id="cb27"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co">## Providing labels to the Data</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>id2label <span class="op">=</span> {<span class="dv">0</span>: <span class="st">"NEGATIVE"</span>, <span class="dv">1</span>: <span class="st">"POSITIVE"</span>}</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>label2id <span class="op">=</span> {<span class="st">"NEGATIVE"</span>: <span class="dv">0</span>, <span class="st">"POSITIVE"</span>: <span class="dv">1</span>}</span></code></pre></div>
</div>
<section id="loading-the-distilbert-base-uncased-model"
class="cell markdown" id="EvWmKT2MVsyW">
<h1><strong>Loading the DistilBERT-Base-Uncased Model</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/"}"
id="YUguf4aQnKW_" data-outputId="9bf8ef65-135b-4c08-aa13-4145402439a0">
<div class="sourceCode" id="cb28"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a> <span class="im">from</span> transformers <span class="im">import</span> AutoModelForSequenceClassification, TrainingArguments, Trainer</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> model <span class="op">=</span> AutoModelForSequenceClassification.from_pretrained(</span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"distilbert/distilbert-base-uncased"</span>, num_labels<span class="op">=</span><span class="dv">2</span>, id2label<span class="op">=</span>id2label, label2id<span class="op">=</span>label2id</span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> )</span></code></pre></div>
<div class="output stream stderr">
<pre><code>Some weights of DistilBertForSequenceClassification were not initialized from the model checkpoint at distilbert/distilbert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight', 'pre_classifier.bias', 'pre_classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
</code></pre>
</div>
</div>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/"}"
id="zHmlhg8uCP8K" data-outputId="0e9b4ea2-666f-418f-d27b-ad6d39fb0ea5">
<div class="sourceCode" id="cb30"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="op">!</span>pip install accelerate <span class="op">-</span>U</span></code></pre></div>
<div class="output stream stdout">
<pre><code>Requirement already satisfied: accelerate in /usr/local/lib/python3.10/dist-packages (0.28.0)
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from accelerate) (1.25.2)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (24.0)
Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate) (5.9.5)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from accelerate) (6.0.1)
Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (2.2.1+cu121)
Requirement already satisfied: huggingface-hub in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.20.3)
Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.4.2)
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.13.3)
Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (4.10.0)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (1.12)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.2.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.1.3)
Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2023.6.0)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (8.9.2.26)
Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.3.1)
Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (11.0.2.54)
Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (10.3.2.106)
Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (11.4.5.107)
Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.0.106)
Requirement already satisfied: nvidia-nccl-cu12==2.19.3 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.19.3)
Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
Requirement already satisfied: triton==2.2.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.2.0)
Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.10.0->accelerate) (12.4.99)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub->accelerate) (2.31.0)
Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub->accelerate) (4.66.2)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.5)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (2024.2.2)
Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)
</code></pre>
</div>
</div>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/"}"
id="uEdFDfnBCTtH" data-outputId="c7d364dd-857f-41bb-e128-92274fb3e12d">
<div class="sourceCode" id="cb32"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="op">!</span>pip install transformers[torch]</span></code></pre></div>
<div class="output stream stdout">
<pre><code>Requirement already satisfied: transformers[torch] in /usr/local/lib/python3.10/dist-packages (4.38.2)
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (3.13.3)
Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (0.20.3)
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (1.25.2)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (24.0)
Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (6.0.1)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (2023.12.25)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (2.31.0)
Requirement already satisfied: tokenizers<0.19,>=0.14 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (0.15.2)
Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (0.4.2)
Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (4.66.2)
Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (2.2.1+cu121)
Requirement already satisfied: accelerate>=0.21.0 in /usr/local/lib/python3.10/dist-packages (from transformers[torch]) (0.28.0)
Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate>=0.21.0->transformers[torch]) (5.9.5)
Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.19.3->transformers[torch]) (2023.6.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.19.3->transformers[torch]) (4.10.0)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (1.12)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (3.2.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (3.1.3)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (12.1.105)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (12.1.105)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (12.1.105)
Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (8.9.2.26)
Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (12.1.3.1)
Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (11.0.2.54)
Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (10.3.2.106)
Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (11.4.5.107)
Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (12.1.0.106)
Requirement already satisfied: nvidia-nccl-cu12==2.19.3 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (2.19.3)
Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (12.1.105)
Requirement already satisfied: triton==2.2.0 in /usr/local/lib/python3.10/dist-packages (from torch->transformers[torch]) (2.2.0)
Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch->transformers[torch]) (12.4.99)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers[torch]) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers[torch]) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers[torch]) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers[torch]) (2024.2.2)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->transformers[torch]) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch->transformers[torch]) (1.3.0)
</code></pre>
</div>
</div>
<section
id="setting-the-training-hyperparamters-and-the-trainer-function"
class="cell markdown" id="rrF7gDJNAat3">
<h1><strong>Setting the training hyperparamters and the trainer
function</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/"}"
id="tG9uTTubnxSp" data-outputId="a837fca7-d8cd-4e71-ea73-c9b973363515">
<div class="sourceCode" id="cb34"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Set max length for trainer</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>training_args <span class="op">=</span> TrainingArguments(</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> output_dir<span class="op">=</span><span class="st">"my_awesome_model"</span>,</span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> learning_rate<span class="op">=</span><span class="fl">2e-5</span>,</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> per_device_train_batch_size<span class="op">=</span><span class="dv">16</span>,</span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> per_device_eval_batch_size<span class="op">=</span><span class="dv">16</span>,</span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a> num_train_epochs<span class="op">=</span><span class="dv">2</span>,</span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a> weight_decay<span class="op">=</span><span class="fl">0.01</span>,</span>
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> evaluation_strategy<span class="op">=</span><span class="st">"epoch"</span>,</span>
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a> save_strategy<span class="op">=</span><span class="st">"epoch"</span>,</span>
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> load_best_model_at_end<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></a> push_to_hub<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb34-13"><a href="#cb34-13" aria-hidden="true" tabindex="-1"></a> max_steps<span class="op">=</span>max_length</span>
<span id="cb34-14"><a href="#cb34-14" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb34-15"><a href="#cb34-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-16"><a href="#cb34-16" aria-hidden="true" tabindex="-1"></a>trainer <span class="op">=</span> Trainer(</span>
<span id="cb34-17"><a href="#cb34-17" aria-hidden="true" tabindex="-1"></a> model<span class="op">=</span>model,</span>
<span id="cb34-18"><a href="#cb34-18" aria-hidden="true" tabindex="-1"></a> args<span class="op">=</span>training_args,</span>
<span id="cb34-19"><a href="#cb34-19" aria-hidden="true" tabindex="-1"></a> train_dataset<span class="op">=</span>tokenized_ds[<span class="st">'train'</span>], <span class="co">#tokenized_imdb["train"],</span></span>
<span id="cb34-20"><a href="#cb34-20" aria-hidden="true" tabindex="-1"></a> eval_dataset<span class="op">=</span>tokenized_ds[<span class="st">'test'</span>], <span class="co">#tokenized_imdb["test"],</span></span>
<span id="cb34-21"><a href="#cb34-21" aria-hidden="true" tabindex="-1"></a> tokenizer<span class="op">=</span>tokenizer,</span>
<span id="cb34-22"><a href="#cb34-22" aria-hidden="true" tabindex="-1"></a> data_collator<span class="op">=</span>data_collator,</span>
<span id="cb34-23"><a href="#cb34-23" aria-hidden="true" tabindex="-1"></a> compute_metrics<span class="op">=</span>compute_metrics,</span>
<span id="cb34-24"><a href="#cb34-24" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<div class="output stream stderr">
<pre><code>/usr/local/lib/python3.10/dist-packages/accelerate/accelerator.py:432: FutureWarning: Passing the following arguments to `Accelerator` is deprecated and will be removed in version 1.0 of Accelerate: dict_keys(['dispatch_batches', 'split_batches', 'even_batches', 'use_seedable_sampler']). Please pass an `accelerate.DataLoaderConfiguration` instead:
dataloader_config = DataLoaderConfiguration(dispatch_batches=None, split_batches=False, even_batches=True, use_seedable_sampler=True)
warnings.warn(
</code></pre>
</div>
</div>
<section id="training-the-model" class="cell markdown"
id="_HlXFoDyAi91">
<h1><strong>Training the model</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":142}"
id="8OnGXg2xouSj" data-outputId="c223b66b-30db-4424-8ef0-3d7a37405293">
<div class="sourceCode" id="cb36"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>trainer.train()</span></code></pre></div>
<div class="output display_data">
<div>
<progress value='512' max='512' style='width:300px; height:20px; vertical-align: middle;'></progress>
[512/512 13:03, Epoch 0/1]
</div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: left;">
<th>Epoch</th>
<th>Training Loss</th>
<th>Validation Loss</th>
<th>Accuracy</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0.308100</td>
<td>0.226634</td>
<td>0.912840</td>
</tr>
</tbody>
</table><p>
</div>
<div class="output execute_result" data-execution_count="12">
<pre><code>TrainOutput(global_step=512, training_loss=0.3065403923392296, metrics={'train_runtime': 787.4252, 'train_samples_per_second': 10.404, 'train_steps_per_second': 0.65, 'total_flos': 1075357923470784.0, 'train_loss': 0.3065403923392296, 'epoch': 0.33})</code></pre>
</div>
</div>
<section id="pushing-the-trained-model-into-the-huggingface-account"
class="cell markdown" id="8JuLMdDJV2ll">
<h2><strong>Pushing the trained model into the huggingface
account.</strong></h2>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":85,"referenced_widgets":["69c132bef5d74e3aacc988c67c719d5e","30b50a9e4c7f4974a7e289868c194c64","e80009bcacfd4f79af9ec3c147a8047e","c2fefe95ff9c4dda8c95cab4e5fd8df0","8e128a3106974f04bdec391571d08ebf","e68032b004164d98a21911b3d2b9ba82","417de7c7222342f091fb04ffa81d3a32","864ea33b8667485caa2ba8b827939dc5","4b3cd790d68040b7944407e58cafa4aa","8b0d13aecf084e328a4f6ba87857f875","01b7b925a00d4c178f0c152ef6ecb91c"]}"
id="qNdb7g6-udRS" data-outputId="d8015f43-8351-45a6-f814-e301e2108bc4">
<div class="sourceCode" id="cb38"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>trainer.push_to_hub()</span></code></pre></div>
<div class="output display_data">
<div class="sourceCode" id="cb39"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"69c132bef5d74e3aacc988c67c719d5e"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output execute_result" data-execution_count="13">
<div class="sourceCode" id="cb40"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"type"</span><span class="fu">:</span><span class="st">"string"</span><span class="fu">}</span></span></code></pre></div>
</div>
</div>
<section
id="the-development-of-the-sentiment-analysis-model-is-completed-with-the-above-step"
class="cell markdown" id="Vk_fzgzX4ict">
<h1><strong>The development of the sentiment analysis model is completed
with the above step.</strong></h1>
<ul>
<li><p>A model is built which can extract the sentiments and the
respective scores.</p></li>
<li><p>This model is pushed into the <strong>huggingface</strong>
profile and can be used by anytime and anyone to extract the sentiments
and the sentiment scores.</p></li>
<li><p>The pre-trained model is named
<strong>"my_awesome_model"</strong> in the huggingface models'
space.</p></li>
<li><p>Now, this pre-trained model will be used on the <strong>customer
churn data</strong> which can directly perform the activities of
retrieving the sentiment labels and score.</p></li>
<li><p>This trained model can be used to any of the tasks that involves
the sentiment extraction studies in the future.</p></li>
</ul>
</section>
<div class="cell code" id="uvayOzlgtItm">
<div class="sourceCode" id="cb41"><pre
class="sourceCode python"><code class="sourceCode python"></code></pre></div>
</div>
<div class="cell code" id="nDlezHfjtIwS">
<div class="sourceCode" id="cb42"><pre
class="sourceCode python"><code class="sourceCode python"></code></pre></div>
</div>
<div class="cell code" id="0eqBQg5DvCQH">
<div class="sourceCode" id="cb43"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="co">## Giving some example text to check the working of the model</span></span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a>text <span class="op">=</span> <span class="st">"This was a masterpiece. Not completely faithful to the books, but enthralling from beginning to end. Might be my favorite of the three."</span></span></code></pre></div>
</div>
<section
id="using-the-trained-model-from-the-huggingface-which-was-pushed-in-the-previous-step"
class="cell markdown" id="Ebc3uMiCW82d">
<h1><strong>Using the trained model from the huggingface which was
pushed in the previous step</strong>.</h1>
<pre><code> "Pipeline" function is used to import the pre-trained model.</code></pre>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":333,"referenced_widgets":["3d44d69621b94faa812b8afe564a4a9b","4d1666cba02543d484d3e68f8666fa0c","adcac645f2c7451798fa8a6fcc642f2e","c3848b5747bf447a9a28a1ac8e76eb83","6133c22f9f534ab8884fa57feb041a9d","f244e6363a5c4689b46f9678011fedbe","88ed14d2ff814194977eb018e54fda3b","7ee05cb140964aefac3867d3f42cbee9","34f9c260e91640ae8344681dd77d5de0","2665b3ffc59e4f7da56bea827c8c8124","5a0b9c3e986d4d84aacf1c95410be5c3","7c056a778233462883c8791f75afeda4","131267adb72a4ac2acea04c5b66c9242","26693dce805c45e3bc0351f99bf461ef","81ad4e01f3fa4cdc8d8f55d2e5e69bac","23027e68a89246d59e16e0cdfa086744","4f219f41762f408ebf120f82263d2964","e2ba7ea0bbd8469aaa8012cc12f75f45","39ae007578fe4e9d970180ca4ed2b3ac","a002f0968b354d35aee28b5fcf0650d2","6fcdfb1267f444fdac63a656f6c2b23d","9edf2acbcfcd4d75afc0348c7cbef266","08379b4c461940a495beed0ad37d097b","ce13771b33cc4425a66a076b107ea711","cb19844223364fad9c829c24946eefc5","209cdc2526054049ae7b252957cffdad","a596956c011b4cb9aa8b7af2883381ab","5f0232a279324a5f9484597f4b87068d","f56b49fee3b841d387c15adab937daee","1560559f3b09456fabfc17bc45d14a5a","df61c0f86d4b43d2b82aa03df4c031e1","cba7c7787d3b4b248db5d7d4a36a0015","a6e8ce3b8edc4e708ba8675287a1a54b","68250a8b4c074f59ae1d0584d3444f24","bc10e11527584267bc20b316f572dde9","8ad519c9a6c54c14a3591143d36a7686","bad3bac055c5432c91e6fb417ef9b881","a604049c65294feda27bf6e340c4ec96","56683e74d642456daa8300b7210e06be","b88d4a418f2443a8a4df56b941188b17","8695a8812e864bae9f7f2e0602e00047","795430768f354f7fa5c0602e3a70599e","6dabf69ae70f4b8c80b68746f58e1f69","406e0c1078374f74acd1f8a400a17f0a","f19f7f5a7294420cae6868e946924436","46655c6400264a36b328b18081591d45","a5793c6d0e654151b00df3c9d651f7a4","992c8fa1f5944e848441a052b4805ae4","09687d2599c148d696851a4876c434a8","025e8fddc1fb47ffb287d798e016f156","e96f5a012dda4f2792096fb5db89e97c","fada9658e8a7465fa1f56043b9c40071","a0c2ade4469146c6b1fe8893d5dcf99c","9df529b45ba9434fa4d3836c34d6fe44","73218fbd160444499f54ef132d44d709","27552e17004f46e093b3a66624469f0e","6fcaa05d4a6c4f4c8d442318da275ce7","b4b6049ca3ed428ea26a0642f2693e66","24ebc81d8738490aafd9fb2956c859fa","378b021b2f5d47d988911b1fd5efbb05","7c43c55f722a45bb9b1cc0e652aed136","60e252f4543c425db46b07706852cdf6","218783b3c79b490c9d24d469870356de","91990ad1ea524359a7dd97fd1fd6f854","ddc96fe599994e5b9d0a2ac1e4be1aa2","683cb868a9d7473ca4ecdf4c3b4496cf"]}"
id="YWoRZVDTvF_5" data-outputId="679b1ad1-6b45-4225-c487-f0a99bebe6e2">
<div class="sourceCode" id="cb45"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> transformers <span class="im">import</span> pipeline</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a>classifier <span class="op">=</span> pipeline(<span class="st">"sentiment-analysis"</span>, model<span class="op">=</span><span class="st">"ganeshkota/my_awesome_model"</span>)</span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a><span class="co"># classifier(text)</span></span></code></pre></div>
<div class="output stream stderr">
<pre><code>/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning:
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
warnings.warn(
</code></pre>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb47"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"3d44d69621b94faa812b8afe564a4a9b"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb48"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"7c056a778233462883c8791f75afeda4"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb49"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"08379b4c461940a495beed0ad37d097b"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb50"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"68250a8b4c074f59ae1d0584d3444f24"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb51"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"f19f7f5a7294420cae6868e946924436"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
<div class="output display_data">
<div class="sourceCode" id="cb52"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"model_id"</span><span class="fu">:</span><span class="st">"27552e17004f46e093b3a66624469f0e"</span><span class="fu">,</span><span class="dt">"version_major"</span><span class="fu">:</span><span class="dv">2</span><span class="fu">,</span><span class="dt">"version_minor"</span><span class="fu">:</span><span class="dv">0</span><span class="fu">}</span></span></code></pre></div>
</div>
</div>
<section id="creating-empty-matrices-to-store-the-labels-and-scores"
class="cell markdown" id="v9LoZNucA63C">
<h1><strong>Creating empty matrices to store the labels and
scores</strong></h1>
</section>
<div class="cell code" id="GPDE69ay1p2i">
<div class="sourceCode" id="cb53"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> []</span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a>scores <span class="op">=</span> []</span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a></span></code></pre></div>
</div>
<section
id="attaching-the-colab-notebook-to-local-system-to-import-the-dataset"
class="cell markdown" id="Q-Pb6FCySLVn">
<h1><strong>Attaching the Colab Notebook to local system to import the
dataset.</strong></h1>
</section>
<div class="cell code"
data-colab="{"base_uri":"https://localhost:8080/","height":73}"
id="6ien1M00qGCl" data-outputId="27dfaee5-5221-440a-8180-069b957ae431">
<div class="sourceCode" id="cb54"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> google.colab <span class="im">import</span> files</span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> google.colab <span class="im">import</span> drive</span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a><span class="co"># drive.mount('/content/drive')</span></span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a>uploaded <span class="op">=</span> files.upload()</span></code></pre></div>
<div class="output display_data">
<input type="file" id="files-0d15bdf4-5aca-400b-ac95-d6c0a527bacc" name="files[]" multiple disabled
style="border:none" />
<output id="result-0d15bdf4-5aca-400b-ac95-d6c0a527bacc">
Upload widget is only available when the cell has been executed in the
current browser session. Please rerun this cell to enable.
</output>
<script>// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Helpers for google.colab Python module.
*/
(function(scope) {
function span(text, styleAttributes = {}) {
const element = document.createElement('span');
element.textContent = text;
for (const key of Object.keys(styleAttributes)) {
element.style[key] = styleAttributes[key];
}
return element;
}
// Max number of bytes which will be uploaded at a time.
const MAX_PAYLOAD_SIZE = 100 * 1024;
function _uploadFiles(inputId, outputId) {
const steps = uploadFilesStep(inputId, outputId);
const outputElement = document.getElementById(outputId);
// Cache steps on the outputElement to make it available for the next call
// to uploadFilesContinue from Python.
outputElement.steps = steps;
return _uploadFilesContinue(outputId);
}
// This is roughly an async generator (not supported in the browser yet),
// where there are multiple asynchronous steps and the Python side is going
// to poll for completion of each step.
// This uses a Promise to block the python side on completion of each step,
// then passes the result of the previous step as the input to the next step.
function _uploadFilesContinue(outputId) {
const outputElement = document.getElementById(outputId);
const steps = outputElement.steps;
const next = steps.next(outputElement.lastPromiseValue);
return Promise.resolve(next.value.promise).then((value) => {
// Cache the last promise value to make it available to the next
// step of the generator.