Softmax の定義
逆温度 λ>0 の softmax:Rd→Rd は次のように定義されます。
softmax(x)=i=1∑dexp(λxi)1exp(λx1)…exp(λxd)
Softmax は log-sum-exp の勾配
log-sum-exp 関数 lse:Rd→R は次のように定義されます。
lse(x)=λ1logi=1∑dexp(λxi)
softmax(x) は、log-sum-exp 関数の勾配です。
つまり、次が成り立ちます。
Proposition 1.
∇lse(x)=softmax(x)
証明
∇lse(x)=λ1∇logi=1∑dexp(λxi)=λ1i=1∑dexp(λxi)1∇i=1∑dexp(λxi)=λ1i=1∑dexp(λxi)λexp(λx1)…exp(λxd)=i=1∑dexp(λxi)1exp(λx1)…exp(λxd)=softmax(x)
Softmax の微分
softmax(x) の微分は、次のように計算できます。
Proposition 2.
Jsoftmax(x)=λ(diag(softmax(x))−softmax(x)softmax(x)T)
証明
∂xj∂(softmax(x))i を計算します。
i=j の場合
∂xi∂(softmax(x))i=∂xi∂k=1∑dexp(λxk)exp(λxi)=(k=1∑dexp(λxk))2λexp(λxi)k=1∑dexp(λxk)−λexp(λxi)2=λsoftmax(x)i−λsoftmax(x)i2
i=j の場合
∂xj∂(softmax(x))i=∂xj∂k=1∑dexp(λxk)exp(λxi)=−(k=1∑dexp(λxk))2λexp(λxi)exp(λxj)=−λsoftmax(x)isoftmax(x)j
よって、微分は次のように計算できます。
Jsoftmax(x)=λ(diag(softmax(x))−softmax(x)softmax(x)T)
References