基本上你在做
softmax
功能,对吗?
预防的一般方法
软最大值
过流/欠流是(从
here
)
# Instead of this . . .
def softmax(x, axis = 0):
return np.exp(x) / np.sum(np.exp(x), axis = axis, keepdims = True)
# Do this
def softmax(x, axis = 0):
e_x = np.exp(x - np.max(x, axis = axis, keepdims = True))
return e_x / e_x.sum(axis, keepdims = True)
这个界限
e_x
介于0和1之间,并确保
EAX
将永远
1
(即元素
np.argmax(x)
)这可以防止溢出和下溢(当
np.exp(x.max())
大于或小于
float64
可以处理。
在这种情况下,由于您不能更改算法,我将接受输入
D
并使
D_ = D - D.min()
因为这在数字上应该等同于上述,因为
W.max()
应该是
-gamma * D.min()
(你只是在翻动标志)。你的算法是关于
D_
编辑:
正如下面@paulbrodersen推荐的,您可以基于
sklearn
实施
here
:
def rbf_kernel_safe(X, Y=None, gamma=None):
X, Y = sklearn.metrics.pairwise.check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 1.0 / X.shape[1]
K = sklearn.metrics.pairwise.euclidean_distances(X, Y, squared=True)
K *= -gamma
K -= K.max()
np.exp(K, K) # exponentiate K in-place
return K
然后在传播中使用它
LabelPropagation(kernel = rbf_kernel_safe, tol = 0.01, gamma = 20).fit(X, Y)
不幸的是我只有
v0.18
,它不接受用户定义的内核函数
LabelPropagation
,所以我不能测试它。
编辑2:
检查你的资料来源为什么你有这么大的
gamma
价值观让我怀疑你是否在使用
gamma = D.min()/3
,这是不正确的。定义是
sigma = D.min()/3
而对
sigma
在里面
w
是
w = exp(-d**2/sigma**2) # Equation (1)
哪个才是正确的
伽马
价值
1/sigma**2
或
9/D.min()**2