tensorflow icon indicating copy to clipboard operation
tensorflow copied to clipboard

Clarify math or paper reference behind AttentionCellWrapper

Open alquraishi opened this issue 10 years ago • 16 comments

The documentation for AttentionCellWrapper in contrib states that it's based on this paper. However, the attention mechanism in the paper appears to be distinct from that in AttentionCellWrapper. For example the TF implementation involves convolutions and requires a fixed attention window, neither of which are a feature of the referenced paper. I suggest that either the correct paper is referenced, or the description is updated to reflect the actual math being implemented. The biggest issue right now is that it's unclear what the various options really mean (attn_length, attn_size, attn_vec_size, etc) as their descriptions are very concise and without a paper reference to ground what the documentation is referring to, it's impossible to know what the attention mechanism is doing short of going through the code line-by-line.

alquraishi avatar Sep 17 '16 20:09 alquraishi

Thanks for the report. Notified the author.

jmchen-g avatar Sep 26 '16 07:09 jmchen-g

Hi @alquraishi, thanks for the report, indeed, this paper is a better fit to describe the model we've implemented. We'll fix the docs as soon as possible.

Monnoroch avatar Sep 26 '16 17:09 Monnoroch

@Monnoroch tbh I don't think that paper is a good description of the model either. I eventually went through the math and as far as I can tell it's not something that's from any paper I've come across before. At any rate, I ended up summarizing it (see below). If you think it would be useful to include the math directly feel free to use any part of this.

image

alquraishi avatar Sep 26 '16 17:09 alquraishi

@alquraishi, here's the original Magenta post, that describes the cell at hand: https://magenta.tensorflow.org/2016/07/15/lookback-rnn-attention-rnn with all the math an examples.

Monnoroch avatar Sep 26 '16 17:09 Monnoroch

@Monnoroch to the best of my understanding of the code, the math described in the Magenta post does not match the math implemented in AttentionCellWrapper, though it does match the math in the paper you cited. Note that the original version of the Magenta code used its own version of attention, but was later changed to use AttentionCellWrapper. The math I wrote above should match the code of AttentionCellWrapper, unless I made a mistake somewhere.

alquraishi avatar Sep 26 '16 18:09 alquraishi

@alquraishi, that is very strange, as we have extensively tested that the original code and AttentionCellWrapper around a usual LSTM cell behave exactly the same way. But thanks, I will look into it again.

Monnoroch avatar Sep 26 '16 18:09 Monnoroch

The original code and AttentionCellWrapper may well be identical. I was just saying that the prose description in the Magenta post is not identical to the code. The differences are not large, but there are some subtle discrepancies.

alquraishi avatar Sep 26 '16 20:09 alquraishi

I haven't checked all the math but this implementation slices off the first encoded hidden state and appends on the most recent decoded hidden state which is not what's done in Bahdanau. Attention is typically used for translation tasks, this looks like a good technique for music generation but I think the class name and the paper reference are misleading.

dillonalaird avatar Dec 01 '16 04:12 dillonalaird

This post gives a comprehensive and easy-to-follow overview of the current TF attention implementation in an encoder-decoder context. According to the author the implementation is based on the details at the end of the Bahdanau paper (https://arxiv.org/pdf/1409.0473v7). The author provides a nice walk-through of the details of attention and then goes into depth on the implementation. The discussion includes some detail on the use and motivation of conv2d in the code, which isn't mentioned in any papers that I've seen. Some of the variable names are slightly different but overall it really helped me understand what's going on.

jessedaniels avatar Dec 06 '16 00:12 jessedaniels

@jessedaniels The author in that post gives an incorrect explanation of the conv2d operation. The conv2d is just a clever trick to calculate W*h_t over all time steps. You can see the original code where he got that from here. You can also verify it yourself by running:

[tf.matmul(tf.squeeze(ai, [1]), tf.squeeze(k, [0,1])) for ai in tf.split(1, attn_length, attention_states)]

on some random matrices and seeing that you get the same values.

@Monnoroch The issue with AttentionCellWrapper is here and here. This is slicing off the oldest encoded hidden state and appending the newest decoding hidden state and then passing those to the next time step. This means that for translation tasks, after you decode the first word you would never be able to run attention over the first encoded word again. Which is not what's done in Bahdanau.

dillonalaird avatar Dec 06 '16 02:12 dillonalaird

@Monnoroch Another problem with AttentionCellWrapper is that the conv2d operation, which in the cited paper can be precomputed because the hidden states (input context) remain unchanged throughout the recurrent steps, is now repeated each time, where (1-1/attn_length) of the computation is not necessary.

kho avatar Dec 08 '16 12:12 kho

Closing due to lack of recent activity, please reopen if you have further. Thank you.

aselle avatar Jan 27 '17 20:01 aselle

Hello, apologies if this might be an obvious question, but I have a basic question regarding applying the AttentionCellWrapper in practice -- in particular it seems that we never specify the memory (e.g. the set of encoder hidden states) during the creation of the wrapper.

How then does the module know where to attend? Does the memory get passed somehow through the initial state of the decoder? A basic example of how to use the wrapper in practice would definitely be much appreciated. Thanks!

classicCoder16 avatar May 30 '17 12:05 classicCoder16

The paper use source sequence hidden state as attention values, but the code use dest hidden state pre?

sfmin avatar Jun 21 '17 03:06 sfmin

It is very strange to have a fixed-sized window in the implementation of AttentionCellWrapper, because there is no description about it in the Bahdanau paper. Of course, the behave of the window is not clear, which makes me confused. Can anyone explain it?

DaveYin avatar Jul 12 '17 04:07 DaveYin

it real confuse me a lot. but i found this https://github.com/tensorflow/tensorflow/commit/ec3f4d62979ef1e70e8e12e2568b13dad45fd39e it shows that the ref has changed from 06733 to 0437. but in my opinion, read the previous one(06733) helps more than 0437. it tells why u should add the windows, and it address on a unit of a seq, and 0437 more like on a seq2seq. of course, the realization of the attentincellwrapper is a little different from the 06733 mentioned. @alquraishi gives a excellent math,but some place i am not very clear: is image should be image and in the code, it only has "state", so the "state" means "h_t and c_t"?

kamendula avatar Dec 21 '17 15:12 kamendula