-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval.py
1297 lines (1060 loc) · 55.4 KB
/
eval.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
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from tqdm import tqdm
from scipy.stats import pearsonr,spearmanr
# from model import SkipGramModel, TimestampedSkipGramModel
from model import SkipGramModel, TimestampedSkipGramModel
from data_reader import DataReader, Word2vecDataset, TimestampledWord2vecDataset
import json
import os
import argparse
import pickle
# from scipy.spatial import distance
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.manifold import TSNE
from matplotlib import pyplot as plt
from sys import platform
import numpy as np
import heapq
import scipy
import pandas as pd
import pickle
if platform != "darwin":
plt.switch_backend('agg')
# word_sin word_cos word_mixed word_linear word_mixed_fixed
parser = argparse.ArgumentParser(description='parameter information')
parser.add_argument('--model_path', dest='time_type', type=str, default="coha",
help='model path with log.txt, vocab.txt and pytorch.bin')
args = parser.parse_args()
def get_score(a, b):
from sklearn.metrics import mutual_info_score
from scipy.stats import entropy
score = mutual_info_score(a, b)
_, p1 = np.unique(a, return_counts=True)
_, p2 = np.unique(b, return_counts=True)
p1 = p1 / p1.sum()
p2 = p2 / p2.sum()
return score / (entropy(p1) + entropy(p2)) * 2
def get_score1(a, b):
from sklearn.metrics import f1_score, fbeta_score
x = [i == j for i in a for j in a]
y = [i == j for i in b for j in b]
# print(x)
# print(y)
return fbeta_score(x, y, beta=5)
def keep_top(arr, k=3):
smallest = heapq.nlargest(k, arr)[-1] # find the top 3 and use the smallest as cut off
arr[arr < smallest] = 0 # replace anything lower than the cut off with 0
return arr
def read_embeddings_from_file(file_name):
embedding_dict = dict()
with open(file_name, encoding="utf-8") as f:
for i, line in enumerate(f):
if i == 0:
vocab_size, emb_dimension = [int(item) for item in line.split()]
# embeddings= np.zeros([vocab_size,emb_dimension])
else:
tokens = line.split()
word, vector = tokens[0], [float(num_str) for num_str in tokens[1:]]
embedding_dict[word] = vector
return embedding_dict
def read_alignment(filename="eval/yao/testset_2(1).csv"):
results = []
with open(filename) as f:
for line in f:
line = line.strip()
w1, w2 = line.split(",")
w1, y1 = w1.split("-")
w2, y2 = w2.split("-")
results.append((w1, y1, w2, y2))
return pd.DataFrame(results, columns=["w1", "y1", "w2", "y2"])
class Timer():
# time = Timer(start_year=1990)
# print(time.get_index(1992))
#
#
# time = Timer(save_path="coha.txt.raw.token.decade-output")
# print(time.get_index(1992))
mapping = {
"newsit": 2007,
"repubblica": 1984,
"nyt_yao.txt": 1986,
"nyt.txt": 1987,
"yao_tiny.txt": 1990,
"coha": 1810,
"coca": 1990,
}
def __init__(self, start_year=None, corpus=None, scale=None):
if start_year is not None:
self.start_year = start_year
elif corpus is not None:
for name, year in Timer.mapping.items():
# print(name,year)
if name in corpus:
print("bingo, found the first year of {} for {}".format(corpus, year))
self.start_year = year
else:
print("error for input")
if scale is None:
if corpus is not None and "decade" in corpus:
self.scale = 10
else:
self.scale = 1
else:
self.scale = scale
print("time scale : {}".format(scale))
def get_index(self, year):
return (year - self.start_year) // self.scale
def get_year(self,index):
return index * self.scale + self.start_year
def get_index_in_batch(self, years):
return [self.get_index(year) for year in years]
class BaseDynamicWordEmbedding:
def __init__(self, corpus ="nyt_yao_tiny.txt"):
self.timer = Timer(corpus=corpus, scale=1 if "decade" not in corpus else 10)
def encode_time(self, year):
if type(year) == str:
year = int(year)
if year > 100:
year = self.timer.get_index(year)
return year
def _get_temporal_embedding(self, word, time): # in single
raise NotImplementedError
def get_temporal_embedding(self, words, times): # in batch
if type(words) == list and type(times) == list:
embeddings = np.array([self._get_temporal_embedding(w, t) for w, t in zip(words, times)])
# from collections import Counter
# print(Counter( [ type(e) for e in embeddings]))
return embeddings
else:
return self._get_temporal_embedding(words, times)
def get_vocab(self):
raise NotImplementedError
def load_dict(self, vocab_file="pmi_vocab.txt", reversed=False):
word2id = dict()
index = -1
with open(vocab_file, encoding="utf-8") as f:
for line in f:
line = line.strip()
tokens = line.split()
if len(tokens) ==2:
word, index =tokens
else:
word = tokens[0]
index = index+1
if not reversed:
word2id[word] = int(index)
else:
word2id[int(index)] = word
return word2id
class DynamicWordEmbeddingWithCompass(BaseDynamicWordEmbedding):
def __init__(self, model_path = "model" , corpus = "yao_tiny.txt" ):
super().__init__(corpus=corpus)
COMPASS_FILE = "static.model"# "compass.model",
from gensim.models.word2vec import Word2Vec
self.embeddings = { int(filename.split(".")[0]) :Word2Vec.load(os.path.join(model_path,filename))
for filename in os.listdir(model_path) if filename != COMPASS_FILE and filename.endswith(".model")}
self.compass = Word2Vec.load(os.path.join(model_path,COMPASS_FILE))
self.vector_size = self.compass.vector_size
# for e in self.embeddings.values():
# print(len(e.wv.vocab.keys()))
# print( len(self.get_vocab()) )
# different model has different vocab
def _get_temporal_embedding(self, word, time):
# exit()
if type(time) == str:
time = int(time)
if time < 100:
time = self.timer.get_year(time)
if time not in self.embeddings:
time = self.timer.get_index(time)
if word in self.embeddings[time].wv:
return self.embeddings[time].wv[word]
else:
return np.random.randn(self.vector_size)
def get_vocab(self):
return self.compass.wv.vocab.keys()
class Compass(BaseDynamicWordEmbedding):
def __init__(self, model_path = "model" , corpus = "yao_tiny.txt" ):
super().__init__(corpus=corpus)
COMPASS_FILE = "compass.model"
from gensim.models.word2vec import Word2Vec
self.compass = Word2Vec.load(os.path.join(model_path,COMPASS_FILE))
self.vector_size = self.compass.vector_size
# for e in self.embeddings.values():
# print(len(e.wv.vocab.keys()))
# print( len(self.get_vocab()) )
# different model has different vocab
def _get_temporal_embedding(self, word, time):
if type(time) == str:
time = int(time)
if time < 100:
time = self.timer.get_year(time)
if word in self.compass.wv:
return self.compass.wv[word]
else:
return np.random.randn(self.vector_size)
def get_vocab(self):
return self.compass.wv.vocab.keys()
class DynamicWordEmbedding(BaseDynamicWordEmbedding):
def __init__(self, timer=None, path="results2", corpus="nyt_yao_tiny.txt.norm",filename = "L10T50G100A1ngU_iter4.p",vocab_file = None):
self.path = path
super().__init__(corpus=corpus)
filename = os.path.join(self.path, filename)
self.embeddings = pickle.load(open(filename, "rb"))
print("have {} years with shape {}".format(len(self.embeddings), self.embeddings[0].shape))
if vocab_file is None:
vocab_file = corpus + ".vocab"
self.word2id = self.load_dict(vocab_file)
self.id2word = self.load_dict(vocab_file, reversed=True)
def _get_temporal_embedding(self, word, time):
if type(word) != int:
word = self.word2id[word]
time = self.encode_time(time)
# print(time)
embedding = self.embeddings[time]
return embedding[word]
def get_vocab(self):
return self.word2id.keys()
class Word2Fun(BaseDynamicWordEmbedding):
def __init__(self,path="coha", epoch=None, do_prune_vocab=False,corpus="nyt_yao_tiny.txt.norm",step =None):
super().__init__(corpus=corpus)
self.id2word = self.load_vocab(os.path.join(path, "vocab.txt"), do_prune_vocab=do_prune_vocab)
self.word2id = {value: int(key) for key, value in self.id2word.items()}
if epoch is not None and step is None: # load indivusual epoch, or the last epoch
path = os.path.join(path, str(epoch))
filename = "pytorch.bin"
elif epoch is not None and step is not None:
filename = "pytorch_{}_{}.bin".format(epoch,step)
else:
filename = "pytorch.bin"
print(path,filename)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if not torch.cuda.is_available():
self.model = torch.load(os.path.join(path, filename), map_location=torch.device('cpu')).to(
self.device)
else:
self.model = torch.load(os.path.join(path, filename)).to(self.device)
self.model = self.model.module if hasattr(self.model, 'module') else self.model
self.model.eval()
def get_vocab(self):
return self.word2id.keys()
def load_vocab(self, vocob_file, base_vocab_file="./vocab.txt", do_prune_vocab=False):
id2word = dict()
with open(vocob_file, encoding="utf-8") as f:
for line in f:
_id, word = line.strip().split()
id2word[int(_id)] = word
if os.path.exists(base_vocab_file) and do_prune_vocab:
base_vocab = set()
with open(base_vocab_file, encoding="utf-8") as f:
for line in f:
_id, word = line.strip().split()
base_vocab.add(word)
raw_length = len(id2word)
id2word = {key: value for key, value in id2word.items() if value in base_vocab}
print("pruning vocab from {} to {}".format(raw_length, len(id2word)))
return id2word
def transform(self,word_tensor, time_tensor):
word_tensor = torch.LongTensor(word_tensor).to(self.device)
time_tensor = torch.LongTensor([int(y) for y in time_tensor]).to(self.device)
return word_tensor, time_tensor
def _get_temporal_embedding(self, word=None, year=0):
if word in self.word2id:
word = [self.word2id[word]]
year = [self.encode_time(year)]
word_tensor, time_tensor = self.transform(word, year)
return self.model.get_temporal_embedding(word_tensor, time_tensor).squeeze()
else:
return None
def get_temporal_embedding(self, words=None, years=0, return_unknow_words=False):
# unknow_words = [i for i, word in enumerate(words) if word not in self.word2id]
word_tensor, time_tensor = [], []
known_index = []
years =[ self.encode_time(year) for year in years]
for index, (word, year) in enumerate(zip(words, years)):
if word in self.word2id:
word_tensor.append(self.word2id[word])
time_tensor.append(year)
known_index.append(True)
else:
# print("unknown word" + word)
known_index.append(False)
word_tensor, time_tensor = self.transform(word_tensor, time_tensor)
embeddings = self.model.get_temporal_embedding(word_tensor, time_tensor) # .cpu().data.numpy()
if return_unknow_words:
return embeddings, np.array(known_index)
return embeddings
def get_sim_between_year(self, target, words=None, years=[i for i in range(1940, 2020, 1)], word2id=None,
name="nuclear"):
name += "-" + target + "_".join(words)
sims = []
words.append(target)
for year in years:
embeddings = self.get_tempory_embedding(words, year)
sim = cosine_similarity(embeddings[-1][np.newaxis, :], embeddings[:-1]).squeeze()
# print(sim.shape)
sims.append(sim)
sims = np.array(sims)
plt.figure(figsize=(10, 10))
for i in range(len(sims[0])):
plt.plot(years, sims[:, i], label=words[i])
plt.legend(loc='upper left')
if platform == "darwin_none":
plt.show()
else:
plt.savefig("{}.pdf".format(name), bbox_inches="tight", pad_inches=0)
plt.close()
def get_embedding_by_year(self, word, year):
if word not in self.word2id:
return None
else:
word_tensor = torch.LongTensor([self.word2id[word]]).to(self.device)
time_tensor = torch.LongTensor([year]).to(self.device)
embedding = self.model.get_temporal_embedding(word_tensor, time_tensor)
return embedding
# dwe = DynamicWordEmbedding(Timer(save_path="nyt_yao_tiny.txt.norm"))
# print(dwe.get_temporal_embedding(1,0))
# print(dwe.get_temporal_embedding("the",1990))
# # exit()
class Evaluator:
def __init__(self, model, timer=None, debug_mode = False):
self.model = model
self.timer= timer
self.debug_mode = debug_mode
# self.skip_gram_model,self.id2word = load_old(path,time_type)
def get_top_k_most_similar_words_in_specific_time(self, words, year, k=3):
if year > 30:
year = self.timer.get_index(year)
all_vectors = self.get_tempory_embedding(self.word2id.keys(), year=year)
# embeddings_vectors = np.array( [vector for word,vector in embeddings])
# all_words = [word for word,vector in embeddings]
not_found_words = [word for word in words if word not in self.word2id]
if len(not_found_words) > 0:
print("do not find {}".format(" ".join(not_found_words)))
words_index = [self.word2id[word] for word in words if word in self.word2id] ## index of in-vocab words
selected_vectors = np.array([all_vectors[index] for index in words_index])
a = np.dot(selected_vectors, all_vectors.T) # /np.norm()
# a = cosine_similarity(selected_vectors,embeddings_vectors)
top_k = a.argsort()[:, -1 * k:] # [::-1]
# top_k = np.partition(a, -3)
words_str = [" ".join([self.id2word[word] for word in top_k_per_word[::-1]]) for top_k_per_word in top_k]
return words_str
def word_change_rate(self, words, years=30):
vectors = []
for year in range(years):
word2id = {value: key for key, value in self.id2word.items()}
embeddings_vectors = self.get_tempory_embedding(self.embedding_dict.keys(), word2id=word2id, year=year)
# embeddings_vectors = np.array( [vector for word,vector in embeddings])
# all_words = [word for word,vector in embeddings]
words_index = [word2id[word] for word in words]
# print(words_index)
selected_vectors = np.array([embeddings_vectors[word] for word in words_index])
vectors.append(selected_vectors)
for j in range(len(words)):
change_rates = []
for year in range(years):
if year == 0:
cur_vector = vectors[year][j]
else:
# change_rate = np.dot(cur_vector,vectors[year][j])
change_rate = scipy.spatial.distance.cosine(cur_vector, vectors[year][j])
cur_vector = vectors[year][j]
change_rates.append(change_rate)
print(words[j], np.mean(np.array(change_rates)))
print(change_rates)
return
def plot_words_in_many_years(self, words=None, years=[i for i in range(1977, 2020, 1)], word2id=None, name="image"):
if words is None:
words = ["president", "reagan", "trump", "biden", "obama", "bush", "carter", "clinton", "ford", "nixon"]
# words = ["weapon" , "nuclear", "energy"]
if word2id is None:
word2id = {value: key for key, value in self.id2word.items()}
vectors = []
names = []
for year in years:
names.extend(["{}-{}".format(word, year) for word in words])
embeddings = self.get_tempory_embedding(words, year, word2id)
vectors.extend(embeddings)
embed = TSNE(n_components=2).fit_transform(vectors)
# print(embed.shape)
plt.figure(figsize=(12, 12))
# from adjustText import adjust_text
texts = []
for i, point in enumerate(embed):
plt.scatter(point[0], point[1], label=names[i])
texts.append(plt.text(point[0], point[1], names[i], size=7))
# plt.plot(embed[:,0],embed[:,1],names)
# adjust_text(texts)
# plt.legend()
if platform == "win32":
plt.show()
else:
plt.savefig("president-{}.pdf".format(name), bbox_inches="tight", pad_inches=0)
plt.close()
# plt.show()
def check_ssd_driver(self):
from data.ssd import Helper
helper = Helper("data/grade.txt", tims_scale=10 )
from scipy.spatial.distance import cosine # cosine distance
helper.adapt(self.model.get_vocab())
words = helper.words
time_stamped_embeddings = []
for timespan in helper.timespans:
all_embeddings = [self.model.get_temporal_embedding(words, [year ]* len(words)) for year in timespan]
mean_embedding = np.mean(np.array(all_embeddings), 0)
time_stamped_embeddings.append(mean_embedding)
assert len(time_stamped_embeddings) == 2, "more timespans than two"
scores = [cosine(time_stamped_embeddings[0][i], time_stamped_embeddings[1][i]) for i, word in enumerate(words)]
return helper.evaluate(scores)
def get_sim_words_diver(self, words, years, real_years, k=100, log_filename="sim_log.txt"):
simwords = []
for year in years:
simwords.append(self.get_top_k_most_similar_words_in_specific_time(words=words, year=year, k=k))
lines = []
with open(log_filename, "w", encoding="utf-8") as f:
for row in range(len(simwords[0])):
line = [real_years[i] + " : " + simword[row] for i, simword in enumerate(simwords)]
print(line)
lines.extend(line)
f.write("\n".join(lines))
return
def semantic_sim_driver(self, log_filename="yao_test1.txt"): # time_mapping
df = pd.read_csv("eval/yao/testset_1.csv")
# df.real_year = df.year.apply(lambda x: int(self.timer.get_index(int(x))))
# print(df.real_year.unique(),df.year.unique())
df = df[df.word.isin(self.model.get_vocab()) ].reset_index()
labels = set(df.label.unique())
labels_mapping = {label: index for index, label in enumerate(labels)}
df.label_id = df.label.apply(lambda x: labels_mapping[x])
# print(df.label_id)
embeddings = self.model.get_temporal_embedding(df.word.tolist(), df.year.tolist())
from spherecluster import SphericalKMeans
scores = []
for n in [10, 15, 20]:
skm = SphericalKMeans(n_clusters=n)
skm.fit(embeddings)
# print(skm.labels_.shape)
# print(len(df.label_id[known_index]))
# print(sum(known_index))
score = get_score(skm.labels_, df.label_id)
score1 = get_score1(skm.labels_, df.label_id)
scores.append(score)
scores.append(score1)
# print(scores)
with open(log_filename, "w", encoding="utf-8") as f:
line = "\t&".join(["{0:.4f}".format(s) for s in scores]) + "\n"
print(line)
f.write(line)
return None
def alignment_quality_driver(self, log_filename="alignment_quality.log", do_normarlization = True):
df1 = read_alignment("eval/yao/testset_2(1).csv")
df2 = read_alignment("eval/yao/testset_2(2).csv")
dfs = [df1, df2]
if self.debug_mode:
df3 = df1[df1.w2==df1.w1].reset_index()
df4 = df1[df1.w2 != df1.w1].reset_index()
# df5 = df2[df2.w2 == df2.w1].reset_index()
# df6 = df2[df2.w2 != df2.w1].reset_index()
dfs.extend( [df3,df4])
with open(log_filename, "w", encoding="utf-8") as f:
for df in dfs:
# df.y1 = df.y1.apply(lambda x: int(self.timer.get_index(int(x))))
# df.y2 = df.y2.apply(lambda x: int(self.timer.get_index(int(x))))
length = len(df)
df = df[df.w1.isin(self.model.get_vocab()) & df.w2.isin(self.model.get_vocab())].reset_index()
print(" {} rows with valid ones counted {}".format(length, len(df)))
# sources = df["w1"].unique()
targets = [word for word in self.model.get_vocab()]
targets_dict = {target: i for i, target in enumerate(targets)}
id2word = {i: target for i, target in enumerate(targets)}
timed_embeddings = dict()
for year in df.y2.unique():
timed_embeddings.setdefault(year, self.model.get_temporal_embedding(targets, [year] * len(targets)) )
if do_normarlization:
norm = np.linalg.norm(timed_embeddings[year], axis =-1 )
dim = len(timed_embeddings[year][0])
z = np.repeat(norm[:, np.newaxis], dim, axis=1)
timed_embeddings[year] = timed_embeddings[year] / z
p1, mr, p3, p5, p10 = [], [], [], [], []
for i, row in tqdm(df.iterrows()):
embedding = self.model._get_temporal_embedding(row.w1, row.y1)
if do_normarlization:
embedding = embedding / np.linalg.norm(embedding)
# timed_embeddings.setdefault(row.y2,self.get_embedding_in_a_year(targets, [row.y2]* len(targets), return_known_index=False))
candicates = timed_embeddings[row.y2]
# print(embedding.shape, candicates.shape)
ranking_scores = np.dot(embedding, candicates.transpose())
# print(np.max(ranking_scores))
ranks = np.argsort(ranking_scores)[::-1]
# print(ranks.shape)
target = targets_dict[row.w2]
# print(row.y1, row.y2, row.w1, row.w2,target)
# print(ranks)
first_index = -1
for index, rank in enumerate(ranks):
if rank == target:
first_index = index
break
assert first_index != -1, "wrong for calculating MRR"
p1.append(1 if first_index == 0 else 0)
p3.append(1 if first_index < 3 else 0)
p5.append(1 if first_index < 5 else 0)
p10.append(1 if first_index < 10 else 0)
mr.append(1 / (first_index + 1))
if self.debug_mode and i <2:
print([id2word[r] for r in ranks[:10]])
print(row.w1, row.y1, row.y2, row.w2,first_index)
scores = [np.mean(s) for s in (mr, p1, p3, p5, p10)]
# print(scores)
# exit()
line = "\t&".join(["{0:.4f}".format(s) for s in scores])
print(line)
f.write(line + "\n")
check_list = [("president", ["nixon", "ford", "carter", "reagan", "clinton", "bush", "obama", "trump", "biden"]),
("olympic",
["moscow", "los", "angeles", "seoul", "barcelona", "atlanta", "sydney", "athens", "beijing", "london",
"rio", "tokyo"]),
("nuclear", ["technology", "threaten", "america", "russian", "cuba", "green", "energy", "china"]),
("nuclear", ["russian", "japan", "weapon", "energy", "ukrainian", "soviet"]),
("olympic", ["sydney", "athens", "beijing", "london", "rio", "tokyo"]),
("president", ["clinton", "bush", "obama", "trump", "biden"]),
]
def draw_figure():
for output in ["coha.txt.raw.token-output/", "coca.txt.raw.token-output/", "arxiv.txt.raw.token-output/"]:
if "coca" in output:
years = [i - 1990 for i in range(1990, 2020, 1)]
else:
years = [i - 1810 for i in range(1810, 2020, 1)]
for time_type in ["word_mixed_fixed", "word_cos"]: # "word_cos",
for epoch in range(1, 10, 1):
args.iterations = epoch
try:
checker = Word2VecChecker(path=output, time_type=time_type)
for target, checked_words in check_list:
# checker.plot_words_in_many_years(words=[target] + checked_words[-9:], years=years,
# name="{}-{}".format(output.split(".")[0], time_type))
checker.get_sim_between_year(target, checked_words[-9:],
name="{}-{}-{}-".format(output.split(".")[0], time_type, epoch),
years=years)
except Exception as e:
print(e)
timetypes = ["cos", "linear_shift", " mixed_shift", "sin_shift", "word_cos", "word_linear_shift", "word_mixed_fixed",
"word_mixed_shift", "word_sin_shift",
"cos_shift mixed", "others_shift", "word2vec", "word_cos_shift", "word_mixed", "word_mixed_fixed_shift",
"word_sin"]
def get_frequencies(model_path="nyt_yao.txt.train-output", timetypes=[], epoch=None, do_prune_vocab=True):
for time_type in timetypes: # "word_cos", , "word_cos"
# for epoch in range(1, 10, 1):
save_filename = "sim_word_{}_{}_{}\n".format(model_path, time_type, epoch)
model_save_path = os.path.join(model_path, time_type)
checker = Word2VecChecker(model_save_path, epoch=epoch, do_prune_vocab=do_prune_vocab)
print(checker.model)
# print(checker.model.time_encoder.para_embedding.weight)
print(checker.model.time_encoder.para_embedding.weight.abs().mean())
def old_test():
# yao_test(model_path=files[0], timetypes=["word_mixed_amplitude"], epoch=19)
# files = ["nyt_yao_tiny.txt.norm.train-waby-300"]
# yao_test(model_path=files[0], timetypes=["word_mixed_fixed"], epoch=12)
files = ["nyt_yao_tiny.txt.norm-waby_amplitude-100", "nyt_yao_tiny.txt.norm.train-waby-300"]
if not torch.cuda.is_available():
files = ["models/{}".format(filename) for filename in files]
yao_test(model_path=files[0], timetypes=["word_mixed_amplitude"], epoch=19)
yao_test(model_path=files[1], timetypes=["word_mixed_fixed"], epoch=12)
words = ["apple", "amazon", "dna", "innovation", "data", "app", "twitter", "ranking", "quantum", "nuclear",
"weapon", "president", "chairman", "soviet", "reagan", "trump", "biden", "obama", "olympic", "olympics",
"china", "america", "ai", "artificial", "intelligence", "neural", "network", "language", "model", "wto",
"media", "software", "computer", "car", "driving",
"information", "retrieval"] + ["iphone", "mp3"] + ["guy", "gay", "program", "america", "television", "stock",
"european", "euro", "best-seller", "phone"] \
+ ["wi-fi", "browser", "android", "huawei", "cpu", "microsoft", "xbox", "pc"]
def case_studies_nyt():
years = [i - 1990 for i in range(1990, 2017, 1)]
real_years = [str(i) for i in range(1990, 2017, 1)] # 1986
files = ["nyt_yao_tiny.txt.norm-waby_amplitude-100", "nyt_yao_tiny.txt.norm.train-waby-300"]
if not torch.cuda.is_available():
files = ["models/{}".format(filename) for filename in files]
checker = Word2VecChecker(os.path.join(files[0], "word_mixed_amplitude"), epoch=19)
# checker = Word2VecChecker(os.path.join(files[1], "word_mixed_fixed"), epoch=12)
checker.get_sim_words_diver(words, years, real_years)
# checker = Word2VecChecker(os.path.join(files[0], "word_mixed_amplitude"), epoch=19)
checker = Word2VecChecker(os.path.join(files[1], "word_mixed_fixed"), epoch=12)
checker.get_sim_words_diver(words, years, real_years, log_filename="sim_log1.txt")
def case_studies():
years = [(i - 1810) // 10 for i in range(1810, 2020, 10)]
real_years = [str(i) + "s" for i in range(1810, 2020, 10)] # 1986
files = ["nyt_yao_tiny.txt.norm-waby_amplitude-100", "nyt_yao_tiny.txt.norm.train-waby-300"]
if not torch.cuda.is_available():
files = ["models/{}".format(filename) for filename in files]
checker = Word2VecChecker(os.path.join(files[0], "word_mixed_amplitude"), epoch=19)
# checker = Word2VecChecker(os.path.join(files[1], "word_mixed_fixed"), epoch=12)
checker.get_sim_words_diver(words, years, real_years)
# checker = Word2VecChecker(os.path.join(files[0], "word_mixed_amplitude"), epoch=19)
checker = Word2VecChecker(os.path.join(files[1], "word_mixed_fixed"), epoch=12)
checker.get_sim_words_diver(words, years, real_years, log_filename="sim_log1.txt")
def main():
timetypes = ["word_mixed_fixed"] # "word_cos", "word_linear", "word_mixed","word_mixed_fixed","word_sin"
files = ["nyt_yao_tiny.txt-20-nodecay-output", "nyt_yao_tiny.txt-20-100dim-output",
"nyt_yao_tiny.txt-20-half-lr-output",
"nyt_yao_tiny.txt-20-half-batchsize-output"] # , "nyt_yao_tiny.txt-20-phase-output"
files = ["nyt_yao_tiny.txt.norm-200-count-output",
"nyt_yao_tiny.txt.norm-200output"] # , "nyt_yao_tiny.txt-20-phase-output"
# for file in files:
# yao_test(model_path=file, timetypes=["word_mixed_fixed"])
# for file
# yao_test("coha",timetypes=["word_mixed_fixed"] )
# exit()
for file in files:
for epoch in range(5):
# ssd_test("coha.txt.raw.token.train-decade-output",timetypes=timetypes,epoch=epoch)
get_frequencies(model_path=file, timetypes=["word_mixed_amplitude"], epoch=epoch, do_prune_vocab=False)
# yao_test(model_path="nyt_yao.txt.train-output", timetypes=["word_mixed_fixed"])
# for epoch in range(5):
# yao_test(model_path="nyt_yao_tiny.txt.norm.train-output",timetypes=["word_mixed_fixed"], epoch=epoch)
# #, "word_mixed"
# for epoch in range(5):
# yao_test(model_path="nyt_yao.txt.train-output",timetypes=["word_mixed_fixed"], epoch=epoch)
#
# exit()
# models/nyt_yao_tiny.txt.norm-waby_decay-10 13 0.538
# models/nyt_yao_tiny.txt.norm-waby_decay-1 16 0.55 +
# nyt_yao_tiny.txt.norm.train-waby-300 12 0.60
# models/nyt_yao_tiny.txt.norm.train-waby1-300\word_mixed_fixed\9 0.5822
def main():
# yao_test(model_path="models/nyt_yao_tiny.txt.norm-waby_decay-1",timetypes = ["word_mixed_fixed"] )
# yao_test(model_path="models/nyt_yao_tiny.txt.norm-waby_decay-1",timetypes = ["word_mixed_fixed"] ,epoch = 12,do_prune_vocab =False)
# yao_test(model_path="models/nyt_yao_tiny.txt.norm-waby_decay-1",timetypes = ["word_mixed_fixed"] ,epoch = 13,do_prune_vocab =True)
# exit()
# yao_test(model_path="models/nyt_yao_tiny.txt.norm-waby_amplitude-100_decay10",timetypes = ["word_mixed_amplitude"] ,do_prune_vocab =True)
files = ["nyt_yao_tiny.txt.norm-waby_amplitude-100_decay100", "nyt_yao_tiny.txt.norm-waby_amplitude-100_decay10",
"nyt_yao_tiny.txt.norm-waby_amplitude-100", "nyt_yao_tiny.txt.norm.train-waby_amplitude-100"]
files = ["nyt_yao_tiny.txt.norm-word2fun"]
# files = [ "nyt_yao_tiny.txt.norm.train-output20",
# "nyt_yao_tiny.txt.norm.train-output20-50", "nyt_yao_tiny.txt.norm.train-waby-50","nyt_yao_tiny.txt.norm.train-waby-300","nyt_yao_tiny.txt.norm.train-waby-100","nyt_yao_tiny.txt.norm.train-waby-200"]
# for file in files:
# # get_frequencies(model_path="models/{}".format(file),timetypes = ["word_mixed_amplitude"], do_prune_vocab =True)
# path = "models" if not torch.cuda.is_available() else "."
# yao_test(model_path="{}/{}".format(path,file),timetypes = [ "word_linear", "word_mixed", "word_mixed_amplitude_shift", "word_mixed_fixed_no_amplitude", "word_mixed_fixed_shift", "word_mixed_shift", ], do_prune_vocab =True)#"time2vec_shift",
# continue
files = ["nyt_yao_tiny.txt.norm-best", "nyt_yao_tiny.txt.norm-best2", "nyt_yao_tiny.txt.norm-best-10",
"nyt_yao_tiny.txt.norm-best2-10"]
# yao_test("coha", timetypes=["word_mixed"], do_prune_vocab=True)
# exit()
files = [
# "nyt_yao_tiny.txt.norm-best.0025-2",
"nyt_yao_tiny.txt.norm-best.001-1",
# "nyt_yao_tiny.txt.norm-best.0025-1",
"nyt_yao_tiny.txt.norm-best.001-2"
]
files = ["nyt_yao_tiny.txt.norm-best.0-100", "nyt_yao_tiny.txt.norm-best.1-100", "nyt_yao_tiny.txt.norm-best.2-100"]
files = [
# "nyt_yao_tiny.txt.norm-word2fun1_1000-50",
# "nyt_yao_tiny.txt.norm-word2fun4_1000-50",
# "nyt_yao_tiny.txt.norm-word2fun16_1000-50",
# "nyt_yao_tiny.txt.norm-word2fun1_10000-50",
# "nyt_yao_tiny.txt.norm-word2fun4_10000-50",
# "nyt_yao_tiny.txt.norm-word2fun16_10000-50",
"nyt_yao_tiny.txt.norm-word2fun1_1000",
# "nyt_yao_tiny.txt.norm-word2fun4_1000",
# "nyt_yao_tiny.txt.norm-word2fun16_1000",
"nyt_yao_tiny.txt.norm-word2fun1_10000",
# "nyt_yao_tiny.txt.norm-word2fun4_10000",
# "nyt_yao_tiny.txt.norm-word2fun16_10000",
# "nyt_yao_tiny.txt.norm-word2fun1_1000-200",
# "nyt_yao_tiny.txt.norm-word2fun4_1000-200",
# "nyt_yao_tiny.txt.norm-word2fun16_1000-200",
# "nyt_yao_tiny.txt.norm-word2fun1_10000-200",
# "nyt_yao_tiny.txt.norm-word2fun4_10000-200",
# "nyt_yao_tiny.txt.norm-word2fun16_10000-200",
]
files = ["nyt_yao_tiny.txt.norm-word2fun"]
if not torch.cuda.is_available():
files = ["models/{}".format(filename) for filename in files]
# for filename in files:
# yao_test(filename, timetypes=["word_mixed_amplitude"], do_prune_vocab=False)
# print("***"*200)
# exit()
for filename in files:
yao_test(filename, timetypes=["time2vec_shift"], epoch=4, do_prune_vocab=True)
# continue
exit()
for epoch in range(5, 20):
yao_test(filename, timetypes=["word_mixed_fixed"], epoch=epoch, do_prune_vocab=True)
# # yao_test(model_path="models/nyt_yao_tiny.txt.norm-waby_decay-1",timetypes = ["word_mixed_fixed"] ,epoch = epoch,do_prune_vocab =False)
# print("_______"*20)
def main(path="nyt_yao_tiny.txt.norm-word2fun1_10000-400-real", timetypes=None):
if not torch.cuda.is_available():
path = "models/{}".format(path)
if timetypes is None:
timetypes = [timetype for timetype in os.listdir(path)]
yao_test(path, timetypes=timetypes, do_prune_vocab=True)
def load_yao_dynamic_embedding(input_path = "D:/codes/dynamicW2v/embeddings" ,output_file = "dyn.pkl"):
import scipy.io as sio
import os
import pickle
embeds = []
path = os.path.join (input_path, "embeddings")
for i in range(27): # the last embedding is fake, which is copied from ours
filename = "embeddings_{}".format(i)
filename = os.path.join(path, filename)
print(filename)
a = sio.loadmat(filename)["U"]
print(a.shape)
embeds.append(a)
pickle.dump(embeds, open(output_file, "wb"))
return output_file
# def main(path,timetypes):
# # path ="nyt_yao_tiny.txt.norm"
# # timetypes= None
# ssd_test(path, timetypes=timetypes,use_yao=True)
# # yao_test(path, timetypes=timetypes, do_prune_vocab=True,use_yao=True)
def alignment_quality():
word2fun1 = Word2Fun(os.path.join("models","nyt_yao_tiny.txt.norm-waby_amplitude-100", "word_mixed_amplitude"),epoch=19)
word2fun2 = Word2Fun(os.path.join("models","nyt_yao_tiny.txt.norm.train-waby-300", "word_mixed_fixed"),epoch=12)
vocab_file = "./nyt.wordid,txt"
dwe = DynamicWordEmbedding( corpus="yao_tiny.txt",path="results507-50-batchsize-512",filename="L10T50G50A1ngU_iter4.p",vocab_file=vocab_file)
# vocab_file = "nyt.wordid,txt" # loading D:\codes\dynamicW2v/read_embedding.py
dwe1 = DynamicWordEmbedding(path="./", corpus="nyt_yao_tiny.txt.norm", filename="dyn.pkl", vocab_file=vocab_file)
compass = DynamicWordEmbeddingWithCompass(model_path="model-nyt")
compass1 = DynamicWordEmbeddingWithCompass(model_path="model_nyt-1")
static_compass = Compass(model_path="model-nyt")
models = [compass1 , static_compass,word2fun1,word2fun2,dwe,dwe1,compass]
# models = [static_compass]
#
for model in models:
print(model)
evaluator = Evaluator(model)
evaluator.alignment_quality_driver()
evaluator.check_ssd_driver()
def ssd():
aw2v = DynamicWordEmbedding(corpus="coha.txt.norm", path="./",
filename="coha-aw2v.pkl")
evaluator = Evaluator(aw2v)
print(evaluator.check_ssd_driver())
def eval_yao_test(path = "nyt_yao_tiny.txt.norm-word2fun-42-1e-11" , time_type = "word_mixed_amplitude",debug_mode= False):
for epoch in range(15,50):
model = Word2Fun(os.path.join("models", path, time_type),
epoch=epoch, do_prune_vocab=False)
evaluator = Evaluator(model, debug_mode=debug_mode)
evaluator.alignment_quality_driver()
for step in range(1,3):
step = step*150-1
model = Word2Fun(os.path.join("models", path, time_type),
epoch=epoch,step =step,do_prune_vocab =False)
evaluator = Evaluator(model, debug_mode = debug_mode)
evaluator.alignment_quality_driver()
def yao_test_ours(path = "nyt_yao_tiny.txt.norm-word2fun-42-1e-11" , time_types = ["word_mixed_fixed", "time2vec", "word_mixed_amplitude", "word_mixed_amplitude_shift", ],debug_mode= False):
for time_type in time_types:#
for epoch in range(20,35):
for step in range(1,2):
step = step*200-1
model = Word2Fun(os.path.join("models", path, time_type),
epoch=epoch,step =step,do_prune_vocab =False)
evaluator = Evaluator(model, debug_mode = debug_mode)
evaluator.alignment_quality_driver()
def demo():
model = Word2Fun(os.path.join("models", "demo"), do_prune_vocab=False)
evaluator = Evaluator(model, debug_mode=False)
evaluator.alignment_quality_driver()
def test_compass():
# compass = DynamicWordEmbeddingWithCompass(model_path="model-coha",corpus="coha.txt.raw.token.decade-output")
#
# evaluator = Evaluator(compass, debug_mode=True)
#
# print(evaluator.check_ssd_driver())
compass = DynamicWordEmbeddingWithCompass(model_path="model-nyt-50")
evaluator = Evaluator(compass, debug_mode=True,timer = Timer())
# evaluator.semantic_sim_driver()
evaluator.alignment_quality_driver()
def compare(debug_mode = False):
compass = DynamicWordEmbeddingWithCompass(model_path="model-nyt")
evaluator = Evaluator(compass, debug_mode=debug_mode)
# evaluator.semantic_sim_driver()
# evaluator.alignment_quality_driver(do_normarlization= False)
evaluator.alignment_quality_driver(do_normarlization=True)
word2fun1 = Word2Fun(os.path.join("models", "nyt_yao_tiny.txt.norm-waby_amplitude-100", "word_mixed_amplitude"),
epoch=19) #,do_prune_vocab=True
evaluator = Evaluator(word2fun1, debug_mode=debug_mode)
evaluator.semantic_sim_driver()
# evaluator.alignment_quality_driver(do_normarlization=True)
# evaluator.alignment_quality_driver(do_normarlization=False)
def test_align_in_compase():
# use genral gensim could load it properly
from gensim.models.word2vec import Word2Vec
model1 = Word2Vec.load("model-nyt/1990.model")
model2 = Word2Vec.load("model-nyt/2015.model")
print(sum(model1.syn1neg[model1.wv.vocab["software"].index][:10]))
print(sum(model2.syn1neg[model2.wv.vocab["software"].index][:10]))
print(model1.most_similar("apple"))
print(model2.most_similar("apple"))
def test_performance(path = "nyt_yao_tiny.txt.norm-word2fun", predix = "./" ):
for time_type in ["word_mixed_fixed", "time2vec", "word_mixed_amplitude_shift", "word_linear" ]:
for epoch in range(15,19):
word2fun1 = Word2Fun(
os.path.join(predix, path, time_type), epoch=epoch)
evaluator = Evaluator(word2fun1, debug_mode=True)
# evaluator.semantic_sim_driver()
evaluator.alignment_quality_driver(do_normarlization=True)
def parameter_word2fun():
file = "coha.txt.raw.token-word2fun"
file = "coha-word2fun"
for epoch in [9]:
model = Word2Fun(os.path.join(file,"word_mixed_amplitude"),do_prune_vocab=False,epoch=epoch )
print(model)
avg = model.model.time_encoder.para_embedding.weight.abs().mean(-1).cpu()
values, indexs = avg.topk(k=100,largest =True)
print([ model.id2word[i] for i in indexs.numpy() ])
# avg = model.model.time_encoder.para_embedding.weight.abs().mean(-1).cpu()
# avg = model.model.time_encoder.amplitude_embedding.weight.abs().mean(-1).cpu()
avg = model.model.u_embeddings.weight.abs().mean(-1).cpu() *-1
# avg = model.model.time_encoder.amplitude_embedding.weight.abs().mean(-1).cpu()/
values, indexs = avg.topk(k=100, largest=False)
print([model.id2word[i] for i in indexs.numpy()])
words, scores =[],[]
for line in open("data/grade.txt"):
word, score = line.split()
word = word.split("_")[0]