你不需要矩阵微积分
You Don't Need Matrix Calculus

原始链接: https://julesjacobs.com/2024/07/28/you-dont-need-matrix-calculus.html

矩阵微积分常被吹捧为机器学习的重要工具,但它往往过于复杂,且并无必要。它要求人们死记硬背那些僵化且形式特定的规则,而这些规则在处理张量和不同向量方向时显得力不从心。 作者认为存在一种更简单、更稳健的替代方案:**指标记号法**。通过写出各个元素(例如 $A_{ij}$)并运用普通微积分,你可以更清晰、更轻松地推导出导数。这种方法具有普适性,避免了专用矩阵规则的陷阱,而且通常计算速度更快。与其费力破译晦涩难懂的矩阵微积分公式,不如直接对展开的指标表达式进行求导,从而获得任何变量的精确结果。归根结底,指标记号法提供了一种直观的、“一举三得”的方法,使复杂的微分任务变得易于管理,且无需额外学习一套新的形式系统。

Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 你不需要矩阵微积分 (julesjacobs.com) 11 点,作者:srean,1 小时前 | 隐藏 | 过往 | 收藏 | 2 条评论 | 帮助 srean 56 分钟前 [–] bollu 在这里也表达了同样的观点:https://news.ycombinator.com/item?id=23359467 回复 marginalia_nu 7 分钟前 | 父评论 [–] 是的,我想几乎所有人都会同意这些矩阵公式带来的困惑远多于帮助。这门学科本身其实并不复杂,但其中的术语和符号让它变得非常难以入门。将运算展开为显式的求和形式,能让你更清楚地理解到底发生了什么。矩阵表达式在某些领域确实很有参考价值且有帮助,但至少在机器学习教学的语境下,这里显然并不适用。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 加入 YC | 联系 搜索:
相关文章

原文

Matrix calculus pops up on social media from time to time, primarily due to the increased popularity of machine learning. It lets you differentiate functions of vector or matrix variables even if you are not a fan of automatic differentiation. It involves all kinds of complex rules, depending on whether your vectors are row or column vectors, and it gets even worse for matrices. Let’s not speak of tensors.

You do not need matrix calculus. When physists started working with tensors almost a century ago, they figured out the easy way to do this: instead of trying to come up with a system for tensor differentiation \(\partial A / \partial B\), just write out the formulas for \(A_{ijk}\) and \(B_{ijk}\) and use ordinary differentiation.

The advantage is that this strategy always works, you only need to know how to do ordinary differentiation, and it’s usually quicker, too. Matrix calculus only works for formulas in very specific form, and you need to memorize a bunch of new rules to apply it. Even then, it doesn’t actually save much time, if any. Just write out the indices!

Consider the neuron activation function:

\[\mathsf{activation}(x) = \max(0, Ax + b)\]

The max here is to be interpreted elementwise:

\[\mathsf{activation}(x)_i = \max(0, (Ax)_i + b_i) = \begin{cases} 0 & \text{ if } (Ax)_i + b_i < 0\\ (Ax)_i + b_i & \text{ otherwise} \end{cases}\]

Let’s just write out the matrix multiplication:

\[\mathsf{activation}(x)_i = \begin{cases} 0 & \text{ if } (Ax)_i + b_i < 0\\ \sum_j A_{ij}x_j + b_i & \text{ otherwise} \end{cases}\]

Let’s differentiate:

\[\mathsf{activation}(x)_i' = \begin{cases} 0 & \text{ if } (Ax)_i + b_i < 0\\ \sum_j (A_{ij}'x_j + A_{ij}x_j' + b_i') & \text{ otherwise} \end{cases}\]

Note that we haven’t said with respect to what we’re differentiating, we just used the \(y'\) notation. We have three possibilities:

  1. Differentiate with respect to \(x_k\)
  2. Differentiate with respect to \(A_{kl}\)
  3. Differentiate with respect to \(b_k\)

To do so, we simply set the corresponding primed variable to \(1\) and the other primed variables to \(0\). For instance to differentiate with respect to \(A_{kl}\) we set \(A_{kl}' = 1\) and all the other \(A_{ij}' = 0\) and \(x_i'= 0\) and \(b_i' = 0\):

\[\frac{\partial \mathsf{activation}(x)_i}{\partial A_{kl}} = \begin{cases} 0 & \text{ if } (Ax)_i + b_i < 0\\ x_l & \text{ otherwise} \end{cases}\]

That’s it. For the others:

\[\frac{\partial \mathsf{activation}(x)_i}{\partial x_{k}} = \begin{cases} 0 & \text{ if } (Ax)_i + b_i < 0\\ A_{ik} & \text{ otherwise} \end{cases}\] \[\frac{\partial \mathsf{activation}(x)_i}{\partial b_{k}} = \begin{cases} 0 & \text{ if } (Ax)_i + b_i < 0 \\ 1 & \text{ if } i = k\\ 0 & \text{ otherwise} \end{cases}\]

We got three for the price of one. Compare with the matrix calculus derivation. Can you decipher their final result for the derivative with respect to \(w\)? It’s trickier than you think.

Just write out the indices!

联系我们 contact @ memedata.com