Expression Templates Library (ETL)
function_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 
14 #pragma once
15 
16 namespace etl {
17 
23 template <etl_expr E>
26 }
27 
33 template <etl_expr E>
36 }
37 
43 template <etl_expr E>
46 }
47 
53 template <etl_expr E>
56 }
57 
63 template <etl_expr E>
66 }
67 
73 template <etl_expr E>
76 }
77 
83 template <etl_expr E>
86 }
87 
93 template <etl_expr E>
96 }
97 
103 template <etl_expr E>
106 }
107 
113 template <etl_expr E>
116 }
117 
123 template <etl_expr E>
126 }
127 
133 template <etl_expr E>
136 }
137 
143 template <etl_expr E>
146 }
147 
153 template <etl_expr E>
156 }
157 
163 template <etl_expr E>
166 }
167 
173 template <typename E>
174 decltype(auto) identity(E&& value) {
175  return std::forward<E>(value);
176 }
177 
183 template <typename E>
184 auto identity_derivative([[maybe_unused]] E&& value) {
185  return 1.0;
186 }
187 
188 //Note: Use of decltype here should not be necessary, but g++ does
189 //not like it without it for some reason
190 
196 template <etl_expr E>
199 }
200 
206 template <etl_expr E>
209 }
210 
216 template <etl_expr E>
217 auto sigmoid_derivative(E&& value) -> decltype(sigmoid(value) >> (1.0 - sigmoid(value))) {
218  return sigmoid(value) >> (1.0 - sigmoid(value));
219 }
220 
229 template <etl_expr E>
232 }
233 
242 template <etl_expr E>
243 auto hard_sigmoid(E&& x) -> decltype(etl::clip(x * 0.2 + 0.5, 0.0, 1.0)) {
244  return etl::clip(x * 0.2 + 0.5, 0.0, 1.0);
245 }
246 
252 template <etl_expr E>
253 auto softmax(E&& e) {
254  if constexpr (etl::is_1d<E>) {
255  return exp(e) / sum(exp(e));
256  } else {
258  }
259 }
260 
267 template <etl_expr E>
268 auto stable_softmax(E&& e) {
269  if constexpr (etl::is_1d<E>) {
270  auto m = max(e);
271  return exp(e - m) / sum(exp(e - m));
272  } else {
274  }
275 }
276 
282 template <typename E>
283 auto softmax_derivative([[maybe_unused]] E&& e) {
284  return 1.0;
285 }
286 
292 template <etl_expr E>
295 }
296 
302 template <etl_expr E>
305 }
306 
312 template <typename E, typename G>
313 auto bernoulli(G& g, E&& value) {
314  return detail::make_stateful_unary_expr<E, bernoulli_unary_g_op<G, value_t<E>>>(value, g);
315 }
316 
322 template <etl_expr E>
323 auto state_bernoulli(const E& value) {
324  return detail::make_stateful_unary_expr<E, state_bernoulli_unary_op<value_t<E>>>(value);
325 }
326 
332 template <etl_expr E>
333 auto state_bernoulli(const E& value, const std::shared_ptr<void*> & states) {
334  return detail::make_stateful_unary_expr<E, state_bernoulli_unary_op<value_t<E>>>(value, states);
335 }
336 
342 template <etl_expr E, typename G>
343 auto state_bernoulli(G& g, E&& value) {
344  return detail::make_stateful_unary_expr<E, state_bernoulli_unary_g_op<G, value_t<E>>>(value, g);
345 }
346 
352 template <etl_expr E, typename G>
353 auto state_bernoulli(G& g, E&& value, const std::shared_ptr<void*> & states) {
354  return detail::make_stateful_unary_expr<E, state_bernoulli_unary_g_op<G, value_t<E>>>(value, g, states);
355 }
356 
362 template <etl_expr E>
365 }
366 
372 template <etl_expr E, typename G>
373 auto r_bernoulli(G& g, E&& value) {
374  return detail::make_stateful_unary_expr<E, reverse_bernoulli_unary_g_op<G, value_t<E>>>(value, g);
375 }
376 
382 template <etl_expr E>
383 auto tanh_derivative(E&& value) -> decltype(1.0 - (tanh(value) >> tanh(value))) {
384  return 1.0 - (tanh(value) >> tanh(value));
385 }
386 
392 template <etl_expr E>
395 }
396 
397 } //end of namespace etl
auto sin(E &&value) -> detail::unary_helper< E, sin_unary_op >
Apply sinus on each value of the given expression.
Definition: function_expression_builder.hpp:114
auto sinh(E &&value) -> detail::unary_helper< E, sinh_unary_op >
Apply hyperbolic sinus on each value of the given expression.
Definition: function_expression_builder.hpp:144
auto max(L &&lhs, R &&rhs)
Create an expression with the max value of lhs or rhs.
Definition: expression_builder.hpp:65
auto state_bernoulli(const E &value)
Apply Bernoulli sampling to the values of the expression.
Definition: function_expression_builder.hpp:323
auto tanh_derivative(E &&value) -> decltype(1.0 -(tanh(value) >> tanh(value)))
Return the derivative of the tanh function of the given ETL expression.
Definition: function_expression_builder.hpp:383
auto hard_sigmoid(E &&x) -> decltype(etl::clip(x *0.2+0.5, 0.0, 1.0))
Return an hard approximation of the logistic sigmoid of the given ETL expression. ...
Definition: function_expression_builder.hpp:243
auto log2(E &&value) -> detail::unary_helper< E, log2_unary_op >
Apply logarithm (base 2) on each value of the given expression.
Definition: function_expression_builder.hpp:74
auto sqrt(E &&value) -> detail::unary_helper< E, sqrt_unary_op >
Apply square root on each value of the given expression.
Definition: function_expression_builder.hpp:24
auto softmax(E &&e)
Return the softmax function of the given ETL expression.
Definition: function_expression_builder.hpp:253
auto relu(const E &value) -> detail::unary_helper< E, relu_unary_op >
Return the relu activation of the given ETL expression.
Definition: function_expression_builder.hpp:207
auto cosh(E &&value) -> detail::unary_helper< E, cosh_unary_op >
Apply hyperbolic cosinus on each value of the given expression.
Definition: function_expression_builder.hpp:134
auto cbrt(E &&value) -> detail::unary_helper< E, cbrt_unary_op >
Apply cubic root on each value of the given expression.
Definition: function_expression_builder.hpp:44
auto bernoulli(const E &value) -> detail::unary_helper< E, bernoulli_unary_op >
Apply Bernoulli sampling to the values of the expression.
Definition: function_expression_builder.hpp:303
auto sigmoid(const E &value) -> detail::unary_helper< E, sigmoid_unary_op >
Return the logistic sigmoid of the given ETL expression.
Definition: function_expression_builder.hpp:197
An unary expression.
Definition: unary_expr.hpp:126
auto cos(E &&value) -> detail::unary_helper< E, cos_unary_op >
Apply cosinus on each value of the given expression.
Definition: function_expression_builder.hpp:104
Root namespace for the ETL library.
Definition: adapter.hpp:15
auto tanh(E &&value) -> detail::unary_helper< E, tanh_unary_op >
Apply hyperbolic tangent on each value of the given expression.
Definition: function_expression_builder.hpp:124
auto softmax_derivative([[maybe_unused]] E &&e)
Return the derivative of the softmax function of the given ETL expression.
Definition: function_expression_builder.hpp:283
auto relu_derivative(const E &value) -> detail::unary_helper< E, relu_derivative_op >
Return the derivative of the relu function of the given ETL expression.
Definition: function_expression_builder.hpp:393
auto invcbrt(E &&value) -> detail::unary_helper< E, invcbrt_unary_op >
Apply inverse cubic root on each value of the given expression.
Definition: function_expression_builder.hpp:54
auto log10(E &&value) -> detail::unary_helper< E, log10_unary_op >
Apply logarithm (base 10) on each value of the given expression.
Definition: function_expression_builder.hpp:84
auto tan(E &&value) -> detail::unary_helper< E, tan_unary_op >
Apply tangent on each value of the given expression.
Definition: function_expression_builder.hpp:94
auto fast_sigmoid(const E &value) -> detail::unary_helper< E, fast_sigmoid_unary_op >
Return a fast approximation of the logistic sigmoid of the given ETL expression.
Definition: function_expression_builder.hpp:230
auto softplus(E &&value) -> detail::unary_helper< E, softplus_unary_op >
Return the softplus of the given ETL expression.
Definition: function_expression_builder.hpp:293
value_t< E > sum(E &&values)
Returns the sum of all the values contained in the given expression.
Definition: expression_builder.hpp:624
auto exp(E &&value) -> detail::unary_helper< E, exp_unary_op >
Apply exponential on each value of the given expression.
Definition: function_expression_builder.hpp:154
auto stable_softmax(E &&e)
Returns the softmax function of the given ETL expression. This version is implemented so that numeric...
Definition: function_expression_builder.hpp:268
auto r_bernoulli(const E &value) -> detail::unary_helper< E, reverse_bernoulli_unary_op >
Apply Reverse Bernoulli sampling to the values of the expression.
Definition: function_expression_builder.hpp:363
auto sign(E &&value) -> detail::unary_helper< E, sign_unary_op >
Apply sign on each value of the given expression.
Definition: function_expression_builder.hpp:164
auto identity_derivative([[maybe_unused]] E &&value)
Return the derivative of the identiy function for the given value.
Definition: function_expression_builder.hpp:184
decltype(auto) identity(E &&value)
Performs the identiy function on the ETL expression.
Definition: function_expression_builder.hpp:174
A batch softmax function expression.
Definition: batch_softmax_expr.hpp:20
auto sigmoid_derivative(E &&value) -> decltype(sigmoid(value) >>(1.0 - sigmoid(value)))
Return the derivative of the logistic sigmoid of the given ETL expression.
Definition: function_expression_builder.hpp:217
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
auto invsqrt(E &&value) -> detail::unary_helper< E, invsqrt_unary_op >
Apply inverse square root on each value of the given expression.
Definition: function_expression_builder.hpp:34
auto clip(E &&value, T min, T max)
Clip each values of the ETL expression between min and max.
Definition: expression_builder.hpp:110