Skip to content
class

RoFormerForTokenClassification

extends_RoFormerInitMixinTokenClassificationModelMaskedLMMixin
RoFormerForTokenClassification(config: RoFormerConfig)

RoFormer with a per-token classification head.

Wraps RoFormerModel with a dropout-regularised linear classifier applied independently at every sequence position. Pattern mirrors BERTForTokenClassification — used for sequence- labelling tasks (NER / POS / chunking) on RoPE-trained encoders.

Parameters

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

Attributes

Underlying RoPE encoder trunk.
dropoutnn.Dropout
Dropout applied to the full sequence hidden states.
classifiernn.Linear
Final linear of shape (hidden_size, num_labels) mapping each token's hidden state to 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).

Loss when labels is provided is the masked cross-entropy inherited from MaskedLMMixin:

L=−1∣V∣∑(b,t)∈Vlog⁡pθ(yb,t∣xb),\mathcal{L} = -\frac{1}{|V|} \sum_{(b, t) \in V} \log p_\theta(y_{b, t} \mid x_b),

where VV is the set of positions with label != -100.

Examples

>>> import lucid
>>> from lucid.models.text.roformer import (
...     RoFormerConfig, RoFormerForTokenClassification,
... )
>>> cfg = RoFormerConfig(num_labels=9, num_hidden_layers=2,
...                      hidden_size=128, num_attention_heads=2,
...                      intermediate_size=512)
>>> model = RoFormerForTokenClassification(cfg).eval()
>>> input_ids = lucid.tensor([[101, 7592, 2088, 102]])
>>> out = model(input_ids)
>>> out.logits.shape   # (B=1, T=4, num_labels=9)
(1, 4, 9)

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.