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

Jurgen提出的Highway Networks:LSTM时间维方法应用到深度维

Jurgen提出的Highway Networks:LSTM时间维方法应用到深度维

具体实例与推演

假设我们有一个离散型随机变量 X X X,它表示掷一枚骰子得到的点数,求 X X X 的期望。

  • 步骤
    1. 列出 X X X 的所有可能取值 x i x_i xi(1, 2, 3, 4, 5, 6)。
    2. 计算每个 x i x_i xi 出现的概率 p i p_i pi(均为 1/6)。
    3. 应用期望公式计算 E ( X ) E(X) E(X)

E ( X ) = 1 ⋅ 1 6 + 2 ⋅ 1 6 + ⋯ + 6 ⋅ 1 6 = 3.5 E(X) = 1 \cdot \frac{1}{6} + 2 \cdot \frac{1}{6} + \cdots + 6 \cdot \frac{1}{6} = 3.5 E(X)=161+261++661=3.5

第一节:LSTM与Highway Networks的类比与核心概念

1.1 LSTM与Highway Networks核心公式

LSTM公式

i t = σ ( W i i x t + W h i h t − 1 + b i ) f t = σ ( W i f x t + W h f h t − 1 + b f ) o t = σ ( W i o x t + W h o h t − 1 + b o ) g t = tanh ⁡ ( W i g x t + W h g h t − 1 + b g ) c t = f t ⊙ c t − 1 + i t ⊙ g t h t = o t ⊙ tanh ⁡ ( c t ) \begin{aligned} i_t &= \sigma(W_{ii} x_t + W_{hi} h_{t-1} + b_i) \\ f_t &= \sigma(W_{if} x_t + W_{hf} h_{t-1} + b_f) \\ o_t &= \sigma(W_{io} x_t + W_{ho} h_{t-1} + b_o) \\ g_t &= \tanh(W_{ig} x_t + W_{hg} h_{t-1} + b_g) \\ c_t &= f_t \odot c_{t-1} + i_t \odot g_t \\ h_t &= o_t \odot \tanh(c_t) \\ \end{aligned} itftotgtctht=σ(Wiixt+Whiht1+bi)=σ(Wifxt+Whfht1+bf)=σ(Wioxt+Whoht1+bo)=tanh(Wigxt+Whght1+bg)=ftct1+itgt=ottanh(ct)

Highway Networks公式

H = σ ( W H x + b H ) T = σ ( W T x + b T ) y = H ⊙ T + x ⊙ ( 1 − T ) \begin{aligned} H &= \sigma(W_H x + b_H) \\ T &= \sigma(W_T x + b_T) \\ y &= H \odot T + x \odot (1 - T) \\ \end{aligned} HTy=σ(WHx+bH)=σ(WTx+bT)=HT+x(1T)

1.2 核心解释

核心概念定义比喻或解释
LSTM一种解决长时间依赖问题的RNN架构,使用门控机制控制信息流动。就像记忆模块,能够选择性记住或忘记信息。
Highway Networks将LSTM的门控机制应用到深度学习网络,允许信息直接通过网络层类似于在复杂路网上增加高速公路,使信息传输更快速高效。

1.3 优势与劣势

方面描述
优势解决了深度网络中的梯度消失问题,提高了信息传递效率。
劣势需要更多的参数和计算资源。

1.4 类比与总结

Highway Networks通过引入门控机制,使得信息在深度网络中能够更有效地传递。这就像在复杂的交通网络中增加高速公路,使得车辆能够更快速地到达目的地。

第四节:核心代码与可视化

4.1 Python代码示例

以下是演示如何应用Highway Networks和LSTM的Python代码示例:

