-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
74 lines (59 loc) · 2.62 KB
/
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
import argparse
import random
import pandas as pd
import torch
import pytorch_lightning as pl
from transformers import AutoModelForSequenceClassification, get_linear_schedule_with_warmup
from torchmetrics.functional import pearson_corrcoef
class Model(pl.LightningModule):
def __init__(self, model_name, lr):
super().__init__()
self.save_hyperparameters()
self.model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=1)
self.lr = lr
self.loss_func = torch.nn.L1Loss()
def forward(self, **inputs):
outputs = self.model(**inputs)
return outputs.logits.squeeze(-1)
def step(self, batch):
inputs = {key: val for key, val in batch.items() if key!= 'labels'}
labels = batch['labels']
predictions = self(**inputs)
loss = self.loss_func(predictions, labels.float())
pearson = pearson_corrcoef(predictions, labels.to(torch.float64))
return loss, pearson
def training_step(self, batch, batch_idx):
train_loss, pearson = self.step(batch)
self.log("train_loss", train_loss, logger=True)
self.log("train_pearson", pearson, logger=True)
return train_loss
def validation_step(self, batch, batch_idx):
val_loss, pearson = self.step(batch)
self.log("val_loss", val_loss, logger=True)
self.log("val_pearson", pearson, logger=True)
def test_step(self, batch, batch_idx):
inputs = {key: val for key, val in batch.items() if key!= 'labels'}
labels = batch['labels']
predictions = self(**inputs)
self.predictions.append(predictions.detach().cpu())
pearson = pearson_corrcoef(predictions, labels.to(torch.float64))
self.log("test_pearson", pearson, logger=True)
def predict_step(self, batch, batch_idx):
inputs = {key: val for key, val in batch.items() if key!= 'labels'}
return self(**inputs)
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.parameters(), lr=self.lr)
# Attached Learning Rate Scheduler
scheduler = {
'scheduler': get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps= 0.1 * (self.total_steps),
num_training_steps=self.total_steps),
'interval': 'step'
}
return {'optimizer': optimizer, 'lr_scheduler': scheduler}
def setup(self, stage='fit'):
if stage =='fit':
self.total_steps=self.trainer.max_epochs * len(self.trainer.datamodule.train_dataloader())
elif stage == 'test':
self.predictions = []