fastNLP.modules.decoder

class fastNLP.modules.decoder.MLP(size_layer, activation='relu', output_activation=None, initial_method=None, dropout=0.0)[源代码]

别名 fastNLP.modules.MLP fastNLP.modules.decoder.MLP

多层感知器

注解

隐藏层的激活函数通过activation定义。一个str/function或者一个str/function的list可以被传入activation。 如果只传入了一个str/function,那么所有隐藏层的激活函数都由这个str/function定义; 如果传入了一个str/function的list,那么每一个隐藏层的激活函数由这个list中对应的元素定义,其中list的长度为隐藏层数。 输出层的激活函数由output_activation定义,默认值为None,此时输出层没有激活函数。

Examples:

>>> net1 = MLP([5, 10, 5])
>>> net2 = MLP([5, 10, 5], 'tanh')
>>> net3 = MLP([5, 6, 7, 8, 5], 'tanh')
>>> net4 = MLP([5, 6, 7, 8, 5], 'relu', output_activation='tanh')
>>> net5 = MLP([5, 6, 7, 8, 5], ['tanh', 'relu', 'tanh'], 'tanh')
>>> for net in [net1, net2, net3, net4, net5]:
>>>     x = torch.randn(5, 5)
>>>     y = net(x)
>>>     print(x)
>>>     print(y)
__init__(size_layer, activation='relu', output_activation=None, initial_method=None, dropout=0.0)[源代码]
参数:
  • size_layer (List[int]) -- 一个int的列表,用来定义MLP的层数,列表中的数字为每一层是hidden数目。MLP的层数为 len(size_layer) - 1
  • activation (Union[str,func,List[str]]) -- 一个字符串或者函数的列表,用来定义每一个隐层的激活函数,字符串包括relu,tanh和 sigmoid,默认值为relu
  • output_activation (Union[str,func]) -- 字符串或者函数,用来定义输出层的激活函数,默认值为None,表示输出层没有激活函数
  • initial_method (str) -- 参数初始化方式
  • dropout (float) -- dropout概率,默认值为0
forward(x)[源代码]
参数:x (torch.Tensor) -- MLP接受的输入
返回:torch.Tensor : MLP的输出结果
class fastNLP.modules.decoder.ConditionalRandomField(num_tags, include_start_end_trans=False, allowed_transitions=None, initial_method=None)[源代码]

别名 fastNLP.modules.ConditionalRandomField fastNLP.modules.decoder.ConditionalRandomField

条件随机场。提供forward()以及viterbi_decode()两个方法,分别用于训练与inference。

__init__(num_tags, include_start_end_trans=False, allowed_transitions=None, initial_method=None)[源代码]
参数:
  • num_tags (int) -- 标签的数量
  • include_start_end_trans (bool) -- 是否考虑各个tag作为开始以及结尾的分数。
  • to_tag_id(int)]] allowed_transitions (List[Tuple[from_tag_id(int),) -- 内部的Tuple[from_tag_id(int), to_tag_id(int)]视为允许发生的跃迁,其他没有包含的跃迁认为是禁止跃迁,可以通过 allowed_transitions()函数得到;如果为None,则所有跃迁均为合法
  • initial_method (str) -- 初始化方法。见initial_parameter
forward(feats, tags, mask)[源代码]

用于计算CRF的前向loss,返回值为一个batch_size的FloatTensor,可能需要mean()求得loss。

参数:
  • feats (torch.FloatTensor) -- batch_size x max_len x num_tags,特征矩阵。
  • tags (torch.LongTensor) -- batch_size x max_len,标签矩阵。
  • mask (torch.ByteTensor) -- batch_size x max_len,为0的位置认为是padding。
返回:

torch.FloatTensor, (batch_size,)

viterbi_decode(logits, mask, unpad=False)[源代码]

给定一个特征矩阵以及转移分数矩阵,计算出最佳的路径以及对应的分数

参数:
  • logits (torch.FloatTensor) -- batch_size x max_len x num_tags,特征矩阵。
  • mask (torch.ByteTensor) -- batch_size x max_len, 为0的位置认为是pad;如果为None,则认为没有padding。
  • unpad (bool) -- 是否将结果删去padding。False, 返回的是batch_size x max_len的tensor; True,返回的是 List[List[int]], 内部的List[int]为每个sequence的label,已经除去pad部分,即每个List[int]的长度是这 个sample的有效长度。
返回:

返回 (paths, scores)。 paths: 是解码后的路径, 其值参照unpad参数. scores: torch.FloatTensor, size为(batch_size,), 对应每个最优路径的分数。

fastNLP.modules.decoder.viterbi_decode(logits, transitions, mask=None, unpad=False)[源代码]

别名 fastNLP.modules.viterbi_decode fastNLP.modules.decoder.viterbi_decode

给定一个特征矩阵以及转移分数矩阵,计算出最佳的路径以及对应的分数

参数:
  • logits (torch.FloatTensor) -- batch_size x max_len x num_tags,特征矩阵。
  • transitions (torch.FloatTensor) -- n_tags x n_tags,[i, j]位置的值认为是从tag i到tag j的转换; 或者(n_tags+2) x (n_tags+2), 其中n_tag是start的index, n_tags+1是end的index;
  • mask (torch.ByteTensor) -- batch_size x max_len, 为0的位置认为是pad;如果为None,则认为没有padding。
  • unpad (bool) -- 是否将结果删去padding。False, 返回的是batch_size x max_len的tensor; True,返回的是 List[List[int]], 内部的List[int]为每个sequence的label,已经除去pad部分,即每个List[int]的长度是这 个sample的有效长度。
返回:

返回 (paths, scores)。 paths: 是解码后的路径, 其值参照unpad参数. scores: torch.FloatTensor, size为(batch_size,), 对应每个最优路径的分数。

fastNLP.modules.decoder.allowed_transitions(tag_vocab: Union[fastNLP.core.vocabulary.Vocabulary, dict], encoding_type=None, include_start_end=False)[源代码]

别名 fastNLP.modules.allowed_transitions fastNLP.modules.decoder.allowed_transitions

给定一个id到label的映射表,返回所有可以跳转的(from_tag_id, to_tag_id)列表。

参数:
  • tag_vocab (Vocabulary,dict) -- 支持类型为tag或tag-label。只有tag的,比如"B", "M"; 也可以是"B-NN", "M-NN", tag和label之间一定要用"-"隔开。如果传入dict,格式需要形如{0:"O", 1:"B-tag1"},即index在前,tag在后。
  • encoding_type (str) -- 支持"bio", "bmes", "bmeso", "bioes"。默认为None,通过vocab自动推断
  • include_start_end (bool) -- 是否包含开始与结尾的转换。比如在bio中,b/o可以在开头,但是i不能在开头; 为True,返回的结果中会包含(start_idx, b_idx), (start_idx, o_idx), 但是不包含(start_idx, i_idx); start_idx=len(id2label), end_idx=len(id2label)+1。为False, 返回的结果中不含与开始结尾相关的内容
返回:

List[Tuple(int, int)]], 内部的Tuple是可以进行跳转的(from_tag_id, to_tag_id)。