Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-class, multi-label and regression to transformers #11012

Merged
merged 23 commits into from
May 4, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/transformers/models/bert/modeling_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACT2FN
from ...file_utils import (
Expand Down Expand Up @@ -1381,7 +1381,7 @@ def forward(
output_attentions=None,
output_hidden_states=None,
return_dict=None,
**kwargs
**kwargs,
):
r"""
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
Expand Down Expand Up @@ -1463,6 +1463,7 @@ class BertForSequenceClassification(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.problem_type = config.problem_type

self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
Expand Down Expand Up @@ -1517,13 +1518,29 @@ def forward(

loss = None
if labels is not None:
if self.num_labels == 1:
# We are doing regression
loss_fct = MSELoss()
loss = loss_fct(logits.view(-1), labels.view(-1))
if self.problem_type is not None:
if self.problem_type == "single_column_regression":
loss_fct = MSELoss()
loss = loss_fct(logits.view(-1), labels.view(-1))
elif self.problem_type == "multi_column_regression":
loss_fct = MSELoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels)
abhishekkrthakur marked this conversation as resolved.
Show resolved Hide resolved
elif self.problem_type in ("binary_classification", "multi_class_classification"):
abhishekkrthakur marked this conversation as resolved.
Show resolved Hide resolved
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
elif self.problem_type in ("multi_label_classification"):
loss_fct = BCEWithLogitsLoss()
loss = loss_fct(logits, labels)
else:
raise Exception("Problem type not understood")
abhishekkrthakur marked this conversation as resolved.
Show resolved Hide resolved
else:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
if self.num_labels == 1:
# We are doing regression
loss_fct = MSELoss()
loss = loss_fct(logits.view(-1), labels.view(-1))
else:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
abhishekkrthakur marked this conversation as resolved.
Show resolved Hide resolved

if not return_dict:
output = (logits,) + outputs[2:]
Expand Down