Skip to content

Commit

Permalink
- ふかうら王、logitの計算の時に、policyが負の値を返すとアンダーフローする可能性があったの修正。
Browse files Browse the repository at this point in the history
 - これ、アンダーフローしたところで微小なものはどうせ0と変わらないのでアンダーフローしても探索に影響はないのだが…。
 - Softmaxまわりのコメント追加。
  • Loading branch information
yaneurao authored and nodchip committed Apr 12, 2024
1 parent e64a624 commit 22994a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/engine/dlshogi-engine/YaneuraOu_dlshogi_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void USI::extra_option(USI::OptionsMap& o)
o["C_base_root"] << USI::Option(25617, 10000, 100000);

// 探索のSoftmaxの温度
o["Softmax_Temperature"] << USI::Option( 174 /* 方策分布を学習させた場合、1400から1500ぐらいが最適値らしいが… */ , 1, 10000);
o["Softmax_Temperature"] << USI::Option( 174 , 1, 10000);

// 各GPU用のDNNモデル名と、そのGPU用のUCT探索のスレッド数と、そのGPUに一度に何個の局面をまとめて評価(推論)を行わせるのか。
// GPUは最大で8個まで扱える。
Expand Down
32 changes: 31 additions & 1 deletion source/eval/deep/nn_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,22 +563,52 @@ namespace Eval::dlshogi

// Softmaxの時の温度パラメーター。
// エンジンオプションの"Softmax_Temperature"で設定できる。

/*
softmax関数は⇓こう。
softmax(x_i) = exp(x_i) / Σ exp(x_j) for j
ここに温度パラメーターTを導入。(これがSoftmax_Temparature)
softmax_T(x_i) = exp(x_i / T) / Σ exp(x_j / T) for j
そうすると分布の分散が変わる。
Tが大きいとx_iの範囲が縮まるため、分散は下がる。
Tが小さいと分散は広がる。
探索で読み抜けする時は、Tを下げるように調整する。
*/

constexpr float default_softmax_temperature = 1.0f;
float beta = 1.0f / default_softmax_temperature;

void set_softmax_temperature(const float temperature) {
beta = 1.0f / temperature;
}

void softmax_temperature_with_normalize(std::vector<float> &log_probabilities) {

// apply beta exponent to probabilities(in log space)
float max = 0.0f;

float max = numeric_limits<float>::min();
for (float& x : log_probabilities) {
x *= beta;
if (x > max) {
max = x;
}
}

// オーバーフローを防止するため最大値で引く

/*
note :
softmax関数の定義は⇓こうなので
softmax(x_i) = exp(x_i) / Σ exp(x_j) for j
x_i + cのように定数加算したところで
softmax(x_i + c) = exp(x_i + c) / Σ exp(x_j + c) for j
= exp(x_i)exp(c) / Σ exp(x_j)exp(c) for j
でexp(c)で約分できて
= softmax(x_i)
となるので分布は変わらない。
*/

float sum = 0.0f;
for (float& x : log_probabilities) {
x = expf(x - max);
Expand Down

0 comments on commit 22994a4

Please sign in to comment.