class
GPTForSequenceClassification
extends
PretrainedModelGPTForSequenceClassification(config: GPTConfig)GPT-1 with a last-token sequence-classification head.
Wraps GPTModel with a dropout-regularised linear classifier
operating on the rightmost non-pad token's hidden state — the
canonical decoder-style pooling. Unlike BERT (CLS at position 0),
decoder LMs only have a meaningful summary at the final attended
position because every preceding position has attended to a strict
prefix only. When attention_mask is provided we honour it;
otherwise the last position of every row is used.
Parameters
configGPTConfigHyperparameters.
config.num_labels sets the output dimension;
config.classifier_dropout (falling back to hidden_dropout)
sets the dropout applied before the linear.Attributes
transformerGPTModelUnderlying decoder trunk.
dropoutnn.DropoutDropout layer applied to the pooled last-token embedding.
classifiernn.LinearFinal linear of shape
(hidden_size, num_labels) producing
per-class logits.Notes
Reference: Radford et al., "Improving Language Understanding by Generative Pre-Training", OpenAI Technical Report, 2018, §3.3.
Pooling picks index (the rightmost real token) per row.
Examples
>>> import lucid
>>> from lucid.models.text.gpt import GPTConfig, GPTForSequenceClassification
>>> cfg = GPTConfig(num_labels=3, num_hidden_layers=2, hidden_size=128,
... num_attention_heads=2, intermediate_size=512)
>>> model = GPTForSequenceClassification(cfg).eval()
>>> input_ids = lucid.tensor([[101, 7592, 102]])
>>> out = model(input_ids)
>>> out.logits.shape # (B=1, num_labels=3)
(1, 3)Used by 2
Constructors
1Instance methods
1forward(input_ids: Tensor, attention_mask: Tensor | None = None, labels: Tensor | None = None)