From de675e3503dde2a93cbfecb75260285c91665a14 Mon Sep 17 00:00:00 2001 From: Tomasz Sobczyk Date: Mon, 30 Nov 2020 20:32:53 +0100 Subject: [PATCH] Reintroduce optional scaling of the teacher signal. --- src/learn/autograd.h | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/learn/learn.cpp | 21 ++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/learn/autograd.h b/src/learn/autograd.h index 4383dfab..7b2853df 100644 --- a/src/learn/autograd.h +++ b/src/learn/autograd.h @@ -455,6 +455,55 @@ namespace Learner::Autograd::UnivariateStatic return Product&&, RhsT&&>(Constant(lhs), std::forward(rhs)); } + template ::ValueType> + struct Quotient : Evaluable> + { + using ValueType = T; + + static constexpr bool is_constant = Detail::AreAllConstantV; + + constexpr Quotient(LhsT&& lhs, RhsT&& rhs) : + m_lhs(std::forward(lhs)), + m_rhs(std::forward(rhs)) + { + } + + template + [[nodiscard]] T calculate_value(const std::tuple& args) const + { + return m_lhs.value(args) / m_rhs.value(args); + } + + template + [[nodiscard]] T calculate_grad(const std::tuple& args) const + { + auto g = m_rhs.value(args); + return (m_lhs.grad(args) * g - m_lhs.value(args) * m_rhs.grad(args)) / (g * g); + } + + private: + StoreValueOrRef m_lhs; + StoreValueOrRef m_rhs; + }; + + template ::ValueType> + [[nodiscard]] constexpr auto operator/(LhsT&& lhs, RhsT&& rhs) + { + return Quotient(std::forward(lhs), std::forward(rhs)); + } + + template ::ValueType> + [[nodiscard]] constexpr auto operator/(LhsT&& lhs, Id rhs) + { + return Quotient&&>(std::forward(lhs), Constant(rhs)); + } + + template ::ValueType> + [[nodiscard]] constexpr auto operator/(Id lhs, RhsT&& rhs) + { + return Quotient&&, RhsT&&>(Constant(lhs), std::forward(rhs)); + } + template ::ValueType> struct Negation : Evaluable> { diff --git a/src/learn/learn.cpp b/src/learn/learn.cpp index 8e32836b..07e5bd4a 100644 --- a/src/learn/learn.cpp +++ b/src/learn/learn.cpp @@ -220,6 +220,25 @@ namespace Learner return loss_; } + template + static auto& scale_score_(ValueT&& v_) + { + using namespace Learner::Autograd::UnivariateStatic; + + // Normalize to [0.0, 1.0]. + static thread_local auto normalized_ = + (std::forward(v_) - ConstantRef(src_score_min_value)) + / (ConstantRef(src_score_max_value) - ConstantRef(src_score_min_value)); + + // Scale to [dest_score_min_value, dest_score_max_value]. + static thread_local auto scaled_ = + normalized_ + * (ConstantRef(dest_score_max_value) - ConstantRef(dest_score_min_value)) + + ConstantRef(dest_score_min_value); + + return scaled_; + } + template static auto& expected_perf_(ValueT&& v_) { @@ -249,7 +268,7 @@ namespace Learner */ static thread_local auto q_ = expected_perf_(VariableParameter{}); - static thread_local auto p_ = expected_perf_(ConstantParameter{}); + static thread_local auto p_ = expected_perf_(scale_score_(ConstantParameter{})); static thread_local auto t_ = (ConstantParameter{} + 1.0) * 0.5; static thread_local auto lambda_ = ConstantParameter{}; static thread_local auto loss_ = cross_entropy_(q_, p_, t_, lambda_);