Skip to content

Add DSL: a topic model trained on soft labels from a small language model - #141

Open
raymondzmc wants to merge 1 commit into
x-tabdeveloping:mainfrom
raymondzmc:dsl-model
Open

raymondzmc wants to merge 1 commit into
x-tabdeveloping:mainfrom
raymondzmc:dsl-model

Conversation

@raymondzmc

Copy link
Copy Markdown

Hi! I'm the first author of Improving Topic Modeling by Distilling Soft Labels from Language Models (DSL, ICML 2026), and I'd like to contribute it to Turftopic. I'm opening this early, as CONTRIBUTING suggests, and am happy to reshape it.

What DSL is

Autoencoding topic models reconstruct a document's bag-of-words. DSL changes the target and the input, and nothing else:

  • Target. A small causal language model reads the document in a prompt that asks for its theme. Its next-token distribution over the vocabulary (softmax at temperature 3) is the reconstruction target. No text is generated.
  • Input. The language model's last hidden state at the end of the prompt is the document representation.

A bag-of-words target can only weight words that occur in the document. The soft label also weights theme words the document never uses, which helps most on short texts.

What this PR adds

  • turftopic.encoders.SoftLabelEncoder: an ExternalEncoder around a Hugging Face causal LM. encode() returns the hidden states; extract() returns the vocabulary logits and hidden states from one forward pass.
  • turftopic.DSL: a ContextualModel with a ProdLDA-style autoencoder in plain PyTorch, trained with KL to the soft labels. fit_transform accepts precomputed embeddings= and logits=.
  • docs/DSL.md with a citation section, a nav entry and rows in the model overview tables.
  • tests/test_dsl.py, which runs on a tiny random GPT-2 so CI does not download a real LM.

No new dependencies: it uses torch and the transformers that sentence-transformers already brings in.

Results

Same vectorizer for every model (2000 most frequent non-stop words), mean ± std over 3 seeds, number of topics = number of gold labels. DSL uses baidu/ERNIE-4.5-0.3B-PT (0.3B parameters); the other models use all-MiniLM-L6-v2. Purity and NMI compare the top topic per document with the gold labels. Coherence is the mean pairwise word2vec-google-news-300 similarity of the top 10 words (the c_ex of your benchmark script), and diversity is the share of unique top words.

20 Newsgroups (18,205 documents, headers, footers and quotes removed, 20 topics)

Model Purity NMI Coherence Diversity
AutoEncodingTopicModel(combined=False) .374 ± .019 .368 ± .007 .175 ± .014 .965 ± .004
AutoEncodingTopicModel(combined=True) .342 ± .016 .321 ± .011 .179 ± .007 .937 ± .025
SemanticSignalSeparation .327 ± .014 .266 ± .010 .201 ± .011 .937 ± .020
KeyNMF .411 ± .000 .383 ± .001 .263 ± .003 .912 ± .002
GMM .565 ± .018 .541 ± .010 .216 ± .004 .928 ± .016
DSL .486 ± .011 .475 ± .005 .269 ± .004 .957 ± .010

TweetTopic (single-label tweets, 19 topics)

Model Purity NMI Coherence Diversity
AutoEncodingTopicModel(combined=False) .631 ± .007 .260 ± .001 .153 ± .002 .916 ± .019
DSL .764 ± .001 .464 ± .002 .285 ± .004 .949 ± .005

DSL improves clearly on the autoencoding models it is closest to. On 20 Newsgroups GMM still assigns documents more accurately. I have not run your full leaderboard; if you would like DSL on it I can add an entry to the benchmark script, though it needs a GPU for the language model pass.

Things you may want changed

  • Naming. DSL and SoftLabelEncoder are placeholders for whatever fits your conventions.
  • Vocabulary. Only words that are a single alphabetic token for the language model are part of the soft label, so the default vectorizer here is the 2000 most frequent non-stop words, not default_vectorizer(). Phrase vectorizers are not a good fit.
  • Scope. The paper also applies the same target to FASTopic and ECRTM. I kept this PR to one model; the soft labels could be offered to FASTopic in a follow-up if you want that.
  • Cost. The language model runs once over the corpus, which is slow on CPU. The docs page says so.

DSL (Li et al., ICML 2026) replaces the bag-of-words reconstruction target with
the next-token distribution of a small causal language model that is asked for
the document's theme, and uses the model's last hidden state as the document
representation.

- SoftLabelEncoder: an ExternalEncoder that returns the hidden states, and the
  vocabulary logits from the same forward pass
- DSL: a ProdLDA-style autoencoder trained with KL to the soft labels
- docs page with citation, model overview rows, tests on a tiny random model

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant