import torch
import torch.nn as nn
import pytorch_lightning as pl
from transformers import BertModel, AdamW
from pytorch_lightning.metrics.functional import accuracy, auroc
BERT_MODEL_NAME = 'bert-base-chinese'
class SentimentTagger(pl.LightningModule):
def __init__(self, n_classes: int,
n_training_steps=None,
n_warmup_steps=None):
super().__init__()
self.roberta = BertModel.from_pretrained(BERT_MODEL_NAME,
return_dict=True)
self.classifier = nn.Linear(self.roberta.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.roberta(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"].reshape(-1, 80)
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"].reshape(-1, 80)
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"].reshape(-1, 80)
loss, outputs = self(input_ids, attention_mask, labels)
self.log("test_loss", loss, prog_bar=True, logger=True)
return loss
def training_epoch_end(self, outputs):
labels = []
predictions = []
for output in outputs:
for out_labels in output["labels"].detach().cpu():
labels.append(out_labels)
for out_predictions in output["predictions"].detach().cpu():
predictions.append(out_predictions)
labels = torch.stack(labels).int()
predictions = torch.stack(predictions)
for i, name in enumerate(LABEL_COLUMNS_ALL):
class_roc_auc = 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'
)
)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
classification reporter precision recall f1-score support micro-avg 0.88 0.85 0.87 300000 macro-avg 0.70 0.59 0.61 300000 weighted-avg 0.87 0.85 0.86 300000 samples-avg 0.88 0.85 0.87 300000
资源推荐
资源详情
资源评论
收起资源包目录
基于Bert 预训练模型的美团评价多标签多分类任务.zip (7个子文件)
multi-label-sentiment-classifications-main
model_test.py 2KB
consts.py 2KB
model_save.py 540B
model
sentiment_tagger.py 3KB
sentiment_dataset.py 2KB
model_eval.py 2KB
model_train.py 3KB
共 7 条
- 1
资源评论
博士僧小星
- 粉丝: 1746
- 资源: 5850
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功