How to vectorize Softmax probability of a multi dimensional matrix

送分小仙女□ 提交于 2019-12-11 10:06:40

问题


I am trying to go through the assignment 1 for Stanford cs244n class. Problem 1b highly recommend optimization for the Softmax function. I managed to get the Softmax of the N dimensional vector. I also got the Softmax of the MxN dimensional matrix but used a for loop through the columns. I have the following code:

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

Can I implement a more optimized Matrix implementation?


回答1:


Using NumPy broadcasting on relevant ufuncs and that covers ndarrays of generic number of dimensions -

exp_max = np.exp(x - np.max(x,axis=-1,keepdims=True))
out = exp_max/np.sum(exp_max,axis=-1,keepdims=True)



回答2:


You can try to use np.apply_along_axis, where you have to specify which axis to execute your code (in your case axis=1 ). Here's a working example:

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]]


来源:https://stackoverflow.com/questions/43438174/how-to-vectorize-softmax-probability-of-a-multi-dimensional-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!