PyTorch:为什么要创建同一类型层的多个实例?

2024-09-27 17:56:45 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码来自PyTorch transformer:

    self.linear1 = Linear(d_model, dim_feedforward, **factory_kwargs)
    self.dropout = Dropout(dropout)
    self.linear2 = Linear(dim_feedforward, d_model, **factory_kwargs)
    self.norm1 = LayerNorm(d_model, eps=layer_norm_eps, **factory_kwargs)
    self.norm2 = LayerNorm(d_model, eps=layer_norm_eps, **factory_kwargs)
    self.norm3 = LayerNorm(d_model, eps=layer_norm_eps, **factory_kwargs)
    self.dropout1 = Dropout(dropout)
    self.dropout2 = Dropout(dropout)
    self.dropout3 = Dropout(dropout)

self.dropout已经存在并且是完全相同的函数时,他们为什么要添加self.dropout1...2...3

另外,(self.linear1self.linear2)和self.linear之间有什么区别


Tags: selflayernormmodelfactoryepskwargsdropout
2条回答

Dropout的情况下,重用层通常不是问题。因此,您可以创建一个self.dropout = Dropout(dropout)层,并在forward函数中多次调用它。但是,当您这样做时,可能会有一些细微的用例表现不同,例如,如果您出于某种原因在网络中的各个层之间进行迭代This thread,特别是{a2},请详细讨论这一点

对于线性层,每个Linear对象都具有一组权重和偏差。如果在forward函数中多次调用它,所有调用都将共享并优化相同的权重集。这可以有合法的用途,但在需要多个线性层时不合适,每个层都有自己的权重和偏差集

这是因为要将一个线性层或衰减层彼此分离。这是非常简单的逻辑。您正在使用self.dropout = Dropout(dropout)在Dropout函数的网络中创建不同的实例或层

相关问题 更多 >

    热门问题