初步了解RNN、LSTM、GRU

<总是容易忘== 接下来不断更新吧>

RNN最为原始的循环神经网络----容易梯度消失、梯度爆炸。

GRU只有两个门(update和reset)LSTM有三个门(forget,input,output)
GRU直接将hidden state 传给下一个单元,而LSTM则用memory cell 把hidden state 包装起来。
在这里插入图片描述

LSTM图解
在这里插入图片描述
在这里插入图片描述

(中间的 cell 里面有四个黄色小框,每一个小黄框代表一个前馈网络层,就是经典的神经网络的结构,num_units就是这个层的隐藏神经元个数。)

  1. cell 的状态是一个向量,是有多个值的;
  2. 上一次的状态 h(t-1)直接和下一次的输入 x(t) 结合(concat)起来的。比如 x是28位的向量,h(t-1)是128位的,那么拼起来就是156位的向量;
  3. cell 的权重共享----图片上有三个绿色的大框,代表三个 cell 。但是实际上,它只是代表了一个 cell 在不同时序时候的状态,所有的数据只会通过一个 cell,然后不断更新它的权重。同一个cell前后时序共享
  4. LSTM参数的数量就是这个 cell 里面用到的参数个数。假设 num_units 是128,输入是28位的,那么根据上面的第 2 点,可以得到,四个小黄框的参数一共有 (128+28)×(128×4),也就是156 ×512。
  5. cell 最上面的一条线的状态即 s(t) 代表了长时记忆,而下面的 h(t)则代表了工作记忆或短时记忆。
class ConvLSTMCell(nn.Module):
 
    def __init__(self, input_size, input_dim, hidden_dim, kernel_size, bias):
        """
        Initialize ConvLSTM cell.
        Parameters
        ----------
        input_size: (int, int)
            Height and width of input tensor as (height, width).
        input_dim: int
            Number of channels of input tensor.
        hidden_dim: int
            Number of channels of hidden state.
        kernel_size: (int, int)
            Size of the convolutional kernel.
        bias: bool
            Whether or not to add the bias.
        """
 
        super(ConvLSTMCell, self).__init__()
 
        self.height, self.width = input_size
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
 
        self.kernel_size = kernel_size
        self.padding = kernel_size[0] // 2, kernel_size[1] // 2
        self.bias = bias
 
        self.conv = nn.Conv2d(in_channels=self.input_dim + self.hidden_dim,
                              out_channels=4 * self.hidden_dim,
                              kernel_size=self.kernel_size,
                              padding=self.padding,
                              bias=self.bias)
 
    def forward(self, input_tensor, cur_state):
        '''
        :param input_tensor:[batch,dim,inp_height,inp_width]
        :param cur_state: [h,c] h:[batch,dim,H,W]
        :return:
        '''
        h_cur, c_cur = cur_state
 
        combined = torch.cat([input_tensor, h_cur], dim=1)  
 
        combined_conv = self.conv(combined)
        cc_i, cc_f, cc_o, cc_g = torch.split(combined_conv, self.hidden_dim, dim=1)
        i = torch.sigmoid(cc_i)
        f = torch.sigmoid(cc_f)
        o = torch.sigmoid(cc_o)
        g = torch.tanh(cc_g)
 
        c_next = f * c_cur + i * g
        h_next = o * torch.tanh(c_next)
 
        return h_next, c_next
 
    def init_hidden(self, batch_size):
        return (Variable(torch.zeros(batch_size, self.hidden_dim, self.height, self.width)),
                Variable(torch.zeros(batch_size, self.hidden_dim, self.height, self.width)))

GRU

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参考

  1. LSTM和GRU的解析从未如此清晰(动图+视频)
  2. 知乎
  3. 人人都能看懂的GRU
  4. Pytorch_LSTM与GRU:
  5. pytorch中RNN,LSTM,GRU使用详解:
  6. LSTM模型与前向反向传播算法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值