解决【TypeError: forward() got an unexpected keyword argument ‘reduction‘】问题

首先,项目项目背景是用BiLSTM+CRF做NER任务,在定义损失函数时,报出了题目的错误,下面是源码:

# model.py
class Model(nn.Module):
    def __init__(self):
        ......
        self.crf = CRF(TARGET_SIZE)

    def forward(self, input, mask):
        ......
        return self.crf.decode(out, mask)

    def loss_fn(self, input, target, mask):
        y_pred = self._get_lstm_feature(input)
        return -self.crf.forward(y_pred, target, mask, reduction='mean')

重点是在loss_fn,因此初始化和前馈层暂且不表,这种写法会在训练模型时会引发标题所示的错误,意思是:

"TypeError: forward()得到了一个意外的关键字参数'reduction'"

表明CRF类的forward方法不接受reduction参数“

尝试将reduction参数去掉,再次训练模型,又报如下错误:

RuntimeError: grad can be implicitly created only for scalar outputs

“RuntimeError: grad只能为标量输出隐式创建”

意思是损失函数返回一个包含多个元素的张量,这在计算梯度时是不允许的。因此不能去掉reduction参数,要保持loss_fn返回标量值。

最后换了一种写法,可以正常运行和模型训练:

    def loss_fn(self, input, target, mask):
        y_pred = self._get_lstm_feature(input)
        loss = -self.crf.forward(y_pred, target, mask)
        return loss.mean()

 先用一个loss变量接收损失值,在return时再取mean()

希望能对你有所帮助

猜你喜欢

转载自blog.csdn.net/weixin_45206129/article/details/130310085
今日推荐