Domain special token embedding

From wenmin-wu/ds-skills · View interactive version

Domain special token embedding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Add domain-specific categorical values as new special tokens, resize embeddings, and prepend them to input so the model learns domain-aware representations

How to use this skill

Skill file

Domain Special Token Embedding

Overview

When a categorical feature (patent section, medical specialty, product category) strongly conditions the semantics of the text, encode it as a new special token rather than a text prefix. The model learns a dedicated embedding vector for each category that participates in self-attention. More parameter-efficient and expressive than prepending the category name as text.

Quick Start

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained('microsoft/deberta-v3-base')

categories = ['[A]', '[B]', '[C]', '[D]', '[E]', '[F]', '[G]', '[H]']
tokenizer.add_special_tokens({'additional_special_tokens': categories})

model = AutoModelForSequenceClassification.from_pretrained(
    'microsoft/deberta-v3-base', num_labels=1)
model.resize_token_embeddings(len(tokenizer))

df['input'] = df['category_token'] + ' ' + df['text_field_1'] + ' [SEP] ' + df['text_field_2']

Workflow

  1. Create bracket-wrapped tokens from unique category values: [A], [B], etc.
  2. Add as additional_special_tokens to the tokenizer
  3. Resize model embeddings with model.resize_token_embeddings(len(tokenizer))
  4. Prepend the category token to each input text
  5. New embeddings are randomly initialized — they learn during fine-tuning

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.