mindtext.modules.encoder.roberta

class mindtext.modules.encoder.roberta.RobertaConfig (seq_length: int = 128, vocab_size: int = 32000, hidden_size: int = 768, num_hidden_layers: int = 12, num_attention_heads: int = 12, intermediate_size=3072, hidden_act: str = “gelu”, hidden_dropout_prob: float = 0.1, attention_probs_dropout_prob: float = 0.1, max_position_embeddings: int = 512, type_vocab_size: int = 16, initializer_range: float = 0.02, dtype: mstype = mstype.float32, compute_type: mstype = mstype.float32)

ROBERTA模型的配置

init (seq_length: int = 128, vocab_size: int = 32000, hidden_size: int = 768, num_hidden_layers: int = 12, num_attention_heads: int = 12, intermediate_size=3072, hidden_act: str = “gelu”, hidden_dropout_prob: float = 0.1, attention_probs_dropout_prob: float = 0.1, max_position_embeddings: int = 512, type_vocab_size: int = 16, initializer_range: float = 0.02, dtype: mstype = mstype.float32, compute_type: mstype = mstype.float32)

参数

  • _vocab_size (int): 每个embedding向量的shape,默认为32000。

  • seq_length (int): 输入序列的的长度,默认为128。

  • hidden_size (int): bert编码器的大小,默认为768。

  • num_hidden_layer (int): 隐藏层的数量,默认为12。

  • num_attention_heads (int): 在BertTransformer编码器里面每一个attention层的注意力头数量,默认为12。

  • intermediate_size (int): 中间层的隐藏维度大小,默认为3072。

  • hidden_act (str): bert的激活函数,默认为“gelu”。

  • hidden_dropout_prob (_float): dropout的大小,默认为0.1。

  • attention_probs_dropout_prob (float): BertAttention的dropout概率,默认为0.1。

  • max_position_embeddings (int): 用于模型的最大序列长度,默认为512。

  • type_vocab_size (int): 典型字典的大小,默认为16。

  • initializer_range (float): 截断正态分布的初始值,默认为0.02。

  • dtype (mindspore.dtype): 数据类型,默认为mstype.float32。

  • compute_type (mindspore.dtype): Bert模型的计算数据类型,默认为mstype.float32。

class mindtext.modules.encoder.roberta.EmbeddingLookup (vocab_size: int, embedding_size: int, embedding_shape: int, use_one_hot_embeddings: bool = False, initializer_range: float = 0.02)

一个embedding的查找表

init (vocab_size: int, embedding_size: int, embedding_shape: int, use_one_hot_embeddings: bool = False, initializer_range: float = 0.02)

参数

  • vocab_size (int): embedding的字典大小。

  • embedding_size (int): embedding向量的大小。

  • embedding_shape (lsit): embedding向量的shape为[batch_size, seq_length, embedding_size]。

  • use_one_hot_embeddings (bool): 是否使用one hot编码格式,默认为False。

  • initializer_range (float): 截断正态分布的初始值,默认为0.02。

construct (input_ids: Tensor)

得到输出和embedding的查找表

参数

  • input_ids (mindspore.Tensor): 一个包含字符和对于ids的信息向量

返回

  • output (mindspore.Tensor): input_ids转换的高维度word embedding。

  • embedding_table (Tensor matrix): 修正过的查找表。

class mindtext.modules.encoder.roberta.RobertaEmbedding (embedding_size: int, embedding_shape: list, use_token_type: bool = False, token_type_vocab_size: int = 16, use_one_hot_embeddings: bool = False, max_position_embeddings: int = 512, dropout_prob: float = 0.1)

类似于BertEmbedding对于位置编码有小的调整

init (embedding_size: int, embedding_shape: list, use_token_type: bool = False, token_type_vocab_size: int = 16, use_one_hot_embeddings: bool = False, max_position_embeddings: int = 512, dropout_prob: float = 0.1)

参数

  • embedding_size (int): embedding向量的大小。

  • embedding_shape (list): embedding向量的shape为[batch_size, seq_length, embedding_size]。

  • use_token_type (bool): 是否使用token类型编码,默认为False。

  • token_type_vocab_size (int): token类型编码的大小,默认为16。

  • use_one_hot_embeddings (bool): 是否使用one hot编码格式,默认为False。

  • max_position_embeddings (int):在模型使用的最大长度,默认为512。

  • dropout_prob (float): dropout的大小,默认为0.1。

construct (input_ids: Tensor, token_type_ids: Tensor, word_embeddings: Tensor)

