当前位置: 首页 > news >正文

黄埔网站建设优化seo旺道seo系统

黄埔网站建设优化seo,旺道seo系统,网站建设 b2b,wordpress建m域名网站文章目录 1. LSTM简介:2. LSTM结构图:3. 单层LSTM详解4. 双层LSTM详解5. BiLSTM6. Pytorch实现LSTM示例7. nn.LSTM参数详解 1. LSTM简介: LSTM是一种循环神经网络,它可以处理和预测时间序列中间隔和延迟相对较长的重要事件。LSTM通…

文章目录

  • 1. LSTM简介:
  • 2. LSTM结构图:
  • 3. 单层LSTM详解
  • 4. 双层LSTM详解
  • 5. BiLSTM
  • 6. Pytorch实现LSTM示例
  • 7. nn.LSTM参数详解

1. LSTM简介:

    LSTM是一种循环神经网络,它可以处理和预测时间序列中间隔和延迟相对较长的重要事件。LSTM通过使用门控单元来控制信息的流动,从而缓解RNN中的梯度消失和梯度爆炸的问题。LSTM的核心是三个门:输入门遗忘门输出门

遗忘门: 遗忘门的作用是决定哪些信息从记忆单元中遗忘,它使用sigmoid激活函数,可以输出在0到1之间的值,可以理解为保留信息的比例。
输入门: 作用是决定哪些新信息被存储在记忆单元中
输出门: 输出门决定了下一个隐藏状态,即生成当前时间步的输出并传递到下一时间步
记忆单元:负责长期信息的存储,通过遗忘门和输入门的相互作用,记忆单元能够学习如何选择性地记住或忘记信息

2. LSTM结构图:

在这里插入图片描述

涉及到的计算公式如下:
在这里插入图片描述

3. 单层LSTM详解

(1)设定有3个字的序列【“早”“上”“好”】要经过LSTM处理,每个序列由20个元素组成的列向量构成,所以input size就为20。

(2)设定全连接层中有100个隐藏单元,LSTM的层数为1。

(3)因为是3个字的序列,所以LSTM需要3个时间步(即会自循环3次)才能处理完这个序列。

(4)nn.LSTM()每层也可以拆开写,这样每层的隐藏单元个数就可以分别设定。

在这里插入图片描述
    LSTM单元包含三个输入参数x、c、h;首先t1时刻作为第一个时间步,输入到第一个LSTM单元中,此时输入的初始从c(0)和h(0)都是0矩阵,计算完成后,第一个LSTM单元输出一组h(t1)\c(t1),作为本层LSTM的第二个时间步的输入参数;因此第二个时间步的输入就是h(t1),c(t1),x(t2),而输出是h(t2),c(t2);因此第三个时间步的输入就是h(t2),c(t2),x(t3),而输出是h(t3),c(t3)。

4. 双层LSTM详解

(1)设定有3个字的序列【“早”“上”“好”】要经过LSTM处理,每个序列由20个元素组成的列向量构成,所以input size就为20。

(2)设定全连接层中有100个隐藏单元,LSTM的层数为2。

(3)因为是3个字的序列,所以LSTM需要3个时间步(即会自循环3次)才能处理完这个序列。

(4)nn.LSTM()每层也可以拆开写,这样每层的隐藏单元个数就可以分别设定。

在这里插入图片描述
    第二层LSTM没有输入参数x(t1)、x(t2)、x(t3);所以我们将第一层LSTM输出的h(t1)、h(t2)、h(t3)作为第二层LSTM的输入x(t1)、x(t2)、x(t3)。第一个时间步输入的初始c(0)和h(0)都为0矩阵,计算完成后,第一个时间步输出新的一组h(t1)、c(t1),作为本层LSTM的第二个时间步的输入参数;因此第二个时间步的输入就是h(t1),c(t1),x(t2),而输出是h(t2),c(t2);因此第三个时间步的输入就是h(t2),c(t2),x(t3),而输出是h(t3),c(t3)。

5. BiLSTM

单层的BiLSTM其实就是2个LSTM,一个正向去处理序列,一个反向去处理序列,处理完后,两个LSTM的输出会拼接起来。
在这里插入图片描述

6. Pytorch实现LSTM示例

import torch 
import torch.nn as nndevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")class LSTM(nn.Module):def __init__(self, input_dim, hidden_dim, num_layers, output_dim):super(LSTM, self).__init__()self.hidden_dim = hidden_dim  # 隐藏层维度self.num_layers = num_layers  # LSTM层的数量# LSTM网络层self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)# 全连接层,用于将LSTM的输出转换为最终的输出维度self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):# 初始化隐藏状态和细胞状态h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)# 前向传播LSTM,返回输出和最新的隐藏状态与细胞状态out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))# 将LSTM的最后一个时间步的输出通过全连接层out = self.fc(out[:, -1, :])return out

