-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords_viz.py
134 lines (109 loc) · 4.68 KB
/
words_viz.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
import argparse
import jsonlines
import torch
from tqdm import tqdm
from coref.coref_model2 import CorefModel
from coref.tokenizer_customization import *
from coref import bert, conll, utils
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
# python words_viz.py roberta --weights 'data_to_be_converted_back/roberta_(e30_2023.06.12_13.48).pt' data_to_be_converted_back/english_test_head.jsonlines
def tsne_plot(labels, tokens, docname):
"Creates and TSNE model and plots it"
# labels = []
# tokens = []
# for word in model.wv.vocab:
# tokens.append(model[word])
# labels.append(word)
tsne_model = TSNE(perplexity=40, n_components=2, init='pca', n_iter=2500, random_state=23)
new_values = tsne_model.fit_transform(tokens)
x = []
y = []
for value in new_values:
x.append(value[0])
y.append(value[1])
plt.figure(figsize=(16, 16))
for i in range(len(x)):
plt.scatter(x[i],y[i])
plt.annotate(labels[i],
xy=(x[i], y[i]),
xytext=(5, 2),
textcoords='offset points',
ha='right',
va='bottom')
print('hi')
plt.savefig(f"{docname}.png")
# plt.show()
def build_doc(doc: dict, model: CorefModel) -> dict:
filter_func = TOKENIZER_FILTERS.get(model.config.bert_model,
lambda _: True)
token_map = TOKENIZER_MAPS.get(model.config.bert_model, {})
word2subword = []
subwords = []
word_id = []
for i, word in enumerate(doc["cased_words"]):
tokenized_word = (token_map[word]
if word in token_map
else model.tokenizer.tokenize(word))
tokenized_word = list(filter(filter_func, tokenized_word))
word2subword.append((len(subwords), len(subwords) + len(tokenized_word)))
subwords.extend(tokenized_word)
word_id.extend([i] * len(tokenized_word))
doc["word2subword"] = word2subword
doc["subwords"] = subwords
doc["word_id"] = word_id
doc["head2span"] = []
if "speaker" not in doc:
doc["speaker"] = ["_" for _ in doc["cased_words"]]
doc["word_clusters"] = []
doc["span_clusters"] = []
doc['cluster_emb'] = []
doc["span_clusters_res"] = []
return doc
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("experiment")
argparser.add_argument("input_file")
# argparser.add_argument("output_file")
argparser.add_argument("--config-file", default="config.toml")
argparser.add_argument("--batch-size", type=int,
help="Adjust to override the config value if you're"
" experiencing out-of-memory issues")
argparser.add_argument("--weights",
help="Path to file with weights to load."
" If not supplied, in the latest"
" weights of the experiment will be loaded;"
" if there aren't any, an error is raised.")
args = argparser.parse_args()
model = CorefModel(args.config_file, args.experiment)
if args.batch_size:
model.config.a_scoring_batch_size = args.batch_size
model.load_weights(path=args.weights, map_location="cpu",
ignore={"bert_optimizer", "general_optimizer",
"bert_scheduler", "general_scheduler"})
model.training = False
with jsonlines.open(args.input_file, mode="r") as input_data:
docs = [build_doc(doc, model) for doc in input_data]
# building the cluster embeddings
with torch.no_grad():
for doc in tqdm(docs, unit="docs"):
labels = []
tokens = []
result, word_emb = model.run(doc)
print(doc['document_id'])
doc["span_clusters_res"] = result.span_clusters
for cluster in doc["span_clusters_res"]:
for span in cluster:
start, end = span
span_str = ''
span_embedding = None
for i in range(start, end):
span_str = span_str + " " + doc['cased_words'][i]
if(span_embedding == None):
span_embedding = word_emb[i]
else:
span_embedding += word_emb[i]
span_embedding /= (end - start)
tokens.append(span_embedding.cpu().detach().numpy())
labels.append(span_str)
tsne_plot(labels, tokens, doc['document_id'])