mindspore/docs/api/api_python/nn/mindspore.nn.TransformerEnc...

22 lines
1.7 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

mindspore.nn.TransformerEncoder
========================================
.. py:class:: mindspore.nn.TransformerEncoder(encoder_layer, num_layers, norm=None)
Transformer编码器模块多层 :class:`mindspore.nn.TransformerEncoderLayer` 的堆叠包括MultiheadAttention层和FeedForward层。可以使用此模块构造BERT(https://arxiv.org/abs/1810.04805)模型。
参数:
- **encoder_layer** (Cell) - :class:`mindspore.nn.TransformerEncoderLayer` 的实例。
- **num_layers** (int) - 编码器层数。
- **norm** (Cell, 可选) - 自定义LayerNorm层。 默认值: ``None``
输入:
- **src** (Tensor) - 源序列。如果源序列没有batchshape是 :math:`(S, E)` ;否则如果 :class:`mindspore.nn.TransformerEncoderLayer` 中batch_first=False则shape为 :math:`(S, N, E)` 如果batch_first=True则shape为 :math:`(N, S, E)`:math:`(S)` 是源序列的长度, :math:`(N)` 是batch个数 :math:`(E)` 是特性个数。数据类型float16、float32或者float64。
- **src_mask** (Tensor, 可选) - 源序列的掩码矩阵。shape是 :math:`(S, S)`:math:`(N*nhead, S, S)` 。其中 `nhead`:class:`mindspore.nn.TransformerEncoderLayer` 中的 `nhead` 参数。数据类型float16、float32、float64或者bool。默认值``None``
- **src_key_padding_mask** (Tensor, 可选) - 源序列Key矩阵的掩码矩阵。如果目标序列没有batchshape是 :math:`(S)` 否则shape为 :math:`(N, S)` 。数据类型float16、float32、float64或者bool。默认值``None``
输出:
Tensor。Tensor的shape和dtype与 `src` 一致。
异常:
- **AssertionError** - 如果 `src_key_padding_mask` 不是bool或float类型。