Info, Uncertainty

19 Jul 2019

Teaching attention, part 3 / N: Thinking more creatively

To this point, our memory mechanisms all have had the same flavor. We had a running summary , which we update with every new input, though gating might mean we prevent some coordinates from changing too much. However, it’s possible to think more creatively about how we should record and attend to new memories. For example,

  1. We don’t need to throw away all but the last when making some prediction. We’ve been saying that a model “forgets” if there are values for early on which are erased by new inputs. But what if we just kept all the ’s, and avoided overload by having a smart lookup strategy?
  2. Why are we forcing the memory to architecturally mirror the inputs – that is, why does need to be a sequence? Considering that, due to gating, we only update some cells at a time, it makes sense to design architectures specifically adapted to recording and looking up useful summaries.
  3. We’ve assumed a pretty diligent sequential processor, which knows to check every input datapoint as it arrives. What if we had a lazy one, which played it fast and loose and tried to process only those inputs that seemed really crucial to the process.

These sorts of preposterous questions will help illuminate the more fundamental principles of memory and attention in deep learning1.

First though, a warning, reiterating points from part 1. Attention is not a well-defined, clearly demarcated mathematical term. It is a principle that is useful for reasoning about many problems, but which is always in flux, as people find variants that are suited to their particular settings.

To keep our discussion grounded, and to leave you with something mathematical examples (and not just cool paper titles) that you can take home, we’re going to explore a few attention mechanisms in detail, before briefly touring across other highlights of the memory and attention literature.

Our three methods mirror the three questions from before.

Attention in Translation Models

We don’t need to force to condense everything from . This observation lies at the heart of attention-based encoder-decoder sequence translation models. Before explaining the mechanism in more detail, a refresher on how translation works with neural networks.

Abstractly, the problem of translation is to map a sequence of words in one language to the corresponding sequence in another. For example,

I don’t speak French –> Je ne parle pas le Francais.

We make this an ML problem by treating each word as a datapoint in a sequence, and hoping to map the first trajectory into the second. That is, we want to map

where is a one-hot encoding of the word in the sentence (with respect to the words in English that we bother taking into account), and does the same for French.

The encoder-decoder approach to the problem is to take the and summarize (“encode”) them using an RNN2. The final state $h_{T}$ is then used as the used as the initial state to a separate RNN, whose states are used to predict the words of the translation. Here’s a basic picture. The shading means that teh same values are copied over.

At the time step during decoding, we use a processor , and then predict using logistic regression. A good processor should be able to capture the salient features of , and these are used for translation.

This seems like a large burden for the encoder to carry. Intuitively, at time of decoding, you really only care about the words in the corresponding neigorhood of time in the source sentence. If the sentences were already aligned, you’d expect the neighborhood around to be most relevant for being able to translate .

So, consider the mechanism, , which adds columns weighted by ’s, which we call attention weights. Of course, we need the ’s to be learned directly from data – this is the same problem as in gatings. The solution shouldn’t be a surprise: we use a differentiable, parameterized function. For example, we could use

to measure the angle between particular coordinates of the encodings.

It’s worth highlighting what doesn’t directly appear in this definition – the times and themselves. This version of attention is a purely “content-based” form of attention, which cares only about the encodings and . There are variants that take the times themselves into account, so that the decoding at time explicitly upweights encodings at around that time in the original sequence2.

There are a lot of moving parts here – an encoder, a decoder, and an attention module to link them together. But everything is end-to-end differentiable, and you can take the gradients of mistranslations with respect to all parameters.

Stepping back, this is a way of reweighting – or attending to – specific encodings as a function of our current position in the decoding task at hand. We have provided some much needed relief for our lone encoder by more richly relating elements of our differentiable system. We no longer need a processor that compresses everything into one statistic because we have a more sophisticated way of navigating a large collection of summaries and pulling out what is useful.

This kind of search for locally relevant information is a hallmark of attention mechanisms. More specifically, this pattern of breaking a task into pieces and finding a way to learn different distributions over relevant summaries is one that appears througout the literature on soft-attention mechanisms.

Neural Turing Machines

This discussion suggests that more carefully structured (if potentially intricate) mechanisms for looking thigns up can alleviate problems of remembering information that was introduced long ago. This is actually a general principle, not just some quirk about encoder-decoder architectures. To illustrate this, we consider the Neural Turing Machine.

Again, there is a bit going on. The diagram below introduces the cast of characters.

Given new input, we produce a weight distribution for what seem like the most relevant memory locations. Notice that if the rows correspond to times , then the matrix is a lot like a stacked version of the from the RNN models before. And, the ’s do seem eerily like the attention weights from before. That said, we’re trying to free ourselves from the idea that memories must be indexed by time.

In this spirit of generalization, we can define soft-attention variant,

Something in this architecture which is new, and not just a variant of what we saw before in translation, is a notion of writing to memory. We use weights, along with the add and erase vectors, to define a memory update,

If the weight is small, we don’t change that location in memory at all. If it is large, we add in and erase everything that had been at that row, with an aggressiveness controlled by (coordinates where that vector is near are almost entirely reset).

The only remaining issue is how to figure out a good combination of parameters , and , upon seeing a new input .

  1. I mean, if there are any. 

  2. See, for example, “local attention” in http://arxiv.org/abs/1508.04025.  2

Kris at 09:12