How did we get here? Part 2

In the last part of this series we introduced the problem of how to predict the next word in a series. We introduced lookup tables and showed how they fall apart quickly. We reached the conclusion that ultimately to predict text well, we want a system that sees a new context and recognises that it’s like ones that it has seen before. In this part we’ll look into how to give a machine that ability.

A bit of history

After some feedback on the last part I’ve decided to incorporate a history dropdown to each part. The work that we’re doing is all based on historical research so I want to highlight the exact contributions that shaped the ideas we explore in this series.

In part 1 the lookup tables we built are known as n-gram models. One of the earliest recorded works that showed how to Generate words from word frequency statistics comes from Claude (yes I think they named Claude after him) Shannon’s 1948 paper A Mathematical Theory of Communication (Shannon, 1948). N-gram models were the dominate form of language modelling for decades after they were first introduced.

The problem of too many contexts that we introduced in the last part was well known in language modelling throughout the 20th century but the difficulty of the scale required to achieve this was best articulated in the paper A Neural Probabilistic Language Model (Bengio et al., 2003).

We’ll base some of this and next part’s work on this paper.

In this part we’re going to introduce the idea that “you shall know a word by the company it keeps.” Which is an idea J.R. Firth introduced in 1957 (Firth, 1957).

Of course there are countless other resources that contributed to these areas but these serve as a good starting point if you would like to read more about the topics we’ve covered so far.

One way you can imagine teaching a system that words are similar is by plotting them on a chart. Words that are similar should be plotted closer together and words that are less similar should be plotted further apart.

For instance on this chart, we plot dog and cat close to each other and post office further away.

catdogpost office

But how do you even begin to plot something like this and what does it all actually mean?

You can imagine that the axis of the graph represent some sort of characteristic. So we can replot the above example to imagine that the x-axis represents how much something is a living thing, rather than a building or object, and the y-axis represents how large it is. In this way, the more that it aligns with this characteristic the further in that direction it should be plotted. So then, items with similar characteristics will naturally be plotted closer together. You can see that on this chart:

building / object → living thingsmall → largecatdogpost officemousehorseletter

Now, it’s important to mention that when models are trained they aren’t told what the axes represent. Instead, the model works out its own arrangement of words, using whatever helps it predict the next word, and we only infer the patterns afterwards. In practice the useful directions don’t line up neatly with a single axis either. It’s also difficult for us to picture. Whilst 2-dimensional graphs are easy to plot, these models place words in hundreds or thousands of dimensions, which isn’t something we, as humans, can visualise.

So to illustrate the point, you’re going to do the job of a model and plot a graph yourself. But because you’re presumably already familiar with English we’re going to use a toy (made-up) language so you can’t rely on your English language knowledge. In addition, just like when a model is trained, you won’t be given the axes definitions either. You’re going to be given some example sentences and based on those you’ll have to work out where words should be plotted.

Here are some example sentences. You can drag the words around the map below so that words used in similar ways end up close together. Their starting positions are completely random. There are no axes, I’m not going to tell you what any particular direction represents, it’s up to you to decide what definitions work best for you.

  • the zindled the
  • the zindled the
  • the zindled the
  • the glorped the
  • the glorped the
  • the glorped the
blorpsnurfmipglim

Assuming you moved around the plots on the chart, you did exactly what we want our model to do. Nobody told you what blorp or snurf meant but you figured out they should be close together because they showed up in similar places in the example sentences.

Now that you’ve constructed the map we’re going to show you two sentences at a time and you’re going to use your map (if you’re not sure you can always just click Show me the correct solution) to work out which sentence makes more sense.

blorpsnurfmipglim

Your map

1 of 3: which sentence fits the language?

You’ll notice that some setences never occured in the example sentences that you had but you were still able to use your map to get to the right answer. This is similar to how the model learns. Even though you (and the model) hadn’t seen these exact phrases before, you had learnt enough information about the toy-language to accurately make predictions.

To reenforce the idea, the lookup tables from part 1 wouldn’t have worked because they required us to have seen the sentences before in order to make accurate predictions. That’s why we want to teach a model how the language itself works, by doing so it can accurately make predictions even if it hasn’t seen the exact sentence.

Of course, this is just a toy example, and real models handle much larger scales. They deal with more words, longer pieces of text and represent these words in much larger dimensions. But the general idea still holds.

Whilst in this part you built a map by hand after looking at six sentences, a real model typically reads billions of sentences, so it doesn’t do this work manually. So how do models build these maps?

The way we do it is to ask the model to use its map to predict the next word, if it gets it wrong we adjust its internal map. We keep doing this until it gets sufficiently good at predicting the next word.

But this raises another problem. When the model makes a bad prediction we need to answer these questions:

Whilst you wait for part 3 you can think about how you would approach these related problems. In the next part we’ll introduce neural networks as a solution for how to adjust this internal map.

References

  1. Bengio, Y., Ducharme, R., Vincent, P., & Jauvin, C. (2003). A neural probabilistic language model. Journal of Machine Learning Research, 3, 1137–1155.
  2. Firth, J. R. (1957). A synopsis of linguistic theory, 1930–1955. Studies in Linguistic Analysis (special volume of the Philological Society), pp. 1–32. Oxford: Basil Blackwell.
  3. Shannon, C. E. (1948). A mathematical theory of communication. Bell System Technical Journal, 27(3), 379–423 and 27(4), 623–656.