top | item 43988533

Show HN: Semantic Calculator (king-man+woman=?)

176 points| nxa | 9 months ago |calc.datova.ai

I've been playing with embeddings and wanted to try out what results the embedding layer will produce based on just word-by-word input and addition / subtraction, beyond what many videos / papers mention (like the obvious king-man+woman=queen). So I built something that doesn't just give the first answer, but ranks the matches based on distance / cosine symmetry. I polished it a bit so that others can try it out, too.

For now, I only have nouns (and some proper nouns) in the dataset, and pick the most common interpretation among the homographs. Also, it's case sensitive.

172 comments

order

godelski|9 months ago

  data + plural = number
  data - plural = research
  king - crown = (didn't work... crown gets circled in red)
  king - princess = emperor
  king - queen = kingdom
  queen - king = worker
  king + queen = queen + king = kingdom
  boy + age = (didn't work... boy gets circled in red)
  man - age = woman
  woman - age = newswoman
  woman + age = adult female body (tied with man)
  girl + age = female child
  girl + old = female child
The other suggestions are pretty similar to the results I got in most cases. But I think this helps illustrate the curse of dimensionality (i.e. distances are ill-defined in high dimensional spaces). This is still quite an unsolved problem and seems a pretty critical one to resolve that doesn't get enough attention.

n2d4|9 months ago

For fun, I pasted these into ChatGPT o4-mini-high and asked it for an opinion:

   data + plural    = datasets
   data - plural    = datum
   king - crown     = ruler
   king - princess  = man
   king - queen     = prince
   queen - king     = woman
   king + queen     = royalty
   boy + age        = man
   man - age        = boy
   woman - age      = girl
   woman + age      = elderly woman
   girl + age       = woman
   girl + old       = grandmother

The results are surprisingly good, I don't think I could've done better as a human. But keep in mind that this doesn't do embedding math like OP! Although it does show how generic LLMs can solve some tasks better than traditional NLP.

The prompt I used:

> Remember those "semantic calculators" with AI embeddings? Like "king - man + woman = queen"? Pretend you're a semantic calculator, and give me the results for the following:

mathgradthrow|9 months ago

Distance is extremely well defined in high dimensional spaces. That isn't the problem.

Affric|9 months ago

Yeah I did similar tests and got similar results.

Curious tool but not what I would call accurate.

gweinberg|9 months ago

I got a bunch of red stuff also. I imagine the author cached embeddings for some words but not really all that many to save on credits. I gave it mermaid - woman and got merman, but when I tried to give it boar + woman - man or ram + woman - man, it turns out it has never heard of rams or boars.

thatguysaguy|9 months ago

Can you elaborate on what the unsolved problem you're referring to is?

sdeframond|9 months ago

Such results are inherently limited because a same word can have different meanings depending on context.

The role of the Attention Layer in LLMs is to give each token a better embedding by accounting for context.

charlieyu1|9 months ago

I think you need to do A-B+C types? A+B or A-B wouldn’t make much sense when the magnitude changes

virgilp|9 months ago

hacker+news-startup = golfer

pjc50|9 months ago

Ah yes, 女 + 子 = girl but if combined in a kanji you get 好 = like.

montebicyclelo|9 months ago

> king-man+woman=queen

Is the famous example everyone uses when talking about word vectors, but is it actually just very cherry picked?

I.e. are there a great number of other "meaningful" examples like this, or actually the majority of the time you end up with some kind of vaguely tangentially related word when adding and subtracting word vectors.

(Which seems to be what this tool is helping to illustrate, having briefly played with it, and looked at the other comments here.)

(Btw, not saying wordvecs / embeddings aren't extremely useful, just talking about this simplistic arithmetic)

loganmhb|9 months ago

I once saw an explanation which I can no longer find that what's really happening here is also partly "man" and "woman" are very similar vectors which nearly cancel each other out, and "king" is excluded from the result set to avoid returning identities, leaving "queen" as the closest next result. That's why you have to subtract and then add, and just doing single operations doesn't work very well. There's some semantic information preserved that might nudge it in the right direction but not as much as the naive algebra suggests, and you can't really add up a bunch of these high-dimensional vectors in a sensible way.

E.g. in this calculator "man - king + princess = woman", which doesn't make much sense. "airplane - engine", which has a potential sensible answer of "glider", instead "= Czechoslovakia". Go figure.

jbjbjbjb|9 months ago

Well when it works out it is quite satisfying

India - Asia + Europe = Italy

Japan - Asia + Europe = Netherlands

China - Asia + Europe = Soviet-Union

Russia - Asia + Europe = European Russia

calculation + machine = computer

groby_b|9 months ago

I think it's worth keeping in mind that word2vec was specifically trained on semantic similarity. Most embedding APIs don't really give a lick about the semantic space

And, worse, most latent spaces are decidedly non-linear. And so arithmetic loses a lot of its meaning. (IIRC word2vec mostly avoided nonlinearity except for the loss function). Yes, the distance metric sort-of survives, but addition/multiplication are meaningless.

(This is also the reason choosing your embedding model is a hard-to-reverse technical decision - you can't just transform existing embeddings into a different latent space. A change means "reembed all")

Retr0id|9 months ago

I think it's slightly uncommon for the vectors to "line up" just right, but here are a few I tried:

actor - man + woman = actress

garden + person = gardener

rat - sewer + tree = squirrel

toe - leg + arm = digit

gregschlom|9 months ago

Also, as I just learned the other day, the result was never equal, just close to "queen" in the vector space.

raddan|9 months ago

> is it actually just very cherry picked?

100%

bee_rider|9 months ago

Hmm, well I got

    cherry - picker = blackwood
if that helps.

spindump8930|9 months ago

First off, this interface is very nice and a pleasure to use, congrats!

Are you using word2vec for these, or embeddings from another model?

I also wanted to add some flavor since it looks like many folks in this thread haven't seen something like this - it's been known since 2013 that we can do this (but it's great to remind folks especially with all the "modern" interest in NLP).

It's also known (in some circles!) that a lot of these vector arithmetic things need some tricks to really shine. For example, excluding the words already present in the query[1]. Others in this thread seem surprised at some of the biases present - there's also a long history of work on that [2,3].

[1] https://blog.esciencecenter.nl/king-man-woman-king-9a7fd2935...

[2] https://arxiv.org/abs/1905.09866

[3] https://arxiv.org/abs/1903.03862

nxa|9 months ago

Thank you! I actually had a hard time finding prior work on this, so I appreciate the references.

The dictionary is based on https://wordnet.princeton.edu/, no word2vec. It's just a plain lookup among precomputed embeddings (with mxbai-embed-large). And yes, I'm excluding words that are present in the query because.

It would be interesting to see how other models perform. I tried one (forgot the name) that was focused on coding, and it didn't perform nearly as well (in terms of human joy from the results).

antidnan|9 months ago

Neat! Reminds me of infinite craft

https://neal.fun/infinite-craft/

thaumasiotes|9 months ago

I went to look at infinite craft.

It provides a panel filled with slowly moving dots. Right of the panel, there are objects labeled "water", "fire", "wind", and "earth" that you can instantiate on the panel and drag around. As you drag them, the background dots, if nearby, will grow lines connecting to them. These lines are not persistent.

And that's it. Nothing ever happens, there are no interactions except for the lines that appear while you're holding the mouse down, and while there is notionally a help window listing the controls, the only controls are "select item", "delete item", and "duplicate item". There is also an "about" panel, which contains no information.

lcnPylGDnU4H9OF|9 months ago

Some of these make more sense than others (and bookshop is hilarious even if it's only the best answer by a small margin; no shade to bookshop owners).

  map - legend = Mercator projection
  noodle - wheat = egg noodle
  noodle - gluten = tagliatelle
  architecture - calculus = architectural style
  answer - question = comment
  shop - income = bookshop
  curry - curry powder = cuisine
  rice - grain = chicken and rice
  rice + chicken = poultry
  milk + cereal = grain
  blue - yellow = Fiji
  blue - Fiji = orange
  blue - Arkansas + Bahamas + Florida - Pluto = Grenada

C-x_C-f|9 months ago

I don't want to dump too many but I found

   chess - checkers = wormseed mustard (63%)
pretty funny and very hard to understand. All the other options are hyperspecific grasslike plants like meadow salsify.

ActionHank|9 months ago

dog - fur = Aegean civilization

jumploops|9 months ago

This is super neat.

I built a game[0] along similar lines, inspired by infinite craft[1].

The idea is that you combine (or subtract) “elements” until you find the goal element.

I’ve had a lot of fun with it, but it often hits the same generated element. Maybe I should update it to use the second (third, etc.) choice, similar to your tool.

[0] https://alchemy.magicloops.app/

[1] https://neal.fun/infinite-craft/

lightyrs|9 months ago

I don't get it but I'm not sure I'm supposed to.

    life + death = mortality
    life - death = lifestyle

    drug + time = occasion
    drug - time = narcotic

    art + artist + money = creativity
    art + artist - money = muse

    happiness + politics = contentment
    happiness + art      = gladness
    happiness + money    = joy
    happiness + love     = joy

bee_rider|9 months ago

    Life + death = mortality  
is pretty good IMO, it is a nice blend of the concepts in an intuitive manner. I don’t really get

   drug + time = occasion
But

   drug - time = narcotic
Is kind of interesting; one definition of narcotic is

> a drug (such as opium or morphine) that in moderate doses dulls the senses, relieves pain, and induces profound sleep but in excessive doses causes stupor, coma, or convulsions

https://www.merriam-webster.com/dictionary/narcotic

So we can see some element of losing time in that type of drug. I guess? Maybe I’m anthropomorphizing a bit.

grey-area|9 months ago

Does the system you’re querying ‘get it’? From the answers it doesn’t seem to understand these words or their relations. Once in a while it’ll hit on something that seems to make sense.

__MatrixMan__|9 months ago

Here's a challenge: find something to subtract from "hammer" which does not result in a word that has "gun" as a substring. I've been unsuccessful so far.

mrastro|9 months ago

The word "gun" itself seems to work. Package this as a game and you've got a pretty fun game on your hands :)

aniviacat|9 months ago

Gun related stuff works: bullet, holster, barrel

Other stuff that works: key, door, lock, smooth

Some words that result in "flintlock": violence, anger, swing, hit, impact

Retr0id|9 months ago

Well that's easy, subtract "gun" :P

ttctciyf|9 months ago

hammer - keyboard = hammerhead

Makes no sense, admittedly!

- dulcimer and - zither are both in firmly in .*gun.* territory it seems..

soxfox42|9 months ago

hammer - red = lock

tough|9 months ago

hammer + man = adult male body (75%)

neom|9 months ago

if I'm allowed only 1 something, I can't find anything either, if I'm allowed a few somethings, "hammer - wine - beer - red - child" will get you there. Guessing given that a gun has a hammer and is also a tool, it's too heavily linked in the small dataset.

grey-area|9 months ago

As you might expect from a system with knowledge of word relations but without understanding or a model of the world, this generates gibberish which occasionally sounds interesting.

nxa|9 months ago

This might be helpful: I haven't implemented it in the UI, but from the API response you can see what the word definitions are, both for the input and the output. If the output has homographs, likeliness is split per definition, but the UI only shows the best one.

Also, if it gets buried in comments, proper nouns need to be capitalized (Paris-France+Germany).

I am planning on patching up the UI based on your feedback.

rdlw|9 months ago

I've always wondered if there's s way to find which vectors are most important in a model like this. The gender vector man-woman or woman-man is the one always used in examples, since English has many gendered terms, but I wonder if it's possible to generate these pairs given the data. Maybe to list all differences of pairs of vectors, and see if there are any clusters. I imagine some grammatical features would show up, like the plurality vector people-person, or the past tense vector walked-walk, but maybe there would be some that are surprisingly common but don't seem to map cleanly to an obvious concept.

Or maybe they would all be completely inscrutable and man-woman would be like the 50th strongest result.

ale42|9 months ago

Not what it's meant for, I guess, but it's not very strong at chemistry ;-)

  salt - chlorine + potassium = sodium
  chlorine + sodium = rubidium
  water - hydrogen = tap water
It also has some other interesting outputs:

  woman + man = adult female body (already reported by someone else)
  man - hand = woman
  woman - hand = businesswoman
  businessman - male + female = industrialist
  telephone + antenna = television equipment
  olive oil - oil = hearth money

skeptrune|9 months ago

This is super fun. Offering the ranked matches makes it significantly more engaging than just showing the final result.

ericdiao|9 months ago

Interesting: parent + male = female (83%)

Can not personally find the connection here, was expecting father or something.

ericdiao|9 months ago

Though dad is in the list with lower confidence (77%).

High dimension vector is always hard to explain. This is an example.

afandian|9 months ago

There was a site like this a few years ago (before all the LLM stuff kicked off) that had this and other NLP functionality. Styling was grey and basic. That’s all I remember.

I’ve been unable to find it since. Does anyone know which site I’m thinking of?

clbrmbr|9 months ago

A few favorites:

wine - beer = grape juice

beer - wine = bowling

astrology - astronomy + mathematics = arithmancy

galaxyLogic|9 months ago

What about starting with the result and finding set of words that when summed together give that result?

That could be seen as trying to find the true "meaning" of a word.

nxa|9 months ago

artificial intelligence - bullsh*t = computer science (34%)

behnamoh|9 months ago

This. I'm tired of so many "it's over, shocking, game changer, it's so over, we're so back" announcements that turn out to be just gpt-wrappers or resume-builder projects.

Very few papers that actually say something meaningful are left unnoticed, but as soon as you say something generic like "language models can do this", it gets featured in "AI influencer" posts.

tiborsaas|9 months ago

I've tried to get to "garage", but failed at a few attempts, ChatGPT's ideas also seemed reasonable, but failed. Any takers? :)

mynameajeff|9 months ago

"car + house + door" worked for me (interestingly "car + home + door" did not)

fallinghawks|9 months ago

goshawk-cocaine = gyrfalcon , which is funny if you know anything about goshawks and gyrfalcons

(Goshawks are very intense, gyrs tend to be leisurely in flight.)

neom|9 months ago

cool but not enough data to be useful yet I guess. Most of mine either didn't have the words or were a few % off the answer, vehicle - road + ocean gave me hydrosphere, but the other options below were boat, ship, etc. Klimt almost made it from Mozart - music + painting. doctor - hospital + school = teacher, nailed it.

Getting to cornbread elegantly has been challenging.

yigitkonur35|9 months ago

shows how bad embeddings are in a practical way

ignat_244639|9 months ago

Huh, that's strange, I wanted to check whether your embeddings have biases, but I cannot use "white" word at all. So I cannot get answer to "man - white + black = ?".

But if I assume the biased answer and rearrange the operands, I get "man - criminal + black = white". Which clearly shows, how biased your embeddings are!

Funny thing, fixing biases and ways to circumvent the fixes (while keeping good UX) might be much challenging task :)

Jimmc414|9 months ago

dog - cat = paleolith

paleolith + cat = Paleolithic Age

paleolith + dog = Paleolithic Age

paleolith - cat = neolith

paleolith - dog = hand ax

cat - dog = meow

Wonder if some of the math is off or I am not using this properly

Glyptodon|9 months ago

I figure the mathematically highest value must defer from the semantically most accurate relatively frequently. (Because Car - Wheel = Touring Car doesn't make a lot of sense to me.)

andrelaszlo|9 months ago

    hand - arm + leg = vertebrate foot
    snowman - man =  snowflake
    snowman - snow = snowbank

e____g|9 months ago

man - intelligence = woman (36%)

woman + intelligence = man (77%)

Oof.

wdutch|9 months ago

It's interesting that I find loops. For example

car + stupid = idiot, car + idiot = stupid

nikolay|9 months ago

Really?!

  man - brain = woman
  woman - brain = businesswoman

nxa|9 months ago

I probably should have prefaced this with "try at your own risk, results don't reflect the author's opinions"

dalmo3|9 months ago

I think subtraction is broken. None of what I tried made any sense. Water - oxygen = gin and tonic.

sapphicsnail|9 months ago

Telling that Jewess, feminist, and spinster were near matches as well.

karel-3d|9 months ago

woman+penis=newswoman (businesswoman is second)

man+vagina=woman (ok that is boring)

2muchcoffeeman|9 months ago

Man - brain = Irish sea

cabalamat|9 months ago

What does it mean when it surrounds a word in red? Is this signalling an error?

iambateman|9 months ago

Try Lower casing, my phone tried to capitalize and it was a problem.

fallinghawks|9 months ago

Seems to be a word not in its dictionary. Seems to not have any country or language names.

Edit: these must be capitalized to be recognized.

nxa|9 months ago

Yes, word in red = word not found mostly the case when you try plurals or non-nouns (for now)

dtj1123|9 months ago

"man-intelligence=woman" is a particularly interesting result.

ericdiao|9 months ago

wine - alcohol = grape juice (32%)

Accurate.

coolcase|9 months ago

Oh you have all the damn words. Even the Ricky Gervais ones.

downboots|9 months ago

mathematics - Santa Claus = applied mathematics

hacker - code = professional golf

krishna-vakx|9 months ago

for founders :

love + time = commitment

boredom + curiosity = exploration

vision + execution = innovation

resilience - fear = courage

ambition + humility = leadership

failure + reflection = learning

knowledge + application = wisdom

feedback + openness = improvement

experience - ego = mastery

idea + validation = product-market fit

matallo|9 months ago

uncle + aunt = great-uncle (91%)

great idea, but I find the results unamusing

HWR_14|9 months ago

Your aunt's uncle is your great-uncle. It's more correct than your intuition.

havkom|9 months ago

I tried:

-red

and:

red-red-red

But it did not work and did not get any response. Maybe I am stupid but should this not work?

hagen_dogs|9 months ago

fluid + liquid = solid (85%) -- didn't expect that

blue + red = yellow (87%) -- rgb, neat

black + {red,blue,yellow,green} = white 83% -- weird

moefh|9 months ago

> blue + red = yellow (87%) -- rgb, neat

Blue + red is magenta. Yellow would be red + green.

None of these results make much sense to me.

MYEUHD|9 months ago

king - man + woman = queen

queen - woman + man = drone

bee_rider|9 months ago

The second makes sense, I think, if you are a bee.

Glyptodon|9 months ago

Car - Wheel(s) doesn't really have results I'd guess at (boat, sled, etc.). Just specific four wheeled vehicles.

Finbel|9 months ago

London-England+France=Maupassant

firejake308|9 months ago

King-man+woman=Navratilova, who is apparently a Czech tennis player. Apparently, it's very case-sensitive. Cool idea!

maxcomperatore|9 months ago

Just use a LLM api to generate results, it will be far better and more accurate than a weird home cooked algorithm

kylecazar|9 months ago

Woman + president = man

zerof1l|9 months ago

male + age = female

female + age = male

jryb|9 months ago

Just inverting the canonical example fails: queen - woman + man = drone

x3y1|9 months ago

This kind of makes sense for bees.

doubtfuluser|9 months ago

doctor - man + woman = medical practitioner

Good to understand this bias before blindly applying these models (Yes- doctor is gender neutral - even women can be doctors!!)

heyitsguay|9 months ago

Fwiw, doctor - woman + man = medical practitioner too

blobbers|9 months ago

rice + fish = fish meat

rice + fish + raw = meat

hahaha... I JUST WANT SUSHI!

7373737373|9 months ago

it doesn't know the word human

G1N|9 months ago

twelve-ten+five=

six (84%)

Close enough I suppose

tlhunter|9 months ago

man + woman = adult female body

downboots|9 months ago

three + two = four (90%)

LadyCailin|9 months ago

Haha, yes, this was my first thought too. It seems it’s quite bad at actual math!

erulabs|9 months ago

dog - fur = Aegean civilization (22%)

huh

atum47|9 months ago

horse+man

78% male horse 72% horseman

adzm|9 months ago

noodle+tomato=pasta

this is pretty fun

growlNark|9 months ago

Surely the correct answer would be `pasta-in-tomato-sauce`? Pasta exists outside of tomato sauce.

ainiriand|9 months ago

dog+woman = man

That's weird.

mannykannot|9 months ago

Now I'm wondering if this could be helpful in doing the NY Times Connections puzzle.

quantum_state|9 months ago

The app produces nonsense ... such as quantum - superposition = quantum theory !!!

kataqatsi|9 months ago

garden + sin = gardening

hmm...

woodruffw|9 months ago

colorless+green+ideas doesn't produce anything of interest, which is disappointing.

dmonitor|9 months ago

well green is not a creative color, so that's to be expected

ephou7|9 months ago

[deleted]

ezbie|9 months ago

Can someone explain me what the fuck this is supposed to be!?

mhitza|9 months ago

Semantical subtraction within embeddings representation of text ("meaning")

spinarrets|9 months ago

cheeseburger-giraffe+space-kidney-monkey = cheesecake