sklearn的MLP如何在内部运作predict_proba函数?

3

我正在尝试理解sklearn的MLP分类器如何检索其predict_proba函数的结果。

该网站简单地列出:

概率估计

而其他许多模型,例如逻辑回归,则有更详细的答案:概率估计。

对于多类问题,如果设置multi_class为“multinomial”,则使用softmax函数找到每个类别的预测概率。否则,使用一对多方法,即假设每个类别为正,并使用逻辑函数计算每个类别的概率,并在所有类别上规范化这些值。

其他的模型类型也有更详细的信息。例如,支持向量机分类器

还有这篇非常好的 Stack Overflow 帖子,深入解释了它。

计算 X 中可能结果的概率。

模型需要在训练时计算概率信息:将 attribute probability 设置为 True 进行拟合。

其他例子

随机森林

预测 X 的类别概率。

输入样本的预测类别概率是森林中树的平均预测类别概率。单棵树的类别概率是叶子中相同类别样本的比例。

高斯过程分类器:

我想了解与上述帖子相同的内容,但是针对MLPClassifierMLPClassifier在内部是如何工作的?

1个回答

2

查看源代码后,我发现:

def _initialize(self, y, layer_units):

    # set all attributes, allocate weights etc for first call
    # Initialize parameters
    self.n_iter_ = 0
    self.t_ = 0
    self.n_outputs_ = y.shape[1]

    # Compute the number of layers
    self.n_layers_ = len(layer_units)

    # Output for regression
    if not is_classifier(self):
        self.out_activation_ = 'identity'
    # Output for multi class
    elif self._label_binarizer.y_type_ == 'multiclass':
        self.out_activation_ = 'softmax'
    # Output for binary class and multi-label
    else:
        self.out_activation_ = 'logistic'

似乎MLP分类器在二元分类中使用逻辑函数,在多标签分类中使用softmax函数来构建输出层。这表明,网络的输出是一个概率向量,基于该向量,网络进行推断预测。
如果我们看predict_proba方法:
def predict_proba(self, X):
    """Probability estimates.
    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        The input data.
    Returns
    -------
    y_prob : ndarray of shape (n_samples, n_classes)
        The predicted probability of the sample for each class in the
        model, where classes are ordered as they are in `self.classes_`.
    """
    check_is_fitted(self)
    y_pred = self._predict(X)

    if self.n_outputs_ == 1:
        y_pred = y_pred.ravel()

    if y_pred.ndim == 1:
        return np.vstack([1 - y_pred, y_pred]).T
    else:
        return y_pred

这证实了在输出层中使用一个softmax或者logistic作为激活函数的作用,以便得到一个概率向量。

希望这能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接