Jurgen提出的Highway Networks:LSTM时间维方法应用到深度维
具体实例与推演
假设我们有一个离散型随机变量 X X X,它表示掷一枚骰子得到的点数,求 X X X 的期望。
- 步骤 :
- 列出 X X X 的所有可能取值 x i x_i xi(1, 2, 3, 4, 5, 6)。
- 计算每个 x i x_i xi 出现的概率 p i p_i pi(均为 1/6)。
- 应用期望公式计算 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)=1⋅61+2⋅61+⋯+6⋅61=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+Whiht−1+bi)=σ(Wifxt+Whfht−1+bf)=σ(Wioxt+Whoht−1+bo)=tanh(Wigxt+Whght−1+bg)=ft⊙ct−1+it⊙gt=ot⊙tanh(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)=H⊙T+x⊙(1−T)
1.2 核心解释
核心概念 | 定义 | 比喻或解释 |
---|---|---|
LSTM | 一种解决长时间依赖问题 的RNN架构,使用门控机制控制信息流动。 | 就像记忆模块,能够选择性记住或忘记信息。 |
Highway Networks | 将LSTM的门控机制应用到深度学习网络 ,允许信息直接通过网络层。 | 类似于在复杂路网上增加高速公路,使信息传输更快速高效。 |
1.3 优势与劣势
方面 | 描述 |
---|---|
优势 | 解决了深度网络中的梯度消失问题,提高了信息传递效率。 |
劣势 | 需要更多的参数和计算资源。 |
1.4 类比与总结
Highway Networks 通过引入门控机制 ,使得信息在深度网络中能够更有效地传递 。这就像在复杂的交通网络中增加高速公路,使得车辆能够更快速地到达目的地。
第四节:核心代码与可视化
4.1 Python代码示例
以下是演示如何应用Highway Networks和LSTM的Python代码示例:
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在训练过程中的损失函数变化,比较二者的收敛速度和效果。
参考文献:
- Srivastava, R. K., Greff, K., & Schmidhuber, J. (2015). Highway Networks. arXiv preprint arXiv:1505.00387.
- 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 #深度学习 #门控机制