question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'SequenceClassifierOutput' object has no attribute 'pooler_output'

See original GitHub issue

I am training the model Distilbert model for multi-label sequence classification.

from transformers import DistilBertTokenizer, DistilBertModel
import torch

tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertModel.from_pretrained('distilbert-base-uncased',return_dict=True)
n_classes=100
criterion = nn.BCELoss()
Batch_size=16

class SRTagger(pl.LightningModule):

  def __init__(self, n_classes: int, n_training_steps=None, n_warmup_steps=None):
    super().__init__()
    self.save_hyperparameters()
    self.bert = DistilBertModel.from_pretrained('distilbert-base-uncased',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

It throws me the error:

AttributeError: ‘SequenceClassifierOutput’ object has no attribute ‘pooler_output’

Can I make changes to above code to overcome this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
NielsRoggecommented, Oct 8, 2021

You can pass it when initializing the model (sorry should have mentioned that), like so:

from transformers import DistilBertForSequenceClassification

model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased", problem_type="multi_label_classification",
num_labels=10)
0reactions
pratikchhapolikacommented, Oct 8, 2021

Thanks. Closing!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Model outputs
The outputs object is a SequenceClassifierOutput, as we can see in the documentation of that class below, it means it has an optional...
Read more >
Extracting Features from BertForSequenceClassification
This fails with: 'SequenceClassifierOutput' object has no attribute 'pooler_output' I think this only works for base Bert models, and not with ...
Read more >
BERT uncased model outputs a tuple instead of a normal ...
... dtype=dtype) AttributeError: 'tuple' object has no attribute 'log_softmax'. Here are the model output and target tensors :.
Read more >
If input.dim() == 2 and bias is not None: AttributeError
If input.dim() == 2 and bias is not None: AttributeError: 'tuple' object has no attribute 'dim.
Read more >
python/cindyxinyiwang/emea/src/transformers ...
LayerNorm is not snake-cased to stick with TensorFlow model variable name and ... pooler output) return outputs @add_start_docstrings( """ Bert Model with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found