mindtext.modules.encoder.luke

class mindtext.modules.encoder.luke.LukeConfig (seq_length=256, vocab_size=50265, hidden_size=1024, num_hidden_layers=24, num_attention_heads=16, intermediate_size=4096, hidden_act=”gelu”, hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=514, type_vocab_size=1, initializer_range=0.02, batch_size=2, use_relative_positions=False, dtype=mstype.float32, compute_type=mstype.float32, bert_model_name=”roberta-large”, bos_token_id=0, entity_emb_size=256, entity_vocab_size=500000, eos_token_id=2, gradient_checkpointing=False, layer_norm_eps=1e-05, model_type=”luke”, output_past=True, pad_token_id=1, position_embedding_type=”absolute”)

luke模型的配置信息

init (seq_length=256, vocab_size=50265, hidden_size=1024, num_hidden_layers=24, num_attention_heads=16, intermediate_size=4096, hidden_act=”gelu”, hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=514, type_vocab_size=1, initializer_range=0.02, batch_size=2, use_relative_positions=False, dtype=mstype.float32, compute_type=mstype.float32, bert_model_name=”roberta-large”, bos_token_id=0, entity_emb_size=256, entity_vocab_size=500000, eos_token_id=2, gradient_checkpointing=False, layer_norm_eps=1e-05, model_type=”luke”, output_past=True, pad_token_id=1, position_embedding_type=”absolute”)

参数

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

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

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

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

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

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

  • 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): 用于模型的最大序列长度,默认为514。

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

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

  • batch_size (int): 数据集的batch size,默认为2。

  • use_relative_positions (bool): 是否使用关系位置,默认为False。

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

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

class mindtext.modules.encoder.luke.LukeConfig (config)

luke模型

init (config)

参数

  • config (LukeConfig): LukeConfig。

class mindtext.modules.encoder.luke.LukeEntityAwareAttentionModel (config)

init (config))

参数

  • config (LukeConfig): LukeConfig。

construct (word_ids, word_segment_ids, word_attention_mask, entity_ids, entity_position_ids, entity_segment_ids, entity_attention_mask)

参数

  • word_ids (mindspore.Tensor): word对应的字典序号。

  • word_segment_ids (mindspore.Tensor):word_segment_ids。

  • word_attention_mask (mindspore.Tensor):word_attention_mask。

  • entity_ids (mindspore.Tensor):entity_ids。

  • entity_position_ids (mindspore.Tensor):entity_position_ids。

  • entity_segment_ids (mindspore.Tensor):entity_segment_ids。

  • entity_attention_mask (mindspore.Tensor):entity_attention_mask。

返回

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

class mindtext.modules.encoder.luke.EntityAwareSelfAttention (config)

init (config)

参数

  • config (LukeConfig): LukeConfig。

construct (word_hidden_states, entity_hidden_states, attention_mask)

参数

  • word_hidden_states (mindspore.Tensor): word的隐藏状态。

  • entity_hidden_states (mindspore.Tensor):实体的隐藏状态。

  • attention_mask (mindspore.Tensor):attention_mask。

返回

  • context_layer (mindspore.Tensor): 输出。

class mindtext.modules.encoder.luke.EntityAwareAttention (config)

init (config)

参数

  • config (LukeConfig): LukeConfig。

construct (word_hidden_states, entity_hidden_states, attention_mask)

参数

  • word_hidden_states (mindspore.Tensor): word的隐藏状态。

  • entity_hidden_states (mindspore.Tensor):实体的隐藏状态。

  • attention_mask (mindspore.Tensor):attention_mask。

返回

  • out1 (mindspore.Tensor): 输出。

  • out2 (mindspore.Tensor): 输出。

class mindtext.modules.encoder.luke.EntityAwareLayer (config)

init (config))

参数

  • config (LukeConfig): LukeConfig。

construct (word_hidden_states, entity_hidden_states, attention_mask)

参数

  • word_hidden_states (mindspore.Tensor): word的隐藏状态。

  • entity_hidden_states (mindspore.Tensor):实体的隐藏状态。

  • attention_mask (mindspore.Tensor):attention_mask。

返回

  • l_out1 (mindspore.Tensor): 输出。

  • l_out2 (mindspore.Tensor): 输出。

class mindtext.modules.encoder.luke.EntityAwareEncoder (config)

init (config)

参数

  • config (LukeConfig): LukeConfig。

construct (word_hidden_states, entity_hidden_states, attention_mask)

参数

  • word_hidden_states (mindspore.Tensor): word的隐藏状态。

  • entity_hidden_states (mindspore.Tensor):实体的隐藏状态。

  • attention_mask (mindspore.Tensor):attention_mask。

返回

  • word_hidden_states (mindspore.Tensor): word的隐藏状态。

  • entity_hidden_states (mindspore.Tensor):实体的隐藏状态。

