Expression Templates Library (ETL)
ml_expression_builder.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 
16 #pragma once
17 
18 #include "etl/impl/cce.hpp"
19 #include "etl/impl/bce.hpp"
20 #include "etl/impl/mse.hpp"
21 
22 namespace etl::ml {
23 
24 // Convolution wrappers
25 
46 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_expr A, etl_expr B>
48  return conv_4d_valid_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true>{std::forward<A>(a), std::forward<B>(b)};
49 }
50 
71 template <etl_expr A, etl_expr B>
73  A&& a, B&& b, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
74  return dyn_conv_4d_valid_expr<detail::build_type<A>, detail::build_type<B>, true>{std::forward<A>(a), std::forward<B>(b), s1, s2, p1, p2};
75 }
76 
97 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_expr A, etl_expr B>
98 conv_4d_backward_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true> convolution_backward(A&& a, B&& b) {
99  return conv_4d_backward_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true>{std::forward<A>(a), std::forward<B>(b)};
100 }
101 
122 template <etl_expr A, etl_expr B>
124  A&& a, B&& b, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
125  return dyn_conv_4d_backward_expr<detail::build_type<A>, detail::build_type<B>, true>{std::forward<A>(a), std::forward<B>(b), s1, s2, p1, p2};
126 }
127 
148 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_expr A, etl_expr B>
149 conv_4d_backward_filter_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true> convolution_backward_filter(A&& a, B&& b) {
150  return conv_4d_backward_filter_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true>{std::forward<A>(a), std::forward<B>(b)};
151 }
152 
173 template <etl_expr A, etl_expr B>
175  A&& a, B&& b, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
176  return dyn_conv_4d_backward_filter_expr<detail::build_type<A>, detail::build_type<B>, true>{std::forward<A>(a), std::forward<B>(b), s1, s2, p1, p2};
177 }
178 
179 // Pooling Wrappers
180 
188 template <size_t C1, size_t C2, size_t S1 = C1, size_t S2 = C2, size_t P1 = 0, size_t P2 = 0, typename E>
189 auto max_pool_forward(E&& value) {
190  return pool_2d_expr<detail::build_type<E>, C1, C2, S1, S2, P1, P2, impl::max_pool_2d>{value};
191 }
192 
200 template <typename E>
201 auto max_pool_forward(E&& value, size_t c1, size_t c2) {
202  return dyn_pool_2d_expr<detail::build_type<E>, impl::max_pool_2d>{value, c1, c2, c1, c2, 0, 0};
203 }
204 
212 template <typename E>
213 auto max_pool_forward(E&& value, size_t c1, size_t c2, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
214  return dyn_pool_2d_expr<detail::build_type<E>, impl::max_pool_2d>{value, c1, c2, s1, s2, p1, p2};
215 }
216 
225 template <size_t C1, size_t C2, size_t C3, typename E>
226 auto max_pool_3d_forward(E&& value) {
227  return pool_3d_expr<detail::build_type<E>, C1, C2, C3, C1, C2, C3, 0, 0, 0, impl::max_pool_3d>{value};
228 }
229 
238 template <typename E>
239 auto max_pool_3d_forward(E&& value, size_t c1, size_t c2, size_t c3) {
240  return dyn_pool_3d_expr<detail::build_type<E>, impl::max_pool_3d>{value, c1, c2, c3, c1, c2, c3, 0, 0, 0};
241 }
242 
250 template <size_t C1, size_t C2, size_t S1 = C1, size_t S2 = C2, size_t P1 = 0, size_t P2 = 0, typename E>
251 auto avg_pool_forward(E&& value) {
252  return pool_2d_expr<detail::build_type<E>, C1, C2, S1, S2, P1, P2, impl::avg_pool_2d>{value};
253 }
254 
262 template <typename E>
263 auto avg_pool_forward(E&& value, size_t c1, size_t c2) {
264  return dyn_pool_2d_expr<detail::build_type<E>, impl::avg_pool_2d>{value, c1, c2, c1, c2, 0, 0};
265 }
266 
274 template <typename E>
275 auto avg_pool_forward(E&& value, size_t c1, size_t c2, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
276  return dyn_pool_2d_expr<detail::build_type<E>, impl::avg_pool_2d>{value, c1, c2, s1, s2, p1, p2};
277 }
278 
287 template <size_t C1, size_t C2, size_t C3, typename E>
288 auto avg_pool_3d_forward(E&& value) {
289  return pool_3d_expr<detail::build_type<E>, C1, C2, C3, C1, C2, C3, 0, 0, 0, impl::avg_pool_3d>{value};
290 }
291 
300 template <typename E>
301 auto avg_pool_3d_forward(E&& value, size_t c1, size_t c2, size_t c3) {
302  return dyn_pool_3d_expr<detail::build_type<E>, impl::avg_pool_3d>{value, c1, c2, c3, c1, c2, c3, 0, 0, 0};
303 }
304 
313 template <size_t C1, size_t C2, size_t S1, size_t S2, size_t P1, size_t P2, etl_expr A, etl_expr B, typename C>
314 pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, C1, C2, S1, S2, P1, P2, true> max_pool_backward(A&& input, B&& output, C&& errors) {
315  return {input, output, errors};
316 }
317 
326 template <etl_expr A, etl_expr B, typename C>
327 dyn_pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, true> max_pool_backward(
328  A&& input, B&& output, C&& errors, size_t c1, size_t c2) {
329  return {input, output, errors, c1, c2, c1, c2, 0, 0};
330 }
331 
340 template <etl_expr A, etl_expr B, typename C>
341 dyn_pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, true> max_pool_backward(
342  A&& input, B&& output, C&& errors, size_t c1, size_t c2, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
343  return {input, output, errors, c1, c2, s1, s2, p1, p2};
344 }
345 
354 template <size_t C1, size_t C2, size_t C3, etl_expr A, etl_expr B, typename C>
355 pool_upsample_3d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, C1, C2, C3, true> max_pool_3d_backward(A&& input,
356  B&& output,
357  C&& errors) {
358  return {input, output, errors};
359 }
360 
369 template <etl_expr A, etl_expr B, typename C>
370 dyn_pool_upsample_3d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, true> max_pool_3d_backward(
371  A&& input, B&& output, C&& errors, size_t c1, size_t c2, size_t c3) {
372  return {input, output, errors, c1, c2, c3};
373 }
374 
383 template <size_t C1, size_t C2, size_t S1, size_t S2, size_t P1, size_t P2, etl_expr A, etl_expr B, typename C>
384 pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, C1, C2, S1, S2, P1, P2, false> avg_pool_backward(A&& input, B&& output, C&& errors) {
385  return {input, output, errors};
386 }
387 
396 template <etl_expr A, etl_expr B, typename C>
397 dyn_pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, false> avg_pool_backward(
398  A&& input, B&& output, C&& errors, size_t c1, size_t c2) {
399  return {input, output, errors, c1, c2, c1, c2, 0, 0};
400 }
401 
410 template <etl_expr A, etl_expr B, typename C>
411 dyn_pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, false> avg_pool_backward(
412  A&& input, B&& output, C&& errors, size_t c1, size_t c2, size_t s1, size_t s2, size_t p1 = 0, size_t p2 = 0) {
413  return {input, output, errors, c1, c2, s1, s2, p1, p2};
414 }
415 
424 template <size_t C1, size_t C2, size_t C3, etl_expr A, etl_expr B, typename C>
425 pool_upsample_3d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, C1, C2, C3, false> avg_pool_3d_backward(A&& input,
426  B&& output,
427  C&& errors) {
428  return {input, output, errors};
429 }
430 
439 template <etl_expr A, etl_expr B, typename C>
440 dyn_pool_upsample_3d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, false> avg_pool_3d_backward(
441  A&& input, B&& output, C&& errors, size_t c1, size_t c2, size_t c3) {
442  return {input, output, errors, c1, c2, c3};
443 }
444 
445 // Derivatives with respect to output
446 
452 template <typename E>
453 auto identity_derivative_out([[maybe_unused]] E&& value) {
454  return 1.0;
455 }
456 
463 template <etl_expr E>
464 auto sigmoid_derivative_out(E&& value) -> decltype(value >> (1.0 - value)) {
465  return value >> (1.0 - value);
466 }
467 
474 template <typename E>
475 auto softmax_derivative_out([[maybe_unused]] E&& e) {
476  return 1.0;
477 }
478 
485 template <etl_expr E>
486 auto tanh_derivative_out(E&& value) -> decltype(1.0 - (value >> value)) {
487  return 1.0 - (value >> value);
488 }
489 
496 template <etl_expr E>
499 }
500 
501 // Fully-Fledged backward activation
502 
509 template <typename O, typename E>
510 decltype(auto) identity_backward([[maybe_unused]] O&& output, E&& errors) {
511  return std::forward<E>(errors);
512 }
513 
520 template <typename O, typename E>
522  return {output, errors};
523 }
524 
531 template <typename O, typename E>
533  return {output, errors};
534 }
535 
542 template <typename O, typename E>
543 decltype(auto) softmax_backward([[maybe_unused]] O&& output, E&& errors) {
544  return std::forward<E>(errors);
545 }
546 
553 template <typename O, etl_expr E>
554 auto tanh_backward(O&& output, E&& errors) {
555  return (1.0 - (output >> output)) >> errors;
556 }
557 
564 template <etl_expr O, etl_expr L>
565 value_t<O> cce_loss(O&& output, L&& labels, value_t<O> scale) {
566  return detail::cce_loss_impl::apply(std::forward<O>(output), std::forward<L>(labels), scale);
567 }
568 
575 template <etl_expr O, etl_expr L>
576 value_t<O> cce_error(O&& output, L&& labels, value_t<O> scale) {
577  return detail::cce_error_impl::apply(std::forward<O>(output), std::forward<L>(labels), scale);
578 }
579 
586 template <etl_expr O, etl_expr L>
587 value_t<O> bce_loss(O&& output, L&& labels, value_t<O> scale) {
588  return detail::bce_loss_impl::apply(std::forward<O>(output), std::forward<L>(labels), scale);
589 }
590 
597 template <etl_expr O, etl_expr L>
598 value_t<O> bce_error(O&& output, L&& labels, value_t<O> scale) {
599  return detail::bce_error_impl::apply(std::forward<O>(output), std::forward<L>(labels), scale);
600 }
601 
608 template <etl_expr O, etl_expr L>
609 std::pair<value_t<O>, value_t<O>> bce(O&& output, L&& labels, value_t<O> alpha, value_t<O> beta) {
610  return detail::bce_impl::apply(std::forward<O>(output), std::forward<L>(labels), alpha, beta);
611 }
612 
619 template <etl_expr O, etl_expr L>
620 std::pair<value_t<O>, value_t<O>> cce(O&& output, L&& labels, value_t<O> alpha, value_t<O> beta) {
621  return detail::cce_impl::apply(std::forward<O>(output), std::forward<L>(labels), alpha, beta);
622 }
623 
630 template <etl_expr O, etl_expr L>
631 value_t<O> mse_loss(O&& output, L&& labels, value_t<O> scale) {
632  return detail::mse_loss_impl::apply(std::forward<O>(output), std::forward<L>(labels), scale);
633 }
634 
641 template <etl_expr O, etl_expr L>
642 value_t<O> mse_error(O&& output, L&& labels, value_t<O> scale) {
643  return detail::mse_error_impl::apply(std::forward<O>(output), std::forward<L>(labels), scale);
644 }
645 
652 template <etl_expr O, etl_expr L>
653 std::pair<value_t<O>, value_t<O>> mse(O&& output, L&& labels, value_t<O> alpha, value_t<O> beta) {
654  return detail::mse_impl::apply(std::forward<O>(output), std::forward<L>(labels), alpha, beta);
655 }
656 
657 } //end of namespace etl::ml
A transposition expression.
Definition: dyn_pool_2d_expr.hpp:19
Selector for the Categorical Cross Entropy reduction implementations.
static std::pair< value_t< O >, value_t< O > > apply(const O &output, const L &labels, value_t< O > alpha, value_t< O > beta)
Apply the functor to e.
Definition: cce.hpp:77
value_t< O > cce_loss(O &&output, L &&labels, value_t< O > scale)
Returns the Categorical Cross Entropy Loss.
Definition: ml_expression_builder.hpp:565
Expression representing a batch of transposed 2D convolution of an batch of image with a set of kerne...
Definition: conv_4d_backward_expr.hpp:44
auto max_pool_forward(E &&value)
Forward 2D Max Pooling of the given matrix expression.
Definition: ml_expression_builder.hpp:189
value_t< O > mse_error(O &&output, L &&labels, value_t< O > scale)
Returns the Binary Cross Entropy Error.
Definition: ml_expression_builder.hpp:642
std::pair< value_t< O >, value_t< O > > bce(O &&output, L &&labels, value_t< O > alpha, value_t< O > beta)
Returns the Binary Cross Entropy Loss and Error.
Definition: ml_expression_builder.hpp:609
auto max_pool_3d_forward(E &&value)
Forward 3D Max Pooling of the given matrix expression.
Definition: ml_expression_builder.hpp:226
auto sigmoid_derivative_out(E &&value) -> decltype(value >>(1.0 - value))
Return the derivative of the logistic sigmoid of the given ETL expression, with respect to the output...
Definition: ml_expression_builder.hpp:464
static std::pair< value_t< O >, value_t< O > > apply(const O &output, const L &labels, value_t< O > alpha, value_t< O > beta)
Apply the functor to e.
Definition: mse.hpp:77
Functor for 2D Average Pooling.
Definition: pooling.hpp:174
A transposition expression.
Definition: pool_2d_expr.hpp:23
pool_upsample_2d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, C1, C2, S1, S2, P1, P2, true > max_pool_backward(A &&input, B &&output, C &&errors)
Derivative of the 2D Max Pooling of the given matrix expression and upsampling.
Definition: ml_expression_builder.hpp:314
pool_upsample_2d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, C1, C2, S1, S2, P1, P2, false > avg_pool_backward(A &&input, B &&output, C &&errors)
Derivative of the 2D Avg Pooling of the given matrix expression and upsampling.
Definition: ml_expression_builder.hpp:384
A derivative of the max pooling (combine derivative and upsampling for performance) ...
Definition: pool_upsample_3d_expr.hpp:26
conv_4d_backward_filter_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, true > convolution_backward_filter(A &&a, B &&b)
Backward convolution for a batch of images with a set of kernels.
Definition: ml_expression_builder.hpp:149
auto tanh_backward(O &&output, E &&errors)
Return the backward activation of the tanh function.
Definition: ml_expression_builder.hpp:554
Expression representing a batch of transposed 2D convolution of an batch of image with a set of kerne...
Definition: dyn_conv_4d_backward_expr.hpp:40
auto relu_backward(O &&output, E &&errors) -> detail::left_binary_helper< O, E, relu_derivative_binary_op >
Return the backward activation of the RELU function.
Definition: ml_expression_builder.hpp:532
static value_t< O > apply(const O &output, const L &labels, value_t< O > scale)
Apply the functor to e.
Definition: mse.hpp:137
auto sigmoid_backward(O &&output, E &&errors) -> detail::left_binary_helper< O, E, sigmoid_derivative_binary_op >
Return the backward activation of the sigmoid function.
Definition: ml_expression_builder.hpp:521
An unary expression.
Definition: unary_expr.hpp:126
A derivative of the 2D max pooling (combine derivative and upsampling for performance) ...
Definition: dyn_pool_upsample_2d_expr.hpp:26
auto avg_pool_forward(E &&value)
Forward Average Pooling of the given matrix expression.
Definition: ml_expression_builder.hpp:251
A binary expression.
Definition: binary_expr.hpp:18
std::pair< value_t< O >, value_t< O > > mse(O &&output, L &&labels, value_t< O > alpha, value_t< O > beta)
Returns the Binary Cross Entropy Loss and Error.
Definition: ml_expression_builder.hpp:653
A transposition expression.
Definition: dyn_pool_3d_expr.hpp:19
static value_t< O > apply(const O &output, const L &labels, value_t< O > scale)
Apply the functor to e.
Definition: mse.hpp:107
A transposition expression.
Definition: conv_4d_valid_expr.hpp:22
Functor for 2D Max Pooling.
Definition: pooling.hpp:94
static value_t< O > apply(const O &output, const L &labels, value_t< O > scale)
Apply the functor to e.
Definition: bce.hpp:137
std::conditional_t< is_etl_value< T >, const std::decay_t< T > &, std::decay_t< T > > build_type
Helper to build the type for a sub expression.
Definition: expression_helpers.hpp:24
Definition: ml_expression_builder.hpp:22
std::pair< value_t< O >, value_t< O > > cce(O &&output, L &&labels, value_t< O > alpha, value_t< O > beta)
Returns the Categorical Cross Entropy Loss and Error.
Definition: ml_expression_builder.hpp:620
auto scale(LE &&lhs, RE &&rhs)
Builds an expression representing the scalar multiplication of lhs and rhs.
Definition: binary_expression_builder.hpp:64
auto tanh_derivative_out(E &&value) -> decltype(1.0 -(value >> value))
Return the derivative of the tanh function of the given ETL expression, with respect to the output va...
Definition: ml_expression_builder.hpp:486
Selector for the Binary Cross Entropy reduction implementations.
value_t< O > bce_error(O &&output, L &&labels, value_t< O > scale)
Returns the Binary Cross Entropy Error.
Definition: ml_expression_builder.hpp:598
static std::pair< value_t< O >, value_t< O > > apply(const O &output, const L &labels, value_t< O > alpha, value_t< O > beta)
Apply the functor to e.
Definition: bce.hpp:77
value_t< O > mse_loss(O &&output, L &&labels, value_t< O > scale)
Returns the Binary Cross Entropy Loss.
Definition: ml_expression_builder.hpp:631
auto relu_derivative_out(const E &value) -> detail::unary_helper< E, relu_derivative_op >
Return the derivative of the relu function of the given ETL expression, with respect for the output v...
Definition: ml_expression_builder.hpp:497
conv_4d_valid_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, true > convolution_forward(A &&a, B &&b)
Forward convolution for a batch of images with a set of kernels.
Definition: ml_expression_builder.hpp:47
A transposition expression.
Definition: dyn_conv_4d_valid_expr.hpp:22
pool_upsample_3d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, C1, C2, C3, true > max_pool_3d_backward(A &&input, B &&output, C &&errors)
Derivative of the 3D Max Pooling of the given matrix expression and upsampling.
Definition: ml_expression_builder.hpp:355
static value_t< O > apply(const O &output, const L &labels, value_t< O > scale)
Apply the functor to e.
Definition: bce.hpp:107
Functor for 3D Average Pooling.
Definition: pooling.hpp:334
auto avg_pool_3d_forward(E &&value)
Forward 3D Average Pooling of the given matrix expression.
Definition: ml_expression_builder.hpp:288
A transposition expression.
Definition: pool_3d_expr.hpp:19
static value_t< O > apply(const O &output, const L &labels, value_t< O > scale)
Apply the functor to e.
Definition: cce.hpp:137
auto identity_derivative_out([[maybe_unused]] E &&value)
Return the derivative of the identiy function for the given output value.
Definition: ml_expression_builder.hpp:453
pool_upsample_3d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, C1, C2, C3, false > avg_pool_3d_backward(A &&input, B &&output, C &&errors)
Derivative of the 2D Avg Pooling of the given matrix expression and upsampling.
Definition: ml_expression_builder.hpp:425
A transposition expression.
Definition: conv_4d_backward_filter_expr.hpp:22
A derivative of the 2D max pooling (combine derivative and upsampling for performance) ...
Definition: pool_upsample_2d_expr.hpp:26
value_t< O > bce_loss(O &&output, L &&labels, value_t< O > scale)
Returns the Binary Cross Entropy Loss.
Definition: ml_expression_builder.hpp:587
value_t< O > cce_error(O &&output, L &&labels, value_t< O > scale)
Returns the Categorical Cross Entropy Error.
Definition: ml_expression_builder.hpp:576
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
auto softmax_derivative_out([[maybe_unused]] E &&e)
Return the derivative of the softmax function of the given ETL expression, with respect to output val...
Definition: ml_expression_builder.hpp:475
Expression representing a batch of transposed 2D convolution of an batch of image with a set of kerne...
Definition: dyn_conv_4d_backward_filter_expr.hpp:40
Functor for 3D Max Pooling.
Definition: pooling.hpp:254
A derivative of the 3D max pooling (combine derivative and upsampling for performance) ...
Definition: dyn_pool_upsample_3d_expr.hpp:26
conv_4d_backward_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, true > convolution_backward(A &&a, B &&b)
Backward convolution for a batch of images with a set of kernels.
Definition: ml_expression_builder.hpp:98
Selector for the Mean Squared Error reduction implementations.
static value_t< O > apply(const O &output, const L &labels, value_t< O > scale)
Apply the functor to e.
Definition: cce.hpp:107