深度学习框架pytorch RNN,LSTM,GRU

Recurrent layers

class torch.nn.RNN( args, * kwargs)(source)

将一个多层的 Elman RNN,激活函数为tanh或者ReLU,用于输入序列。

对输入序列中每个元素,RNN每层的计算公式为 h t = t a n h ( w i h x t + b i h + w h h h t − 1 + b h h ) h_t=tanh(w_{ih} x_t+b_{ih}+w_{hh} h_{t-1}+b_{hh}) ht=tanh(wihxt+bih+whhht1+bhh) h t h_t ht是时刻 t t t的隐状态。 x t x_t xt是上一层时刻 t t t的隐状态,或者是第一层在时刻 t t t的输入。如果nonlinearity=‘relu’,那么将使用relu代替tanh作为激活函数。

参数说明:

  • input_size – 输入x的特征数量。
  • hidden_size – 隐层的特征数量。
  • num_layers – RNN的层数。
  • nonlinearity – 指定非线性函数使用tanh还是relu。默认是tanh。
  • bias – 如果是False,那么RNN层就不会使用偏置权重 b i h b_{ih} bih b h h b_{hh} bhh,默认是True
  • batch_first – 如果True的话,那么输入Tensor的shape应该是[batch_size, time_step, feature],输出也是这样。
  • dropout – 如果值非零,那么除了最后一层外,其它层的输出都会套上一个dropout层。
  • bidirectional – 如果True,将会变成一个双向RNN,默认为False。

RNN的输入: (input, h 0 h_0 h0)

  • input (seq_len, batch, input_size): 保存输入序列特征的tensor。input可以是被填充的变长的序列。细节请看torch.nn.utils.rnn.pack_padded_sequence(input, lengths, batch_first=False)source

  • h 0 h_0 h0 (num_layers * num_directions, batch, hidden_size): 保存着初始隐状态的tensor

RNN的输出: (output, h n h_n hn)

  • output (seq_len, batch, hidden_size * num_directions): 保存着RNN最后一层的输出特征。如果输入是被填充过的序列,那么输出也是被填充的序列。
  • h n h_n hn (num_layers * num_directions, batch, hidden_size): 保存着最后一个时刻隐状态。

RNN模型参数:

  • w e i g h t i h l weight_ih_l weightihl[k] – 第k层的 input-hidden 权重, 可学习,形状是(input_size x hidden_size)。
  • w e i g h t h h l weight_hh_l weighthhl[k] – 第k层的 hidden-hidden 权重, 可学习,形状是(hidden_size x hidden_size)
  • b i a s i h l bias_ih_l biasihl[k] – 第k层的 input-hidden 偏置, 可学习,形状是(hidden_size)
  • b i a s h h l bias_hh_l biashhl[k] – 第k层的 hidden-hidden 偏置, 可学习,形状是(hidden_size)

示例:

rnn = nn.RNN(10, 20, 2)
input = Variable(torch.randn(5, 3, 10))
h0 = Variable(torch.randn(2, 3, 20))
output, hn = rnn(input, h0)

class torch.nn.LSTM( args, * kwargs)(source)

将一个多层的 (LSTM) 应用到输入序列。

对输入序列的每个元素,LSTM的每层都会执行以下计算: i t = s i g m o i d ( W i i x t + b i i + W h i h t − 1 + b h i )   f t = s i g m o i d ( W i f x t + b i f + W h f h t − 1 + b h f )   o t = s i g m o i d ( W i o x t + b i o + W h o h t − 1 + b h o )   g t = t a n h ( W i g x t + b i g + W h g h t − 1 + b h g )   c t = f t c t − 1 + i t g t   h t = o t ∗ t a n h ( c t ) \begin{aligned} i_t &= sigmoid(W_{ii}x_t+b_{ii}+W_{hi}h_{t-1}+b_{hi}) \ f_t &= sigmoid(W_{if}x_t+b_{if}+W_{hf}h_{t-1}+b_{hf}) \ o_t &= sigmoid(W_{io}x_t+b_{io}+W_{ho}h_{t-1}+b_{ho})\ g_t &= tanh(W_{ig}x_t+b_{ig}+W_{hg}h_{t-1}+b_{hg})\ c_t &= f_tc_{t-1}+i_tg_t\ h_t &= o_t*tanh(c_t) \end{aligned} it=sigmoid(Wiixt+bii+Whiht1+bhi) ft=sigmoid(Wifxt+bif+Whfht1+bhf) ot=sigmoid(Wioxt+bio+Whoht1+bho) gt=tanh(Wigxt+big+Whght1+bhg) ct=ftct1+itgt ht=ottanh(ct) h t h_t ht是时刻 t t t的隐状态, c t c_t ct是时刻 t t t的细胞状态, x t x_t xt是上一层的在时刻 t t t的隐状态或者是第一层在时刻 t t t的输入。 i t , f t , g t , o t i_t, f_t, g_t, o_t it,ft,gt,ot 分别代表 输入门,遗忘门,细胞和输出门。