import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import seaborn as sns# 定义LSTM模型
class LSTMModel(nn.Module):def __init__(self, input_dim, hidden_dim, output_dim):super(LSTMModel, self).__init__()self.lstm = nn.LSTM(input_dim, hidden_dim, batch_first=True)self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):h0 = torch.zeros(1, x.size(0), hidden_dim).to(device)c0 = torch.zeros(1, x.size(0), hidden_dim).to(device)out, _ = self.lstm(x, (h0, c0))out = self.fc(out[:, -1, :])return out# 定义Highway Network模型
class HighwayModel(nn.Module):def __init__(self, input_dim, hidden_dim, output_dim):super(HighwayModel, self).__init__()self.fc1 = nn.Linear(input_dim, hidden_dim)self.fc2 = nn.Linear(hidden_dim, output_dim)self.t = nn.Linear(hidden_dim, output_dim)def forward(self, x):H = torch.relu(self.fc1(x))T = torch.sigmoid(self.t(x))out = H * T + x * (1 - T)return out# 生成数据并训练模型
input_dim = 10
hidden_dim = 20
output_dim = 1
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")# 创建模型实例
lstm_model = LSTMModel(input_dim, hidden_dim, output_dim).to(device)
highway_model = HighwayModel(input_dim, hidden_dim, output_dim).to(device)# 损失函数和优化器
criterion = nn.MSELoss()
optimizer_lstm = optim.Adam(lstm_model.parameters(), lr=0.01)
optimizer_highway = optim.Adam(highway_model.parameters(), lr=0.01)# 训练过程示例
epochs = 100
for epoch in range(epochs):# 生成随机输入数据inputs = torch.randn(100, 1, input_dim).to(device)targets = torch.randn(100, output_dim).to(device)# 训练LSTM模型outputs_lstm = lstm_model(inputs)loss_lstm = criterion(outputs_lstm, targets)optimizer_lstm.zero_grad()loss_lstm.backward()optimizer_lstm.step()# 训练Highway Network模型inputs_highway = inputs.view(-1, input_dim)outputs_highway = highway_model(inputs_highway)loss_highway = criterion(outputs_highway, targets)optimizer_highway.zero_grad()loss_highway.backward()optimizer_highway.step()# 可视化损失函数
sns.set_theme(style="whitegrid")
plt.plot(range(epochs), [loss_lstm.item() for _ in range(epochs)], label='LSTM Loss')
plt.plot(range(epochs), [loss_highway.item() for _ in range(epochs)], label='Highway Network Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('LSTM vs Highway Network Loss')
plt.legend()
plt.show()

4.2 解释与可视化

  • 代码功能:定义LSTM和Highway Networks模型,对比二者在训练过程中的损失函数变化。
  • 可视化结果:展示LSTM和Highway Networks在训练过程中的损失函数变化,比较二者的收敛速度和效果。

参考文献

  1. Srivastava, R. K., Greff, K., & Schmidhuber, J. (2015). Highway Networks. arXiv preprint arXiv:1505.00387.
  2. He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep Residual Learning for Image Recognition. Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 770-778).

关键词:

#Highway Networks #LSTM #ResNet #深度学习 #门控机制

http://www.lryc.cn/news/517407.html

相关文章:

  • Netron可视化深度学习的模型框架,大大降低了大模型的学习门槛
  • Android客制化------7.0设置壁纸存在的一些问题
  • VuePress2配置unocss的闭坑指南
  • 海陵HLK-TX510人脸识别模块 stm32使用
  • 安卓14无法安装应用解决历程
  • 【Linux】传输层协议UDP
  • 玩机搞机基本常识-------列举安卓机型一些不常用的adb联机命令
  • unity学习14:unity里的C#脚本的几个基本生命周期方法, 脚本次序order等
  • pytorch 比较两个张量的是否相等的函数介绍
  • MySQL Windows 11 的 MySQL 配置文件 (my.ini) 路径查找指南
  • 06-RabbitMQ基础
  • 关于markdown实现页面跳转(调查测试:csdn(博客编写效果、发布效果)、typroa中md转pdf的使用情况)
  • el-dialog 组件 在<style lang=“scss“ scoped>标签
  • 《深度学习梯度消失问题:原因与解决之道》
  • 中高级运维工程师运维面试题(十一)之 Docker
  • Gitee图形界面上传(详细步骤)
  • WebSocket 实现指南
  • TRELLIS - 生成 3D 作品的开源模型
  • uni-app图文列表到详情页面切换
  • ros2-3.4话题通信最佳实践
  • Vmware安装centos
  • 51单片机——按键实验
  • QT c++ 自定义按钮类 加载图片 美化按钮
  • Django:构建高效Web应用的强大框架
  • 代码随想录算法【Day11】
  • [SeaTunnel] [MySql CDC] Generate Splits for table db.table error
  • Spring Boot | 基于MinIO实现文件上传和下载
  • 企业手机号搜索API接口
  • VirtualBox Main API 学习笔记
  • [Linux]Mysql9.0.1服务端脱机安装配置教程(redhat)