转载自: http://www.erogol.com/online-hard-example-mining-pytorch/
import torch as th
class NLL_OHEM(th.nn.NLLLoss):
""" Online hard example mining.
Needs input from nn.LogSotmax() """
def __init__(self, ratio):
super(NLL_OHEM, self).__init__(None, True)
self.ratio = ratio
def forward(self, x, y, ratio=None):
if ratio is not None:
self.ratio = ratio
num_inst = x.size(0)
num_hns = int(self.ratio * num_inst)
x_ = x.clone()
inst_losses = th.autograd.Variable(th.zeros(num_inst)).cuda()
for idx, label in enumerate(y.data):
inst_losses[idx] = -x_.data[idx, label]
#loss_incs = -x_.sum(1)
_, idxs = inst_losses.topk(num_hns)
x_hn = x.index_select(0, idxs)
y_hn = y.index_select(0, idxs)
return th.nn.functional.nll_loss(x_hn, y_hn)
Q1. 这里的理解应该是挑出不那么好的数据,再计算loss,而不是通过loss的判定,挑出一组好的数据再去计算新的loss?
Q2. 之前在pytorch中将20行:inst_losses[idx] = -x_.data[idx, label]
替换为 criterion 代码直接退出,不会报任何错误