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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""AiProjectTest.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1E4AHYbuRi_FbOMhQntdAMMZMY14hWh2e
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from sklearn.model_selection import train_test_split
|
| 12 |
+
import torch
|
| 13 |
+
from torch.utils.data import Dataset
|
| 14 |
+
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
|
| 15 |
+
from transformers import Trainer, TrainingArguments
|
| 16 |
+
from torch.utils.data import DataLoader
|
| 17 |
+
from transformers import AdamW
|
| 18 |
+
import pandas as pd
|
| 19 |
+
|
| 20 |
+
df_train = pd.read_csv('train.csv')
|
| 21 |
+
df_test = pd.read_csv('test.csv')
|
| 22 |
+
df_test_labels = pd.read_csv('test_labels.csv')
|
| 23 |
+
|
| 24 |
+
model_name = "distilbert-base-uncased"
|
| 25 |
+
|
| 26 |
+
def read_file(f):
|
| 27 |
+
texts = f['comment_text'].tolist()
|
| 28 |
+
labels = []
|
| 29 |
+
for i in range(len(f)):
|
| 30 |
+
temp = []
|
| 31 |
+
temp.append(f['toxic'][i])
|
| 32 |
+
temp.append(f['severe_toxic'][i])
|
| 33 |
+
temp.append(f['obscene'][i])
|
| 34 |
+
temp.append(f['threat'][i])
|
| 35 |
+
temp.append(f['insult'][i])
|
| 36 |
+
temp.append(f['identity_hate'][i])
|
| 37 |
+
labels.append(temp)
|
| 38 |
+
return texts, labels
|
| 39 |
+
|
| 40 |
+
train_texts, train_labels = read_file(df_train)
|
| 41 |
+
test_texts = df_test['comment_text'].tolist()
|
| 42 |
+
test_labels = []
|
| 43 |
+
for i in range(len(df_test_labels)):
|
| 44 |
+
temp = []
|
| 45 |
+
temp.append(df_test_labels['toxic'][i])
|
| 46 |
+
temp.append(df_test_labels['severe_toxic'][i])
|
| 47 |
+
temp.append(df_test_labels['obscene'][i])
|
| 48 |
+
temp.append(df_test_labels['threat'][i])
|
| 49 |
+
temp.append(df_test_labels['insult'][i])
|
| 50 |
+
temp.append(df_test_labels['identity_hate'][i])
|
| 51 |
+
test_labels.append(temp)
|
| 52 |
+
|
| 53 |
+
train_texts, val_texts, train_labels, val_labels = train_test_split(train_texts, train_labels, test_size=.2)
|
| 54 |
+
|
| 55 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
|
| 56 |
+
|
| 57 |
+
ind = 0
|
| 58 |
+
train_encodings = {'input_ids': [], 'attention_mask': []}
|
| 59 |
+
|
| 60 |
+
for i in range(len(train_texts)//16):
|
| 61 |
+
temp = tokenizer(train_texts[ind:ind+16], truncation=True, padding=True)
|
| 62 |
+
train_encodings['input_ids'] += temp['input_ids']
|
| 63 |
+
train_encodings['attention_mask'] += temp['attention_mask']
|
| 64 |
+
ind += 16
|
| 65 |
+
|
| 66 |
+
ind = 0
|
| 67 |
+
val_encodings = {'input_ids': [], 'attention_mask': []}
|
| 68 |
+
|
| 69 |
+
for i in range(len(val_texts)//16):
|
| 70 |
+
temp = tokenizer(val_texts[ind:ind+16], truncation=True, padding=True)
|
| 71 |
+
val_encodings['input_ids'] += temp['input_ids']
|
| 72 |
+
val_encodings['attention_mask'] += temp['attention_mask']
|
| 73 |
+
ind += 16
|
| 74 |
+
|
| 75 |
+
ind = 0
|
| 76 |
+
test_encodings = {'input_ids': [], 'attention_mask': []}
|
| 77 |
+
|
| 78 |
+
for i in range(len(test_texts)//16):
|
| 79 |
+
temp = tokenizer(test_texts[ind:ind+16], truncation=True, padding=True)
|
| 80 |
+
test_encodings['input_ids'] += temp['input_ids']
|
| 81 |
+
test_encodings['attention_mask'] += temp['attention_mask']
|
| 82 |
+
ind += 16
|
| 83 |
+
|
| 84 |
+
while True:
|
| 85 |
+
if len(train_labels) > len(train_encodings):
|
| 86 |
+
train_labels.pop()
|
| 87 |
+
else:
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
while True:
|
| 91 |
+
if len(val_labels) > len(val_encodings):
|
| 92 |
+
val_labels.pop()
|
| 93 |
+
else:
|
| 94 |
+
break
|
| 95 |
+
|
| 96 |
+
while True:
|
| 97 |
+
if len(test_labels) > len(test_encodings):
|
| 98 |
+
test_labels.pop()
|
| 99 |
+
else:
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
class dataset(Dataset):
|
| 103 |
+
def __init__(self, encodings, labels):
|
| 104 |
+
self.encodings = encodings
|
| 105 |
+
self.labels = labels
|
| 106 |
+
|
| 107 |
+
def __getitem__(self, idx):
|
| 108 |
+
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
|
| 109 |
+
item['labels'] = torch.tensor(self.labels[idx])
|
| 110 |
+
return item
|
| 111 |
+
|
| 112 |
+
def __len__(self):
|
| 113 |
+
return(len(self.labels))
|
| 114 |
+
|
| 115 |
+
train_dataset_list = [[], [], [], [], [], []]
|
| 116 |
+
for i in train_labels:
|
| 117 |
+
for j in range(6):
|
| 118 |
+
train_dataset_list[j].append(i[j])
|
| 119 |
+
|
| 120 |
+
val_dataset_list = [[], [], [], [], [], []]
|
| 121 |
+
for i in val_labels:
|
| 122 |
+
for j in range(6):
|
| 123 |
+
val_dataset_list[j].append(i[j])
|
| 124 |
+
|
| 125 |
+
train_dataset_0 = dataset(train_encodings, train_dataset_list[0])
|
| 126 |
+
train_dataset_1 = dataset(train_encodings, train_dataset_list[1])
|
| 127 |
+
train_dataset_2 = dataset(train_encodings, train_dataset_list[2])
|
| 128 |
+
train_dataset_3 = dataset(train_encodings, train_dataset_list[3])
|
| 129 |
+
train_dataset_4 = dataset(train_encodings, train_dataset_list[4])
|
| 130 |
+
train_dataset_5 = dataset(train_encodings, train_dataset_list[5])
|
| 131 |
+
|
| 132 |
+
val_dataset_0 = dataset(val_encodings, val_dataset_list[0])
|
| 133 |
+
val_dataset_1 = dataset(val_encodings, val_dataset_list[1])
|
| 134 |
+
val_dataset_2 = dataset(val_encodings, val_dataset_list[2])
|
| 135 |
+
val_dataset_3 = dataset(val_encodings, val_dataset_list[3])
|
| 136 |
+
val_dataset_4 = dataset(val_encodings, val_dataset_list[4])
|
| 137 |
+
val_dataset_5 = dataset(val_encodings, val_dataset_list[5])
|
| 138 |
+
|
| 139 |
+
training_args = TrainingArguments(output_dir='./results',
|
| 140 |
+
num_train_epochs=2,
|
| 141 |
+
per_device_train_batch_size=16,
|
| 142 |
+
per_device_eval_batch_size=64,
|
| 143 |
+
warmup_steps=500, learning_rate=5e-5,
|
| 144 |
+
weight_decay=.01, logging_dir='./logs',
|
| 145 |
+
logging_steps=10)
|
| 146 |
+
|
| 147 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_name)
|
| 148 |
+
|
| 149 |
+
trainer_0 = Trainer(model=model, args=training_args, train_dataset=train_dataset_0, eval_dataset=val_dataset_0)
|
| 150 |
+
trainer_0.train()
|
| 151 |
+
|
| 152 |
+
trainer_1 = Trainer(model=model, args=training_args, train_dataset=train_dataset_1, eval_dataset=val_dataset_1)
|
| 153 |
+
trainer_1.train()
|
| 154 |
+
|
| 155 |
+
trainer_2 = Trainer(model=model, args=training_args, train_dataset=train_dataset_2, eval_dataset=val_dataset_2)
|
| 156 |
+
trainer_2.train()
|
| 157 |
+
|
| 158 |
+
trainer_3 = Trainer(model=model, args=training_args, train_dataset=train_dataset_3, eval_dataset=val_dataset_3)
|
| 159 |
+
trainer_3.train()
|
| 160 |
+
|
| 161 |
+
trainer_4 = Trainer(model=model, args=training_args, train_dataset=train_dataset_4, eval_dataset=val_dataset_4)
|
| 162 |
+
trainer_4.train()
|
| 163 |
+
|
| 164 |
+
trainer_5 = Trainer(model=model, args=training_args, train_dataset=train_dataset_5, eval_dataset=val_dataset_5)
|
| 165 |
+
trainer_5.train()
|
| 166 |
+
|
| 167 |
+
# train_dataset = dataset(train_encodings, train_labels)
|
| 168 |
+
# val_dataset = dataset(val_encodings, val_labels)
|
| 169 |
+
# test_dataset = dataset(test_encodings, test_labels)
|
| 170 |
+
|
| 171 |
+
# -----------------------------------------------------------------
|
| 172 |
+
|
| 173 |
+
# test_dataset_list = [[], [], [], [], [], []]
|
| 174 |
+
# for i in test_labels:
|
| 175 |
+
# for j in range(6):
|
| 176 |
+
# test_dataset_list[j].append(i[j])
|
| 177 |
+
|
| 178 |
+
# -----------------------------------------------------------------
|
| 179 |
+
|
| 180 |
+
# val_dataset = dataset(val_encodings, val_labels)
|
| 181 |
+
|
| 182 |
+
# test_dataset_0 = dataset(test_encodings, test_dataset_list[0])
|
| 183 |
+
# test_dataset_1 = dataset(test_encodings, test_dataset_list[1])
|
| 184 |
+
# test_dataset_2 = dataset(test_encodings, test_dataset_list[2])
|
| 185 |
+
# test_dataset_3 = dataset(test_encodings, test_dataset_list[3])
|
| 186 |
+
# test_dataset_4 = dataset(test_encodings, test_dataset_list[4])
|
| 187 |
+
# test_dataset_5 = dataset(test_encodings, test_dataset_list[5])
|
| 188 |
+
|
| 189 |
+
# -----------------------------------------------------------------
|
| 190 |
+
|
| 191 |
+
# device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 192 |
+
|
| 193 |
+
# model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased')
|
| 194 |
+
# model.to(device)
|
| 195 |
+
# model.train()
|
| 196 |
+
|
| 197 |
+
# train_loader = DataLoader(train_dataset_0, batch_size=16, shuffle=True)
|
| 198 |
+
|
| 199 |
+
# optim = AdamW(model.parameters(), lr=5e-5)
|
| 200 |
+
|
| 201 |
+
# num_train_epochs = 2
|
| 202 |
+
# for epoch in range(num_train_epochs):
|
| 203 |
+
# for batch in train_loader:
|
| 204 |
+
# optim.zero_grad()
|
| 205 |
+
# input_ids = batch['input_ids'].to(device)
|
| 206 |
+
# attention_mask = batch['attention_mask'].to(device)
|
| 207 |
+
# labels = batch['labels'].to(device)
|
| 208 |
+
|
| 209 |
+
# outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
|
| 210 |
+
|
| 211 |
+
# loss = outputs[0]
|
| 212 |
+
# loss.backward()
|
| 213 |
+
# optim.step()
|
| 214 |
+
|
| 215 |
+
# model.eval()
|