【发布时间】:2019-02-03 02:02:51
【问题描述】:
在使用LabelPropagation时,我经常遇到这个警告(恕我直言,这应该是一个错误,因为它完全无法传播):
/usr/local/lib/python3.5/dist-packages/sklearn/semi_supervised/label_propagation.py:279:RuntimeWarning:在 true_divide 中遇到无效值 self.label_distributions_ /= 规范化器
所以在尝试了几次 RBF 内核后,我发现参数 gamma 有影响。
编辑:
问题来自these lines:
if self._variant == 'propagation':
normalizer = np.sum(
self.label_distributions_, axis=1)[:, np.newaxis]
self.label_distributions_ /= normalizer
我不明白 label_distributions_ 怎么可以全为零,尤其是当它的定义是:
self.label_distributions_ = safe_sparse_dot(
graph_matrix, self.label_distributions_)
Gamma 对 graph_matrix 有影响(因为 graph_matrix 是调用内核函数的 _build_graph() 的结果)。好的。但还是。出了点问题
旧帖(编辑前)
我提醒你如何为传播计算图权重:W = exp(-gamma * D), D 数据集所有点之间的成对距离矩阵。
问题是:np.exp(x) 如果 x 非常小,则返回 0.0。
假设我们有两个点i 和j,这样dist(i, j) = 10。
>>> np.exp(np.asarray(-10*40, dtype=float)) # gamma = 40 => OKAY
1.9151695967140057e-174
>>> np.exp(np.asarray(-10*120, dtype=float)) # gamma = 120 => NOT OKAY
0.0
实际上,我不是手动设置伽玛,而是使用this paper(第 2.4 节)中描述的方法。
那么,如何避免这种除以零来获得正确的传播?
我能想到的唯一方法是在每个维度上标准化数据集,但我们会丢失数据集的一些几何/拓扑属性(例如,2x10 的矩形变成 1x1 的正方形) p>
可重现的例子:
在这个例子中,最糟糕的是:即使 gamma = 20,它也会失败。
In [11]: from sklearn.semi_supervised.label_propagation import LabelPropagation
In [12]: import numpy as np
In [13]: X = np.array([[0, 0], [0, 10]])
In [14]: Y = [0, -1]
In [15]: LabelPropagation(kernel='rbf', tol=0.01, gamma=20).fit(X, Y)
/usr/local/lib/python3.5/dist-packages/sklearn/semi_supervised/label_propagation.py:279: RuntimeWarning: invalid value encountered in true_divide
self.label_distributions_ /= normalizer
/usr/local/lib/python3.5/dist-packages/sklearn/semi_supervised/label_propagation.py:290: ConvergenceWarning: max_iter=1000 was reached without convergence.
category=ConvergenceWarning
Out[15]:
LabelPropagation(alpha=None, gamma=20, kernel='rbf', max_iter=1000, n_jobs=1,
n_neighbors=7, tol=0.01)
In [16]: LabelPropagation(kernel='rbf', tol=0.01, gamma=2).fit(X, Y)
Out[16]:
LabelPropagation(alpha=None, gamma=2, kernel='rbf', max_iter=1000, n_jobs=1,
n_neighbors=7, tol=0.01)
In [17]:
【问题讨论】:
-
也许用阈值编码 RBF 内核以避免返回 0。
-
但是如果我这样做,d=15 的点的影响将小于 d=100 的 2 点。 (类似于 1e-99 对 2*1e-99)
-
通常我会说降低伽玛值,但这无济于事,因为您可以通过算法找到它。人们倾向于采用 gamme = 1/n_features。 20 对于伽马来说真的很大。我不确定您在进行 LabelPropagation 时是否会碰到它。使用 Gamma
-
1/n_features 显然不适用于像
sklearn.datasets.make_moons(1000, noise=0.06)这样的卫星之类的数据集 -
我认为大于 10 的值非常大,但这当然取决于您的数据。至于
np.exp(x)对于非常小的 x 为零,试试这个:np.exp(np.asarray(-10*120, dtype=np.float128))
标签: python numpy machine-learning scikit-learn