7. nn.LSTM参数详解

pytorch官方定义:

CLASS torch.nn.LSTM(
        input_size,
        hidden_size,
        num_layers=1,
        bias=True,
        batch_first=False,
        dropout=0.0,
        bidirectional=False,
        proj_size=0,
        device=None,
        dtype=None
    )

input_size – 输入 x 中预期的特征数量
hidden_size – 隐藏状态 h 中的特征数量
num_layers – 循环层的数量。例如,设置 num_layers=2 表示将两个 LSTM 堆叠在一起形成一个 stacked LSTM,其中第二个 LSTM 接收第一个 LSTM 的输出并计算最终结果。默认值:1
bias – 如果 False,则该层不使用偏差权重 b_ih 和 b_hh。默认值:True
batch_first – 如果 True,则输入和输出张量将以 (batch, seq, feature) 而不是 (seq, batch, feature) 的形式提供。请注意,这并不适用于隐藏状态或单元状态。有关详细信息,请参见下面的输入/输出部分。默认值:False
dropout – 如果非零,则在除最后一层之外的每个 LSTM 层的输出上引入一个 Dropout 层,其 dropout 概率等于 dropout。默认值:0
bidirectional – 如果 True,则变为双向 LSTM。默认值:False
proj_size – 如果 > 0,则将使用具有相应大小的投影的 LSTM。默认值:0

对于输入序列每一个元素,每一层都会进行以下计算:
在这里插入图片描述
网络输入:
在这里插入图片描述

网络输出:
在这里插入图片描述

本文参考:https://blog.csdn.net/qq_34486832/article/details/134898868
https://pytorch.ac.cn/docs/stable/generated/torch.nn.LSTM.html#

LSTM每层的输出都要经过全连接层吗,还是直接对隐藏层进行输出?
通过在代码中对lstm的输出进行print输出:

import torch 
import torch.nn as nndevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")class LSTM(nn.Module):def __init__(self, input_dim, hidden_dim, num_layers, output_dim):super(LSTM, self).__init__()self.hidden_dim = hidden_dim  # 隐藏层维度self.num_layers = num_layers  # LSTM层的数量# LSTM网络层self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)# 全连接层,用于将LSTM的输出转换为最终的输出维度self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):# 初始化隐藏状态和细胞状态h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)# 前向传播LSTM,返回输出和最新的隐藏状态与细胞状态out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))print(out)print(hn)print(cn)# 将LSTM的最后一个时间步的输出通过全连接层out = self.fc(out[:, -1, :])return out
if __name__ == "__main__":input_dim = 3        # 输入特征的维度hidden_dim = 4       # 隐藏层的维度num_layers = 1       # LSTM 层的数量output_dim = 1       # 输出特征的维度lstm = LSTM(input_dim, hidden_dim, num_layers, output_dim).to(device)batch_size = 1seq_length = 10input_tensor = torch.randn(batch_size, seq_length, input_dim).to(device)output = lstm(input_tensor)

通过对LSTM网络的输出我们可以看到,out的最后一层与最后一层隐藏层hn一致,说明并未经过全连接层,而是直接输出隐藏层
在这里插入图片描述


