fastnlp / fastNLP

fastNLP: A Modularized and Extensible NLP Framework. Currently still in incubation.
https://gitee.com/fastnlp/fastNLP
Apache License 2.0
3.06k stars 450 forks source link

Vocabulary named target #310

Closed laomagic closed 3 years ago

laomagic commented 4 years ago

Describe the bug File "/home/serverai/anaconda3/envs/yanGPU/lib/python3.6/site-packages/fastNLP/io/data_bundle.py", line 100, in get_vocab raise KeyError(error_msg) KeyError: "DataBundle do NOT have Vocabulary named target. It should be one of dict_keys(['vocab'])."

To Reproduce def get_vocab(self, field_name: str) -> Vocabulary: r""" 获取field名为field_name对应的vocab

    :param str field_name: 名称
    :return: Vocabulary
    """
    if field_name in self.vocabs.keys():
        return self.vocabs[field_name]
    else:
        error_msg = f'DataBundle do NOT have Vocabulary named {field_name}. ' \
                    f'It should be one of {self.vocabs.keys()}.'
        logger.error(error_msg)
        raise KeyError(error_msg)
  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error 重现这个bug的步骤

Expected behavior A clear and concise description of what you expected to happen. 清晰而简要地描述你期望的结果

Screenshots If applicable, add screenshots to help explain your problem. 如果可以,请提供截图

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here. 备注

yhcc commented 4 years ago

就是data_bundle中的Vocabulary被设置成了vocab这个名称,而不是target,所以需要data_bundle.get_vocab('vocab')这样获取。如果是的确是有一个target的vocab的话,在data_bundle创建的时候需要通过 data_bundle.set_vocab(vocab=target_vocab, name='target')这样先设置才能在之后获取到。