我正在尝试将softmax函数应用于numpy数组。但我没有得到预期的结果。这是我尝试过的代码:
import numpy as np
x = np.array([[1001,1002],[3,4]])
softmax = np.exp(x - np.max(x))/(np.sum(np.exp(x - np.max(x)))
print softmax
我认为x - np.max(x)
代码没有减去每行的最大值。需要从x中减去最大值以防止非常大的数字。
这应该输出
np.array([
[0.26894142, 0.73105858],
[0.26894142, 0.73105858]])
但我得到了:
np.array([
[0.26894142, 0.73105858],
[0, 0]])
答案 0 :(得分:3)
保持“减少”操作(例如max
或sum
)所消耗的轴的便捷方法是keepdims
关键字:
mx = np.max(x, axis=-1, keepdims=True)
mx
# array([[1002],
# [ 4]])
x - mx
# array([[-1, 0],
# [-1, 0]])
numerator = np.exp(x - mx)
denominator = np.sum(numerator, axis=-1, keepdims=True)
denominator
# array([[ 1.36787944],
# [ 1.36787944]])
numerator/denominator
# array([[ 0.26894142, 0.73105858],
[ 0.26894142, 0.73105858]])
答案 1 :(得分:2)
修改即可。从版本1.2.0开始,scipy包含softmax作为特殊功能:
https://scipy.github.io/devdocs/generated/scipy.special.softmax.html
我写了一个非常通用的softmax函数,在任意轴上运行,包括棘手的最大减法位。功能如下,我写了blog post about it here。
def softmax(X, theta = 1.0, axis = None):
"""
Compute the softmax of each element along an axis of X.
Parameters
----------
X: ND-Array. Probably should be floats.
theta (optional): float parameter, used as a multiplier
prior to exponentiation. Default = 1.0
axis (optional): axis to compute values along. Default is the
first non-singleton axis.
Returns an array the same size as X. The result will sum to 1
along the specified axis.
"""
# make X at least 2d
y = np.atleast_2d(X)
# find axis
if axis is None:
axis = next(j[0] for j in enumerate(y.shape) if j[1] > 1)
# multiply y against the theta parameter,
y = y * float(theta)
# subtract the max for numerical stability
y = y - np.expand_dims(np.max(y, axis = axis), axis)
# exponentiate y
y = np.exp(y)
# take the sum along the specified axis
ax_sum = np.expand_dims(np.sum(y, axis = axis), axis)
# finally: divide elementwise
p = y / ax_sum
# flatten if X was 1D
if len(X.shape) == 1: p = p.flatten()
return p
答案 2 :(得分:1)
x - np.max(x)
代码没有进行逐行减法。
让我们逐步做到。首先,我们将通过平铺或复制列来创建“maxes”数组:
maxes = np.tile(np.max(x,1), (2,1)).T
这将创建一个2X2矩阵,通过制作重复列(tile)来对应每行的最大值。在此之后你可以这样做:
x = np.exp(x - maxes)/(np.sum(np.exp(x - maxes), axis = 1))
你应该得到你的结果。 axis = 1
适用于您在答案标题中提到的行式softmax。希望这会有所帮助。
答案 3 :(得分:1)
这个怎么样?
沿着行取max
只需将参数指定为axis=1
,然后使用np.newaxis/None
将结果转换为列向量(实际上是2D数组)。
In [40]: x
Out[40]:
array([[1001, 1002],
[ 3, 4]])
In [41]: z = x - np.max(x, axis=1)[:, np.newaxis]
In [42]: z
Out[42]:
array([[-1, 0],
[-1, 0]])
In [44]: softmax = np.exp(z) / np.sum(np.exp(z), axis=1)[:, np.newaxis]
In [45]: softmax
Out[45]:
array([[ 0.26894142, 0.73105858],
[ 0.26894142, 0.73105858]])
在最后一步中,再次获取总和时,只需指定参数axis=1
即可将其与行相加。
答案 4 :(得分:1)
我的5-liner(使用scipy logsumexp获取棘手的位):
def softmax(a, axis=None):
"""
Computes exp(a)/sumexp(a); relies on scipy logsumexp implementation.
:param a: ndarray/tensor
:param axis: axis to sum over; default (None) sums over everything
"""
from scipy.special import logsumexp
lse = logsumexp(a, axis=axis) # this reduces along axis
if axis is not None:
lse = np.expand_dims(lse, axis) # restore that axis for subtraction
return np.exp(a - lse)
如果您的scipy版本较旧,则可能必须使用from scipy.misc import logsumexp
。