mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 14:27:45 +00:00
Simplify passing constants that may vary between calls.
This commit is contained in:
@@ -281,6 +281,36 @@ namespace Learner::Autograd::UnivariateStatic
|
|||||||
T m_x;
|
T m_x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The "constant" may change between executions, but is assumed to be
|
||||||
|
// constant during a single evaluation.
|
||||||
|
template <typename T>
|
||||||
|
struct ConstantRef : Evaluable<T, ConstantRef<T>>
|
||||||
|
{
|
||||||
|
using ValueType = T;
|
||||||
|
|
||||||
|
static constexpr bool is_constant = true;
|
||||||
|
|
||||||
|
constexpr ConstantRef(const T& x) :
|
||||||
|
m_x(x)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... ArgsTs>
|
||||||
|
[[nodiscard]] T calculate_value(const std::tuple<ArgsTs...>&) const
|
||||||
|
{
|
||||||
|
return m_x;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... ArgsTs>
|
||||||
|
[[nodiscard]] T calculate_grad(const std::tuple<ArgsTs...>&) const
|
||||||
|
{
|
||||||
|
return T(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const T& m_x;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename LhsT, typename RhsT, typename T = typename std::remove_reference_t<LhsT>::ValueType>
|
template <typename LhsT, typename RhsT, typename T = typename std::remove_reference_t<LhsT>::ValueType>
|
||||||
struct Sum : Evaluable<T, Sum<LhsT, RhsT, T>>
|
struct Sum : Evaluable<T, Sum<LhsT, RhsT, T>>
|
||||||
{
|
{
|
||||||
|
|||||||
+13
-4
@@ -220,6 +220,16 @@ namespace Learner
|
|||||||
return loss_;
|
return loss_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename ValueT>
|
||||||
|
static auto& expected_perf_(ValueT&& v_)
|
||||||
|
{
|
||||||
|
using namespace Learner::Autograd::UnivariateStatic;
|
||||||
|
|
||||||
|
static thread_local auto perf_ = sigmoid(std::forward<ValueT>(v_) * ConstantRef<double>(winning_probability_coefficient));
|
||||||
|
|
||||||
|
return perf_;
|
||||||
|
}
|
||||||
|
|
||||||
static ValueWithGrad<double> get_loss(Value shallow, Value teacher_signal, int result, int ply)
|
static ValueWithGrad<double> get_loss(Value shallow, Value teacher_signal, int result, int ply)
|
||||||
{
|
{
|
||||||
using namespace Learner::Autograd::UnivariateStatic;
|
using namespace Learner::Autograd::UnivariateStatic;
|
||||||
@@ -238,8 +248,8 @@ namespace Learner
|
|||||||
auto loss_ = pow(q_ - p_, 2.0) * (1.0 / (2400.0 * 2.0 * 600.0));
|
auto loss_ = pow(q_ - p_, 2.0) * (1.0 / (2400.0 * 2.0 * 600.0));
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static thread_local auto q_ = sigmoid(VariableParameter<double, 0>{} * ConstantParameter<double, 4>{});
|
static thread_local auto q_ = expected_perf_(VariableParameter<double, 0>{});
|
||||||
static thread_local auto p_ = sigmoid(ConstantParameter<double, 1>{} * ConstantParameter<double, 4>{});
|
static thread_local auto p_ = expected_perf_(ConstantParameter<double, 1>{});
|
||||||
static thread_local auto t_ = (ConstantParameter<double, 2>{} + 1.0) * 0.5;
|
static thread_local auto t_ = (ConstantParameter<double, 2>{} + 1.0) * 0.5;
|
||||||
static thread_local auto lambda_ = ConstantParameter<double, 3>{};
|
static thread_local auto lambda_ = ConstantParameter<double, 3>{};
|
||||||
static thread_local auto loss_ = cross_entropy_(q_, p_, t_, lambda_);
|
static thread_local auto loss_ = cross_entropy_(q_, p_, t_, lambda_);
|
||||||
@@ -248,8 +258,7 @@ namespace Learner
|
|||||||
(double)shallow,
|
(double)shallow,
|
||||||
(double)teacher_signal,
|
(double)teacher_signal,
|
||||||
(double)result,
|
(double)result,
|
||||||
calculate_lambda(teacher_signal),
|
calculate_lambda(teacher_signal)
|
||||||
winning_probability_coefficient
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return loss_.eval(args).clamp_grad(max_grad);
|
return loss_.eval(args).clamp_grad(max_grad);
|
||||||
|
|||||||
Reference in New Issue
Block a user