mindtext.modules.encoder.attention¶
class mindtext.modules.encoder.attention.CastWrapper (src_type: mindspore.dtype = mstype.float32, dst_type: mindspore.dtype = mstype.float32)
init (src_type: mindspore.dtype = mstype.float32, dst_type: mindspore.dtype = mstype.float32)
参数
src_type (mindspore.dtype): 输入的向量类型。
dst_type (mindspore.dtype): 转换输出的向量类型。
construct (x: mindspore.Tensor)
参数
x (mindspore.Tensor):需要转换类型的向量。
返回
x(mindspore.Tensor):转换类型后的向量。
class mindtext.modules.encoder.attention.LayerPreprocess (in_channels: Optional[int] = None)
每层的预处理
init (in_channels: Optional[int] = None)
参数
in_channels (Optional[int]): 输入的大小,通常是输入的最后一维。
construct (input_tensor: mindspore.Tensor)
参数
input_tensor (mindspore.Tensor):预处理层的输入。
返回
output(mindspore.Tensor):预处理层的输出。
class mindtext.modules.encoder.attention.LayerPostprocess (dropout_prob: float = 0.1)
每层的后处理
init (dropout_prob: float = 0.1)
参数
dropout_prob (float): 后处理层的dropout,默认为0.1。
construct (hidden_tensor: mindspore.Tensor, input_tensor: mindspore.Tensor)
参数
hidden_tensor (mindspore.Tensor):隐藏层的输出。
input_tensor (mindspore.Tensor):隐藏层的输入。
返回
output(mindspore.Tensor):后处理层的输出。
class mindtext.modules.encoder.attention.DotAttention (key_size: int, value_size: int, dropout: float = 0.0, has_attn_mask: bool = False)
Transformer里的DoAttention
init (key_size: int, value_size: int, dropout: float = 0.0, has_attn_mask: bool = False)
参数
key_size (int): key的最后一维大小。
value_size (int): value的最后一维大小。
dropout (float): dropout,默认为0.0。
has_attn_mask (bool): 是否有attention mask, 默认为False。
construct (q: mindspore.Tensor, k: mindspore.Tensor, v: mindspore.Tensor, attn_mask: Optional[mindspore.Tensor] = None)
参数
q (mindspore.Tensor):Attention的queries,shape为(batch_size, q_len, q_size)。
k (mindspore.Tensor):Attention的keys,shape为(batch_size, q_len, q_size)。
v (mindspore.Tensor):Attention的value,shape为(batch_size, q_len, q_size)。
attn_mask (Optional[mindspore.Tensor]):Attention的mask矩阵,值为True或者False,默认为None,shape为(batch_size, q_len, q_len)。
返回
output(mindspore.Tensor):DoAttention的输出。
class mindtext.modules.encoder.attention.MultiHeadAttention (batch_size: int, from_tensor_width: int, to_tensor_width: int, out_tensor_width: int, num_attention_heads: int = 1, size_per_head: int = 512, query_act: Optional[str] = None, key_act: Optional[str] = None, value_act: Optional[str] = None, out_act: Optional[str] = None, has_attention_mask: bool = True, attention_probs_dropout_prob: float = 0.0, use_one_hot_embeddings: bool = False, initializer_range: float = 0.02, do_return_2d_tensor: bool = True, compute_type: mindspore.dtype = mstype.float32)
多头注意力机制
init (batch_size: int, from_tensor_width: int, to_tensor_width: int, out_tensor_width: int, num_attention_heads: int = 1, size_per_head: int = 512, query_act: Optional[str] = None, key_act: Optional[str] = None, value_act: Optional[str] = None, out_act: Optional[str] = None, has_attention_mask: bool = True, attention_probs_dropout_prob: float = 0.0, use_one_hot_embeddings: bool = False, initializer_range: float = 0.02, do_return_2d_tensor: bool = True, compute_type: mindspore.dtype = mstype.float32)
参数
batch_size (int): 输入数据集的batch size。
from_tensor_width (int): from_tensor的最后一维大小。
to_tensor_width (int): to_tensor的最后一维大小。
num_attention_heads (int): 注意力头数量,默认为1。
size_per_head (int): 每个注意力头的维度大小,默认为512。
query_act (str): query transformer的激活函数,默认为None。
key_act (str): key transformer的激活函数,默认为None。
value_act (str): value transformer的激活函数,默认为None。
out_act (str): output transformer的激活函数,默认为None。
has_attention_mask (bool): 是否使用attention mask,默认为False。
attention_probs_dropout_prob (float): 自注意力的dropout,默认为0.1。
use_one_hot_embeddings (bool): 是否使用one hot编码模式,默认为False。
initializer_range (float): 截断正态分布的初始值,默认为0.02。
do_return_2d_tensor (bool): True返回2维张量,False返回3维张量,默认为False。
computer_type (mindspore.dtype): 注意力计算的类型,默认为mstype.float32。
construct (from_tensor: mindspore.Tensor, to_tensor: mindspore.Tensor, seq_length: int, enc_seq_length: int, attention_mask: Optional[mindspore.Tensor] = None)
参数
from_tensor (mindspore.Tensor):from_tensor,通常是一个attention的query向量(Q),shape是(batch_size, from_seq_len, dim)。
to_tensor (mindspore.Tensor):to_tensor,通常是key和value对于attention来说K=V,shape是(batch_size, to_seq_len, dim)。
seq_length (int):from_tensor的长度。
enc_seq_length (int):to_tensor的长度。
attn_mask (Optional[mindspore.Tensor]):注意力的mask矩阵(2D或者3D),值是[0/1]或者[True/False],默认为None,shape为(from_seq_len, to_seq_len)或者(batch_size, from_seq_len, to_seq_len)。
返回
output(mindspore.Tensor):多头注意力的输出。
class mindtext.modules.encoder.attention.SelfAttention (batch_size: int, hidden_size: int, num_attention_heads: int = 16, attention_probs_dropout_prob: float = 0.1, use_one_hot_embeddings: bool = False, initializer_range: float = 0.02, hidden_dropout_prob: float = 0.1, has_attention_mask: bool = True, is_encdec_att: bool = False, compute_type: mindspore.dtype = mstype.float32)
自注意力机制
init (batch_size: int, hidden_size: int, num_attention_heads: int = 16, attention_probs_dropout_prob: float = 0.1, use_one_hot_embeddings: bool = False, initializer_range: float = 0.02, hidden_dropout_prob: float = 0.1, has_attention_mask: bool = True, is_encdec_att: bool = False, compute_type: mindspore.dtype = mstype.float32)
参数
batch_size (int): 输入数据集的batch size。
hidden_size (int): 注意力层的大小。
num_attention_heads (int): 注意力头的数量,默认为16。
attention_probs_dropout_prob (float): 自注意力的dropout,默认为0.1。
use_one_hot_embeddings (bool): 是否使用one hot编码模式,默认为False。
initializer_range (float): 截断正态分布的初始值,默认为0.02。
hidden_dropout_prob (float): 输出的dropout,默认为0.1。
has_attention_mask (bool): 是否使用attention mask,默认为False。
is_encdec_att (bool): 是否query序列和memory序列是不一样的,默认为False。
computer_type (mindspore.dtype): 注意力计算的类型,默认为mstype.float32。
construct (input_tensor: mindspore.Tensor, memory_tensor: mindspore.Tensor, attention_mask: mindspore.Tensor, seq_length: int, enc_seq_length: int)
参数
input_tensor (mindspore.Tensor):输入序列通常是自注意力的query向量(Q),shape是(batch_size, seq_len, hidden_units)。
memory_tensor (mindspore.Tensor):输入序列通常是key和value对于attention来说K=V,shape是 (batch_size, seq_len, hidden_units)。
attention_mask (mindspore.Tensor):自注意力隐藏状态的mask矩阵(2D或者3D),值是[0/1]或者[True/False],shape为(from_seq_len, to_seq_len)或者(batch_size, from_seq_len, to_seq_len)。
seq_length (int):输入的长度。
__ enc_seq_length__ (int):memory_tensor的长度。
返回
output(mindspore.Tensor):自注意力的输出。