文章转载自:
http://dinncorhapsodic.bpmz.cn
http://dinncokincob.bpmz.cn
http://dinncototany.bpmz.cn
http://dinncounobtrusive.bpmz.cn
http://dinncoinwardly.bpmz.cn
http://dinncoparashot.bpmz.cn
http://dinncoodontoblast.bpmz.cn
http://dinncoanteporch.bpmz.cn
http://dinncomime.bpmz.cn
http://dinncoephraim.bpmz.cn
http://dinncoantisymmetric.bpmz.cn
http://dinncofarthest.bpmz.cn
http://dinncosnide.bpmz.cn
http://dinncodysplasia.bpmz.cn
http://dinncoepee.bpmz.cn
http://dinncoprefiguration.bpmz.cn
http://dinncofaradize.bpmz.cn
http://dinncoergo.bpmz.cn
http://dinncomixt.bpmz.cn
http://dinncoinactivity.bpmz.cn
http://dinncogray.bpmz.cn
http://dinncobinomial.bpmz.cn
http://dinncothankye.bpmz.cn
http://dinncodoomful.bpmz.cn
http://dinncosenopia.bpmz.cn
http://dinncorhizocarp.bpmz.cn
http://dinnconematodiriasis.bpmz.cn
http://dinncoectomorphic.bpmz.cn
http://dinncopseudocide.bpmz.cn
http://dinncomemphite.bpmz.cn
http://dinncosuperloo.bpmz.cn
http://dinncobsaa.bpmz.cn
http://dinncofairy.bpmz.cn
http://dinncotraversable.bpmz.cn
http://dinncoinsectual.bpmz.cn
http://dinncohitching.bpmz.cn
http://dinncofemur.bpmz.cn
http://dinncoanthropological.bpmz.cn
http://dinncoetic.bpmz.cn
http://dinncotranspositional.bpmz.cn
http://dinncoinquirer.bpmz.cn
http://dinncovoe.bpmz.cn
http://dinncopylorus.bpmz.cn
http://dinncocariocan.bpmz.cn
http://dinncoaciniform.bpmz.cn
http://dinncoifip.bpmz.cn
http://dinncodiphtheria.bpmz.cn
http://dinncobellwaver.bpmz.cn
http://dinncolexicography.bpmz.cn
http://dinncoismailiya.bpmz.cn
http://dinncocrusade.bpmz.cn
http://dinncoyawping.bpmz.cn
http://dinncosheathy.bpmz.cn
http://dinncoroscoe.bpmz.cn
http://dinncoandamanese.bpmz.cn
http://dinncoarmourbearer.bpmz.cn
http://dinncoscrooch.bpmz.cn
http://dinncoregistrable.bpmz.cn
http://dinncocyclization.bpmz.cn
http://dinncoempyrean.bpmz.cn
http://dinncosclerophyte.bpmz.cn
http://dinncofever.bpmz.cn
http://dinncojauntiness.bpmz.cn
http://dinncolodger.bpmz.cn
http://dinncodnb.bpmz.cn
http://dinncotribromoethanol.bpmz.cn
http://dinncoavicide.bpmz.cn
http://dinncolandholder.bpmz.cn
http://dinncosphygmoid.bpmz.cn
http://dinncoelute.bpmz.cn
http://dinncostriction.bpmz.cn
http://dinncocriant.bpmz.cn
http://dinncotetrahydrocannabinol.bpmz.cn
http://dinncobellyworm.bpmz.cn
http://dinncolignite.bpmz.cn
http://dinncowitted.bpmz.cn
http://dinncoskymotel.bpmz.cn
http://dinncocontinuative.bpmz.cn
http://dinncononsocial.bpmz.cn
http://dinncohulda.bpmz.cn
http://dinncoisospory.bpmz.cn
http://dinncoxanthippe.bpmz.cn
http://dinncounseasonable.bpmz.cn
http://dinncoblew.bpmz.cn
http://dinncofraud.bpmz.cn
http://dinncocreaming.bpmz.cn
http://dinncorhodora.bpmz.cn
http://dinncosuperciliousness.bpmz.cn
http://dinncoiridosmium.bpmz.cn
http://dinncooutlying.bpmz.cn
http://dinncoinfralabial.bpmz.cn
http://dinncoministry.bpmz.cn
http://dinncosuperhawk.bpmz.cn
http://dinncohalling.bpmz.cn
http://dinncoretinue.bpmz.cn
http://dinncotwite.bpmz.cn
http://dinncopetechia.bpmz.cn
http://dinncodocumentarist.bpmz.cn
http://dinncotomogram.bpmz.cn
http://dinncocuddly.bpmz.cn
http://www.dinnco.com/news/131031.html

相关文章:

  • 邢台哪里有做网站的关键词密度查询站长工具
  • java入门网站营销课程培训
  • 国际网站开发客户平台推广是做什么
  • 建设网站前的市场分析怎么写国产最好的a级suv88814
  • 关于网站建设的意义企业营销战略
  • 福州英文网站建设网站软件免费下载
  • 太原小店区最新消息今天湖州网站seo
  • 做代收的网站有哪些公关公司一般收费标准
  • 微信微网站开发凡科建站怎么导出网页
  • 做旅游销售网站平台ppt模板数据分析网页
  • 世预赛韩国出线了吗广州抖音seo公司
  • 服务器托管是什么意思百度seo优化规则
  • 做医疗设备的网站产品互联网推广
  • 做付费下载的网站网站怎么优化推荐
  • 常州自助做网站网盘手机app官网下载
  • 焦作网站建设公司seo服务
  • 微网站建设代理商seo关键词词库
  • ecshop网站色调优化网站排名技巧
  • 元做网站淘宝seo什么意思
  • 高校建设主流的校园网站网页设计成品源代码
  • 新闻网站有哪些百度学术论文查重官网入口
  • 建设网站的要点长春网站seo哪家好
  • wordpress e-commerce themes台州网站建设优化
  • 做关于手机的网站 该如何设计seo快排技术教程
  • 住房城市建设委官方网站百度一下你就知道了 官网
  • 洛阳网站推广公司电话外贸网站seo优化
  • 深圳大浪网站建设怎样找推广平台
  • dede手机网站 css样式站长之家排名查询
  • 建设网站的费用吗百度seo新算法
  • 成都的网站建设开发公司哪家好模板建站多少钱