mindspore/docs/api/api_python/nn/mindspore.nn.TransformerDec...

32 lines
3.3 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.TransformerDecoderLayer
========================================
.. py:class:: mindspore.nn.TransformerDecoderLayer(d_model, nhead, dim_feedforward=2048, dropout=0.1, activation='relu', layer_norm_eps=1e-5, batch_first=False, norm_first=False, dtype=mstype.float32)
Transformer的解码器层。Transformer解码器的单层实现包括Self Attention层、MultiheadAttention层和FeedForward层。
参数:
- **d_model** (int) - 输入的特征数。
- **nhead** (int) - MultiheadAttention模块中注意力头的数量。
- **dim_feedforward** (int) - FeedForward层的维数。默认值``2048``
- **dropout** (float) - 随机丢弃比例。默认值:``0.1``
- **activation** (Union[str, callable, Cell]) - 中间层的激活函数,可以输入字符串( ``"relu"````"gelu"`` )、函数接口( :func:`mindspore.ops.relu`:func:`mindspore.ops.gelu` )或激活函数层实例( :class:`mindspore.nn.ReLU`:class:`mindspore.nn.GELU` )。默认值: ``'relu'``
- **layer_norm_eps** (float) - LayerNorm层的eps值默认值``1e-5``
- **batch_first** (bool) - 如果为 ``True`` 则输入输出shape为 :math:`(batch, seq, feature)` 反之shape为 :math:`(seq, batch, feature)` 。默认值: ``False``
- **norm_first** (bool) - 如果为 ``True`` 则LayerNorm层位于Self Attention层、MultiheadAttention层和FeedForward层之前反之位于其后。默认值 ``False``
- **dtype** (:class:`mindspore.dtype`) - Parameter的数据类型。默认值 ``mstype.float32``
输入:
- **tgt** (Tensor) - 目标序列。如果目标序列没有batchshape是 :math:`(T, E)` ;否则如果 batch_first=False则shape为 :math:`(T, N, E)` 如果batch_first=True则shape为 :math:`(N, T, E)`:math:`(T)` 是目标序列的长度。数据类型float16、float32或者float64。
- **memory** (Tensor) - TransformerEncoder的最后一层输出序列。数据类型float16、float32或者float64。
- **tgt_mask** (Tensor, 可选) - 目标序列的掩码矩阵。shape是 :math:`(T, T)`:math:`(N*nhead, T, T)` 。数据类型float16、float32、float64或者bool。默认值``None``
- **memory_mask** (Tensor, 可选) - memory序列的掩码矩阵。shape是 :math:`(T, S)` 。数据类型float16、float32、float64或者布尔。默认值``None``
- **tgt_key_padding_mask** (Tensor, 可选) - 目标序列Key矩阵的掩码矩阵。如果目标序列没有batchshape是 :math:`(T)` 否则shape为 :math:`(N, T)`。数据类型float16、float32、float64或者bool。默认值``None``
- **memory_key_padding_mask** (Tensor, 可选) - memory序列Key矩阵的掩码矩阵∂。如果目标序列没有batchshape是 :math:`(S)` 否则shape为 :math:`(N, S)` 。数据类型float16、float32、float64或者布尔。默认值``None``
输出:
Tensor。Tensor的shape和dtype与 `tgt` 一致。
异常:
- **ValueError** - 如果 `activation` 不是str 、 callable 或 Cell的实例。
- **ValueError** - 如果 `activation` 不是 :class:`mindspore.nn.ReLU`:class:`mindspore.nn.GELU` 的子类、:func:`mindspore.ops.relu`:func:`mindspore.ops.gelu```"relu"````"gelu"``