Spaces:
Runtime error
Runtime error
File size: 10,204 Bytes
70ead2f |
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# -*- coding: utf-8 -*-
"""
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/193Qwk9yyPHgI0H84JJOchTovg_CELJuw
"""
import pandas as pd
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, BertModel, AdamW, get_linear_schedule_with_warmup
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
from sklearn.model_selection import train_test_split
RANDOM_SEED = 42
np.random.seed(RANDOM_SEED)
torch.manual_seed(RANDOM_SEED)
# Preparing training data
train_file_path = '/content/sample_data/train_data.csv'
train_data = pd.read_csv(train_file_path)
filtro = (train_data['emotion'] == 'anger') | (train_data['emotion'] == 'fear') | (train_data['emotion'] == 'joy') | (train_data['emotion'] == 'sadness') | (train_data['emotion'] == 'neutral') | (train_data['emotion'] == 'surprise')
df = train_data[filtro]
angerColumn = []
fearColumn = []
surpriseColumn = []
sadnessColumn = []
joyColumn = []
neutralColumn = []
for e in df['emotion']:
if e == 'anger':
angerColumn.append(1)
joyColumn.append(0)
sadnessColumn.append(0)
fearColumn.append(0)
surpriseColumn.append(0)
neutralColumn.append(0)
elif e == 'joy':
joyColumn.append(1)
angerColumn.append(0)
sadnessColumn.append(0)
fearColumn.append(0)
surpriseColumn.append(0)
neutralColumn.append(0)
elif e == 'sadness':
sadnessColumn.append(1)
angerColumn.append(0)
joyColumn.append(0)
fearColumn.append(0)
surpriseColumn.append(0)
neutralColumn.append(0)
elif e == 'fear':
fearColumn.append(1)
angerColumn.append(0)
joyColumn.append(0)
sadnessColumn.append(0)
surpriseColumn.append(0)
neutralColumn.append(0)
elif e == 'surprise':
surpriseColumn.append(1)
angerColumn.append(0)
joyColumn.append(0)
sadnessColumn.append(0)
fearColumn.append(0)
neutralColumn.append(0)
elif e == 'neutral':
neutralColumn.append(1)
surpriseColumn.append(0)
angerColumn.append(0)
joyColumn.append(0)
sadnessColumn.append(0)
fearColumn.append(0)
df['anger'] = angerColumn
df['fear'] = fearColumn
df['surprise'] = surpriseColumn
df['joy'] = joyColumn
df['sadness'] = sadnessColumn
df['neutral'] = neutralColumn
df.drop(['emotion', 'message_id', 'response_id', 'article_id', 'empathy', 'distress',
'empathy_bin', 'distress_bin', 'gender', 'education','race', 'age','income','personality_conscientiousness',
'personality_openess','personality_extraversion','personality_agreeableness','personality_stability',
'iri_perspective_taking','iri_personal_distress', 'iri_fantasy', 'iri_empathatic_concern','raw_input_emotions'],
axis=1, inplace=True)
print(df.head())
train_df, val_df = train_test_split(df, test_size=0.05)
train_df.shape, val_df.shape
LABEL_COLUMNS = ['anger','joy','fear','surprise','sadness', 'neutral']
sample_row = train_df.iloc[16]
sample_comment = sample_row.essay
sample_labels = sample_row[LABEL_COLUMNS]
print(sample_comment)
print(sample_labels.to_dict())
BERT_MODEL_NAME = 'bert-base-cased'
tokenizer = BertTokenizer.from_pretrained(BERT_MODEL_NAME)
encoding = tokenizer.encode_plus(
sample_comment,
add_special_tokens=True,
max_length=512,
return_token_type_ids=False,
padding="max_length",
return_attention_mask=True,
return_tensors='pt',
)
encoding.keys()
encoding["input_ids"].shape, encoding["attention_mask"].shape
encoding["input_ids"].squeeze()[:20]
encoding["attention_mask"].squeeze()[:20]
print(tokenizer.convert_ids_to_tokens(encoding["input_ids"].squeeze())[:20])
class EmotionDataset(Dataset):
def __init__(
self,
data: pd.DataFrame,
tokenizer: BertTokenizer,
max_token_len: int = 128
):
self.tokenizer = tokenizer
self.data = data
self.max_token_len = max_token_len
def __len__(self):
return len(self.data)
def __getitem__(self, index: int):
data_row = self.data.iloc[index]
comment_text = data_row.essay
labels = data_row[LABEL_COLUMNS]
encoding = self.tokenizer.encode_plus(
comment_text,
add_special_tokens=True,
max_length=self.max_token_len,
return_token_type_ids=False,
padding="max_length",
truncation=True,
return_attention_mask=True,
return_tensors='pt',
)
return dict(
comment_text=comment_text,
input_ids=encoding["input_ids"].flatten(),
attention_mask=encoding["attention_mask"].flatten(),
labels=torch.FloatTensor(labels)
)
bert_model = BertModel.from_pretrained(BERT_MODEL_NAME, return_dict=True)
train_dataset = EmotionDataset(train_df,tokenizer)
sample_item = train_dataset[0]
sample_item.keys()
sample_batch = next(iter(DataLoader(train_dataset, batch_size=8, num_workers=2)))
sample_batch["input_ids"].shape, sample_batch["attention_mask"].shape
output = bert_model(sample_batch["input_ids"], sample_batch["attention_mask"])
output.last_hidden_state.shape, output.pooler_output.shape
class EmotionDataModule(pl.LightningDataModule):
def __init__(self, train_df, test_df, tokenizer, batch_size=8, max_token_len=128):
super().__init__()
self.batch_size = batch_size
self.train_df = train_df
self.test_df = test_df
self.tokenizer = tokenizer
self.max_token_len = max_token_len
def setup(self, stage=None):
self.train_dataset = EmotionDataset(
self.train_df,
self.tokenizer,
self.max_token_len
)
self.test_dataset = EmotionDataset(
self.test_df,
self.tokenizer,
self.max_token_len
)
def train_dataloader(self):
return DataLoader(
self.train_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=2
)
def val_dataloader(self):
return DataLoader(
self.test_dataset,
batch_size=self.batch_size,
num_workers=2
)
def test_dataloader(self):
return DataLoader(
self.test_dataset,
batch_size=self.batch_size,
num_workers=2
)
N_EPOCHS = 10
BATCH_SIZE = 12
MAX_TOKEN_COUNT = 512
data_module = EmotionDataModule(
train_df,
val_df,
tokenizer,
batch_size=BATCH_SIZE,
max_token_len=MAX_TOKEN_COUNT
)
class EmotionTagger(pl.LightningModule):
def __init__(self, n_classes: int, n_training_steps=None, n_warmup_steps=None):
super().__init__()
self.bert = BertModel.from_pretrained(BERT_MODEL_NAME, return_dict=True)
self.classifier = nn.Linear(self.bert.config.hidden_size, n_classes)
self.n_training_steps = n_training_steps
self.n_warmup_steps = n_warmup_steps
self.criterion = nn.BCELoss()
def forward(self, input_ids, attention_mask, labels=None):
output = self.bert(input_ids, attention_mask=attention_mask)
output = self.classifier(output.pooler_output)
output = torch.sigmoid(output)
loss = 0
if labels is not None:
loss = self.criterion(output, labels)
return loss, output
def training_step(self, batch, batch_idx):
input_ids = batch["input_ids"]
attention_mask = batch["attention_mask"]
labels = batch["labels"]
loss, outputs = self(input_ids, attention_mask, labels)
self.log("train_loss", loss, prog_bar=True, logger=True)
return {"loss": loss, "predictions": outputs, "labels": labels}
def validation_step(self, batch, batch_idx):
input_ids = batch["input_ids"]
attention_mask = batch["attention_mask"]
labels = batch["labels"]
loss, outputs = self(input_ids, attention_mask, labels)
self.log("val_loss", loss, prog_bar=True, logger=True)
return loss
def test_step(self, batch, batch_idx):
input_ids = batch["input_ids"]
attention_mask = batch["attention_mask"]
labels = batch["labels"]
loss, outputs = self(input_ids, attention_mask, labels)
self.log("test_loss", loss, prog_bar=True, logger=True)
return loss
for i, name in enumerate(LABEL_COLUMNS):
class_roc_auc = pytorch_lightning.metrics.functional.auroc(predictions[:, i], labels[:, i])
self.logger.experiment.add_scalar(f"{name}_roc_auc/Train", class_roc_auc, self.current_epoch)
def configure_optimizers(self):
optimizer = AdamW(self.parameters(), lr=2e-5)
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=self.n_warmup_steps,
num_training_steps=self.n_training_steps
)
return dict(
optimizer=optimizer,
lr_scheduler=dict(
scheduler=scheduler,
interval='step'
)
)
steps_per_epoch=len(train_df) // BATCH_SIZE
total_training_steps = steps_per_epoch * N_EPOCHS
warmup_steps = total_training_steps // 5
model = EmotionTagger(
n_classes=len(LABEL_COLUMNS),
n_warmup_steps=warmup_steps,
n_training_steps=total_training_steps
)
!rm -rf lightning_logs/
!rm -rf checkpoints/
checkpoint_callback = ModelCheckpoint(
dirpath="checkpoints",
filename="best-checkpoint",
save_top_k=1,
verbose=True,
monitor="val_loss",
mode="min"
)
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=2)
trainer = pl.Trainer(
max_epochs=N_EPOCHS,
callbacks=[early_stopping_callback,checkpoint_callback],)
trainer.fit(model, data_module)
trained_model = EmotionTagger.load_from_checkpoint(
trainer.checkpoint_callback.best_model_path,
n_classes=len(LABEL_COLUMNS)
)
trained_model.eval()
trained_model.freeze()
def run_sentiment_analysis (txt) :
THRESHOLD = 0.5
encoding = tokenizer.encode_plus(
txt,
add_special_tokens=True,
max_length=512,
return_token_type_ids=False,
padding="max_length",
return_attention_mask=True,
return_tensors='pt',
)
_, test_prediction = trained_model(encoding["input_ids"], encoding["attention_mask"])
test_prediction = test_prediction.flatten().numpy()
predictions = []
for label, prediction in zip(LABEL_COLUMNS, test_prediction):
if prediction < THRESHOLD:
continue
predictions.append("{label}: {prediction}")
return predictions |