class mindtext.modules.encoder.luke.BertOutput (in_channels, out_channels, initializer_range=0.02, dropout_prob=0.1, compute_type=mstype.float32)

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

参数

  • in_channels (int): 输入大小。

  • out_channels (int): 输出大小。

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

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

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

construct (hidden_status, input_tensor)

参数

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

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

返回

  • out (mindspore.Tensor): 输出。

class mindtext.modules.encoder.luke.BertEncoderCell (hidden_size=1024, seq_length=512, num_attention_heads=16, intermediate_size=3072, attention_probs_dropout_prob=0.02, use_one_hot_embeddings=False, initializer_range=0.02, hidden_dropout_prob=0.1, use_relative_positions=False, hidden_act=”gelu”, compute_type=mstype.float32)

init (hidden_size=1024, seq_length=512, num_attention_heads=16, intermediate_size=3072, attention_probs_dropout_prob=0.02, use_one_hot_embeddings=False, initializer_range=0.02, hidden_dropout_prob=0.1, use_relative_positions=False, hidden_act=”gelu”, compute_type=mstype.float32)

参数

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

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

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

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

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

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

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

  • use_relative_positions (bool): 是否使用关系位置,默认为False。

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

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

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

construct (hidden_states, attention_mask)

参数

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

  • attention_mask (mindspore.Tensor): attention_mask。

返回

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

class mindtext.modules.encoder.luke.BertSelfOutput (config, compute_type=mstype.float32)

init (config, compute_type=mstype.float32)

参数

  • config (LukeConfig): LukeConfig。

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

construct (hidden_status, input_tensor)

参数

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

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

class mindtext.modules.encoder.luke.BertSelfAttention (seq_length, hidden_size=768, num_attention_heads=12, attention_probs_dropout_prob=0.1, use_one_hot_embeddings=False, initializer_range=0.02, hidden_dropout_prob=0.1, use_relative_positions=False, compute_type=mstype.float32)

init (seq_length, hidden_size=768, num_attention_heads=12, attention_probs_dropout_prob=0.1, use_one_hot_embeddings=False, initializer_range=0.02, hidden_dropout_prob=0.1, use_relative_positions=False, compute_type=mstype.float32)

参数

  • seq_length (int): 输入的长度。

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

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

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

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

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

  • use_relative_positions (bool): 是否使用关系位置,默认为False。

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

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

construct (input_tensor, attention_mask)

参数

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

  • attention_mask (mindspore.Tensor): attention_mask。

返回

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

class mindtext.modules.encoder.luke.BertAttention (from_tensor_width, to_tensor_width, from_seq_length, to_seq_length, num_attention_heads=1, size_per_head=512, query_act=None, key_act=None, value_act=None, has_attention_mask=False, attention_probs_dropout_prob=0.0, use_one_hot_embeddings=False, initializer_range=0.02, do_return_2d_tensor=False, use_relative_positions=False, compute_type=mstype.float32)

使用多头治理力机制

init (from_tensor_width, to_tensor_width, from_seq_length, to_seq_length, num_attention_heads=1, size_per_head=512, query_act=None, key_act=None, value_act=None, has_attention_mask=False, attention_probs_dropout_prob=0.0, use_one_hot_embeddings=False, initializer_range=0.02, do_return_2d_tensor=False, use_relative_positions=False, compute_type=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。

  • out_act (str): output transformer的激活函数,默认为None。

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

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

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

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

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

  • use_relative_positions (bool): 是否使用关系位置,默认为False。

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

construct (from_tensor, to_tensor, attention_mask)

参数

  • 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.luke.SaturateCast (dst_type=mstype.float32)

执行安全的转换

init (_dst_type=mstype.float32)

参数

  • dst_type (int): 输出向量的类型,默认为mstype.float32。

construct (x)

返回

  • x (mindspore.Tensor): 转换后的输出。

class mindtext.modules.encoder.luke.RelaPosEmbeddingsGenerator (length, depth, max_relative_position, initializer_range, use_one_hot_embeddings=False)

生成size为[length, length, depth]的向量

init (_length, depth, max_relative_position, initializer_range, use_one_hot_embeddings=False)

参数

  • length (int): 生成的矩阵一维的长度。

  • depth (int): 注意力头的大小。

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

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

  • max_relative_position (int): 相关位置的最大值。

construct ()

返回

  • embeddings (mindspore.Tensor): 输出。

class mindtext.modules.encoder.luke.RelaPosMatrixGenerator (length, max_relative_position)

生成输入直接的相关位置矩阵

init (_length, max_relative_position)

参数

  • length (int): 生成的矩阵一维的长度。

  • max_relative_position (int): 相关位置的最大值。

construct ()

返回

  • final_mat (mindspore.Tensor): 输出。