-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
1475 lines (1048 loc) · 48.2 KB
/
model.py
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
# -*- coding: utf-8 -*-
"""Copy of Assignment #1 for DS 207: Introduction for Natural Language Processing
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1XiTAWB8D2MTr815ibCp6gE5t9acHAAtS
# Assignment 1
**DS: 207 Introduction to Natural Language Processing**
**Due (Feb 6, 16:59 PM)**
Development & Design: Debarpan Bhattacharya & Nicy Scaria.
Testing: Kinshuk Vasisht
"""
from google.colab import drive
drive.mount('/content/drive')
"""The goal of this assignment is introduce the basics of text processing, by building a few text classifiers, and learning to represent words.
You'll have to add your code wherever you see the comment `# ADD YOUR CODE HERE`. Please make a copy of this assignment, and you can use Google Colab notebooks to work on this. Later, you can download this notebook as a python file and submit it as per the following instructions.
## Submission Instructions:
1. In the notebook, a few example codes are provided, whereas a few are kept blank for you to fill them up.
2. The evaluation will be based on the results obtained by functions impleted by you. Do not change the print statements having `EVALUATION` as they are used for auto-evaluation.
3. **Submission file(s)**:
* Save this `.ipynb` to your drive, complete the required code blocks and run the notebook.
* After completing this assignment, download the notebook as`.py`. Name it as `SAPname_SRno_assignment1.py`, where `SAPname` refers to your name as per SAP record, and `SRno` refers to the last 5 digits of your IISc SR number. For example, IISc student with SAP name Twyla Linda (SR no - 04-03-00-10-22-20-1-15329) would name it as `Twyla_Linda_15329_assignment1.py`.
* The files associated with the Word2Vec training, i.e., the `model.pt`, `vocab.pt`, `word_embeddings.npy` and `loss.json`, will be downloaded and saved in a folder with the name `SAPname_SRno`. Zip this folder along with the `.py` file, save it as `SAPname_SRno_assigment1` and upload on MS Teams. The zip folder should contain: (1) `SAPname_SRno_assignment1.py`, (2) a subfolder called `SAPname_SRno` which will contain `model.pt`, `vocab.pt` , `word_embeddings.npy` and `loss.json`.
Because submissions are auto-graded, please ensure that the naming of the files is consistent with the instructions.
## Part I Text Classification (TA: Debarpan Bhattacharya)
**Dataset**:
We will dive into a basic text-based sentiment classification task. The dataset consists of sentences with two different kinds of sentiments- `positive`, and `negative` sentiments. Following are a set of examples,
* **`positive`**: *I really like your new haircut!*
* **`negative`**: *Your new haircut is awful!*
The Dataset has a training set (`train_data.csv`- provided), a validation set (`val_data.csv`- provided) and a blind test set (`test_data.csv`- not provided). The notebook uses a `test_data.csv` file, but it is just a duplicate of `val_data.csv`, and the blind `test_data.csv` will replace it while grading your solutions.
**Important**: Fix seed as 42 whenever performing any randomized operations, e.g., initializing ML models.
### Download the dataset required for the assignment
"""
import random
random.seed(42)
# download train data
!wget -O train_data.csv "https://docs.google.com/spreadsheets/d/176-KrOP8nhLpoW91UnrOY9oq_-I0XYNKS1zmqIErFsA/gviz/tq?tqx=out:csv&sheet=train_data.csv"
# download validation data
!wget -O val_data.csv "https://docs.google.com/spreadsheets/d/1YxjoAbatow3F5lbPEODToa8-YWvJoTY0aABS9zaXk-c/gviz/tq?tqx=out:csv&sheet=val_data.csv"
# download test data
!wget -O test_data.csv "https://docs.google.com/spreadsheets/d/1YxjoAbatow3F5lbPEODToa8-YWvJoTY0aABS9zaXk-c/gviz/tq?tqx=out:csv&sheet=val_data.csv"
#@title Read data
import numpy as np
import pandas as pd
df = pd.read_csv('train_data.csv')
df_val = pd.read_csv('val_data.csv')
# Note that we will change the test file
# when we grade assignments ...
# For now it is the same as the validation set
df_test = pd.read_csv('test_data.csv')
df.head()
#@title Prepare training, validation and test data.
X_train, y_train = df.review.values.tolist(), df.sentiment.values.tolist()
X_val, y_val = df_val.review.values.tolist(), df_val.sentiment.values.tolist()
X_test, y_test = df_test.review.values.tolist(), df_test.sentiment.values.tolist()
labels = ['negative', 'positive']
# converting the sentiment labels into labels
# class 0 for negagtive, and class 1 for positive
y_train = [labels.index(i) for i in y_train]
y_val = [labels.index(i) for i in y_val]
y_test = [labels.index(i) for i in y_test]
import string
p={}
n={}
for i in range(0,len(X_train)):
x_split = [word.rstrip(string.punctuation) for word in X_train[i].split(' ')]
if(y_train[i] ==1):
for word in x_split:
p[word]=p.get(word,0)+1
else:
for word in x_split:
n[word]=n.get(word,0)+1
word_ratios = {word: n.get(word, 0) / p.get(word, 1) for word in n}
# print("Word Ratios:", word_ratios)
sorted_word_ratios = dict(sorted(word_ratios.items(), key=lambda item: item[1], reverse=True))
# print("Sorted Word Ratios:", sorted_word_ratios)
filtered_negative_word_ratios = {word: ratio for word, ratio in sorted_word_ratios.items() if n.get(word, 0) > 200 }
# print("Filtered Word Ratios (>300 in dict n):", filtered_negative_word_ratios)
word_ratios = {word: p.get(word, 0) / n.get(word, 1) for word in p}
# print("Word Ratios:", word_ratios)
sorted_word_ratios = dict(sorted(word_ratios.items(), key=lambda item: item[1], reverse=True))
# print("Sorted Word Ratios:", sorted_word_ratios)
filtered_positive_word_ratios = {word: ratio for word, ratio in sorted_word_ratios.items() if p.get(word, 0) > 200 }
# print("Filtered Word Ratios (>300 in dict n):", filtered_positive_word_ratios)
# positive_words=list(filtered_positive_word_ratios.keys())
# negative_words=list(filtered_negative_word_ratios.keys())
positive_words=list(filtered_positive_word_ratios.keys())
negative_words=list(filtered_negative_word_ratios.keys())
N=20
very_good_words=positive_words[:N]
good_words=positive_words[N:2*N]
bad_words=negative_words[N:2*N]
very_bad_words=negative_words[:N]
# print(good_words)
"""#### Approach 1: Rule based classification
The rule-based classification works using a few hand-crafted rules. In sentiment classification, few words are associated with positive sentiment and few others with negative sentiment. Let's attempt to build a classifier that predicts the sentiment of the reviews based on such words.
Let's first write a few rules to extract important features about the input movie review. As an example, we provide a function that counts the number of `good` and `bad` words in the input.
Below, you can see a sample rule-based classification system. Later, you will be building one yourself.
"""
def sample_extract_features(X):
"""
Extracts features from a text input.
Args:
X (string): Text input.
Returns:
dictionary: features extracted from X.
"""
features = {}
X_split = X.split(' ')
# Count the number of "good words" and "bad words" in the text
good_words = ['love', 'good', 'brilliant', 'fantastic', 'amazing', 'great']
bad_words = ['hate', 'bad', 'horrible', 'awful', 'terrible', 'mess', 'frustating', 'frustatingly']
features['good_word_count'], features['bad_word_count'] = 0, 0
for x in X_split:
if x in good_words:
features['good_word_count'] = features.get('good_word_count', 0) + 1
if x in bad_words:
features['bad_word_count'] = features.get('bad_word_count', 0) + 1
# The "bias" value can be set to one, to allow us to assign a "default" score to the text
features['bias'] = 1
return features
def sample_predict(X, feature_weights):
"""
Classifies the sentiment of a text input.
Args:
X (string): Text input.
feature_weights: weightage of different features.
Returns:
int: binary sentiment represented by 0/1.
"""
score = 0
# Here we just multiply the feature value
# with its corresponding weight and aggregate
for feat_name, feat_value in sample_extract_features(X).items():
score = score + feat_value * feature_weights[feat_name]
# the prediction is based on whether the aggregated score is above 0 or not
if score > 0:
return 1
else:
return 0
def get_sample_features_weights():
"""
To obtain feature weightage for different features.
Args:
None here.
Returns:
dictionary: feature names and their weightage.
"""
# Based on the selected features, you can manually assign them weights
feature_weights = {'good_word_count': 1.0, 'bad_word_count': -1.0, 'bias': 0.5}
return feature_weights
#@title Computing the accuracy of the system
def calculate_accuracy(Y_true, Y_pred):
"""
Calculates accuracy of predictions given the ground truth.
Args:
Y_true (list): Ground truth labels.
Y_pred (list): Predictions.
Returns:
float: Prediction accuracy in range [0.0-100.0].
"""
correct = 0.0
total = len(Y_true)
# verify if we have the same number of predictions as labels
assert len(Y_true) == len(Y_pred)
# count the number of correct predictions
for y_true, y_pred in zip(Y_true, Y_pred):
if y_true == y_pred:
correct += 1.0
if total > 0:
return 100. * correct / total
# return 0 if there the total number of examples are zero
return 0.0
#@title Putting the sample rule-based classifier together
# get the sample weights
sample_feature_weights = get_sample_features_weights()
predictions = []
# for each test example, make a prediction
for input_example in X_test:
y = sample_predict(input_example, sample_feature_weights)
predictions.append(y)
# compute and print the accuracy
print (calculate_accuracy(y_test, predictions))
"""As you can observe here, the model achieves about 60% accuracy (note that the performance of a random classifier would be close to 50%).
### Build your own rule-based classifier (10 marks)
Your have to write your own `extract_features`, `get_feature_weights` and `predict` functions for your rule-based classifier.
"""
def extract_features(X):
features = {}
features['bias'] = 1
# ADD YOUR CODE HERE
X_split = [word.rstrip(string.punctuation) for word in X.split(' ')]
X_split=[element.lower() for element in X_split]
features['very_good_words']=0
features['good_words']=0
features['bad_words']=0
features['very_bad_words']=0
for x in X_split:
if x in good_words:
features['good_words']=features.get('good_words',0)+1
elif x in very_good_words:
features['very_good_words']=features.get('very_good_words',0)+1
elif x in bad_words:
features['bad_words']=features.get('bad_words',0)+1
elif x in very_bad_words:
features['very_bad_words']=features.get('very_bad_words',0)+1
return features
def get_feature_weights():
feature_weights = {}
feature_weights['bias'] = 0.5
# ADD YOUR CODE HERE (***Modified***)
feature_weights['very_good_words']=1.8
feature_weights['good_words']=0.7
feature_weights['very_bad_words']=-2
feature_weights['bad_words']=-1
# please manually assign feature weights to the features you've extracted
return feature_weights
def predict(X, feature_weights):
"""
Classifies the sentiment of a text input.
Args:
X (string): Text input.
feature_weights: weightage of different features.
Returns:
int: binary sentiment represented by 0/1.
"""
# ADD YOUR CODE HERE
score=0
for feat_name, feat_value in extract_features(X).items():
score = score + feat_value * feature_weights[feat_name]
if score >= 0:
return 1
else:
return 0
return 1.0
#@title Evaluating your rule-based classifier
## Please do not change anything in this code block.
feature_weights = get_feature_weights()
predictions = []
for input_example in X_test:
y = predict(input_example, feature_weights)
predictions.append(y)
print (f"EVALUATION of rule-based classifier is: {calculate_accuracy(y_test, predictions)}")
"""You will be evaluated based on the performance of your rule-based classifier. Please note that the sample rule-based classifier achieves about 60% accuracy. Anything below 60% will not yield any points.
***Please do not change the evaluation code in the block above, as it would be used to grade your classifier***.
Below, we will directly learn the feature weights, rather than manually assigning those ourselves as manual assignment can be error-prone and labor intensive.
"""
#@title Learning the weights of extracted features using logistic regression.
from sklearn.linear_model import LogisticRegression
def get_sample_learnable_weights(X_data, Y_data, sample_extract_features):
"""
Learn feature weights using the training data.
Args:
X_data (list of strings): All the text data points in training data.
Y_data (list of int): Ground truth labels for text data points in X_data.
sample_extract_features: A Function that extracts features from text sample.
The sample_extract_features function should be of the
same format as sample_extract_features(X) function
implemented above.
Returns:
dictionary: feature names and their learned weights.
"""
# training a logistic regression model for classification using
# the features obtained by sample_extract_features(X) function.
# get all feature names
feature_names = list(sample_extract_features(X_data[0]).keys())
# Below code snippet accumulates features extracted from all the
# text data points in X_data.
all_features = []
# iterate over all text data points in X_data.
for input_example in X_data:
feature = [] # to store features extracted from input_example.
feat_dict = sample_extract_features(input_example)
# iterate over different feature names and store the corresonding values.
for name in feature_names:
feature.append(feat_dict[name])
all_features.append(feature) # append features obtained from input_example to all_features.
# Below, we show how to fit a logistic regression (LR) model using the features and the target labels.
# We use Sklearn's 'LogisticRegression' to do this. While initiating, 'fit_intercept' is set False,
# because 'bias' is already included in the extracted feature (see sample_extract_features() implementation).
# Also, random state is set to 42 to avoid different initialization of the model while running the codebook
# multiple times.
clf = LogisticRegression(fit_intercept=False, random_state=42).fit(all_features, Y_data)
# As training a logistic regression model assigns weights to each of the features (refer class notes),
# these weights are learnable and we will use them as learned feature weights.
# extract feature weights.
coffs = clf.coef_[0]
# convert to dictionary
coffs_dict = {feature_names[i]: coffs[i] for i in range(len(feature_names))}
return coffs_dict
#@title Putting the sample rule-based classifier with learnable weights together
# get the sample weights
sample_feature_weights_lr = get_sample_learnable_weights(X_train, y_train, sample_extract_features)
predictions = []
for input_example in X_test:
y = sample_predict(input_example, sample_feature_weights_lr)
predictions.append(y)
print (calculate_accuracy(y_test, predictions))
"""As you can observe here, the model achieves about 63% accuracy (note that the performance of a random classifier would be close to 50%). Note that, the performance significantly improves by making the weights learnable as compared to the manual weights.
## How does learnable weights-based classifier work on the features you implemented (3 marks)
Your don't have to write anything here except simply running the below kernel.
Depending upon the quality of your features, your classifier performance will vary.
"""
#@title Evaluating your rule-based classifier with learnable weights.
## Please do not change anything in this code block.
feature_weights_lr = get_sample_learnable_weights(X_train, y_train, extract_features)
predictions = []
for input_example in X_test:
y = predict(input_example, feature_weights_lr)
predictions.append(y)
print (f"EVALUATION of rule-based classifier with learnable weights is: {calculate_accuracy(y_test, predictions)}")
# feature_weights_lr
#@title Visualization and comparison between manual and learnable weights.
# visualize and compare the manual weights vs learnt weights.
import matplotlib.pyplot as plt
import numpy as np
feature_names = list(feature_weights.keys())
manual_weights = [feature_weights[name] for name in feature_names]
learnt_weights = [feature_weights_lr[name] for name in feature_names]
weights_dict = {
'Manual weights': manual_weights,
'Learnt weights': learnt_weights,
}
x = np.arange(len(feature_names)) # the label locations
width = 0.35 # the width of the bars
multiplier = 0
fig, ax = plt.subplots(layout='constrained')
for attribute, measurement in weights_dict.items():
offset = width * multiplier
rects = ax.bar(x + offset, measurement, width, label=attribute)
ax.bar_label(rects, padding=3)
multiplier += 1
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('weight')
ax.set_title('Manual vs learnable weights comparison')
ax.set_xticks(x + width, feature_names)
ax.legend(loc='upper left', ncols=2)
ax.set_ylim(-3, 3)
plt.savefig('manual_vs_learned_weights.png')
# Refer to the saved manual_vs_learned_weights.png to see how manual and learned weights compare.
"""### Approach 2: Bag-of-Words (BoW) (10 marks)
The BoW vector representations is based on the unordered counts of words piece of text (similar to a "bag" of words).
Let's attempt to build a classifier that tries to classify the reviews based on such vectors.
"""
# implement your BoW classifier. In case the total number of words are very large,
# consider using top-k most frequent words (e.g. k=10,000) while creating BoW vectors.
# The below function finds out all words present in the corpus and assigns each word to
# an index.
def word_to_idx_BOW(X_data, K=10000):
"""
Generates a set of word-index pairs after analyzing all words present in entire
data corpus X_data.
Args:
X_data (list of strings): All the text data points in training data.
K (int): The number of most frequent words to be considered.
In case (K-p)th to (K+q)th words in most frequent words order
have same frequency, you can choose p words randomly from those
(p+q) number of words.
Returns:
dictionary: words as keys and indices as values.
"""
word_to_idx = {}
# ADD YOUR CODE HERE
#let us try to implement the naive classifier which does not remove stopwords
word_count={}
for X in X_data:
X_split=X.split(' ')
for word in X_split:
word_count[word]=word_count.get(word, 0) + 1
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# Take the top 10,000 words
top_words = sorted_words[:10000]
# Create a word_to_idx dictionary
word_to_idx = {word: idx for idx, (word, _) in enumerate(top_words)}
return word_to_idx
# generate BoW feature for text input X.
def extract_features_BoW(X, word_to_idx):
"""
Generates BoW feature for X using word_to_idx.
Args:
X (string): text input.
word_to_idx (dictionary): word-index mapping with words as keys and
indices as values.
Returns:
dictionary: features of X.
"""
features = {}
# ADD YOUR CODE HERE
features={word:0 for word in word_to_idx}
words=X.split(' ')
for word in words:
if word in word_to_idx:
features[word]+=1
return features
# checking the outputs of the above functions on a small set of examples
sample_data = [
"When is the homework due ?",
"When are the TAs' office hours ?",
"How hard is the homework ?",
]
word_to_idx = word_to_idx_BOW(sample_data)
features = extract_features_BoW(sample_data[0], word_to_idx)
print(features)
def sigmoid(z):
return 1 / (1 + np.exp(-z))
#@title Implement your feature weight learning for BoW classifier
def get_learnable_weights_BoW(X_data, Y_data, word_to_idx_BOW, extract_features_BoW):
"""
Learning feature weights for BoW features, using training data.
Args:
X_data (list of strings): All the text data points in training data.
Y_data (list of int): Ground truth labels for text data points in X_data.
word_to_idx_BOW: A Function that looks at all the words in data corpus X_data
and returns word-index mapping. You had to implement
word_to_idx_BOW() funtion above.
extract_features_BoW: A Function that extracts BoW features from text sample.
The extract_features_BoW() function had to be implemented
by you above.
Returns:
dictionary: feature names and their learned weights.
"""
# ADD YOUR CODE HERE
# Now i will try to implement the logistic regression algorithm here to learn the feature weights
word_to_idx=word_to_idx_BOW(X_data)
# Extract BoW features for each training sample
X_bow = np.array([list(extract_features_BoW(X, word_to_idx).values()) for X in X_data])
# print(X_bow)
# Initialize logistic regression model
logistic_model = LogisticRegression(max_iter=1000)
# Train the model
logistic_model.fit(X_bow, Y_data)
feature_names = list(word_to_idx.keys())
coffs_dict = {feature_names[i]: logistic_model.coef_[0][i] for i in range(len(feature_names))}
return coffs_dict
#@title Evaluating your BoW classifier
def predict(X, feature_weights):
"""
Classifies the sentiment of a text input.
Args:
X (string): Text input.
feature_weights: weightage of different features.
Returns:
int: binary sentiment represented by 0/1.
"""
# ADD YOUR CODE HERE
words = X.split()
score = 0
for word in words:
if word in feature_weights:
score += feature_weights[word]
# Apply sigmoid activation
# probability = sigmoid(score)
# Threshold the probability
if score>0:
return 1
else:
return 0
return 1.0
# get the sample weights
BoW_feature_weights_lr = get_learnable_weights_BoW(X_train, y_train, word_to_idx_BOW, extract_features_BoW)
# print(BoW_feature_weights_lr)
predictions = []
for input_example in X_test:
y = predict(input_example, BoW_feature_weights_lr)
predictions.append(y)
print (f"EVALUATION of BoW classifier is: {calculate_accuracy(y_test, predictions)}")
"""## Finding most positive and most negative words (2 Marks)
Based on the magnitude of weights corresponding to different words, write down code to find the 5 most positive words and 5 most negative words.
"""
most_positive_words = []
most_negative_words = []
## WRITE CODE HERE TO POPULATE THESE LISTS
most_positive_words=sorted(BoW_feature_weights_lr, key=BoW_feature_weights_lr.get, reverse=True)[:5]
most_negative_words = sorted(BoW_feature_weights_lr, key=BoW_feature_weights_lr.get)[:5]
## most_postive_words and most_negative_words should be list of strings
assert (len(most_positive_words) == 5)
assert (len(most_negative_words) == 5)
print("EVALUATION five most positive words: " + " ".join(most_positive_words))
print("EVALUATION five most negative words: " + " ".join(most_negative_words))
"""## Part II Word2Vec (TA: Nicy Scaria) (25 Marks)
Word2vec is one of the most popular techniques to learn word embeddings. The idea behind word2vec was that the meaning of a word is determined by the context in which it occurs. A word embedding is a learned representation for text where words that have the same meaning have a similar representation.
**Word2vec** model has 2 architectures:
1. **Continuous bag of word (CBOW):**
CBOW predicts the center word from the surrounding context words.
2. **Skip-gram:**
Skip-gram predicts surrounding context words from the center word.
#### SkipGram from Scratch
In this exercise, you will code the skipgram model from scratch using the PyTorch library.
"""
# Just added by me for the GPU utilization
import torch,gc
gc.collect()
torch.cuda.empty_cache()
!pip install -U portalocker
#make sure you install the library and restart the session
torch.cuda.is_available() # added by me for my checking purposes
# importing the necessary libraries
import torch
from functools import partial
import torch.nn as nn
from torch.utils.data import DataLoader
from torchtext.data import to_map_style_dataset
from torchtext.data.utils import get_tokenizer
from torchtext.vocab import build_vocab_from_iterator
from torchtext.datasets import WikiText2
import torch.optim as optim
from torch.optim.lr_scheduler import LambdaLR
# import torch.nn.functional as F #added by me
import os
import numpy as np
import json
import argparse
"""> **In the following code the add your hyperparameters for the network.**"""
# Initialization
"""choose your hyperparameter and see the difference in performance"""
# ADD YOUR CODE HERE
# CHANGE THE None VALUES TO YOUR DESIRED VALUES
# Please free to play with these hyperparameters to see the effects on the
# quality of generated embeddings
SKIPGRAM_N_WORDS = 4 # the length of the context on each side (k)
MIN_WORD_FREQUENCY = 40 # only words with a minimum word frequency considered
MAX_SEQUENCE_LENGTH = 250 # sentences with length more than this value truncated
EMBED_DIMENSION = 300 # dimension of the word2vec vectors
EMBED_MAX_NORM = 1
def get_english_tokenizer():
"""
Documentation:
https://pytorch.org/text/stable/_modules/torchtext/data/utils.html#get_tokenizer
"""
tokenizer = get_tokenizer("basic_english", language="en")
return tokenizer
def get_data_iterator(ds_name, ds_type, data_dir):
"""
input dataset used:
https://paperswithcode.com/dataset/wikitext-2
This is directly imported from PyTorch.
"""
data_iter = WikiText2(root=data_dir, split=(ds_type))
data_iter = to_map_style_dataset(data_iter)
return data_iter
def build_vocab(data_iter, tokenizer):
"""Builds vocabulary from iterator"""
vocab = build_vocab_from_iterator(
map(tokenizer, data_iter),
specials=["<unk>"], #adding special tokens to the vocabulary
min_freq=MIN_WORD_FREQUENCY,
)
vocab.set_default_index(vocab["<unk>"])
return vocab
def collate_skipgram(batch, text_pipeline):
"""
This function prepares data for training the skipgram model.
It generates pairs of center and context words from the batch of text.
batch: A batch of text data
text_pipeline: A pipeline function that processes text into tokens
(batch_input, batch_output) -> (center word, context words)
"""
batch_input, batch_output = [], []
# Process each text in the batch
for text in batch:
text_tokens_ids = text_pipeline(text)
# Skip texts shorter than the required window size
if len(text_tokens_ids) < SKIPGRAM_N_WORDS * 2 + 1:
continue
# Truncate texts to a maximum sequence length if specified
if MAX_SEQUENCE_LENGTH:
text_tokens_ids = text_tokens_ids[:MAX_SEQUENCE_LENGTH]
# Create training pairs for each word in the text
for idx in range(len(text_tokens_ids) - SKIPGRAM_N_WORDS * 2):
token_id_sequence = text_tokens_ids[idx : (idx + SKIPGRAM_N_WORDS * 2 + 1)]
input_ = token_id_sequence.pop(SKIPGRAM_N_WORDS)
outputs = token_id_sequence
# Add each context word with the center word to the output lists
for output in outputs:
batch_input.append(input_)
batch_output.append(output)
# Convert lists to PyTorch tensors
batch_input = torch.tensor(batch_input, dtype=torch.long)
batch_output = torch.tensor(batch_output, dtype=torch.long)
return batch_input, batch_output
def get_dataloader_and_vocab(
model_name, ds_name, ds_type, data_dir, batch_size, shuffle, vocab=None
):
"""
Prepares a DataLoader and builds a vocabulary for the dataset.
model_name: Name of the model to be used
ds_name: Name of the dataset
ds_type: Type of the dataset (e.g., train, test)
data_dir: Directory where the dataset is stored
batch_size: Size of each batch
vocab: An existing vocabulary, if available
"""
data_iter = get_data_iterator(ds_name, ds_type, data_dir)
tokenizer = get_english_tokenizer()
if not vocab:
vocab = build_vocab(data_iter, tokenizer)
text_pipeline = lambda x: vocab(tokenizer(x))
collate_fn = collate_skipgram
# creates a DataLoader for the dataset
"""
dataloader documentation
https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader
"""
dataloader = DataLoader(
data_iter,
batch_size=batch_size,
shuffle=shuffle,
collate_fn=partial(collate_fn, text_pipeline=text_pipeline),
)
return dataloader, vocab
"""### Initialize the SkipGram Model
**Complete the `initialization` and `forward` function in the following SkipGram_Model class**
"""
class SkipGram_Model(nn.Module):
"""
Implementation of Skip-Gram model described in paper:
https://arxiv.org/abs/1301.3781
"""
def __init__(self, vocab_size: int):
super(SkipGram_Model, self).__init__()
self.embeddings=nn.Embedding(num_embeddings=vocab_size,embedding_dim=EMBED_DIMENSION,max_norm=EMBED_MAX_NORM)
self.linear=nn.Linear(EMBED_DIMENSION,vocab_size)
def forward(self, inputs_):
"""define forward function"""
# ADD YOUR CODE HERE
x=self.embeddings(inputs_)
x=self.linear(x)
# return the output of your final layer to find the minimize the loss
return x
def get_word_embedding(self):
""" return the associated word embeddings for center words """
# ADD YOUR CODE HERE
embeddings =list(model.parameters())[0]
embeddings = embeddings.cpu().detach().numpy()
norms = np.linalg.norm(embeddings, axis=1)
norms = norms.reshape(-1, 1)
embeddings = embeddings / (norms)
return embeddings
"""> **The following is the Trainer class for the skip-gram model. Add your code for the `training` and `validation` loops.**"""
class Trainer:
"""Main class for model training"""
# NOTE: you are free to add additional inputs/functions
# to the trainer class to make training better
# make sure to define and add it within the input
# and initialization if you are using any additional inputs
# for usage in the function
def __init__(
self,
model,
epochs,
train_dataloader,
val_dataloader,
criterion,
optimizer,
lr_scheduler,
device,
model_dir,
model_name,
):
self.model = model
self.epochs = epochs
self.train_dataloader = train_dataloader
self.val_dataloader = val_dataloader
self.criterion = criterion
self.optimizer = optimizer
self.lr_scheduler=lr_scheduler
self.device = device
self.model_dir = model_dir
self.model_name = model_name
self.loss = {"train": [], "val": []}
self.model.to(self.device)
def train(self):
for epoch in range(self.epochs):
self._train_each_epoch(epoch)
self._validate_each_epoch(epoch)
print("Epoch: {}/{}".format(epoch + 1, self.epochs),"Train Loss={:.5f}, Val Loss={:.5f}".format(self.loss["train"][-1], self.loss["val"][-1]))
self.lr_scheduler.step()
def _train_each_epoch(self,epoch):
self.model.train() # sets the model to training mode
running_loss=[]
for i,batch_data in enumerate(self.train_dataloader,1):
inputs=batch_data[0].to(self.device)
labels=batch_data[1].to(self.device)
self.optimizer.zero_grad()
outputs=self.model(inputs) # forward pass of data
loss=self.criterion(outputs,labels)
loss.backward()
self.optimizer.step()
running_loss.append(loss.item())
epoch_loss=np.mean(running_loss)
self.loss["train"].append(epoch_loss)
def _validate_each_epoch(self,epoch):
self.model.eval() # set the model to evaluation mode
running_loss=[]
with torch.no_grad(): #No need to do the parameter updates in validation and testing time so
for i,batch_data in enumerate(self.val_dataloader,1):
inputs=batch_data[0].to(self.device)
labels=batch_data[1].to(self.device)
outputs=self.model(inputs)
loss=self.criterion(outputs,labels)
running_loss.append(loss.item())
epoch_loss=np.mean(running_loss)
self.loss["val"].append(epoch_loss)
def save_model(self):
"""
Save final model to directory
"""
model_path = os.path.join(self.model_dir, "model.pt")
torch.save(self.model, model_path)
def save_loss(self):
"""
Save train/val loss as json file to the directory