Exemplo n.º 1
0
def KL(pred, target, temperature=5, verbose=False):
    """ 计算pred和target的KL散度
    [IN]  pred: numpy.ndarray, 预测值
          target: numpy.ndarray, 目标值
          temperature: int, 温度参数T
          verbose: boolean, true则展示除以T, softmax后的pred和target的输出
    [OUT] loss: float, KL散度结果
    """
    #logging.info("pred before: {}".format(pred.numpy()[:2]))
    pred = L.softmax(pred / temperature)
    if verbose:
        logging.info("pred: {}".format(pred.numpy()[:2]))
    pred = L.log(pred)
    #logging.info("target before: {}".format(target.numpy()[:2]))
    target = L.softmax(target / temperature)
    if verbose:
        logging.info("target: {}".format(target.numpy()[:2]))
    loss = L.kldiv_loss(pred, target)
    if verbose:
        logging.info("kl loss: {}".format(loss.numpy()))
    return loss
Exemplo n.º 2
0
def KL(pred, target):
    pred = L.log(L.softmax(pred))
    target = L.softmax(target)
    loss = L.kldiv_loss(pred, target)
    return loss