torch.nn.Module和torch.nn.LSTM 相关文档

torch.nn.Module和torch.nn.LSTM 相关文档

搬运官方链接
class torch.nn.Module
所有网络的基类,你的模型也应该继承这个类。

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)# submodule: Conv2d
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
       x = F.relu(self.conv1(x))
       return F.relu(self.conv2(x))

**

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_{ih},b_{hh}$,默认为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 ,详见 [pack_padded_sequence](#torch.nn.utils.rnn.pack_padded_sequence(input, lengths, batch_first=False[source])

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。

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_{ii}|W_{if}|W_{ig}|W_{io}$),形状为(input_size x 4*hidden_size)

weight_hh_l[k] – 第k层可学习的hidden-hidden权重($W_{hi}|W_{hf}|W_{hg}|W_{ho}$),形状为(hidden_size x 4*hidden_size)。

bias_ih_l[k] – 第k层可学习的input-hidden偏置($b_{ii}|b_{if}|b_{ig}|b_{io}$),形状为( 4*hidden_size)

bias_hh_l[k] – 第k层可学习的hidden-hidden偏置($b_{hi}|b_{hf}|b_{hg}|b_{ho}$),形状为( 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))
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值