小编yan*_*wii的帖子

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

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

    ...
    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)

loss pytorch

5
推荐指数
1
解决办法
2628
查看次数

标签 统计

loss ×1

pytorch ×1