On this page
TransformerEncoderLayer
class torch.nn.TransformerEncoderLayer(d_model, nhead, dim_feedforward=2048, dropout=0.1, activation=<function relu>, layer_norm_eps=1e-05, batch_first=False, norm_first=False, device=None, dtype=None)[source]-
TransformerEncoderLayer is made up of self-attn and feedforward network. This standard encoder layer is based on the paper “Attention Is All You Need”. Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000-6010. Users may modify or implement in a different way during application.
- Parameters:
-
- d_model (int) – the number of expected features in the input (required).
- nhead (int) – the number of heads in the multiheadattention models (required).
- dim_feedforward (int) – the dimension of the feedforward network model (default=2048).
- dropout (float) – the dropout value (default=0.1).
- activation (Union[str, Callable[[Tensor], Tensor]]) – the activation function of the intermediate layer, can be a string (“relu” or “gelu”) or a unary callable. Default: relu
- layer_norm_eps (float) – the eps value in layer normalization components (default=1e-5).
- batch_first (bool) – If
True, then the input and output tensors are provided as (batch, seq, feature). Default:False(seq, batch, feature). - norm_first (bool) – if
True, layer norm is done prior to attention and feedforward operations, respectively. Otherwise it’s done after. Default:False(after).
- Examples::
-
>>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> src = torch.rand(10, 32, 512) >>> out = encoder_layer(src) Alternatively, when batch_first is True:-
>>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8, batch_first=True) >>> src = torch.rand(32, 10, 512) >>> out = encoder_layer(src) - Fast path:
-
forward() will use a special optimized implementation if all of the following conditions are met:
- Either autograd is disabled (using
torch.inference_modeortorch.no_grad) or no tensor argumentrequires_grad - training is disabled (using
.eval()) - batch_first is
Trueand the input is batched (i.e.,src.dim() == 3) - activation is one of:
"relu","gelu",torch.functional.relu, ortorch.functional.gelu - at most one of
src_maskandsrc_key_padding_maskis passed - if src is a NestedTensor, neither
src_masknorsrc_key_padding_maskis passed - the two
LayerNorminstances have a consistentepsvalue (this will naturally be the case unless the caller has manually modified one without modifying the other)
If the optimized implementation is in use, a NestedTensor can be passed for
srcto represent padding more efficiently than using a padding mask. In this case, a NestedTensor will be returned, and an additional speedup proportional to the fraction of the input that is padding can be expected. - Either autograd is disabled (using
forward(src, src_mask=None, src_key_padding_mask=None)[source]-
Pass the input through the encoder layer.
- Parameters:
- Return type:
- Shape:
-
see the docs in Transformer class.
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/1.13/generated/torch.nn.TransformerEncoderLayer.html