luopeixiang / named_entity_recognition

中文命名实体识别(包括多种模型:HMM,CRF,BiLSTM,BiLSTM+CRF的具体实现)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bilistm_crf模型中的为啥要sort_by_lengths(word_lists, tag_lists),作用是啥。

tianke0711 opened this issue · comments

commented

bilistm_crf模型中的为啥要sort_by_lengths(word_lists, tag_lists),作用是啥。在训练中有啥好处。 如果不排序是否也没事呢。


def sort_by_lengths(word_lists, tag_lists):
    pairs = list(zip(word_lists, tag_lists))
    indices = sorted(range(len(pairs)),
                     key=lambda k: len(pairs[k][0]),
                     reverse=True)
    pairs = [pairs[i] for i in indices]
    # pairs.sort(key=lambda pair: len(pair[0]), reverse=True)

    word_lists, tag_lists = list(zip(*pairs))

    return word_lists, tag_lists, indices
commented

前面没对batch中的句子做补零,在取到batch之后,用batch中的第一个句子的长度作为最大长度 对batch内的剩余句子进行补零,所以排序是有用的