矩阵乘法的梯度怎么推导
- User
矩阵乘法的梯度怎么推导
- Assistant
矩阵乘法的梯度怎么推导
矩阵乘法在许多机器学习和深度学习算法中起着关键作用,尤其是在反向传播(backpropagation)过程中计算梯度时。理解矩阵乘法的梯度推导对于实现高效的优化算法至关重要。以下是对矩阵乘法梯度推导的详细解释。
假设有两个矩阵:
矩阵乘法的结果 C = AB,其形状为 ( m \times p )。
元素级别的定义为:
[ C_{i,j} = \sum_{k=1}^{n} A_{i,k} B_{k,j} ]
推导 C = AB 相对于 A 和 B 的梯度,即 (\frac{\partial C}{\partial A}) 和 (\frac{\partial C}{\partial B})。
假设我们有一个损失函数 ( L ) ,它依赖于 C。我们需要计算 (\frac{\partial L}{\partial A})。
根据链式法则:
[ \frac{\partial L}{\partial A_{i,k}} = \sum_{j=1}^{p} \frac{\partial L}{\partial C_{i,j}} \frac{\partial C_{i,j}}{\partial A_{i,k}} ]
由于:
[ \frac{\partial C_{i,j}}{\partial A_{i,k}} = B_{k,j} ]
因此:
[ \frac{\partial L}{\partial A_{i,k}} = \sum_{j=1}^{p} \frac{\partial L}{\partial C_{i,j}} B_{k,j} ]
用矩阵表示,整体梯度为:
[ \frac{\partial L}{\partial A} = \frac{\partial L}{\partial C} B^{\top} ]
这里,(\frac{\partial L}{\partial C}) 的形状为 ( m \times p ),而 ( B^{\top} ) 的形状为 ( p \times n ),因此结果 (\frac{\partial L}{\partial A}) 的形状为 ( m \times n ),与 A 的形状一致。
同样地,使用链式法则:
[ \frac{\partial L}{\partial B_{k,j}} = \sum_{i=1}^{m} \frac{\partial L}{\partial C_{i,j}} \frac{\partial C_{i,j}}{\partial B_{k,j}} ]
由于:
[ \frac{\partial C_{i,j}}{\partial B_{k,j}} = A_{i,k} ]
因此:
[ \frac{\partial L}{\partial B_{k,j}} = \sum_{i=1}^{m} \frac{\partial L}{\partial C_{i,j}} A_{i,k} ]
用矩阵表示,整体梯度为:
[ \frac{\partial L}{\partial B} = A^{\top} \frac{\partial L}{\partial C} ]
这里,( A^{\top} ) 的形状为 ( n \times m ),而 (\frac{\partial L}{\partial C}) 的形状为 ( m \times p ),因此结果 (\frac{\partial L}{\partial B}) 的形状为 ( n \times p ),与 B 的形状一致。
给定 C = AB,其梯度计算如下:
[ \boxed{ \begin{aligned} \frac{\partial L}{\partial A} &= \frac{\partial L}{\partial C} \cdot B^{\top} \ \frac{\partial L}{\partial B} &= A^{\top} \cdot \frac{\partial L}{\partial C} \end{aligned} } ]
让我们通过一个简单的数值示例来验证上述公式。
假设:
[ A = \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} \quad (2 \times 2) ] [ B = \begin{bmatrix} 5 & 6 \ 7 & 8 \end{bmatrix} \quad (2 \times 2) ] [ C = AB = \begin{bmatrix} (1 \cdot 5 + 2 \cdot 7) & (1 \cdot 6 + 2 \cdot 8) \ (3 \cdot 5 + 4 \cdot 7) & (3 \cdot 6 + 4 \cdot 8) \end{bmatrix} = \begin{bmatrix} 19 & 22 \ 43 & 50 \end{bmatrix} ]
假设损失函数为:
[ L = C_{1,1} + 2C_{1,2} + 3C_{2,1} + 4C_{2,2} ]
则:
[ \frac{\partial L}{\partial C} = \begin{bmatrix} \frac{\partial L}{\partial C_{1,1}} & \frac{\partial L}{\partial C_{1,2}} \ \frac{\partial L}{\partial C_{2,1}} & \frac{\partial L}{\partial C_{2,2}} \end{bmatrix} = \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} ]
根据梯度公式:
[ \frac{\partial L}{\partial A} = \frac{\partial L}{\partial C} B^{\top} = \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 & 7 \ 6 & 8 \end{bmatrix} = \begin{bmatrix} (1 \cdot 5 + 2 \cdot 6) & (1 \cdot 7 + 2 \cdot 8) \ (3 \cdot 5 + 4 \cdot 6) & (3 \cdot 7 + 4 \cdot 8) \end{bmatrix} = \begin{bmatrix} 17 & 23 \ 39 & 53 \end{bmatrix} ]
[ \frac{\partial L}{\partial B} = A^{\top} \frac{\partial L}{\partial C} = \begin{bmatrix} 1 & 3 \ 2 & 4 \end{bmatrix} \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} = \begin{bmatrix} (1 \cdot 1 + 3 \cdot 3) & (1 \cdot 2 + 3 \cdot 4) \ (2 \cdot 1 + 4 \cdot 3) & (2 \cdot 2 + 4 \cdot 4) \end{bmatrix} = \begin{bmatrix} 10 & 14 \ 14 & 20 \end{bmatrix} ]
上述结果可以通过逐元素计算验证,确保梯度公式的正确性。
更一般地,在矩阵微积分中处理矩阵乘法的梯度时,经常使用如下规则:
[ dC = dA \cdot B + A \cdot dB ]
这意味着损失函数 ( L ) 的微分为:
[ dL = \text{Tr}\left( \frac{\partial L}{\partial C}^{\top} dC \right) = \text{Tr}\left( \frac{\partial L}{\partial C}^{\top} (dA \cdot B + A \cdot dB) \right) = \text{Tr}\left( \frac{\partial L}{\partial C}^{\top} dA \cdot B \right) + \text{Tr}\left( \frac{\partial L}{\partial C}^{\top} A \cdot dB \right) ]
利用迹(Trace)的性质 (\text{Tr}(X Y) = \text{Tr}(Y X)),可以重新排列项:
[ dL = \text{Tr}\left( B \cdot \frac{\partial L}{\partial C}^{\top} \cdot dA \right) + \text{Tr}\left( \frac{\partial L}{\partial C}^{\top} \cdot A \cdot dB \right) = \text{Tr}\left( \frac{\partial L}{\partial C} \cdot B^{\top} \cdot dA \right) + \text{Tr}\left( A^{\top} \cdot \frac{\partial L}{\partial C} \cdot dB \right) ]
由于 ( dL = \text{Tr}\left( \frac{\partial L}{\partial A}^{\top} dA \right) + \text{Tr}\left( \frac{\partial L}{\partial B}^{\top} dB \right) ),可以直接得到:
[ \frac{\partial L}{\partial A} = \frac{\partial L}{\partial C} B^{\top} ] [ \frac{\partial L}{\partial B} = A^{\top} \frac{\partial L}{\partial C} ]
当 ( C = AB ) 时,
这些梯度公式在实现反向传播算法和其他需要梯度计算的优化算法时非常有用。
Not yet included in a published release.