Add `min` and `max` rev specializations?

オープン
#3,249 コメント 10 件 リアクション 3 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
cpp
領域
backend

調査の方向性

まずリンクされた Stack Exchange と PyTorch の議論を読み、その後、ライブラリに既にある min/max API と reverse-mode の特殊化パターンを調べてください。等しい引数と提案された勾配を含め、スカラーおよびベクトルの意図された挙動を明確にしてください。合意された特殊化が、これらのケースをカバーするテスト付きで実装されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

good first issue

From this stackoverflow post it looks like we can phrase max(a,b) into

$$
max(a,b) = \frac{a + b + |a-b|}{2}
$$

and similarly for min

$$
min(a,b) = \frac{a + b - |a-b|}{2}
$$

So except for the case when $a=b$ this function is differentiable. For max we have

$$
\begin{align}
\frac{\partial max}{a} = \frac{1}{2} + \frac{sign(a-b)}{2}
\frac{\partial max}{a} = \frac{1}{2} + \frac{sign(a-b)}{2}
\end{align}
$$

so if a>b the gradient is 1 and if a<b the gradient is 1/2. For min we have

$$
\begin{align}
\frac{\partial min}{a} = \frac{1}{2} - \frac{sign(a-b)}{2}
\frac{\partial min}{a} = \frac{1}{2} - \frac{sign(a-b)}{2}
\end{align}
$$

where if $a>b$ the gradient is 0 and if $a<b$ the gradient is 1/2

From this it seems like pytorch does something similar to the above. I think this would be nice to implement. We can also write min(vector) for these as well.

主要言語
C++
スター
845
フォーク
225
平均マージ
3日 9時間
マージ済み PR(30日)
12

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

stan-dev/math のほかの issue

stan-dev/math の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。