如何将SVM类概率转换为logit?

时间:2018-09-16 13:11:42

标签: python-2.7 scikit-learn libsvm softmax

我想将SVM输出的概率类转换为logits。

为了获得每个班级的概率

model = svm.SVC(probability=True)
model.fit(X, Y)
results = model.predict_proba(test_data)[0]
# gets a dictionary of {'class_name': probability}
prob_per_class_dictionary = dict(zip(model.classes_, results))
# gets a list of ['most_probable_class', 'second_most_probable_class', ..., 'least_class']
results_ordered_by_probability = map(lambda x: x[0], sorted(zip(model.classes_, results), key=lambda x: x[1], reverse=True))

这些概率我想做什么?

将概率转换为logits。

为什么?

我想将SVM的结果与神经网络的结果合并。这样损失神经网络输出logits。因此,我正在寻找一种将SVM输出的概率转换为logits的方法,而不是使用相等的权重将神经网络logits与SVM logits合并:

SVM logits + neural network logits = overal_logits

overal_probabilities= softmax(overal_logits)

编辑:

等价于对数求和然后获得直接对除以2的概率求和的概率吗?

proba_nn_class_1=[0.8,0.002,0.1,...,0.00002]

proba_SVM_class_1=[0.6,0.1,0.21,...,0.000003]

overall_proba=[(0.8+0.6)/2,(0.002+0.1)/2,(0.1+0.21)/2,..., (0.00002+0.000003)/2 ]

此过程在数值上是否等于SVM和NN的对数的总和,然后通过softmax获得概率?

谢谢

1 个答案:

答案 0 :(得分:0)

def probs_to_logits(probs, is_binary=False):
    r"""
    Converts a tensor of probabilities into logits. For the binary case,
    this denotes the probability of occurrence of the event indexed by `1`.
    For the multi-dimensional case, the values along the last dimension
    denote the probabilities of occurrence of each of the events.
    """
    ps_clamped = clamp_probs(probs)
    if is_binary:
        return torch.log(ps_clamped) - torch.log1p(-ps_clamped)
    return torch.log(ps_clamped)

def clamp_probs(probs):
    eps = _finfo(probs).eps
    return probs.clamp(min=eps, max=1 - eps)

来自https://github.com/pytorch/pytorch/blob/master/torch/distributions/utils.py#L107