如何处理 Pytorch 中的 mini-batch loss?

yan*_*wii 5 loss pytorch

我将小批量数据提供给模型,我只想知道如何处理损失。我可以累积损失,然后像这样调用向后:

    ...
    def neg_log_likelihood(self, sentences, tags, length):
        self.batch_size = sentences.size(0)

        logits = self.__get_lstm_features(sentences, length)
        real_path_score = torch.zeros(1)
        total_score = torch.zeros(1)
        if USE_GPU:
            real_path_score = real_path_score.cuda()
            total_score = total_score.cuda()

        for logit, tag, leng in zip(logits, tags, length):
            logit = logit[:leng]
            tag = tag[:leng]
            real_path_score += self.real_path_score(logit, tag)
            total_score += self.total_score(logit, tag)
        return total_score - real_path_score
    ...
loss = model.neg_log_likelihood(sentences, tags, length)
loss.backward()
optimizer.step()
Run Code Online (Sandbox Code Playgroud)

我想知道如果积累会导致梯度爆炸吗?

所以,我应该在循环中调用向后:

for sentence, tag , leng in zip(sentences, tags, length):
    loss = model.neg_log_likelihood(sentence, tag, leng)
    loss.backward()
    optimizer.step()
Run Code Online (Sandbox Code Playgroud)

或者,像tensorflow中的 reduce_mean一样使用均值损失

loss = reduce_mean(losses)
loss.backward()
Run Code Online (Sandbox Code Playgroud)

sca*_*row 2

必须通过使用小批量大小loss来减少。mean如果您查看本机 PyTorch 损失函数(例如CrossEntropyLoss ),就会发现有一个单独的参数专门用于此目的,并且默认行为是针对小批量大小reduction执行的。mean