MCPcopy Create free account
hub / github.com/PaddlePaddle/Research / Encoder

Class Encoder

CV/SMILE/backbones/vit.py:253–301  ·  view source on GitHub ↗

Transformer encoder Encoder encoder contains a list of TransformerLayer, and a LayerNorm. Attributes: layers: nn.LayerList contains multiple EncoderLayers encoder_norm: nn.LayerNorm which is applied after last encoder layer

Source from the content-addressed store, hash-verified

251
252
253class Encoder(nn.Layer):
254 """Transformer encoder
255 Encoder encoder contains a list of TransformerLayer, and a LayerNorm.
256 Attributes:
257 layers: nn.LayerList contains multiple EncoderLayers
258 encoder_norm: nn.LayerNorm which is applied after last encoder layer
259 """
260 def __init__(self,
261 embed_dim,
262 num_heads,
263 depth,
264 attn_head_size=None,
265 qkv_bias=True,
266 mlp_ratio=4.0,
267 dropout=0.,
268 attention_dropout=0.,
269 droppath=0.):
270 super().__init__()
271 # stochatic depth decay
272 depth_decay = [x.item() for x in paddle.linspace(0, droppath, depth)]
273
274 layer_list = []
275 for i in range(depth):
276 layer_list.append(TransformerLayer(embed_dim,
277 num_heads,
278 attn_head_size,
279 qkv_bias,
280 mlp_ratio,
281 dropout,
282 attention_dropout,
283 depth_decay[i]))
284 self.layers = nn.LayerList(layer_list)
285
286 w_attr_1, b_attr_1 = self._init_weights()
287 self.encoder_norm = nn.LayerNorm(embed_dim,
288 weight_attr=w_attr_1,
289 bias_attr=b_attr_1,
290 epsilon=1e-6)
291
292 def _init_weights(self):
293 weight_attr = paddle.ParamAttr(initializer=nn.initializer.Constant(1.0))
294 bias_attr = paddle.ParamAttr(initializer=nn.initializer.Constant(0.0))
295 return weight_attr, bias_attr
296
297 def forward(self, x):
298 for layer in self.layers:
299 x = layer(x)
300 x = self.encoder_norm(x)
301 return x
302
303
304class VisionTransformer(nn.Layer):

Callers 3

__init__Method · 0.85
evaluateFunction · 0.85
trainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected