1 Building your Recurrent Neutal Network - Step by Step
注意
上标 [l] [ l ] 代表第 [l] [ l ] 层参数,例如 a[4] a [ 4 ] 代表第 4th 4 t h 激活函数
上标 (i) ( i ) 代表第 ith i t h 个样本,例如 x(i) x ( i ) 代表第 ith i t h 个训练样本输入
上标 <t> < t > <script type="math/tex" id="MathJax-Element-9"> </script>代表第 tth t t h 个时间步
x<t> x < t > 代表输入 x 的第 tth t t h 个时间步, x(i)<t> x ( i ) < t > 代表第 i i 个样本的第个时间步
下标i代表向量的第i个入口点
a[l]i a i [ l ] 代表 l l 层的第个entry
1.1 基本循环神经网络的前向传播
Tx=Ty T x = T y
1.2 RNN cell
循环神经网络是单个cell的堆叠,下面是一个基本的时间步RNN cell的运算细节:
- x<t> x < t > 当前神经元输入
- a<t−1> a < t − 1 > 前面隐藏层包含的信息
- a<t> a < t > 输出激活值给下一层RNN神经元,同样用来预测 y<t> y < t >
- 计算tanh激活值 a<t> a < t >
- 使用激活值计算预测值 y^<t> y ^ < t >
- 保存 a<t>,a<t−1>,x<t>,parameters a < t > , a < t − 1 > , x < t > , p a r a m e t e r s 到cache中
- 返回 a<t>,y<t> a < t > , y < t > 和cache
def rnn_cell_forward(xt, a_prev, parameters):
"""RNN基本单元
Args:
xt: 第 t 时间步的输入,shape=(n_x, m) m为样本数,n_x为one-hot编码
a_prev: 第 t-1 时间步的激活值,shape=(n_a,m)
parameters: 参数字典,包括:
Wax -- input矩阵,shape = (n_a, n_x)
Waa -- 前一层隐藏层的权重,shape = (n_a, n_a)
Wya -- 预测y的权重,shape = (n_y, n_a)
ba -- a的偏置,shape = (n_a, 1)
by -- y的偏置,shape = (n_y, 1)
Returns:
a_next -- 下一个时间步的输入激活层(n_a, m)
yt_pred -- t时间步的预测值,(n_y, m)
cache -- 计算反向传播时候需要的值,包括(a_next, a_prev, xt, parameters)
"""
Wax = parameters["Wax"]
Waa = parameters["Waa"]
Wya = parameters["Wya"]
ba = parameters["ba"]
by = parameters["by"]
# 计算a_next
a_next = np.tanh(np.dot(Wax, xt) + np.dot(Waa, a_prev) + ba)
# 计算yt_pred
yt_pred = softmax(np.dot(Wya, a_next) + by)
# 保存需要计算反向传播的值到cache中
cache = (a_next, a_prev, xt, parameters)
return a_next, yt_pred, cache
1.3 RNN前向传播
每个时间步输入 a<t−1> a < t − 1 > 、 x<t> x < t > ,输出 、a<t>、y<t> 、 a < t > 、 y < t >
- 创建一个隐藏层0向量去存储所有a值
- 用 a0 a 0 去初始化a
- 以t为索引,每个时间步迭代:
- 更新“a_next”以及cache,通过运行
rnn_step_forward
- 将“a_next”保存到a[第t个位置]
- 保存y预测值
- 将cache放到caches
- 更新“a_next”以及cache,通过运行
- 返回a, y, caches
def rnn_forward(x, a0, parameters):
"""RNN前向传播
Args:
x: 每个时间步的输入,shape = (n_x, m, T_x)
a0: 初始激活值,shape = (n_a, m)
parameters: 参数字典,包括:
Wax -- input矩阵,shape = (n_a, n_x)
Waa -- 前一层隐藏层的权重,shape = (n_a, n_a)
Wya -- 预测y的权重,shape = (n_y, n_a)
ba -- a的偏置,shape = (n_a, 1)
by -- y的偏置,shape = (n_y, 1)
Returns:
a: 每个时间步的a值,shape = (n_a, m, T_x)
y_pred: 每个时间步的预测值y,shape = (n_y, m, T_x)
caches: 需要计算反向传播的值,contains(list of caches, x)
"""
# caches,保存所有的cache
caches = []
# 取得 x 和 Wy 的值
n_x, m, T_x = x.shape
n_y, n_a = parameters["Wya"].shape
# 用0初始化"a"以及"y"
a = np.zeros(shape=(n_a, m, T_x))
y_pred = np.zeros(shape=(n_y, m, T_x))
# 初始化a_next
a_next = a0
for t in range(T_x):
a_next, yt_pred, cache = rnn_cell_forward(x[:, :, t], a_next, parameters)
# 保存a_next 值到a中
a[:, :, t] = a_next
y_pred[:, :, t] = yt_pred
caches.append(cache)
caches = (caches, x)
return a, y_pred, caches
2 长短是记忆网络
2.1长短时记忆网络基本单元
def lstm_cell_forward(xt, a_prev, c_prev, parameters):
"""一个单独的LSTM单元
Args:
xt: 第t个时间步的输入,shape=(n_x, m)
a_prev: 第t-1时间步的a激活值,shape=(n_a, m)
c_prev: 第t-1时间步的记忆状态,shape=(n_a,m)
parameters:---python字典格式
Returns:
a_next, c_next, yt_pred, cache
"""
Wf = parameters["Wf"]
bf = parameters["bf"]
Wi = parameters["Wi"]
bi = parameters["bi"]
Wc = parameters["Wc"]
bc = parameters["bc"]
Wo = parameters["Wo"]
bo = parameters["bo"]
Wy = parameters["Wy"]
by = parameters["by"]
# 得到xt和Wy的shape
n_x, m = xt.shape
n_y, n_a = Wy.shape
# 连接a_prev 和 xt
concat = np.zeros(shape=(n_x+n_a, m))
concat[:n_a, :] = a_prev
concat[n_a:, :] = xt # 这里将代码分成了3段来创建,也可以直接一段代码写掉np.vstack((a_prev, xt)
# 计算ft, kt, cct, c_next, ot, a_next
ft = sigmoid(np.dot(Wf, concat) + bf)
it = sigmoid(np.dot(Wi, concat) + bi)
cct = np.tanh(np.dot(Wc, concat) + bc)
c_next = np.multiply(ft, c_prev) + np.multiply(it, cct)
ot = sigmoid(np.dot(Wo, concat) + bo)
a_next = np.multiply(ot, np.tanh(c_next))
# 计算Y预测值
yt_pred = softmax(np.dot(Wy, a_next) + by)
# 需要反向传播的内容保存到cache中
cache = (a_next, c_next, a_prev, ft, it, cct, ot, xt, parameters)
return a_next, c_next, yt_pred, cache
2.2 长短时记忆网络的前向传播
def lstm_forward(x, a0, parameters):
"""LSTM前向传播过程
Args:
x: 输入数据,shape=(n_x,m,T_x)
a0: 初始激活a0,shape=(n_a,m)
parameters: 参数python字典
Returns:
a: 每一个时间步的a,shape=(n_a,m,T_x)
y: shape=(n_y, m, T_x)
cache: 保存反向传播时候需要计算的值,(lists of all the caches, x)
"""
# 初始化caches,使用列表来放置
caches = []
# 得到xt,Wy的shape
n_x, m, T_x = x.shape
n_y, n_a = parameters["Wy"].shape
# 用0初始值初始化:a,c,y
a = np.zeros(shape=(n_a, m, T_x))
c = np.zeros(shape=(n_a, m, T_x))
y = np.zeros(shape=(n_y, m, T_x))
# 初始化 a_next 和 c_next
a_next = a0
c_next = a0
# 每次迭代
for t in range(T_x):
# 更新next hidden state, next memory state, compute the preddiction, get the cache
a_next, c_next, yt, cache = lstm_cell_forward(x[:,:,t], a_prev=a_next, c_prev=c_next, parameters=parameters) # 索引会缩减规模
a[:,:,t] = a_next
y[:,:,t] = yt
c[:,:,t] = c_next
caches.append(cache)
caches = (caches, x)
return a, y, c, caches