AttributeError: 'SequenceClassifierOutput' object has no attribute 'pooler_output'
See original GitHub issueI 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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can pass it when initializing the model (sorry should have mentioned that), like so:
Thanks. Closing!!