此代码来自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.linear1
,self.linear2
)和self.linear
之间有什么区别
在
Dropout
的情况下,重用层通常不是问题。因此,您可以创建一个self.dropout = Dropout(dropout)
层,并在forward
函数中多次调用它。但是,当您这样做时,可能会有一些细微的用例表现不同,例如,如果您出于某种原因在网络中的各个层之间进行迭代This thread,特别是{a2},请详细讨论这一点对于线性层,每个
Linear
对象都具有一组权重和偏差。如果在forward
函数中多次调用它,所有调用都将共享并优化相同的权重集。这可以有合法的用途,但在需要多个线性层时不合适,每个层都有自己的权重和偏差集这是因为要将一个线性层或衰减层彼此分离。这是非常简单的逻辑。您正在使用
self.dropout = Dropout(dropout)
在Dropout函数的网络中创建不同的实例或层相关问题 更多 >
编程相关推荐