Pytorch中nn.LSTM与nn.LSTMCell

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

在这里插入图片描述

对输入序列的每个元素,LSTM的每层都会执行以下计算:
在这里插入图片描述

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 – 输入的特征维度,(特征向量的长度,如2048)
    hidden_size – 隐状态的特征维度,(每个LSTM单元或者时间步的输出的ht的维度,单元内部有权重与偏差计算)
    num_layers – 层数(和时序展开要区分开), RNN层的个数(在竖直方向堆叠的多个相同个数单元的层数)
    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, c_0)

  • input (seq_len, batch, input_size): 包含输入序列特征的Tensor。也可以是packed variable。 时间步数或序列长度,batch数,输入特征维度。如果设置了batch_first,则batch为第一维。
  • h_0 (num_layers * num_directions, batch,hidden_size):保存着batch中每个元素的初始化隐状态的Tensor
  • c_0 (num_layers *num_directions, batch, hidden_size): 保存着batch中每个元素的初始化细胞状态的Tensor

LSTM输出 output, (h_n, c_n)

  • output (seq_len, batch, hidden_size * num_directions):保存RNN最后一层的输出的Tensor。 如果输入是torch.nn.utils.rnn.PackedSequence,那么输出也是torch.nn.utils.rnn.PackedSequence。包含每一个时刻的输出特征,如果设置了batch_first,则batch为第一维
  • h_n (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的隐状态。
  • c_n (num_layers * num_directions, batch, hidden_size): Tensor,保存着RNN最后一个时间步的细胞状态。

LSTM模型参数:

  • weight_ih_l[k] – 第k层可学习的input-hidden权重(

    W

    i

    i

    W

    i

    f

    W

    i

    g

    W

    i

    o

    W_{ii}|W_{if}|W_{ig}|W_{io}

    Wii?∣Wif?∣Wig?∣Wio?),形状为(input_size x 4*hidden_size)

  • weight_hh_l[k] – 第k层可学习的hidden-hidden权重(

    W

    h

    i

    W

    h

    f

    W

    h

    g

    W

    h

    o

    W_{hi}|W_{hf}|W_{hg}|W_{ho}

    Whi?∣Whf?∣Whg?∣Who?),形状为(hidden_size x 4*hidden_size)。

  • bias_ih_l[k] – 第k层可学习的input-hidden偏置(

    b

    i

    i

    b

    i

    f

    b

    i

    g

    b

    i

    o

    b_{ii}|b_{if}|b_{ig}|b_{io}

    bii?∣bif?∣big?∣bio?),形状为( 4*hidden_size)

  • bias_hh_l[k] –第k层可学习的hidden-hidden偏置(

    b

    h

    i

    b

    h

    f

    b

    h

    g

    b

    h

    o

    b_{hi}|b_{hf}|b_{hg}|b_{ho}

    bhi?∣bhf?∣bhg?∣bho?),形状为(4*hidden_size)。

示例:

1
2
3
4
5
lstm = nn.LSTM(10, 20, 2)#[feature_len,hidden_len,num_layers]
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.LSTMCell(input_size, hidden_size, bias=True)

该模块构建LSTM中的一个Cell,同一层会共享这一个Cell,但要手动处理每个时刻的迭代计算过程。如果要建立多层的LSTM,就要建立多个nn.LSTMCell。
在这里插入图片描述
参数:

  • input_size – 输入的特征维度。
  • hidden_size – 隐状态的维度。
  • bias – 如果为False,那么将不会使用bias。默认为True。

输入: input, (h_0, c_0)

  • input (seq_len, batch, input_size): 包含输入序列特征的Tensor。也可以是packed variable
  • h_0 ( batch, hidden_size):保存着batch中每个元素的初始化隐状态的Tensor
  • c_0 (batch, hidden_size): 保存着batch中每个元素的初始化细胞状态的Tensor

输出: h_1, c_1

  • h_1 (batch, hidden_size): 下一个时刻的隐状态。
  • c_1 (batch, hidden_size): 下一个时刻的细胞状态。

模型参数:

  • weight_ih – input-hidden权重(

    W

    i

    i

    W

    i

    f

    W

    i

    g

    W

    i

    o

    W_{ii}|W_{if}|W_{ig}|W_{io}

    Wii?∣Wif?∣Wig?∣Wio?),形状为(input_size x 4*hidden_size)

  • weight_hh – hidden-hidden权重(

    W

    h

    i

    W

    h

    f

    W

    h

    g

    W

    h

    o

    W_{hi}|W_{hf}|W_{hg}|W_{ho}

    Whi?∣Whf?∣Whg?∣Who?),形状为(hidden_size x 4*hidden_size)。

  • bias_ih – input-hidden偏置(

    b

    i

    i

    b

    i

    f

    b

    i

    g

    b

    i

    o

    b_{ii}|b_{if}|b_{ig}|b_{io}

    bii?∣bif?∣big?∣bio?),形状为( 4*hidden_size)

  • bias_hh – hidden-hidden偏置(

    b

    h

    i

    b

    h

    f

    b

    h

    g

    b

    h

    o

    b_{hi}|b_{hf}|b_{hg}|b_{ho}

    bhi?∣bhf?∣bhg?∣bho?),形状为( 4*hidden_size)。

实例:

1
2
3
4
5
6
7
8
rnn = nn.LSTMCell(10, 20)
input = Variable(torch.randn(6, 3, 10))
hx = Variable(torch.randn(3, 20))
cx = Variable(torch.randn(3, 20))
output = []
for i in range(6):
   hx, cx = rnn(input[i], (hx, cx))
   output.append(hx)