在 Python 中从头开始计算雅可比矩阵

Compute a Jacobian matrix from scratch in Python

我正在尝试实现 softmax 函数的导数矩阵(Softmax 的雅可比矩阵)。

我从数学上知道 Softmax(Xi) 关于 Xj 的导数是:

其中红色三角洲是克罗内克三角洲。

到目前为止我实现的是:

def softmax_grad(s):
    # input s is softmax value of the original input x. Its shape is (1,n) 
    # e.i. s = np.array([0.3,0.7]), x = np.array([0,1])

    # make the matrix whose size is n^2.
    jacobian_m = np.diag(s)

    for i in range(len(jacobian_m)):
        for j in range(len(jacobian_m)):
            if i == j:
                jacobian_m[i][j] = s[i] * (1-s[i])
            else: 
                jacobian_m[i][j] = -s[i]*s[j]
    return jacobian_m

当我测试时:

In [95]: x
Out[95]: array([1, 2])

In [96]: softmax(x)
Out[96]: array([ 0.26894142,  0.73105858])

In [97]: softmax_grad(softmax(x))
Out[97]: 
array([[ 0.19661193, -0.19661193],
       [-0.19661193,  0.19661193]])

你们是如何实现雅可比矩阵的?我想知道是否有更好的方法来做到这一点。任何对 website/tutorial 的引用也将不胜感激。

您可以像下面这样矢量化 softmax_grad

soft_max = softmax(x)
​
# reshape softmax to 2d so np.dot gives matrix multiplication
def softmax_grad(softmax):
    s = softmax.reshape(-1,1)
    return np.diagflat(s) - np.dot(s, s.T)

softmax_grad(soft_max)

#array([[ 0.19661193, -0.19661193],
#       [-0.19661193,  0.19661193]])

详情sigma(j) * delta(ij)是一个以sigma(j)为对角元素的对角矩阵,可以用np.diagflat(s)创建; sigma(j) * sigma(i) 是 softmax 的矩阵乘法(或外积),可以使用 np.dot:

计算

我一直在修改这个,这就是我想出的。也许你会发现它很有用。我认为它比 Psidom 提供的解决方案更明确。

def softmax_grad(probs):
    n_elements = probs.shape[0]
    jacobian = probs[:, np.newaxis] * (np.eye(n_elements) - probs[np.newaxis, :])
    return jacobian