Skip to content
class

RoFormerForSequenceClassification

extends_RoFormerInitMixinSequenceClassificationModel
RoFormerForSequenceClassification(config: RoFormerConfig)

RoFormer with a pooled-CLS sequence-classification head.

Wraps RoFormerModel (built without its pooler, which this head never reads) with the reference's own classification head: the raw CLS hidden state passes through dropout, a hidden_size -> hidden_size dense, the configured activation, dropout again, and finally the num_labels projection. Use for GLUE-style fine-tunes when the RoPE-based relative position bias is preferred.

Parameters

Hyperparameters. config.num_labels sets the output dimension; config.classifier_dropout (falling back to hidden_dropout) sets the dropout applied before the linear.

Attributes

Underlying RoPE encoder trunk.
classifiernn.Module
The reference's classification head: dropout, classifier.dense (hidden_size -> hidden_size), activation, dropout, then classifier.out_proj (hidden_size -> num_labels) producing per-class logits.

Notes

Reference: Su, Lu, Pan, Murtadha, Wen, and Liu, "RoFormer: Enhanced Transformer with Rotary Position Embedding", Neurocomputing, vol. 568, 2024 (arXiv:2104.09864).

Checkpoints saved before the head was nested (top-level dense.* / classifier.*, plus an unused roformer.pooler.*) are migrated on load_state_dict.

Loss (when labels is provided) is the standard softmax cross-entropy:

L=−1B∑b=1Blog⁡pθ(yb∣xb).\mathcal{L} = -\frac{1}{B} \sum_{b=1}^{B} \log p_\theta(y_b \mid x_b).

Examples

>>> import lucid
>>> from lucid.models.text.roformer import (
...     RoFormerConfig, RoFormerForSequenceClassification,
... )
>>> cfg = RoFormerConfig(num_labels=3, num_hidden_layers=2,
...                      hidden_size=128, num_attention_heads=2,
...                      intermediate_size=512)
>>> model = RoFormerForSequenceClassification(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

1
dunder

__init__

→None
__init__(config: RoFormerConfig)

Instance methods

1
forward(input_ids: Tensor, attention_mask: Tensor | None = None, token_type_ids: Tensor | None = None, labels: Tensor | None = None)

Override in subclasses to define the computation.