Sunday, September 11, 2016

No, really, why does word2vec work?

There are a lot of notes out there about why word2vec works. Here are a few from the first page of Google:
How does word2vec work? on Quora
Why word2vec works on Andy's Blog
How exactly does word2vec work?
Making sense of word2vec by Radim Rehurek

I've read some of these pages, and they've all been helpful in their own way, but I feel like they don't really get at the heart of it. They all say that word2vec is learning the relationships between words, and then show the math that means they are maximizing for a function that does that. Which is true in its way, but it doesn't satisfy my need for an explanation I really understand.
What I would like to do is start with some simple principles, and show that they imply the analogy finding capability of word2vec, and show an easy test for when it will break down.
You only need to accept one premise: that the process of assigning word vectors assigns words that are similar to similar vectors.
High dimensional vectors behave differently than you might expect. There are three million words embedded in Mikolov's Google News word2vec space, and three million is a very tiny number compared to the number of possible locations in 300 dimensions. Because of this, there is plenty of room for a vector to be close to several different clusters at the same time.
This certainly works for some kinds of words. For example, here are the nearest neighbors of the word 'royal':

 'royal'    'royals'    'monarch'    'prince'    'princes'    'Prince Charles'    'monarchy'    'palace'    'Windsors'    'commoner'    'Mrs Parker Bowles'    'queen'    'commoners'    'Camilla'    'fiance Kate Middleton'    'Queen Elizabeth II'    'monarchs'    'royal palace'    'princess'    'Queen Consort'

Clearly it has mapped several words that have to do with royalty together. There are similar clusters of terms having to do with male, female, and person, though it isn't quite as easy to find them as just searching for words near to the word 'male' or 'female' or 'person'.

Because this is a vector space, the words near the average of two vectors a and b will be nearly the same as the intersection of the set of words near a and the set of words near b. If you picture a Venn diagram, the average of a and b will fall into the overlap of the circle surrounding a and the circle surrounding b.
This is all we need to show that word2vec works. Lets call royal r, female f, male m and person p.

The word 'king' is in the intersection of 'royal' and 'male' so it is approximately r + m. (I should mention that I've normalized the vectors, so taking the sum is essentially the same as taking the average.) 'queen' is close to r + f. 'man' is close to m + p, and woman is close to f + p.
Putting it like that, the analogical property just falls out:

king + woman - man = queen


r + m ) + ( f + p ) - ( m + p ) = ( r + f )

This also tells us an easy way to tell when this property will totally fail. For example, I think this is a pretty obvious analogy:

blueberry:blue jay::strawberry:cardinal

If we try this in word2vec, we get

 'blue jays'    'red bellied woodpecker'    'grackle'    'ovenbird'    'downy woodpecker'    'indigo bunting'    'tufted titmouse'    'Carolina wren'    'chickadee'    'nuthatch'    'rose breasted grackle'    'bluejays'    'raccoon'    'spruce grouse'    'robin'

It seems to have picked up that we are looking for a bird, but it has missed the idea that we are looking for a red bird. So what went wrong?

Let's break it down like we did above. call red r, blue b, fruit f, and songbird s. The equation is
( b + s ) + ( r + f ) - ( b + f ) = ( r + s )

We can easily find clusters of fruit and songbirds (terms near those words are close enough.) But what about red things or blue things? Is there some cluster which contains firetrucks, strawberries, and cardinals? Probably not, at least, not a great cluster. Their color just isn't that relevant to the way newspapers talk about those things. (If we trained word2vec on books for toddlers, that might be different). Because red things aren't clustered together in word2vec, the analogy fails.

This doesn't mean this word2vec space is useless for this analogy, though. Suppose we used a dictionary to look at the different sense of words. One sense of cardinal would be a 'Catholic dignitary', while another would be 'red bird.' If we averaged those terms to get new vectors for 'cardinal: sense 1' and 'cardinal: sense 2,' and did the same for strawberries and blueberries and bluejays, we could engineer a version of word2vec that would be able to solve the analogy. It's adding information in by hand rather than learning it from scratch, which some would call cheating, but I just call it efficiently mining the corpus consisting of the dictionary.

Monday, August 1, 2016

No Man's Sky

There's been a lot of hype over a videogame about to be released called No Man's Sky. The game is set in "a procedurally generated deterministic open universe, which includes over 18 quintillion (1.8×1019) planets."
This is what I refer to in Machinamenta as the kaleidoscope pattern. They have come up with a limited set of ways that planets can vary, and then counted all the variations as unique creations. But there is going to be little joy in discovering new combinations of those patterns, once the patterns themselves are recognized from a few examples. I understand the appeal! But ultimately I think people will be disappointed once they realize that the variation is limited, as it must be for this kind of universe.
I do like that they are using Lindenmeyer systems for the plants! It's the difference between producing new sentences with a grammar versus producing them with mad-libs.

How could a future game do better? Here are a few suggestions:
All of which are too computationally intensive for a game right now.

Thursday, June 2, 2016

Generated Kanji

This is for another project I'm working on with my brother David. I don't want to go into details yet, this is just a teaser.

wordplay utilities

I wrote a couple of wordplay programs. One generates an acrostic for a particular word and a rough theme, and the other generates rhyming pairs of words on a given theme. They make use of distributional semantic vector spaces, of course.

Here are examples of the rhymes it comes up with. These are chosen by hand from a list of about 30 candidates:
cowboy: colorado desperado
llama: coat goat
Star_Wars: groovy movie, halloween onscreen, iconic hypersonic, cute reboot, droid overjoyed, etc...
friar: yeast priest, barbarian seminarian
spaceship: moon balloon
pillow: head bed
trampoline: elastic gymnastic
rocking chair: knitter sitter
weed: vegetation eradication
novel: fiction depiction
juice: dilute fruit
cookie: naughty biscotti
Oreo: black snack
orca: dalmation cetacean
Mad Max: futuristic sadistic
oven: boiler broiler, roaster toaster
clock: chimes times
stopwatch: clocks jocks, elapse laps
dragon: wizard lizard
Lost_Ark: horror explorer
brain: logical neurological
Lincoln: desegregation oration
Beatles: guitars superstars
honey: bees cheese
flower: bloom perfume, frilly lily
Clinton: she nominee
sun: skylight twilight
Bollywood: Hindi indie, Delhi telly
An interesting question to explore is whether different people prefer the same choices, and what exactly it is that separates the preferred choices to the rejected ones. Probably the most important thing is that both words contribute to a true description-- there were plenty of other "bee" rhymes it came up with for honey, but only the one for a food product produced by an animal seemed like a good fit. That's the minimum to be acceptable, but wittiness goes farther-- it requires some non-obvious insight.

I used the CMU pronunciation dictionary for the rhyming patterns. It has a few oddities (sci rhymes with flee?) but overall I was very pleased with it.

Wednesday, February 17, 2016

try out deep learning style transfer

Here's a webpage where you can submit your own image and style reference, and see what the deep learning algorithm comes up with.

This was a style I particularly liked:

I wonder what would happen if you applied the same principle to text. Could you rephrase a paragraph from your textbook in the style of Shakespeare or Hemingway?