Expression Templates Library (ETL)
bce.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 namespace etl::impl::standard {
16 
23 template <typename O, typename L>
24 value_t<O> bce_loss(const O& output, const L& labels, value_t<O> scale) {
25  return scale * sum((labels >> log(output)) + ((1.0 - labels) >> log(1.0 - output)));
26 }
27 
34 template <typename O, typename L>
35 value_t<O> bce_error(const O& output, const L& labels, value_t<O> scale) {
36  return scale * asum(labels - output);
37 }
38 
45 template <typename O, typename L>
46 std::pair<value_t<O>, value_t<O>> bce(const O& output, const L& labels, value_t<O> alpha, value_t<O> beta) {
47  return std::make_pair(bce_loss(output, labels, alpha), bce_error(output, labels, beta));
48 }
49 
50 } //end of namespace etl::impl::standard
Definition: prob_pooling.hpp:10
auto scale(LE &&lhs, RE &&rhs)
Builds an expression representing the scalar multiplication of lhs and rhs.
Definition: binary_expression_builder.hpp:64
value_t< E > asum(E &&values)
Returns the sum of all the absolute values contained in the given expression.
Definition: expression_builder.hpp:637
value_t< E > sum(E &&values)
Returns the sum of all the values contained in the given expression.
Definition: expression_builder.hpp:624
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
auto log(E &&value) -> detail::unary_helper< E, log_unary_op >
Apply logarithm (base e) on each value of the given expression.
Definition: function_expression_builder.hpp:64