> For the complete documentation index, see [llms.txt](https://ztlevi.gitbook.io/ml-101/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ztlevi.gitbook.io/ml-101/nlp/untitled.md).

# Attention without RNN

## Attention Layer: Attention without RNN

Compared with Simple RNN + Attention, it's not using hidden states from RNN to compute Key, Value, Query parameters.

* We study Seq2Seq model (encoder + decoder).
* Encoder’s inputs are vectors $$x\_1,x\_2,...,x\_m$$.
* Decoder’s inputs are vectors $$x'\_1,x'\_2,...,x'\_t$$.
* Keys and values are based on encoder’s inputs $$x\_1,x\_2,...,x\_m$$.
  * Key: $$k\_{:i}=W\_K x\_i$$
  * Value: $$v\_{:i}=W\_V x\_i$$
* Queries are based on decoder’s inputs $$x'\_1,x'\_2,...,x'\_t$$
* Query: $$q\_{:j}=W\_Q x'\_j$$

![](/files/15mhpUl4aT0DBMNsoW4t)

* Compute weights: $$\alpha\_{:j}=Softmax(K^T q\_{:j}) \in R^m$$

![](/files/sOFnavZcI1gSowoPdJLx)

* Context vector: $$c\_j=\alpha\_{ij} v\_{:1} + ... +\alpha\_{mj} v\_{:m}=V \alpha\_{:j}$$
  * Thus, $$c\_{:j}$$is a function of $$x'\_j$$and $$\[x\_1,...x\_m]$$

![](/files/rF6cbMjvJ0DcfD7pSQDE)

* Output of attention layer: $$C=\[c\_{:1}, c\_{:2},c\_{:3},...,c\_{:t}]$$

### Attention layer summary

* Attention layer: $$C=Attn(X,X')$$
  * Encoder's inputs: $$X=\[x\_1, x\_2,...,x\_m]$$
  * Decoder's inputs: $$X'=\[x'\_1,x'\_2,...,x'\_t]$$
  * Parameters: $$W\_Q,W\_K, W\_V$$

![](/files/dsEGA8UbMU0eYr1xHHIA)

### Self-Attention layer summary

* Self-Attention layer: $$C=Attn(X,X)$$
  * Inputs: $$X=\[x\_1, x\_2,...,x\_m]$$
  * Parameters: $$W\_Q,W\_K, W\_V$$

![](/files/ZZaGnxfYaKFgqqucV3Td)

## Summary

* Attention was originally developed for Seq2Seq RNN models \[1].
* Self-attention: attention for all the RNN models (not necessarily Seq2Seq models \[2].
* Attention can be used without RNN \[3].
* We learned how to build attention layer and self-attention layer.

## Reference

* Original paper: Vaswani et al. Attention Is All You Need. In NIPS, 2017.
* [youtube](https://www.youtube.com/watch?v=aButdUV0dxI\&t=1s)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ztlevi.gitbook.io/ml-101/nlp/untitled.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
