class Encoder(nn.Layer):
"""Transformer encoder
Encoder encoder contains a list of EncoderLayer, and a LayerNorm.
Attributes:
layers: nn.LayerList contains multiple EncoderLayers
encoder_norm: nn.LayerNorm which is applied after last encoder layer
"""
def __init__(self,
embed_dim,
num_heads,
depth,
qkv_bias=True,
mlp_ratio=4.0,
dropout=0.,
attention_dropout=0.,
droppath=0.):
super(Encoder, self).__init__()
# stochatic depth decay
depth_decay = [x.item() for x in paddle.linspace(0, droppath, depth)]
layer_list = []
for i in range(depth):
encoder_layer = EncoderLayer(embed_dim,
num_heads,
qkv_bias=True,
mlp_ratio=4.,
dropout=0.,
attention_dropout=0.,
droppath=depth_decay[i])
layer_list.append(copy.deepcopy(encoder_layer))
self.layers = nn.LayerList(layer_list)
……
应该改成:
class Encoder(nn.Layer):
"""Transformer encoder
Encoder encoder contains a list of EncoderLayer, and a LayerNorm.
Attributes:
layers: nn.LayerList contains multiple EncoderLayers
encoder_norm: nn.LayerNorm which is applied after last encoder layer
"""
def __init__(self,
embed_dim,
num_heads,
depth,
qkv_bias=True,
mlp_ratio=4.0,
dropout=0.,
attention_dropout=0.,
droppath=0.):
super(Encoder, self).__init__()
# stochatic depth decay
depth_decay = [x.item() for x in paddle.linspace(0, droppath, depth)]
layer_list = []
for i in range(depth):
encoder_layer = EncoderLayer(embed_dim,
num_heads,
qkv_bias=qkv_bias,
mlp_ratio=mlp_ratio,
dropout=dropout,
attention_dropout=attention_dropout,
droppath=depth_decay[i])
layer_list.append(copy.deepcopy(encoder_layer))
self.layers = nn.LayerList(layer_list)
……
PaddleViT/image_classification/ViT/transformer.py 的
L300
创建EncoderLayer
时,参数qkv_bias
,mlp_ratio
,dropout
,attention_dropout
存在硬编码的问题,导致Encoder
的__init__
方法传入的参数形同虚设:应该改成:
以上~