python - Custom weighted loss in cntk -
i calculate weighted error below:
def calc_err(pred, targets, weights) : nclass = np.size(pred, axis=0) = [1.0 in range(nclass)] nontargets = c.minus(is, targets) wrongpred = c.minus(is, pred) wcolumn = c.times(targets, weights) wtarget = c.element_times(wcolumn, targets) wnontarget = c.element_times(wcolumn, nontargets) c1 = c.negate(c.reduce_sum(c.element_times(wtarget, c.log(pred)), axis = -1)) c2 = c.negate(c.reduce_sum(c.element_times(wnontarget, c.log(wrongpred)), axis = -1)) ce = c1 + c2 return ce.eval() where pred prediction probabilities, targets expected one-hot array, , weights 2d array. i've created corresponding custom loss below:
def weightedcrossentropy(z, targets): pred = c.softmax(z) nclass = np.size(pred, axis=0) = [1 in range(nclass)] nontargets = c.minus(is, targets) wrongpred = c.minus(is, pred) wcolumn = c.times(targets, weights) wtarget = c.element_times(wcolumn, targets) wnontarget = c.element_times(wcolumn, nontargets) c1 = c.negate(c.reduce_sum(c.element_times(wtarget, c.log(pred)), axis=-1)) c2 = c.negate(c.reduce_sum(c.element_times(wnontarget, c.log(wrongpred)), axis=-1)) ce = c1 + c2 return ce when tried train, have noticed while custom loss indeed decreasing, test error calc_err(pred, targets, weights) decrease 1 or 2 epochs or not @ all. weightedcrossentropy(z, targets) ok or did wrong?
is weights constant or parameter? please make sure these 2 function take same inputs, parameters , constants.
Comments
Post a Comment