-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_causal_sentences.py
216 lines (164 loc) · 7.9 KB
/
predict_causal_sentences.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import spacy
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_recall_fscore_support, matthews_corrcoef
from transformers import BertForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
from transformers import AdamW, get_linear_schedule_with_warmup
from tqdm import tqdm, trange
import random
import os
import torch.nn.functional as F
import torch
from torch.nn import CrossEntropyLoss
import torch.nn as nn
from torch.utils.data import DataLoader, DataLoader
import transformers
from tqdm import tqdm, trange
#from google.colab import drive, files
import io
import sys
from utils import *
from torch.nn.parallel import DistributedDataParallel
import torch.multiprocessing as mp
import torch.distributed as dist
import tqdm
import time
import sys
from tqdm import tqdm
diabetes_keywords = [
"glucose", "#glucose","blood glucose", "#bloodglucose",
"insulin", "#insulin", "insulin pump", "#insulinpump",
"diabetes", "#diabetes", "t1d", "#t1d", "#type1diabetes",
"#type1", "t2d", "#t2d", "#type2diabetes", "#type2",
"#bloodsugar", "#dsma", "#bgnow", "#wearenotwaiting",
"#insulin4all", "dblog", "#dblog", "diyps", "#diyps",
"hba1c", "#hba1c", "#cgm", "#freestylelibre",
"diabetic", "#diabetic", "#gbdoc", "finger prick",
"#fingerprick", "#gestational", "gestational diabetes",
"#gdm", "freestyle libre", "#changingdiabetes",
"continuous glucose monitoring", "#continuousglucosemonitoring",
"#thisisdiabetes", "#lifewithdiabetes", "#stopdiabetes",
"#diabetesadvocate", "#diabadass", "#diabetesawareness",
"#diabeticproblems", "#diaversary", "#justdiabeticthings",
"#diabetestest", "#t1dlookslikeme", "#t2dlookslikeme",
"#duckfiabetes", "#kissmyassdiabetes", "#GBDoc",
"#changingdiabetes", "freestyle libre", "#freestylelibre",
"#cgm"
]
# Transform labels + encodings into Pytorch DataSet object (including __len__, __getitem__)
class TweetDataSet(torch.utils.data.Dataset):
# def __init__(self, text, labels, tokenizer):
def __init__(self, text, tokenizer):
self.text = text
#self.labels = labels
self.tokenizer = tokenizer
def __getitem__(self, idx):
inputs = self.tokenizer(self.text, padding=True, truncation=True, return_token_type_ids=True)
ids = inputs["input_ids"]
mask = inputs["attention_mask"]
token_type_ids = inputs["token_type_ids"]
return {
"input_ids" : torch.tensor(ids[idx], dtype=torch.long)
, "attention_mask" : torch.tensor(mask[idx], dtype=torch.long)
, "token_type_ids" : torch.tensor(token_type_ids[idx], dtype=torch.long)
# , "labels" : torch.tensor(self.labels[idx], dtype=torch.long)
}
def __len__(self):
return len(self.text)
def compute_metrics(pred, labels):
precision, recall, f1, _ = precision_recall_fscore_support(labels, pred, average='weighted')
acc = accuracy_score(labels, pred)
return {
'accuracy': acc,
'f1': f1,
'precision': precision,
'recall': recall
}
class CausalityBERT(torch.nn.Module):
""" Model Bert"""
def __init__(self):
super(CausalityBERT, self).__init__()
self.num_labels = 2
self.bert = transformers.BertModel.from_pretrained("vinai/bertweet-base")
self.dropout = torch.nn.Dropout(0.3)
self.linear1 = torch.nn.Linear(768, 256)
self.linear2 = torch.nn.Linear(256, self.num_labels)
self.softmax = torch.nn.Softmax(-1)
def forward(self, input_ids, attention_mask, token_type_ids):
_, output_1 = self.bert(input_ids, attention_mask = attention_mask, token_type_ids=token_type_ids, return_dict=False) # if output 1 is our cls token
output_2 = self.dropout(output_1)
output_3 = self.linear1(output_2)
output_4 = self.dropout(output_3)
output_5 = self.linear2(output_4)
# cross entory will take care of the logits - we don't need if we are usign cross entropy for loss function
# if doing yourself - use nll loss and logSoftmax
# logit = self.softmax(output_5)
return output_5
softmax = torch.nn.Softmax(-1)
#################### MODEL PARAMETERS #####################
#device = torch.device('cuda:7' if torch.cuda.is_available() else 'cpu')
bert_model = "vinai/bertweet-base"
# data_path = "/home/adrian/PhD/Data/Tweets20210128/Tweets_per_months_personal_noJokes/matching-tweets_diab_noRT-noDupl_20210128_personal_noJokes_withFullText_emotional.parquet"
data_path = "./data/matching-tweets_diab_noRT-noDupl_20210128_personal_noJokes_withFullText_emotional.parquet"
causal_model = "./model-causal-model/model_4_finetuned-8-epochs-lr_1e-05.pth"
# causal_model = "./model-causal-tweet/model_4_finetuned-8-epochs-lr_1e-05.pth"
result_dir = "result_causal_sentence_prediction"
# whatever we will write here - it is each device - in one go it will process batch_size*no of devicve/
batch_size = 1024 #65552 #1024 #512 # # 65552 #32776 #16388 #8194 #2048
######### LOAD TOKENIZER AND MODEL ####################
tokenizer = AutoTokenizer.from_pretrained(bert_model)
model = CausalityBERT()
model.load_state_dict(torch.load(causal_model, map_location='cpu'))
############ load data #######################
data = pd.read_parquet(data_path)#.sample(n=1000, random_state=33)
print("Total count:", data.shape[0])
# data.head()
# print(data.shape)
print("*************")
####### SPLIT TWEETS INTO SENTENCES ######################
TweetsSplit = data["full_text"].map(lambda full_text: split_into_sentences(normalizeTweet(full_text)))
print(TweetsSplit.shape[0])
sentences = TweetsSplit.explode()
print("tweets to sentences:", sentences.shape[0])
######### Exclude questions and sentences with less than 5 words
# and sentences without diabetes related keyword #################
trainingData = sentences[sentences.str.split(" ").str.len() > 5] # keep only sentence with more than 3 tokens
trainingData = trainingData[~trainingData.str.endswith("?")]
trainingData = trainingData[trainingData.str.contains("|".join(diabetes_keywords))]
print("N sentences with > 5 words & no question & all with diabetes keyword:", trainingData.shape)
text = trainingData.values.tolist()
############ Define Trainer and predict ##############
# set inference arguments
training_args = TrainingArguments(
output_dir='./results', # output directory
# per_device_train_batch_size=64, # batch size per device during training
per_device_eval_batch_size=batch_size, # batch size for evaluation
logging_dir='./logs', # directory for storing logs
seed=0,
# local_rank = rank
)
# we only use Trainer for inference
trainer = Trainer(model=model, args=training_args)
print("build trainer on device:", training_args.device, "with n gpus:", training_args.n_gpu)
###### ITERATE OVER DATASET ###############
for i in range(0, len(text), batch_size)): # takes always batch_size many tweets
tweet_subset = text[i:i+batch_size]
test_dataset = TweetDataSet(tweet_subset, tokenizer)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
with tqdm(total = 10000, file = sys.stdout) as pbar:
pbar.update(10)
logits = trainer.predict(test_dataset)
# pbar.update(10)
predictions = pd.Series(torch.argmax(torch.Tensor(logits.predictions),dim=1).flatten())
probas = pd.Series(torch.softmax(torch.Tensor(logits.predictions), dim = -1)[...,-1:].to('cpu').numpy().squeeze())
#causalDF = pd.DataFrame({"text":text, "causal_predictions": predictions, "proba":probas})
causalDF = pd.DataFrame({"text":tweet_subset, "causal_predictions": predictions, "proba":probas})
# causalDF = causalDF[causalDF["causal_predictions"] == 1]
print("causal sentences:", causalDF.shape[0])
causalDF.head()
#causalDF.to_csv(save_path, sep=",")
causalDF.to_csv(result_dir+"/causal_sentences_predictions_part_{}.csv".format(i), sep=",")