如何向量化多维矩阵的 Softmax 概率
How to vectorize Softmax probability of a multi dimensional matrix
我正在尝试完成 Stanford cs244n class 的 assignment 1。问题 1b 强烈建议对 Softmax 函数进行优化。我设法得到了N维向量的Softmax。我还得到了 MxN 维矩阵的 Softmax,但在列中使用了 for 循环。我有以下代码:
def softmax(x):
orig_shape = x.shape
# Matrix
if len(x.shape) > 1:
softmax = np.zeros(orig_shape)
for i,col in enumerate(x):
softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col - np.max(col)))
# Vector
else:
softmax = np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
return softmax
我可以实现更优化的 Matrix 实现吗?
您可以尝试使用 np.apply_along_axis
,您必须在其中指定执行代码的轴(在您的情况下为 axis=1
)。
这是一个工作示例:
In [1]: import numpy as np
In [2]: def softmax(x):
...: orig_shape = x.shape
...:
...: # Matrix
...: if len(x.shape) > 1:
...: softmax = np.zeros(orig_shape)
...: for i,col in enumerate(x):
...: softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col - np.max(col)))
...: # Vector
...: else:
...: softmax = np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
...: return softmax
...:
In [3]: def softmax_vectorize(x):
...: return np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
...:
In [4]: X = np.array([[1, 0, 0, 4, 5, 0, 7],
...: [1, 0, 0, 4, 5, 0, 7],
...: [1, 0, 0, 4, 5, 0, 7]])
In [5]: print softmax(X)
[[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]]
In [6]: print np.apply_along_axis(softmax_vecorize, axis=1, arr=X)
[[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]]
使用 NumPy broadcasting
on relevant ufuncs
并且涵盖通用维数的 ndarrays -
exp_max = np.exp(x - np.max(x,axis=-1,keepdims=True))
out = exp_max/np.sum(exp_max,axis=-1,keepdims=True)
我正在尝试完成 Stanford cs244n class 的 assignment 1。问题 1b 强烈建议对 Softmax 函数进行优化。我设法得到了N维向量的Softmax。我还得到了 MxN 维矩阵的 Softmax,但在列中使用了 for 循环。我有以下代码:
def softmax(x):
orig_shape = x.shape
# Matrix
if len(x.shape) > 1:
softmax = np.zeros(orig_shape)
for i,col in enumerate(x):
softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col - np.max(col)))
# Vector
else:
softmax = np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
return softmax
我可以实现更优化的 Matrix 实现吗?
您可以尝试使用 np.apply_along_axis
,您必须在其中指定执行代码的轴(在您的情况下为 axis=1
)。
这是一个工作示例:
In [1]: import numpy as np
In [2]: def softmax(x):
...: orig_shape = x.shape
...:
...: # Matrix
...: if len(x.shape) > 1:
...: softmax = np.zeros(orig_shape)
...: for i,col in enumerate(x):
...: softmax[i] = np.exp(col - np.max(col))/np.sum(np.exp(col - np.max(col)))
...: # Vector
...: else:
...: softmax = np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
...: return softmax
...:
In [3]: def softmax_vectorize(x):
...: return np.exp(x - np.max(x))/np.sum(np.exp(x - np.max(x)))
...:
In [4]: X = np.array([[1, 0, 0, 4, 5, 0, 7],
...: [1, 0, 0, 4, 5, 0, 7],
...: [1, 0, 0, 4, 5, 0, 7]])
In [5]: print softmax(X)
[[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]]
In [6]: print np.apply_along_axis(softmax_vecorize, axis=1, arr=X)
[[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]
[ 2.08239574e-03 7.66070581e-04 7.66070581e-04 4.18260365e-02
1.13694955e-01 7.66070581e-04 8.40098401e-01]]
使用 NumPy broadcasting
on relevant ufuncs
并且涵盖通用维数的 ndarrays -
exp_max = np.exp(x - np.max(x,axis=-1,keepdims=True))
out = exp_max/np.sum(exp_max,axis=-1,keepdims=True)