Providing a bool or integral fill value without setting the optional `dtype` or `out`...

本文介绍了解决FastNLP在使用特定版本PyTorch时遇到的兼容性错误的方法。针对RuntimeError异常,通过指定填充值的数据类型为torch.long解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用fastnlp的时候,由于使用的pytorch==1.6.0,而fastnlp版本较低,出现以下问题:

Traceback (most recent call last):
  File "train_zen_cn.py", line 175, in <module>
    data_bundle, embed, bi_embed, tencent_words_embed, train_feature_data, dev_feature_data, test_feature_data, feature2id, id2feature = load_data()
  File "/home/.../fastNLP/core/utils.py", line 155, in wrapper
    results = func(*args, **kwargs)
  File "train_zen_cn.py", line 145, in load_data
    embed = StaticEmbedding(data_bundle.get_vocab('chars'),
  File "/home/.../fastNLP/embeddings/static_embedding.py", line 154, in __init__
    embedding = self._load_with_vocab(model_path, vocab=vocab, init_method=init_method)
  File "/home/.../fastNLP/embeddings/static_embedding.py", line 266, in _load_with_vocab
    self.register_buffer('words_to_words', torch.full((len(vocab), ), fill_value=unknown_idx), dtype=torch.long())
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out` arguments is currently unsupported. In PyTorch 1.7, when `dtype` and `out` are not set a bool fill value will return a tensor of torch.bool dtype, and an integral fill value will return a tensor of torch.long dtype.

需要做出以下修改

# self.register_buffer('words_to_words', torch.full((len(vocab), ), fill_value=unknown_idx).long())
self.register_buffer('words_to_words', torch.full((len(vocab), ), fill_value=unknown_idx, dtype=torch.long))
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值