在fastNLP框架中加入LSTM遇到的一个问题

本文详细解析了在使用FastNLP框架与LSTM进行深度学习任务时遇到的兼容性错误,深入探讨了错误原因,并提供了一种有效的解决方案,即通过判断条件动态调整LSTM的使用,确保模型训练的顺利进行。

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

Traceback (most recent call last):
File “train_tener_cn.py”, line 176, in
use_tqdm=True, print_every=300, save_path=None)
File “/usr/local/lib/python3.6/dist-packages/fastNLP/core/trainer.py”, line 520, in init
batch_size=check_batch_size)
File “/usr/local/lib/python3.6/dist-packages/fastNLP/core/trainer.py”, line 920, in _check_code
pred_dict = model(**refined_batch_x)
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 532, in call
result = self.forward(*input, **kwargs)
File “/content/drive/My Drive/NER/TENER-weibo_msra/models/TENER.py”, line 105, in forward
return self._forward(words, target, bigrams)
File “/content/drive/My Drive/NER/TENER-weibo_msra/models/TENER.py”, line 86, in forward
words,(hidden,
) = self.lstm(words,seq_len=words.size(1))
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 532, in call
result = self.forward(*input, **kwargs)
File “/usr/local/lib/python3.6/dist-packages/fastNLP/modules/encoder/lstm.py”, line 68, in forward
sort_lens, sort_idx = torch.sort(seq_len, dim=0, descending=True)
TypeError: sort() received an invalid combination of arguments - got (int, descending=bool, dim=int), but expected one of:

  • (Tensor input, name dim, bool descending, tuple of Tensors out)
  • (Tensor input, int dim, bool descending, tuple of Tensors out)
words,hidden = self.lstm(words)

问题原因:
LSTM中的输入words对应矩阵----[batch_size, seq_len, feature_dim]
其中,batch_size=16,feature_dim=768, seq_len是句子长度,在不断变化。
但是fastnlp框架下,一开始会随机选择两个例子来进行测试,防止出错,如矩阵为[2,26],这样的话就与我的设置不符,导致了出错。

解决办法:
加入一个判断条件,如果batch_size==16,则加入LSTM,否则不加入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值