Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.nn.TransformerEncoder

View Source On Gitee
class mindspore.nn.TransformerEncoder(encoder_layer, num_layers, norm=None)[source]

Transformer Encoder module with multi-layer stacked of mindspore.nn.TransformerEncoderLayer, including multihead attention and feedforward layer. Users can build the BERT(https://arxiv.org/abs/1810.04805) model with corresponding parameters.

Parameters
Inputs:
  • src (Tensor) - The sequence to the encoder. For unbatched input, the shape is (S,E) ; otherwise if batch_first=False in mindspore.nn.TransformerEncoderLayer, the shape is (S,N,E) and if batch_first=True , the shape is (N,S,E), where (S) is the source sequence length, (N) is the batch number and (E) is the feature number. Supported types: float16, float32, float64.

  • src_mask (Tensor, optional) - The mask of the src sequence. The shape is (S,S) or (Nnhead,S,S) , where nhead is the arguent in mindspore.nn.TransformerEncoderLayer. Supported types: float16, float32, float64, bool. Default: None.

  • src_key_padding_mask (Tensor, optional) - the mask of the src keys per batch. The shape is (S) for unbatched input, otherwise (N,S) . Supported types: float16, float32, float64, bool. Default: None.

Outputs:

Tensor. The shape and dtype of Tensor is the same with src .

Raises

AssertionError – If the input argument src_key_padding_mask is not bool or floating types.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> encoder_layer = ms.nn.TransformerEncoderLayer(d_model=512, nhead=8)
>>> transformer_encoder = ms.nn.TransformerEncoder(encoder_layer, num_layers=6)
>>> src = ms.Tensor(np.random.rand(10, 32, 512), ms.float32)
>>> out = transformer_encoder(src)
>>> print(out.shape)
(10, 32, 512)