参数说明:

  • input_size – 输入的特征维度
  • hidden_size – 隐状态的特征维度
  • num_layers – 层数(和时序展开要区分开)
  • bias – 如果为False,那么LSTM将不会使用 b i h , b h h b_{ih},b_{hh} bih,bhh,默认为True。
  • batch_first – 如果为True,那么输入和输出Tensor的形状为(batch, seq, feature)
  • dropout – 如果非零的话,将会在RNN的输出上加个dropout,最后一层除外。
  • bidirectional – 如果为True,将会变成一个双向RNN,默认为False。

LSTM输入: input, ( h 0 h_0 h0, c 0 c_0 c0)

  • input (seq_len, batch, input_size): 包含输入序列特征的Tensor。也可以是packed variable ,详见 [pack_padded_sequence](#torch.nn.utils.rnn.pack_padded_sequence(input, lengths, batch_first=False[source])
  • h 0 h_0 h0 (num_layers * num_directions, batch, hidden_size):保存着batch中每个元素的初始化隐状态的Tensor
  • c 0 c_0 c0 (num_layers * num_directions, batch, hidden_size): 保存着batch中每个元素的初始化细胞状态的Tensor

LSTM输出 output, ( h n h_n hn, c n c_n cn)

  • output (seq_len, batch, hidden_size * num_directions): 保存RNN最后一层的输出的Tensor。 如果输入是torch.nn.utils.rnn.PackedSequence,那么输出也是torch.nn.utils.rnn.PackedSequence
  • h n h_n hn (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的隐状态。
  • c n c_n cn (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的细胞状态。

LSTM模型参数:

  • w e i g h t i h l weight_ih_l weightihl[k] – 第k层可学习的input-hidden权重( W i i ∣ W i f ∣ W i g ∣ W i o W_{ii}|W_{if}|W_{ig}|W_{io} WiiWifWigWio),形状为(input_size x 4*hidden_size)
  • w e i g h t h h l weight_hh_l weighthhl[k] – 第k层可学习的hidden-hidden权重( W h i ∣ W h f ∣ W h g ∣ W h o W_{hi}|W_{hf}|W_{hg}|W_{ho} WhiWhfWhgWho),形状为(hidden_size x 4*hidden_size)。
  • b i a s i h l bias_ih_l biasihl[k] – 第k层可学习的input-hidden偏置( b i i ∣ b i f ∣ b i g ∣ b i o b_{ii}|b_{if}|b_{ig}|b_{io} biibifbigbio),形状为( 4*hidden_size)
  • b i a s h h l bias_hh_l biashhl[k] – 第k层可学习的hidden-hidden偏置( b h i ∣ b h f ∣ b h g ∣ b h o b_{hi}|b_{hf}|b_{hg}|b_{ho} bhibhfbhgbho),形状为( 4*hidden_size)。

示例:

lstm = nn.LSTM(10, 20, 2)
input = Variable(torch.randn(5, 3, 10))
h0 = Variable(torch.randn(2, 3, 20))
c0 = Variable(torch.randn(2, 3, 20))
output, hn = lstm(input, (h0, c0))

class torch.nn.GRU( args, * kwargs)(source)

将一个多层的GRU用于输入序列。
对输入序列中的每个元素,每层进行了一下计算:
r t = s i g m o i d ( W i r x t + b i r + W h r h ( t − 1 ) + b h r )   i t = s i g m o i d ( W i i x t + b i i + W h i h ( t − 1 ) + b h i )   n t = t a n h ( W i n x t + b i n + r t ( W h n h ( t − 1 ) + b h n ) )   h t = ( 1 − i t ) n t + i t ∗ h ( t − 1 ) \begin{aligned} r_t&=sigmoid(W_{ir}x_t+b_{ir}+W_{hr}h_{(t-1)}+b_{hr})\ i_t&=sigmoid(W_{ii}x_t+b_{ii}+W_{hi}h_{(t-1)}+b_{hi})\ n_t&=tanh(W_{in}x_t+b_{in}+rt(W_{hn}h_{(t-1)}+b_{hn}))\ h_t&=(1-i_t) nt+i_t*h(t-1) \end{aligned} rt=sigmoid(Wirxt+bir+Whrh(t1)+bhr) it=sigmoid(Wiixt+bii+Whih(t1)+bhi) nt=tanh(Winxt+bin+rt(Whnh(t1)+bhn)) ht=(1it)nt+ith(t1) h t h_t ht是是时间 t t t的上的隐状态, x t x_t xt是前一层 t t t时刻的隐状态或者是第一层的 t t t时刻的输入, r t , i t , n t r_t, i_t, n_t rt,it,nt分别是重置门,输入门和新门。

参数说明:

  • input_size – 期望的输入 x x x的特征值的维度
  • hidden_size – 隐状态的维度
  • num_layers – RNN的层数。
  • bias – 如果为False,那么RNN层将不会使用bias,默认为True。
  • batch_first – 如果为True的话,那么输入和输出的tensor的形状是(batch, seq, feature)。
  • dropout – 如果非零的话,将会在RNN的输出上加个dropout,最后一层除外。
  • bidirectional – 如果为True,将会变成一个双向RNN,默认为False。

GRU输入: (input, h 0 h_0 h0)

  • input (seq_len, batch, input_size): 包含输入序列特征的Tensor。也可以是packed variable ,详见 [pack_padded_sequence](#torch.nn.utils.rnn.pack_padded_sequence(input, lengths, batch_first=False[source])。
  • h 0 h_0 h0 (num_layers * num_directions, batch, hidden_size):保存着batch中每个元素的初始化隐状态的Tensor

GRU输出: (output, h n h_n hn)

  • output (seq_len, batch, hidden_size * num_directions): ten保存RNN最后一层的输出的Tensor。 如果输入是torch.nn.utils.rnn.PackedSequence,那么输出也是torch.nn.utils.rnn.PackedSequence。
  • h n h_n hn (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的隐状态。

变量:

  • w e i g h t i h l weight_ih_l weightihl[k] – 第k层可学习的input-hidden权重( W i r ∣ W i i ∣ W i n W_{ir}|W_{ii}|W_{in} WirWiiWin),形状为(input_size x 3*hidden_size)
  • w e i g h t h h l weight_hh_l weighthhl[k] – 第k层可学习的hidden-hidden权重( W h r ∣ W h i ∣ W h n W_{hr}|W_{hi}|W_{hn} WhrWhiWhn),形状为(hidden_size x 3*hidden_size)。
  • b i a s i h l bias_ih_l biasihl[k] – 第k层可学习的input-hidden偏置( b i r ∣ b i i ∣ b i n b_{ir}|b_{ii}|b_{in} birbiibin),形状为( 3*hidden_size)
  • b i a s h h l bias_hh_l biashhl[k] – 第k层可学习的hidden-hidden偏置( b h r ∣ b h i ∣ b h n b_{hr}|b_{hi}|b_{hn} bhrbhibhn),形状为( 3*hidden_size)。

例子:

 rnn = nn.GRU(10, 20, 2)
 input = Variable(torch.randn(5, 3, 10))
 h0 = Variable(torch.randn(2, 3, 20))
 output, hn = rnn(input, h0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值