Nodes/ComfyUI-WordEmbeddings/WordEmbeddings: Equation
ComfyUI Node

WordEmbeddings: Equation

King - man + woman, without leaving ComfyUI

By jtrue·Created about a year ago·Updated about a year ago· 1
WordEmbeddings: Equation
  • we_model
  • words_csv
  • debug_txt
expressionking - man + woman
k10
topn_pool2000
normalize_termstrue
allow_inputsfalse

This is the "famous one." You've seen the result a thousand times - king − man + woman ≈ queen is the poster child of word embeddings, and this node finally puts it in ComfyUI as a free-text input. Type an expression, get back the top-k words whose vectors land closest to the answer. It's the fastest way to understand what a loaded embedding actually encodes, and it's a genuinely fun thing to play with while you're waiting on a batch.

What you're doing, mechanically, is vector arithmetic. The node parses your expression into signed terms, adds and subtracts the corresponding word vectors (normalized by default), and then searches the vocabulary for the words whose vectors are most similar to the result. If the model is any good, the nearest neighbors to king − man + woman really do look like a queen.

The inputs that matter

  • we_model (required) - the handle from a Loader or Local Loader.
  • expression - default king - man + woman. The parser handles +, -, the words plus/minus, coefficients (0.5*happy), and it ignores anything after = or equals - so you can write king - man + woman = ? and it just works. Terms that aren't in the model's vocabulary are skipped and listed in the debug output.
  • k - how many results to return, up to 100. Default 10.
  • topn_pool - how many candidate words the similarity search considers before filtering, default 2000. This is your quality/speed dial; a bigger pool means a more careful search on big vocabularies.
  • normalize_terms - whether each term is unit-normalized before being added, default true. Leave it on; it stops a long word's raw magnitude from dominating the sum.
  • allow_inputs - default false. When false, the input words themselves (king, man, woman) are filtered out of the results. Flip it on if you want to see how close your inputs come to the answer.

Two outputs: words_csv, a comma-separated list of the winning words, and debug_txt, which lists the used terms, the missing ones, and each result with its cosine to the target. Wire words_csv into a Show Text node to actually read it - this node isn't an output node itself.

Installation

Same pack install as everything here: ComfyUI Manager → search "ComfyUI-WordEmbeddings" → install → restart, or

cd ComfyUI/custom_nodes
git clone https://github.com/jtrue/ComfyUI-WordEmbeddings
pip install gensim numpy

No extra downloads beyond the model itself, which you load upstream in a Loader node.

Where people get burned

The usual trap is vocabulary. Type King with a capital K into a model that only stores lowercase, or a word the corpus never saw, and it silently vanishes - the node reports it in debug_txt as missing_terms, but the expression still runs with what's left. So when an equation comes back nonsense, read the debug output before blaming the math.

Second trap: bigger is not automatically better. word2vec-google-news-300 gives you fancier analogies but also a much slower first load and bigger candidate pool to chew through. The 50-dim default GloVe model is surprisingly capable for this exact party trick and answers in milliseconds.

And a reality check worth internalizing: this computes in the word embedding's semantic space, not your text encoder's. It's a lens on word meaning as GloVe learned it circa 2014 - useful, transparent, and great for prompt research, but it is not a pipeline into your sampler.

CategoryWordEmbeddings

Inputs (6)

NameTypeDefaultDescription
we_modelWE_MODEL
expressionoptSTRINGking - man + woman
koptINT101–100
topn_pooloptINT2000100–20000
normalize_termsoptBOOLEANtrue
allow_inputsoptBOOLEANfalse

Outputs (2)

NameTypeDescription
words_csvSTRING
debug_txtSTRING