pytorch nn.LSTM模块参数详解 您所在的位置:网站首页 pytorch默认参数 pytorch nn.LSTM模块参数详解

pytorch nn.LSTM模块参数详解

2024-06-03 05:22| 来源: 网络整理| 查看: 265

nn.LSTM模块参数

input_size :输入的维度

hidden_size:h的维度

num_layers:堆叠LSTM的层数,默认值为1

bias:偏置 ,默认值:True

batch_first: 如果是True,则input为(batch, seq, input_size)。默认值为:False(seq_len, batch, input_size)

bidirectional :是否双向传播,默认值为False

 

输入

(input_size, hideen_size)

以训练句子为例子,假如每个词是100维的向量,每个句子含有24个单词,一次训练10个句子。那么batch_size=10,seq=24,input_size=100。(seq指的是句子的长度,input_size作为一个的输入) ,所以在设置LSTM网络的过程中input_size=100。由于seq的长度是24,那么这个LSTM结构会循环24次最后输出预设的结果。如下图所示。

预设的hidden_size,这个hideen_size主要是下面LSTM公式中的各个W和b的维度设置,以g_{t}为例子,假设hideen_size为16,则W_{ig}为16*100,x_{t}为100*1,W_{hg}为16*16,h_{t-1}为16*1。

 

输出

output:(seq_len, batch, num_directions * hidden_size)

h_n:(num_layers * num_directions, batch, hidden_size)

c_n :(num_layers * num_directions, batch, hidden_size)注:num_directions 表示单向、双向

单向

import torch.nn as nn import torch x = torch.rand(10,24,100) lstm = nn.LSTM(100,16,num_layers=2) output,(h,c) = lstm(x) print(output.size()) print(h.size()) print(c.size()) output: torch.Size([24, 10, 16]) torch.Size([2, 10, 16]) torch.Size([2, 10, 16])

双向

import torch.nn as nn import torch x = torch.rand(10,24,100) lstm = nn.LSTM(100,16,bidirectional=True) output,(h,c) = lstm(x) print(output.size()) print(h.size()) print(c.size()) output: torch.Size([24, 10, 32]) torch.Size([2, 10, 16]) torch.Size([2, 10, 16])

使用h0、c0

import torch.nn as nn import torch x = torch.rand(24,10,100) #seq,batch,input_size h0 = torch.rand(1,10,16)# num_layers*num_directions, batch, hidden_size c0 = torch.rand(1,10,16) lstm = nn.LSTM(100,16) output,(h,c) = lstm(x,(h0,c0))

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有