-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemb.py
201 lines (168 loc) · 5.99 KB
/
emb.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
from transformers import AutoTokenizer, AutoModel, T5Model, AutoModelWithLMHead
from transformers import Trainer, TrainingArguments
from transformers import AutoModelForPreTraining, BertForPreTraining,BertConfig
from transformers import DataCollatorForLanguageModeling, DataCollatorWithPadding
from sklearn.model_selection import train_test_split
from datasets import load_dataset
import torch
import random
import os
import math
import pickle as pickle
import numpy as np
import pandas as pd
import scipy.sparse as sps
import torch.nn as nn
import torch.nn.init as init
from torch.nn import functional as F
import matplotlib.pyplot as plt
import torch.utils.data as data
from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence
import warnings
import torch.nn.functional as F
import torch as th
import pickle as pickle
warnings.filterwarnings("ignore")
device = torch.device("cuda:0")
op1=[]
op2=[]
op3=[]
with open('./PubMed/node.dat','r') as original_meta_file:
for line in original_meta_file:
temp1,temp2,temp3=line.split('\t')
op1.append(int(temp1))
op2.append(temp2)
op3.append(temp3[:-1])
G=[[] for i in range(len(op3))]
with open('./PubMed/link.dat', 'r') as original_meta_file:
for line in original_meta_file:
start, end, edge_type, edge_class = line.split('\t')
G[int(start)].append([int(end),int(edge_type)])
line_idx = op1
rand = random.Random()
patient_patient_path = []
alpha = 0.05
path_length = 1000000
path_num = 450000
dic = {}
for line in range(path_num):
temp_path = []
start_path = rand.choice(line_idx)
temp_path.append([start_path,-1])
dic[start_path] = 1
for i in range(path_length):
cur = temp_path[-1][0]
if (len(G[cur]) > 0):
if rand.random() >= alpha:
cur_path = rand.choice(G[cur])
temp_path.append(cur_path)
dic[cur_path[0]] = 1
else:
break
else:
break
if (len(temp_path) >= 2):
patient_patient_path.append(temp_path)
line_name={}
line_name[0]="and"
line_name[1]="causing"
line_name[2]="and"
line_name[3]="in"
line_name[4]="in"
line_name[5]="and"
line_name[6]="in"
line_name[7]="with"
line_name[8]="with"
line_name[9]="and"
with open('./PubMed/output.txt', 'w') as f:
for i in range(len(patient_patient_path)):
print(op2[patient_patient_path[i][0][0]],line_name[patient_patient_path[i][1][1]],op2[patient_patient_path[i][1][0]],end='',file=f)
for j in range(1,len(patient_patient_path[i])-2):
print(' '+line_name[patient_patient_path[i][j+1][1]],op2[patient_patient_path[i][j+1][0]],end='',file=f)
if(len(patient_patient_path[i])>2):
print(' '+line_name[patient_patient_path[i][-1][1]],op2[patient_patient_path[i][-1][0]],end='',file=f)
print("\n",end='',file=f)
with open('./PubMed/output.txt', 'r') as file:
corpus = [line.rstrip("\n") for line in file.readlines()]
print(len(corpus))
train_text, val_text = train_test_split(corpus, test_size=0.15, random_state=42)
with open('./PubMed/train_corpus.txt', 'w') as file:
for paragraph in train_text:
file.write(paragraph + "\n")
with open('./PubMed/val_corpus.txt', 'w') as file:
for paragraph in val_text:
file.write(paragraph + "\n")
datasets = load_dataset("text", data_files={"train": './PubMed/train_corpus.txt',
"validation": './PubMed/val_corpus.txt'})
card = "distilroberta-base"
tokenizer = AutoTokenizer.from_pretrained(card, use_fast=True)
model = AutoModelForPreTraining.from_pretrained(card)
def tokenize_function(samples):
return tokenizer(samples["text"], truncation=True)
tokenized_datasets = datasets.map(tokenize_function, batched=True, num_proc=4, remove_columns=["text"])
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=True, mlm_probability=0.15)
training_args = TrainingArguments(
output_dir="model/test",
overwrite_output_dir=False,
per_device_train_batch_size=16,
per_device_eval_batch_size=16,
evaluation_strategy="epoch",
save_strategy="epoch",
fp16=True,
dataloader_num_workers=8,
load_best_model_at_end=True,
gradient_accumulation_steps=20,
num_train_epochs=6,
learning_rate=0.0005,
weight_decay=0.01
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
)
trainer.train()
trainer.evaluate()
trainer.save_model("model/xyz")
model_name = "model/xyz"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name).to(device)
op1=[]
op2=[]
op3=[]
entity_count, relation_count = 0, 0
with open('./PubMed/node.dat','r') as original_meta_file:
for line in original_meta_file:
temp1,temp2,temp3=line.split('\t')
op1.append(temp1)
op2.append(temp2)
op3.append(temp3)
def get_word_embeddings(word,device):
encoded_word = tokenizer.encode(word, add_special_tokens=False)
tokens_tensor = torch.tensor([encoded_word]).to(device)
with torch.no_grad():
output = model(tokens_tensor)
embeddings = output[0][0].mean(dim=0)
return embeddings.cpu().numpy()
emb = get_word_embeddings("hello",device)
emb = np.zeros((len(op2), len(emb)))
def get_word_embeddings(word,device):
encoded_word = tokenizer.encode(word, add_special_tokens=False)
tokens_tensor = torch.tensor([encoded_word]).to(device)
with torch.no_grad():
output = model(tokens_tensor)
embeddings = output[0][0].mean(dim=0)
return embeddings.cpu().numpy()
for i in range(len(op2)):
word = op2[i]
emb[i] = get_word_embeddings(word,device)
with open('./PubMed/emb.dat', 'w') as file:
file.write('pubmed\n')
for i in range(len(op2)):
file.write(f'{i}\t')
file.write(' '.join(emb[i].astype(str)))
file.write('\n')