【问题标题】:How to vectorize Softmax probability of a multi dimensional matrix如何向量化多维矩阵的 Softmax 概率
【发布时间】:2017-09-12 06:51:35
【问题描述】:

我正在尝试通过 assignment 1 参加斯坦福 cs244n 课程。问题 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

我可以实现更优化的矩阵实现吗?

【问题讨论】:

    标签: python performance numpy vectorization softmax


    【解决方案1】:

    在相关的ufuncs 上使用NumPy broadcasting 并涵盖通用维数的ndarray -

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

    【讨论】:

    • 感谢您的回答。我将您的回复标记为答案,因为作业建议查看广播。
    【解决方案2】:

    您可以尝试使用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]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多