-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainingarc.py
170 lines (132 loc) · 5.22 KB
/
trainingarc.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
# |‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾|
# | fine tune |
# |_____________________________|
from torch import cuda
import streamlit as st
from transformers import pipeline
from transformers import AutoModelForSequenceClassification, AutoTokenizer, DistilBertForSequenceClassification
import torch
import torch.nn.functional as F
import emoji
import time
from PIL import Image
import pandas as pd
import numpy as np
import re
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader, RandomSampler, SequentialSampler
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
from transformers import Trainer, TrainingArguments
from datasets import load_dataset
import numpy as np
import pandas as pd
import transformers
import torch
from torch.utils.data import Dataset, DataLoader, RandomSampler, SequentialSampler
from transformers import DistilBertModel, DistilBertTokenizer
from tqdm import tqdm
from sklearn.preprocessing import MultiLabelBinarizer
from torch import cuda
model_name = 'distilbert-base-uncased'
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
device = 'cuda' if cuda.is_available() else 'cpu'
EPOCHS = 1
LEARNING_RATE = 1e-05
training_args = TrainingArguments(
output_dir='./results',
num_train_epochs=1,
per_device_train_batch_size=16,
per_device_eval_batch_size=64,
warmup_steps=500,
learning_rate=0.00005,
weight_decay=0.01,
logging_dir='./logs',
logging_steps=10,
)
# prep dataset (load csv)
# 1. load dataset
# 2. clean dataset
# 3. separate into
# load pretrained token izer, cal with dataset, encoding
# build pytorch dataset
# load pretrained model
# load huggingface trainer
class MultiLabelDataset(Dataset):
def __init__(self, dataframe, tokenizer, max_len):
self.tokenizer = tokenizer
self.data = dataframe
self.text = dataframe.text
self.targets = self.data.labels
self.max_len = max_len
def __len__(self):
return len(self.text)
def __getitem__(self, index):
text = str(self.text[index])
text = " ".join(text.split())
inputs = self.tokenizer.encode_plus(
text,
None,
add_special_tokens=True,
max_length=self.max_len,
pad_to_max_length=True,
return_token_type_ids=True
)
ids = inputs['input_ids']
mask = inputs['attention_mask']
return {
'ids': torch.tensor(ids, dtype=torch.long),
'mask': torch.tensor(mask, dtype=torch.long),
'targets': torch.tensor(self.targets[index], dtype=torch.float)
}
class DistilBERTClass(torch.nn.Module):
def __init__(self):
super(DistilBERTClass, self).__init__()
self.l1 = DistilBertModel.from_pretrained("distilbert-base-uncased")
self.pre_classifier = torch.nn.Linear(768, 768)
self.dropout = torch.nn.Dropout(0.3)
self.classifier = torch.nn.Linear(768, 5)
def forward(self, input_ids, attention_mask):
output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
hidden_state = output_1[0]
pooler = hidden_state[:, 0]
pooler = self.pre_classifier(pooler)
pooler = torch.nn.ReLU()(pooler)
pooler = self.dropout(pooler)
output = self.classifier(pooler)
return output
def loss_fn(outputs, targets):
return torch.nn.BCEWithLogitsLoss()(outputs, targets)
model = DistilBertTokenizerFast.from_pretrained(model_name, num_labels=5, problem_type='multi_label_classification')
optimizer = torch.optim.Adam(params=model.parameters(), lr=LEARNING_RATE)
# credits to @yashj302 on medium
# https://medium.com/@yashj302/text-cleaning-using-regex-python-f1dded1ac5bd
def clean_text(text):
text = text.lower()
text = re.sub(r'\n', ' ', text)
text = re.sub(r'scuse', 'excuse', text) # lol
text = re.sub(r'http[s]?\://\S+', "", text) # remove http://medium.com
text = re.sub(r'\s+', ' ', text) # remove 'VERY EXTRA SPACE '
text = re.sub(r'[0-9]', "", text) # remove numbers
text = re.sub(r'[^\w]', ' ', text) # remove characters
text = re.sub(r' +', ' ', text)
text = text.strip(' ')
return text
# test
# print(clean_text('oasdfn12312351ahttps://omg.comsfds\n\n\n\n'))
# labels
label_cols = ['toxic', 'severe_toxic', 'obscene',
'threat', 'insult', 'identity_hate']
df_train = pd.read_csv('data/train.csv')
df_train.drop('id', axis=1, inplace=True)
df_train['comment_text'] = df_train['comment_text'].apply(clean_text)
df_train['labels'] = df_train[label_cols].apply(lambda x: list(x), axis=1)
# df_train['tokens'] = [tokenizer(a, max_length=512, truncation=True)
# for a in tqdm(df_train['comment_text'].values)]
df_train.drop(label_cols, axis=1, inplace=True)
# model = DistilBertForSequenceClassification.from_pretrained(model_name)
# trainer = Trainer(
# model=model,
# args=training_args,
# train_dataset=train_dataset,
# eval_dataset=val_dataset,
# )