-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathner_bert.py
212 lines (151 loc) · 6.24 KB
/
ner_bert.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
import pandas as pd
import torch
import numpy as np
from transformers import BertTokenizerFast, BertForTokenClassification
from torch.utils.data import DataLoader
from tqdm import tqdm
from torch.optim import SGD
from src.util import get_device, set_seed
from seqeval.metrics import f1_score
device = get_device()
set_seed()
LEARNING_RATE = 1e-2
EPOCHS = 5
# Download data set from: https://www.kaggle.com/datasets/rajnathpatel/ner-data
tokenizer = BertTokenizerFast.from_pretrained("bert-base-cased")
def align_label(texts, labels, labels_to_ids, label_all_tokens=True):
tokenized_inputs = tokenizer(
texts, padding="max_length", max_length=512, truncation=True
)
word_ids = tokenized_inputs.word_ids()
previous_word_idx = None
label_ids = []
for word_idx in word_ids:
if word_idx is None:
label_ids.append(-100)
elif word_idx != previous_word_idx:
try:
label_ids.append(labels_to_ids[labels[word_idx]])
except:
label_ids.append(-100)
else:
try:
label_ids.append(
labels_to_ids[labels[word_idx]] if label_all_tokens else -100
)
except:
label_ids.append(-100)
previous_word_idx = word_idx
return label_ids
class DataSequence(torch.utils.data.Dataset):
def __init__(self, df, labels_to_ids):
lb = [i.split() for i in df["labels"].values.tolist()]
txt = df["text"].values.tolist()
self.texts = [
tokenizer(
str(i),
padding="max_length",
max_length=512,
truncation=True,
return_tensors="pt",
)
for i in txt
]
self.labels = [align_label(i, j, labels_to_ids) for i, j in zip(txt, lb)]
def __len__(self):
return len(self.labels)
def get_batch_data(self, idx):
return self.texts[idx]
def get_batch_labels(self, idx):
return torch.LongTensor(self.labels[idx])
def __getitem__(self, idx):
batch_data = self.get_batch_data(idx)
batch_labels = self.get_batch_labels(idx)
return batch_data, batch_labels
def main():
df = pd.read_csv("~/develop/dataset/ner/ner.csv")
df = df[0:1000]
labels = [i.split() for i in df["labels"].values.tolist()]
# Check how many labels are there in the dataset
unique_labels = set()
for lb in labels:
[unique_labels.add(i) for i in lb if i not in unique_labels]
# Map each label into its id representation and vice versa
labels_to_ids = {k: v for v, k in enumerate(sorted(unique_labels))}
ids_to_labels = {v: k for v, k in enumerate(sorted(unique_labels))}
labels = [i.split() for i in df["labels"].values.tolist()]
# Check how many labels are there in the dataset
unique_labels = set()
for lb in labels:
[unique_labels.add(i) for i in lb if i not in unique_labels]
model = BertModel(unique_labels)
df_train, df_val, df_test = np.split(
df.sample(frac=1, random_state=42), [int(0.8 * len(df)), int(0.9 * len(df))]
)
def train_loop(model, df_train, df_val):
train_dataset = DataSequence(df_train, labels_to_ids)
val_dataset = DataSequence(df_val, labels_to_ids)
train_dataloader = DataLoader(
train_dataset, num_workers=4, batch_size=1, shuffle=True
)
val_dataloader = DataLoader(val_dataset, num_workers=4, batch_size=1)
optimizer = SGD(model.parameters(), lr=LEARNING_RATE)
model = model.to(device)
best_acc = 0
best_loss = 1000
for epoch_num in range(EPOCHS):
total_acc_train = 0
total_loss_train = 0
model.train()
for train_data, train_label in tqdm(train_dataloader):
train_label = train_label[0].to(device)
mask = train_data["attention_mask"][0].to(device)
input_id = train_data["input_ids"][0].to(device)
optimizer.zero_grad()
loss, logits = model(input_id, mask, train_label)
logits_clean = logits[0][train_label != -100]
label_clean = train_label[train_label != -100]
predictions = logits_clean.argmax(dim=1)
acc = (predictions == label_clean).float().mean()
total_acc_train += acc
total_loss_train += loss.item()
loss.backward()
optimizer.step()
model.eval()
total_acc_val = 0
total_loss_val = 0
for val_data, val_label in val_dataloader:
val_label = val_label[0].to(device)
mask = val_data["attention_mask"][0].to(device)
input_id = val_data["input_ids"][0].to(device)
loss, logits = model(input_id, mask, val_label)
logits_clean = logits[0][val_label != -100]
label_clean = val_label[val_label != -100]
predictions = logits_clean.argmax(dim=1)
acc = (predictions == label_clean).float().mean()
total_acc_val += acc
total_loss_val += loss.item()
val_accuracy = total_acc_val / len(df_val)
val_loss = total_loss_val / len(df_val)
print(
f"Epochs: {epoch_num + 1} "
f"| Loss: {total_loss_train / len(df_train): .3f} "
f"| Accuracy: {total_acc_train / len(df_train): .3f} "
f"| Val_Loss: {total_loss_val / len(df_val): .3f} "
f"| Val_Accuracy: {total_acc_val / len(df_val): .3f}"
f"| Val_f1: {f1_score(label_clean, predictions): .3f}"
)
train_loop(model, df_train, df_val)
class BertModel(torch.nn.Module):
def __init__(self, unique_labels):
super(BertModel, self).__init__()
self.bert = BertForTokenClassification.from_pretrained(
"bert-base-cased", num_labels=len(unique_labels)
)
def forward(self, input_id, mask, label):
output = self.bert(
input_ids=input_id, attention_mask=mask, labels=label, return_dict=False
)
return output
if __name__ == "__main__":
main()