-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLP_Model.py
128 lines (106 loc) · 4.11 KB
/
NLP_Model.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
import pandas as pd
import tez
import torch
import torch.nn as nn
import transformers
from sklearn import metrics, model_selection
from transformers import AdamW, get_linear_schedule_with_warmup
class BERTDataset:
def __init__(self, review, target):
self.review = review
self.target = target
self.tokenizer = transformers.BertTokenizer.from_pretrained(
"bert-base-uncased", do_lower_case=True
)
self.max_len = 64
def __len__(self):
return len(self.review)
def __getitem__(self, item):
review = str(self.review[item])
review = " ".join(review.split())
inputs = self.tokenizer.encode_plus(
review,
None,
add_special_tokens=True,
max_length=self.max_len,
padding="max_length",
truncation=True,
)
ids = inputs["input_ids"]
mask = inputs["attention_mask"]
token_type_ids = inputs["token_type_ids"]
return {
"ids": torch.tensor(ids, dtype=torch.long),
"mask": torch.tensor(mask, dtype=torch.long),
"token_type_ids": torch.tensor(token_type_ids, dtype=torch.long),
"targets": torch.tensor(self.target[item], dtype=torch.float),
}
class TextModel(tez.Model):
def __init__(self, num_train_steps):
super().__init__()
self.tokenizer = transformers.BertTokenizer.from_pretrained(
"bert-base-uncased", do_lower_case=True
)
self.bert = transformers.BertModel.from_pretrained(
"bert-base-uncased", return_dict=False
)
self.bert_drop = nn.Dropout(0.3)
self.out = nn.Linear(768, 1) # 768,1 we write 1 as it is a binary classification
self.num_train_steps = num_train_steps
self.step_scheduler_after = "batch"
def fetch_optimizer(self):
opt = AdamW(self.parameters(), lr = 3e-5)
return opt
def fetch_scheduler(self):
sch = get_linear_schedule_with_warmup(
self.optimizer, num_warmup_steps=0, num_training_steps=self.num_train_steps
)
return sch
def loss(self, outputs, targets):
if targets is None:
return None
return nn.BCEWithLogitsLoss()(outputs, targets.view(-1, 1))
def monitor_metrics(self, outputs, targets):
if targets is None:
return {}
outputs = torch.sigmoid(outputs).cpu().detach().numpy() >= 0.5
targets = targets.cpu().detach().numpy()
accuracy = metrics.accuracy_score(targets, outputs)
return {"accuracy": accuracy}
def forward(self, ids, mask, token_type_ids, targets = None):
_, x = self.bert(ids, attention_mask=mask, token_type_ids = token_type_ids)
x = self.bert_drop(x)
x = self.out(x)
if targets is not None:
loss = self.loss(x,targets)
met = self.monitor_metrics(x, targets)
return x, loss, met
return x, 0, {}
if __name__ == "__main__":
dfx = pd.read_csv("https://raw.githubusercontent.com/RashikRahman/Sentiment_Classification_PyTorch_FastApi/main/Data/imdb.csv")
dfx.sentiment = dfx.sentiment.apply(lambda x: 1 if x == "positive" else 0)
df_train, df_valid = model_selection.train_test_split(
dfx, test_size=0.1, random_state=42, stratify=dfx.sentiment.values
)
df_train = df_train.reset_index(drop=True)
df_valid = df_valid.reset_index(drop=True)
train_dataset = BERTDataset(
review=df_train.review.values, target=df_train.sentiment.values
)
valid_dataset = BERTDataset(
review=df_valid.review.values, target=df_valid.sentiment.values
)
n_train_steps = int(len(df_train) / 32 * 10)
model = TextModel(num_train_steps=n_train_steps)
tb_logger = tez.callbacks.TensorBoardLogger(log_dir="logs/")
es = tez.callbacks.EarlyStopping(monitor="valid_loss", model_path="model.bin")
model.fit(
train_dataset,
valid_dataset=valid_dataset,
train_bs=20,
device="cuda",
epochs=10,
callbacks=[tb_logger, es],
fp16=True,
)
model.save("logs/model.bin")