ytrue = Y[idx] lens = inputs[1].any(2).sum(1) avg_ones = np.hstack([Y[k,:l,0] for k,l in zip(idx, lens)]).mean() if avg_ones > 0: w = c*1/avg_ones net.logit_loss_fn = nn.CrossEntropyLoss(reduce = False, weight = torch.FloatTensor([1, w])) net.count_loss_fn = nn.CrossEntropyLoss(reduce = False, weight = torch.FloatTensor([1, c2*1/avg_ones])) lloss, closs, ylogits, ycount = net.run(*inputs, ytrue, lens) ctrue = ytrue.any(1) ltp, ltn, lfp, lfn = confusion(ylogits, ytrue, lens) ctp, ctn, cfp, cfn = confusion1d(ycount, ytrue.any(1).flatten()) ltpr = ltp/(ltp+lfn) ltnr = ltn/(ltn+lfp) lacc = (ltp + ltn)/(ltp+lfp+ltn+lfn) ctpr = ctp/(ctp+cfn) ctnr = ctn/(ctn+cfp) cacc = (ctp + ctn)/(ctp+cfp+ctn+cfn) if ltpr < .1 and ltnr > .5: c = np.minimum(c*1.001, 5) elif ltnr < .1 and ltpr > .5: c = np.maximum(.999*c, .1) if ctpr < .1 and ctnr > .5: c2 = np.minimum(c2*1.01, 5)
idx = np.random.choice(n, size=batch_size) inputs = (A[idx], Z[idx]) ytrue = Y[idx] lens = inputs[1].any(2).sum(1) avg_ones = np.hstack([Y[k, :l, 0] for k, l in zip(idx, lens)]).mean() if avg_ones > 0: w = c * 1 / avg_ones net.logit_loss_fn = nn.CrossEntropyLoss(reduce=True, weight=torch.FloatTensor( [1, w])) lloss, closs, ylogits, ycount = net.run(*inputs, ytrue) cacc = np.mean(ycount.round() == ytrue.sum(1)) tp, tn, fp, fn = confusion(ylogits, ytrue, lens) tpr = tp / (tp + fn) tnr = tn / (tn + fp) lacc = (tp + tn) / (tp + fp + tn + fn) if tpr < .1: c = np.minimum(c * 1.001, 5) if tnr < .1: c = np.maximum(.999 * c, .1) msg = "{:1.4f},{:1.4f},{:1.4f},"\ "{:1.4f},{:1.4f},{:1.4f},{:1.4f}"\ .format(lloss, closs, tpr, # True positive rate tnr, # True negative rate