fastNLP.modules.decoder

fastNLP.modules.decoder.CRF

class fastNLP.modules.decoder.CRF.ConditionalRandomField(num_tags, include_start_end_trans=False, allowed_transitions=None, initial_method=None)[source]
forward(feats, tags, mask)[source]

Calculate the neg log likelihood :param feats:FloatTensor, batch_size x max_len x num_tags :param tags:LongTensor, batch_size x max_len :param mask:ByteTensor batch_size x max_len :return:FloatTensor, batch_size

viterbi_decode(data, mask, get_score=False, unpad=False)[source]

Given a feats matrix, return best decode path and best score. :param data:FloatTensor, batch_size x max_len x num_tags :param mask:ByteTensor batch_size x max_len :param get_score: bool, whether to output the decode score. :param unpad: bool, 是否将结果unpad,

如果False, 返回的是batch_size x max_len的tensor, 如果True,返回的是List[List[int]], List[int]为每个sequence的label,已经unpadding了,即每个

List[int]的长度是这个sample的有效长度
Returns:如果get_score为False,返回结果根据unpadding变动 如果get_score为True, 返回 (paths, List[float], )。第一个仍然是解码后的路径(根据unpad变化),第二个List[Float]
为每个seqence的解码分数。
fastNLP.modules.decoder.CRF.allowed_transitions(id2label, encoding_type='bio')[source]
Parameters:
  • id2label – dict, key是label的indices,value是str类型的tag或tag-label。value可以是只有tag的, 比如”B”, “M”; 也可以是 “B-NN”, “M-NN”, tag和label之间一定要用”-“隔开。一般可以通过Vocabulary.get_id2word()id2label。
  • encoding_type – str, 支持”bio”, “bmes”。
:return:List[Tuple(int, int)]], 内部的Tuple是(from_tag_id, to_tag_id)。 返回的结果考虑了start和end,比如”BIO”中,B、O可以
位于序列的开端,而I不行。所以返回的结果中会包含(start_idx, B_idx), (start_idx, O_idx), 但是不包含(start_idx, I_idx). start_idx=len(id2label), end_idx=len(id2label)+1。
fastNLP.modules.decoder.CRF.is_transition_allowed(encoding_type, from_tag, from_label, to_tag, to_label)[source]
Parameters:
  • encoding_type – str, 支持”BIO”, “BMES”。
  • from_tag – str, 比如”B”, “M”之类的标注tag. 还包括start, end等两种特殊tag
  • from_label – str, 比如”PER”, “LOC”等label
  • to_tag – str, 比如”B”, “M”之类的标注tag. 还包括start, end等两种特殊tag
  • to_label – str, 比如”PER”, “LOC”等label
Returns:

bool,能否跃迁

fastNLP.modules.decoder.MLP

class fastNLP.modules.decoder.MLP.MLP(size_layer, activation='relu', initial_method=None, dropout=0.0)[source]
forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class fastNLP.modules.decoder.ConditionalRandomField(num_tags, include_start_end_trans=False, allowed_transitions=None, initial_method=None)[source]
forward(feats, tags, mask)[source]

Calculate the neg log likelihood :param feats:FloatTensor, batch_size x max_len x num_tags :param tags:LongTensor, batch_size x max_len :param mask:ByteTensor batch_size x max_len :return:FloatTensor, batch_size

viterbi_decode(data, mask, get_score=False, unpad=False)[source]

Given a feats matrix, return best decode path and best score. :param data:FloatTensor, batch_size x max_len x num_tags :param mask:ByteTensor batch_size x max_len :param get_score: bool, whether to output the decode score. :param unpad: bool, 是否将结果unpad,

如果False, 返回的是batch_size x max_len的tensor, 如果True,返回的是List[List[int]], List[int]为每个sequence的label,已经unpadding了,即每个

List[int]的长度是这个sample的有效长度
Returns:如果get_score为False,返回结果根据unpadding变动 如果get_score为True, 返回 (paths, List[float], )。第一个仍然是解码后的路径(根据unpad变化),第二个List[Float]
为每个seqence的解码分数。
class fastNLP.modules.decoder.MLP(size_layer, activation='relu', initial_method=None, dropout=0.0)[source]
forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.