参数

  • input_ids (mindspore.Tensor): 输入向量。

  • token_type_ids (mindspore.Tensor): segment id的向量。

  • word_embeddings (mindspore.Tensor): word embedding向量。

返回

  • output (mindspore.Tensor): 融合位置编码和segment编码的向量。

class mindtext.modules.encoder.luke.EncoderOutput (in_channels: int, out_channels: int, initializer_range: float = 0.02, dropout_prob: float = 0.1, compute_type: mstype = mstype.float32)

用于位置和token类型编码到word embedding的后处理

init (in_channels: int, out_channels: int, initializer_range: float = 0.02, dropout_prob: float = 0.1, compute_type: mstype = mstype.float32)

参数

  • in_channels (int): 输入向量。

  • out_channels (list): 输出向量。

  • initializer_range (float): 截断正态分布的初始值,默认为0.02。

  • dropout_prob (float): dropout的大小,默认为0.1。

  • compute_type (mindspore.dtype): Bert模型的计算数据类型,默认为mstype.float32。

construct (hidden_status: Tensor, input_tensor: Tensor)

参数

  • hidden_status (mindspore.Tensor): 隐藏层状态。

  • input_tensor (mindspore.Tensor): 残差计算的输入。

返回

  • output (mindspore.Tensor): 输入的线性计算和残差计算。

class mindtext.modules.encoder.luke.AttentionLayer (from_tensor_width: int, to_tensor_width: int, num_attention_heads: int = 1, size_per_head: int = 512, has_attention_mask: bool = True, query_act: str = None, key_act: str = None, value_act: str = None, attention_probs_dropout_prob: float = 0.0, initializer_range: float = 0.02, do_return_2d_tensor: bool = False, batch_size: int = None, from_seq_length: int = None, to_seq_length: int = None, compute_type: mstype = mstype.float32)

使用多头治理力机制

init (from_tensor_width: int, to_tensor_width: int, num_attention_heads: int = 1, size_per_head: int = 512, has_attention_mask: bool = True, query_act: str = None, key_act: str = None, value_act: str = None, attention_probs_dropout_prob: float = 0.0, initializer_range: float = 0.02, do_return_2d_tensor: bool = False, batch_size: int = None, from_seq_length: int = None, to_seq_length: int = None, compute_type: mstype = mstype.float32)

参数

  • from_tensor_width (int): from_tensor的最后一维大小。

  • to_tensor_width (int): to_tensor的最后一维大小。

  • from_seq_length (int): from_tensor的序列长度。

  • to_seq_length (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。

  • has_attention_mask (bool): 是否使用attention mask,默认为True。

  • attention_probs_dropout_prob (float): 自注意力的dropout,默认为0.0。

  • initializer_range (float): 截断正态分布的初始值,默认为0.02。

  • do_return_2d_tensor (bool): True返回2维张量,False返回3维张量,默认为False。

  • batch_size (int): 数据集的batch size。

  • computer_type (mindspore.dtype): 注意力计算的类型,默认为mstype.float32。

construct (from_tensor: Tensor, to_tensor: Tensor, attention_mask: 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)。

  • attention_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)。

返回

  • context_layer (mindspore.Tensor): 注意力层的输出。

class mindtext.modules.encoder.roberta.BertEncoderCell (batch_size: int, hidden_size: int = 768, seq_length: int = 512, num_attention_heads: int = 12, intermediate_size: int = 3072, attention_probs_dropout_prob: float = 0.02, initializer_range: float = 0.02, hidden_dropout_prob: float = 0.1, hidden_act: str = “gelu”, compute_type: mstype = mstype.float32)

init (batch_size: int, hidden_size: int = 768, seq_length: int = 512, num_attention_heads: int = 12, intermediate_size: int = 3072, attention_probs_dropout_prob: float = 0.02, initializer_range: float = 0.02, hidden_dropout_prob: float = 0.1, hidden_act: str = “gelu”, compute_type: mstype = mstype.float32)

参数

  • batch_size (int): 数据集的batch size。

  • hidden_size (int): bert编码层的大小,默认为768。

  • seq_length (int): 输入的长度,默认为512。

  • num_attention_heads (int): 注意力头数量,默认为12。

  • intermediate_size (int): 中间层的隐藏维度大小,默认为3072。

  • hidden_act (str): bert的激活函数,默认为“gelu”。

  • hidden_dropout_prob (_float): dropout的大小,默认为0.1。

  • attention_probs_dropout_prob (float): BertAttention的dropout概率,默认为0.02。

  • initializer_range (float): 截断正态分布的初始值,默认为0.02。

  • compute_type (mindspore.dtype): Bert模型的计算数据类型,默认为mstype.float32。

