-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_embeddings.py
72 lines (64 loc) · 2.19 KB
/
generate_embeddings.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
import argparse
import json
import os
from models import HFmodel, miniLMSPECTER, scibertSPECTER
parser = argparse.ArgumentParser()
parser.add_argument("--model", help="path to the pretrained model")
parser.add_argument(
"--paper_data", help="path to the paper metadata json file", required=True
)
parser.add_argument(
"--output",
help="path to the file where the embeddings will be written to",
required=True,
)
parser.add_argument("--batch_size", help="batch_size", type=int, default=2)
parser.add_argument(
"--pooling",
default=None,
type=str,
help="pooling mechanism during training (mean, cls, pretrain).",
)
parser.add_argument(
"--sep",
default=None,
type=str,
help="set to 'tokenizer' to use the tokenizer sep token, otherwise use a space instead",
)
args = parser.parse_args()
if os.path.exists(args.model):
try:
model = miniLMSPECTER(model_path=args.model, pooling=args.pooling, sep=args.sep)
except:
model = scibertSPECTER(
model_path=args.model, pooling=args.pooling, sep=args.sep
)
else:
model = HFmodel(model_path=args.model, pooling=args.pooling, sep=args.sep)
d = None
with open(args.paper_data) as file: # "../scidocs/data/paper_metadata_mag_mesh.json"
d = json.load(file)
print("Extracting papers...", end=" ")
papers = []
for paper_id, paper_metadata in d.items():
paper = {}
paper["paper_id"] = paper_id
paper["title"] = paper_metadata["title"]
paper["text"] = paper_metadata["abstract"] # rename abstract key into "text"
papers.append(paper)
print("Done")
print(f"Extracted {len(papers)} papers")
print("Computing embeddings...")
embeddings = model.encode_corpus(corpus=papers, batch_size=args.batch_size)
print("Finished computing embeddings")
print("Writing to file...")
with open(args.output, "a") as file: # "mag_mesh_embed.jsonl"
for j in range(embeddings.shape[0]):
paper_data = papers[j]
paper_data["abstract"] = paper_data.pop(
"text"
) # scidocs embeddings need should have an abstract
paper_data["embedding"] = embeddings[j].tolist()
json.dump(paper_data, file)
file.write("\n")
print("Finished writing to file")