# 1.9 GRU单元（Gated Recurrent Unit（GRU））

门控循环单元：改变了**RNN**的隐藏层，使其可以更好地捕捉深层连接，并改善了梯度消失问题

## 简化的GRU模型

**RNN**隐藏层的单元的可视化：

[![1521560729](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/1521560729.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/1521560729.png)

$$a^{<t>}$$表达式为：

$$
a^{<t>}=tanh(W\_a\[a^{<t-1>},x^{<t>}]+b\_a)
$$

为了解决梯度消失问题，对上述单元进行修改，添加了记忆单元，构建GRU，如下图所示：

![](/files/-Le0csQZYN5Aqw1Mlm3M)

表达式为：

$$
\tilde c^{<t>}=tanh(W\_c\[c^{<t-1>},x^{<t>}]+b\_c)
$$

$$
\Gamma\_u=\sigma(W\_u\[c^{<t-1>},x^{<t>}]+b\_u)
$$

$$
c^{<t>}=\Gamma\_u\*\tilde c^{<t>}+(1-\Gamma\_u)\*c^{<t-1>}
$$

$$c^{<t-1>}=a^{<t-1>}$$，$$c^{<t>}=a^{<t>}$$。符号$$c$$表示记忆细胞的值，$$a$$表示输出的激活值，$$\tilde c^{<t>}$$是个候选值，替代了c的值，$$\Gamma\_u$$（0到1）意为gate，**u**表示“**update**”，当$$\Gamma\_u=1$$时，代表更新；当$$\Gamma\_u=0$$时，代表记忆，保留之前的模块输出。$$\Gamma\_u$$能够保证RNN模型中跨度很大的依赖关系不受影响，消除梯度消失问题

## 完整的GRU

完整的GRU添加了另外一个gate，即$$\Gamma\_r$$，表达式如下：

$$
\tilde c^{<t>}=tanh(W\_c\[\Gamma\_r\*c^{<t-1>},x^{<t>}]+b\_c)
$$

$$
\Gamma\_u=\sigma(W\_u\[c^{<t-1>},x^{<t>}]+b\_u)
$$

$$
\Gamma\_r=\sigma(W\_r\[c^{<t-1>},x^{<t>}]+b\_r)
$$

$$
c^{<t>}=\Gamma\_u\*\tilde c^{<t>}+(1-\Gamma\_u)\*c^{<t-1>}
$$

$$
a^{<t>}=c^{<t>}
$$

$$\Gamma\_{r}$$门：计算出的下一个$$c^{<t>}$$的候选值$${\tilde{c}}^{<t>}$$跟$$c^{<t-1>}$$有多大的相关性

$$c^{<t>}$$可以是一个向量（编号1），如果有100维的隐藏的激活值，那么$$c^{<t>}$$、$${\tilde{c}}^{<t>}$$、$$\Gamma\_{u}$$还有画在框中的其他值也是100维

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/c1df3f793dcb1ec681db6757b4974cee.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/c1df3f793dcb1ec681db6757b4974cee.png)

$$*$$实际上就是元素对应的乘积（$$c^{<t>}=\Gamma\_u*\tilde c^{<t>}+(1-\Gamma\_u)\*c^{<t-1>}$$），若$$\Gamma\_{u}$$（$$\Gamma\_u=\sigma(W\_u\[c^{<t-1>},x^{<t>}]+b\_u)$$）是一个100维的向量，而里面的值**几乎**都是0或者1，则这100维的记忆细胞$$c^{<t>}$$（$$c^{<t>}=a^{<t>}$$，编号1）就是要更新的比特


---

# Agent Instructions: 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://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-wu-men-ke-xu-lie-mo-xing-sequence-models/di-wu-men-kexulie-mo-578b28-sequence-models/recurrent-neural-networks/19-grudan-yuan-ff08-gated-recurrent-unit-gru.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.