construct (input_tensor: Tensor, attention_mask: Tensor)

参数

  • input_tensor (mindspore.Tensor): BertEncoderCell的输入。

  • attention_mask (mindspore.Tensor): attention_mask。

返回

  • output (mindspore.Tensor): encoder的输出。

class mindtext.modules.encoder.roberta.BertTransformer (batch_size: int, hidden_size: int, seq_length: int, num_hidden_layers: int, num_attention_heads: int = 12, intermediate_size: int = 3072, attention_probs_dropout_prob: float = 0.1, initializer_range: float = 0.02, hidden_dropout_prob: float = 0.1, hidden_act: str = “gelu”, compute_type: mstype = mstype.float32, return_all_encoders: bool = False)

init (batch_size: int, hidden_size: int, seq_length: int, num_hidden_layers: int, num_attention_heads: int = 12, intermediate_size: int = 3072, attention_probs_dropout_prob: float = 0.1, initializer_range: float = 0.02, hidden_dropout_prob: float = 0.1, hidden_act: str = “gelu”, compute_type: mstype = mstype.float32, return_all_encoders: bool = False)

参数

  • batch_size (int): 数据集的batch size。

  • hidden_size (int): bert编码层的大小。

  • seq_length (int): 输入的长度,默认为512。

  • num_hidden_layers (int): 编码器隐含层的层数。

  • num_attention_heads (int): 注意力头数量,默认为12。

  • intermediate_size (int): 中间层的隐藏维度大小,默认为3072。

  • hidden_act (str): bert的激活函数,默认为“gelu”。

  • hidden_dropout_prob (_float): dropout的大小,默认为0.1。

  • attention_probs_dropout_prob (float): BertAttention的dropout概率,默认为0.1。

  • initializer_range (float): 截断正态分布的初始值,默认为0.02。

  • compute_type (mindspore.dtype): Bert模型的计算数据类型,默认为mstype.float32。

  • __ return_all_encoders__ (bool): 是否返回所有的编码,默认为False。

construct (input_tensor: Tensor, attention_mask: Tensor)

参数

  • input_tensor (mindspore.Tensor): BertEncoderCell的输入。

  • attention_mask (mindspore.Tensor): attention_mask。

返回

  • all_encoder_layers (mindspore.Tensor): encoder的输出。

class mindtext.modules.encoder.roberta.SecurityCast (src_type: str)

把字符串转换为Mstype

init (src_type: str)

参数

  • src_type (str): mstype的字符串。

返回

  • desc_type (mstypr): 转换后的输出。

class mindtext.modules.encoder.roberta.numbtpye2mstype (dst_type: mstype = mstype.float32)

提供一个安全的转换

init (dst_type: mstype = mstype.float32)

参数

  • dst_type (mstype): 输出元素的类型,默认为mstype.float32。

construct (x: Tensor)

参数

  • x (mindspore.Tensor): 输入数据。

返回

  • output (mindspore.Tensor): 转换后的数据。

class mindtext.modules.encoder.roberta.CreateAttentionMaskFromInputMask (config: RobertaConfig)

从输入的mask创建attention mask

init (config: RobertaConfig)

参数

  • config (RobertaConfig): RobertaConfig。

construct (input_mask: Tensor)

参数

  • input_mask (mindspore.Tensor): 输入的mask。

返回

  • attention_mask (mindspore.Tensor): 创建的attention mask。

class mindtext.modules.encoder.roberta.RobertaModel (config: BertConfig, is_training: bool, use_one_hot_embeddings: bool = False)

init (config: BertConfig, is_training: bool, use_one_hot_embeddings: bool = False)

参数

  • config (RobertaConfig): RobertaConfigo。

  • is_training (bool): True是训练,False是评估。

  • use_one_hot_embeddings (bool): 是否使用one hot编码格式,默认为False。

construct (input_ids: Tensor, token_type_ids: Tensor, input_mask: Tensor)

参数

  • input_ids (mindspore.Tensor): 包含字符到ids的向量。

  • token_type_ids (mindspore.Tensor): 包含segment ids的向量。

  • input_mask (mindspore.Tensor): 输入的mask。

返回

  • sequence_output (mindspore.Tensor): 序列输出。

  • pooled_output (mindspore.Tensor): 第一个token cls。

  • embedding_table (mindspore.Tensor): 修正的查找表。