Reintroduce optional scaling of the teacher signal.

This commit is contained in:
Tomasz Sobczyk
2020-11-30 20:32:53 +01:00
committed by nodchip
parent 01ae7b1e2c
commit de675e3503
2 changed files with 69 additions and 1 deletions

View File

@@ -455,6 +455,55 @@ namespace Learner::Autograd::UnivariateStatic
return Product<Constant<T>&&, RhsT&&>(Constant(lhs), std::forward<RhsT>(rhs));
}
template <typename LhsT, typename RhsT, typename T = typename std::remove_reference_t<LhsT>::ValueType>
struct Quotient : Evaluable<T, Quotient<LhsT, RhsT, T>>
{
using ValueType = T;
static constexpr bool is_constant = Detail::AreAllConstantV<LhsT, RhsT>;
constexpr Quotient(LhsT&& lhs, RhsT&& rhs) :
m_lhs(std::forward<LhsT>(lhs)),
m_rhs(std::forward<RhsT>(rhs))
{
}
template <typename... ArgsTs>
[[nodiscard]] T calculate_value(const std::tuple<ArgsTs...>& args) const
{
return m_lhs.value(args) / m_rhs.value(args);
}
template <typename... ArgsTs>
[[nodiscard]] T calculate_grad(const std::tuple<ArgsTs...>& 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<LhsT> m_lhs;
StoreValueOrRef<RhsT> m_rhs;
};
template <typename LhsT, typename RhsT, typename T = typename std::remove_reference_t<LhsT>::ValueType>
[[nodiscard]] constexpr auto operator/(LhsT&& lhs, RhsT&& rhs)
{
return Quotient<LhsT&&, RhsT&&>(std::forward<LhsT>(lhs), std::forward<RhsT>(rhs));
}
template <typename LhsT, typename T = typename std::remove_reference_t<LhsT>::ValueType>
[[nodiscard]] constexpr auto operator/(LhsT&& lhs, Id<T> rhs)
{
return Quotient<LhsT&&, Constant<T>&&>(std::forward<LhsT>(lhs), Constant(rhs));
}
template <typename RhsT, typename T = typename std::remove_reference_t<RhsT>::ValueType>
[[nodiscard]] constexpr auto operator/(Id<T> lhs, RhsT&& rhs)
{
return Quotient<Constant<T>&&, RhsT&&>(Constant(lhs), std::forward<RhsT>(rhs));
}
template <typename ArgT, typename T = typename std::remove_reference_t<ArgT>::ValueType>
struct Negation : Evaluable<T, Negation<ArgT, T>>
{

View File

@@ -220,6 +220,25 @@ namespace Learner
return loss_;
}
template <typename ValueT>
static auto& scale_score_(ValueT&& v_)
{
using namespace Learner::Autograd::UnivariateStatic;
// Normalize to [0.0, 1.0].
static thread_local auto normalized_ =
(std::forward<ValueT>(v_) - ConstantRef<double>(src_score_min_value))
/ (ConstantRef<double>(src_score_max_value) - ConstantRef<double>(src_score_min_value));
// Scale to [dest_score_min_value, dest_score_max_value].
static thread_local auto scaled_ =
normalized_
* (ConstantRef<double>(dest_score_max_value) - ConstantRef<double>(dest_score_min_value))
+ ConstantRef<double>(dest_score_min_value);
return scaled_;
}
template <typename ValueT>
static auto& expected_perf_(ValueT&& v_)
{
@@ -249,7 +268,7 @@ namespace Learner
*/
static thread_local auto q_ = expected_perf_(VariableParameter<double, 0>{});
static thread_local auto p_ = expected_perf_(ConstantParameter<double, 1>{});
static thread_local auto p_ = expected_perf_(scale_score_(ConstantParameter<double, 1>{}));
static thread_local auto t_ = (ConstantParameter<double, 2>{} + 1.0) * 0.5;
static thread_local auto lambda_ = ConstantParameter<double, 3>{};
static thread_local auto loss_ = cross_entropy_(q_, p_, t_, lambda_);