forked from kaisugi/hyperdoc2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrope2vec.py
149 lines (121 loc) · 4.56 KB
/
trope2vec.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
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from gensim.models.word2vec import Word2Vec
from nltk.tokenize import word_tokenize
import argparse
from distutils.util import strtobool
import logging
logging.basicConfig(level=logging.DEBUG)
import os
import sys
import json
from util import *
from datetime import datetime
SEED = 42
VDIM = 200 # dimension of vectors
WSIZE = 50 # window size
def prepare_datasets(references, summaries):
limit = 100
summary_data = []
with open(summaries, "r") as descriptor:
data = json.loads(descriptor.read())
length = len(list(data.keys()))
index = 0
for key in data:
summary_data.append(TaggedDocument(words=word_tokenize(data[key]), tags=[key]))
index += 1
if (index%limit == 0):
now = datetime.now()
print(now.strftime("%d-%m-%Y@%H:%M:%S"))
print(key+" @ "+str(index)+" / "+str(length))
logging.debug(summary_data[:int(float(limit)**0.5)])
reference_data = []
with open(references, "r") as descriptor:
data = json.loads(descriptor.read())
length = len(list(data.keys()))
index = 0
for key in data:
reference = data[key]
for context in reference:
destinations = reference[context]
tokens = word_tokenize(context)
for destination in destinations:
reference_data.append(list(flatten([key, destination, tokens])))
index += 1
if (index%limit == 0):
now = datetime.now()
print(now.strftime("%d-%m-%Y@%H:%M:%S"))
print(key+" @ "+str(index)+" / "+str(length))
return summary_data, reference_data
# initialize document vector with pv-dm (retrofitting mode)
def pretraining(summary_data):
if not os.path.exists("./models/pv-dm.txt"):
model = Doc2Vec(
vector_size=VDIM,
window=WSIZE,
epochs=5,
dm=1, # use pv-dm
workers=1, # to ensure reproducibility
seed=SEED
)
model.build_vocab(summary_data, min_count=1)
logging.info("--- Pre-training started. ---")
model.train(
summary_data,
epochs=model.epochs,
total_examples=model.corpus_count
)
logging.info("--- Pre-training ended. ---")
# save pre-trained doc2vec as word2vec format file
with open("./models/pv-dm.txt", "w") as descriptor:
descriptor.write(f"{len(summary_data)} {VDIM}\n")
for k in model.docvecs.doctags:
descriptor.write(f"{k} {' '.join([str(i) for i in model[k].tolist()])}\n")
logging.info("--- Pre-trained doc2vec saved. ---")
# hyperdoc2vec
def training(reference_data, retrofit):
if ((retrofit and not os.path.exists("./models/t2v-retrofit.model"))
or (not retrofit and not os.path.exists("./models/t2v-random.model"))):
model = Word2Vec(
size=VDIM,
window=WSIZE,
negative=1000,
sg=0, # use CBOW model
cbow_mean=1, # use average vector
workers=10, # to ensure reproducibility
seed=SEED
)
model.build_vocab(reference_data, min_count=1)
if retrofit:
model.intersect_word2vec_format("./models/pv-dm.txt", lockf=1.0, binary=False)
logging.info("--- Training started. ---")
model.train(
reference_data,
epochs=100,
total_examples=model.corpus_count
)
logging.info("--- Training ended. ---")
type_string = "retrofit" if retrofit else "random"
model.save(f"./models/t2v-{type_string}.model")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--retrofit', default=str(True))
parser.add_argument('--references')
parser.add_argument('--summaries')
args = parser.parse_args()
retrofit = strtobool(args.retrofit)
references = args.references
summaries = args.summaries
summary_data, reference_data = prepare_datasets(references, summaries)
#sys.exit(0)
if retrofit:
pretraining(summary_data)
training(reference_data, retrofit)
type_string = "retrofit" if retrofit else "random"
model = Word2Vec.load(f"./models/t2v-{type_string}.model")
#print("IN vectors")
#print(len(model.wv.vectors))
#print(model.wv.vocab)
#print(model.wv.vectors)
print("OUT vectors")
print(len(model.trainables.syn1neg))
print(model.trainables.syn1neg)