# (04강) LSTM and GRU

> LSTM, GRU에 대해서 알아보고 기존 RNN과의 차의를 알아보자
>
> **Further Reading**
>
> * [Understanding LSTM Networks](http://colah.github.io/posts/2015-08-Understanding-LSTMs/)
>
> **Further Question**
>
> * BPTT 이외에 RNN/LSTM/GRU의 구조를 유지하면서 gradient vanishing/exploding 문제를 완화할 수 있는 방법이 있을까요?
> * RNN/LSTM/GRU 기반의 Language Model에서 초반 time step의 정보를 전달하기 어려운 점을 완화할 수 있는 방법이 있을까요?

**※ 기존 RNN이 가진 문제점**

* 현재노드와 먼 과거의 상태를 사용한 문맥처리가 어렵다.
* 거리가 멀어짐에 따라 Gradient vanishing/exploding 현상이 발생한다.

## 1. LSTM (Long Short-Term Memory)

* Pass cell state information straightly without any transformation
* Short-Term memory(단기기억)을 길게 가져간다는 의미

### 1.1. Basic Structure

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcKlzGhHYe2bvmxTwS%2F-MjmaJ7HjsKNge8OAoZd%2F-Mjmct5D5_XmTu9h5XDA%2Fimage.png?alt=media\&token=6a27d05c-eb12-43cc-bf2f-273877bb1757)

* i : input gate, whether to write to cell
* f : forget gate, whether to erase cell
* o : output gate, how much to reveal cell
* g : gate gate, how much to write to cell

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2FLeldpfEqVSyhlazZXYmx%2Fimage.png?alt=media\&token=7663e634-2bfe-45ca-aab5-b4f9e001090e)

LSTM은 위 그림과같이 기존 RNN의 input값인 $$x\_t, h\_t$$뿐만 아니라 Cell State라는 $$c\_t$$값도 Input으로 가지게된다. 이 $$c\_t$$값은 지금까지 지나온 layer들 즉, 과거의 단어에 대한 정보를 잘 담고 있다.

#### Forget Gate

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcKlzGhHYe2bvmxTwS%2F-Mk0_mfqDo-b27_MG60y%2F-Mk0a6-Mhz_Pm3YryqCR%2Fimage.png?alt=media\&token=182f4367-94e5-4063-a6f7-dea2efeadc38)

* $$f\_t = \sigma(W\_f\ \cdot \ \[h\_{t-1}, x\_t] + b\_f)$$
* 과거로부터 넘어온 데이터에서 $$w\_f$$만큼 가중치를 제거한다.(생략할 부분을 정한다.)

#### Gate Gate (Input Gate)

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcKlzGhHYe2bvmxTwS%2F-Mk0_mfqDo-b27_MG60y%2F-Mk0aqFvbj6Y4xltwy2B%2Fimage.png?alt=media\&token=fba9d800-8b40-4d59-bea6-9670c5a4d2ae)

* Generate information to be added and cut it by input gate &#x20;
  * $$i\_t = \sigma(W\_f \cdot  \[h\_{t-1}, x\_t] + b\_f)$$
  * $$\widetilde{C} = tanh(W\_c \cdot  \[h\_{t-1}, x\_t]+b\_c)$$
* Generate new cell state by adding cureent information to previous cell state &#x20;
  * $$C\_t = f\_t \cdot C{t-1} + i\_t \cdot \widetilde{C}\_t$$

#### Output Gate

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcKlzGhHYe2bvmxTwS%2F-Mk0_mfqDo-b27_MG60y%2F-Mk0e32f6h2I1uIyDDM4%2Fimage.png?alt=media\&token=dbca7ae4-85bc-4604-99ef-fdc03357a6a1)

* Generate gidden state by passing cell state to tanh and output gate
* Pass this hidden state to next time step, and output or next layer if needed &#x20;
  * $$o\_t = \sigma(W\_o\[h\_{t-1}, x\_t] + b\_o)$$
  * $$h\_t = o\_t  \cdot tanh(C\_t)$$

## 2. GRU (Gated Recurrent Unit)

LSTM의 모델을 경량화한 모델

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcKlzGhHYe2bvmxTwS%2F-Mk0_mfqDo-b27_MG60y%2F-Mk0eXWzdA8nbByAWfdI%2Fimage.png?alt=media\&token=75553473-274a-489a-9bff-a724ed02497f)

* $$z\_t = \sigma(W\_z  \cdot \[h\_{t-1}, x\_t])$$
* $$r\_t = \sigma(W\_r  \cdot \[h\_{t-1}, x\_t])$$
* $$\widetilde{h\_t} = tanh(W  \cdot \[r\_t \cdot h\_{t-1}, x\_t])$$
* $$h\_t = (1-z\_t)\cdot h\_{t-1} + z\_t \cdot \widetilde{h\_t}$$ 🌟가중치의 합이  1이 되게끔 되어있다.  &#x20;
* c.f) $$C\_t = f\_t \cdot C\_{t-1} + i\_t \cdot \widetilde{C\_t}$$ in LSTM

적은 메모리 요구량과 빠른 계산이 가능하도록 하였다. LSTM에는 Cell State와 Hidden State가 있는 반면 GRU에는 Hidden State 만이 존재한다.

* $$C\_t$$를 사용하지 않고 $$h\_t$$를 사용&#x20;
* forget gate를 1 - input gate 가중치로 사용

## 3. Backpropagation in LSTM\&GRU

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2FCC526TBr81VuHO987c8c%2Fimage.png?alt=media\&token=3091121b-84d7-4caf-8ab9-8caf044a2545)

* 필요로 하는 정보를 곱셈이 아닌 덧셈연산으로 이루어지게되어 RNN에 비해 길이에 대해 Gradient Vanishing, exploding 문제를 완화할수 있게 되었다.

## 4. Summary

![](https://3944465397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjcKlzGhHYe2bvmxTwS%2Fuploads%2FTO8t27QQkuBlopmklj2P%2Fimage.png?alt=media\&token=cf35a4a1-f947-4e67-9e73-5409bf49d806)

## 관련코드 Link

{% embed url="<https://drive.google.com/file/d/1jKVJ2YnIpmayjbMCGlqU5QhPQ1Eo6roF/view?usp=sharing>" %}
