论文辅助笔记:T2VEC一个疑虑:stackingGRUCell和GRU的区别在哪里?

1 stackingGRUCell

python 复制代码
class StackingGRUCell(nn.Module):
    """
    Multi-layer CRU Cell
    """
    def __init__(self, input_size, hidden_size, num_layers, dropout):
        super(StackingGRUCell, self).__init__()
        self.num_layers = num_layers
        self.grus = nn.ModuleList()
        self.dropout = nn.Dropout(dropout)

        self.grus.append(nn.GRUCell(input_size, hidden_size))
        for i in range(1, num_layers):
            self.grus.append(nn.GRUCell(hidden_size, hidden_size))
python 复制代码
    def forward(self, input, h0):
        """
        Input:
        input (batch, input_size): input tensor
        h0 (num_layers, batch, hidden_size): initial hidden state
        ---
        Output:
        output (batch, hidden_size): the final layer output tensor
        hn (num_layers, batch, hidden_size): the hidden state of each layer
        """
        hn = []
        output = input
        for i, gru in enumerate(self.grus):
            hn_i = gru(output, h0[i])
            #在每一次循环中,输入output会经过一个GRU单元并更新隐藏状态

            hn.append(hn_i)
            if i != self.num_layers - 1:
                output = self.dropout(hn_i)
            else:
                output = hn_i
            #如果不是最后一层,输出会经过一个dropout层。

        hn = torch.stack(hn)
        #将hn列表转变为一个张量
        return output, hn
  • nn.GRU中,hn表示每层的最后一个时间步的隐藏状态。这意味着,对于一个具有seq_len的输入序列,hn会包含每层的seq_len时间步中的最后一个时间步的隐藏状态。
  • StackingGRUCell中,hn是通过每层的GRUCell为给定的单一时间步计算得到的。
  • 所以,**如果seq_len为1,那么nn.GRU的hn和StackingGRUCell的hn应该是相同的?**output更应是如此

2 作为对比的普通GRU

啥也没有的一个普通GRU:

python 复制代码
class StackingGRU_tst(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, dropout):
        super(StackingGRU_tst, self).__init__()
        self.gru = nn.GRU(input_size, hidden_size, num_layers=num_layers, dropout=dropout, batch_first=True)

    def forward(self, input, h0):
        output, hn = self.gru(input, h0)
        return output, hn
python 复制代码
input_size = 5
hidden_size = 10
num_layers = 3
dropout = 0.1
batch_size = 7

3 二者对比前的一些工作

3.1 创建模型

python 复制代码
gru_cell_model = StackingGRUCell(input_size, hidden_size, num_layers, dropout)
gru_cell_model
'''
StackingGRUCell(
  (grus): ModuleList(
    (0): GRUCell(5, 10)
    (1): GRUCell(10, 10)
    (2): GRUCell(10, 10)
  )
  (dropout): Dropout(p=0.1, inplace=False)
)
'''

gru_model = nn.GRU(input_size, hidden_size, num_layers, dropout=dropout)
gru_model
'''
GRU(5, 10, num_layers=3, dropout=0.1)
'''

3.2 参数复制:

python 复制代码
with torch.no_grad():
    for i in range(num_layers):
        # 对于每一层,复制权重和偏置
        getattr(gru_model, 'weight_ih_l' + str(i)).copy_(gru_cell_model.grus[i].weight_ih)
        getattr(gru_model, 'weight_hh_l' + str(i)).copy_(gru_cell_model.grus[i].weight_hh)
        getattr(gru_model, 'bias_ih_l' + str(i)).copy_(gru_cell_model.grus[i].bias_ih)
        getattr(gru_model, 'bias_hh_l' + str(i)).copy_(gru_cell_model.grus[i].bias_hh)

3.3 设置输入和相同的初始hidden state

python 复制代码
input_data = torch.randn(batch_size, input_size)
h0_cell = torch.randn(num_layers, batch_size, hidden_size)
h0_gru = h0_cell.clone()  # 确保从相同的初始状态开始

3.4 分别生成输出结果

由于有dropping的存在,所以每次前向传播之前,都需要设置相同的随机种子

python 复制代码
torch.manual_seed(1215)
output_cell, hn_cell = gru_cell_model(input_data, h0_cell)
torch.manual_seed(1215)
output_gru, hn_gru = gru_model(input_data.unsqueeze(0), h0_gru)

4 比较结果

python 复制代码
torch.allclose(output_cell, output_gru.squeeze(0)),torch.allclose(hn_cell, hn_gru)

#(True, True)

结果是一样的的,所以似乎论文代码里的stackingGRUCell可以被GRU平替?

相关推荐
m0_7482299918 小时前
帝国CMS后台搭建全攻略
java·c语言·开发语言·学习
weixin_4624462318 小时前
PaddleX 3.2 人脸识别实战:自定义人脸库 + CartoonFace 官方案例 Top-K 识别完整指南
开发语言·r语言
Testopia19 小时前
走一遍 AI 学习之路 —— AI实例系列说明
开发语言·人工智能·python
Tony Bai19 小时前
【分布式系统】11 理论的试金石:用 Go 从零实现一个迷你 Raft 共识
开发语言·后端·golang
机 _ 长19 小时前
YOLO26 改进 | 训练策略 | 知识蒸馏 (Response + Feature + Relation)
python·深度学习·yolo·目标检测·机器学习·计算机视觉
Beginner x_u19 小时前
JavaScript 原型、原型链与原型继承的核心机制解析
开发语言·javascript·原型模式·原型原型链
离离茶19 小时前
【笔记1-11】Qt 关闭QToolbar的拓展菜单
开发语言·笔记·qt
曹牧19 小时前
C#:窗体构造函数无法引用窗体控件
开发语言·c#
草青工作室19 小时前
java-FreeMarker3.4自定义异常处理
java·前端·python
xb113219 小时前
C#使用Cancellation来取消异步任务
开发语言·c#