CellularAutomata
Loading...
Searching...
No Matches
activations.inl
1#include "activations.hpp"
2#include "../core/common.hpp"
3#include <cmath>
4
5#ifdef __CUDACC__
6#include <cuda_runtime.h>
7#endif
8
9#define EULER_NUMBER_F 2.71828182846f
10
11namespace CellularAutomata
12{
13 namespace Activations
14 {
15 template<typename T>
16 cudaFn T normal<T>::operator()(const T x) const
17 {
18 return static_cast<T>(std::min(0, std::max(x, 1)));
19 }
20
21 template<typename T>
22 cudaFn T life<T>::operator()(const T x) const
23 {
24 unsigned char u = static_cast<unsigned char>(x);
25
26 unsigned char neighbours = u & 0xF; // low nibble
27 bool alive = u & 0xF0; // high nibble
28
29 switch (neighbours)
30 {
31 case 2:
32 // staying alive
33 return static_cast<T>((alive) ? 1 : 0);
34 case 3:
35 // birth
36 return static_cast<T>(1);
37 default:
38 // under- / overpopulation
39 return static_cast<T>(0);
40 }
41 }
42
43 template<typename T>
44 cudaFn T sigmoid<T>::operator()(const T x) const
45 {
46 return (1 / (1 + std::pow(EULER_NUMBER_F, -x)));
47 }
48
49 template<typename T>
50 cudaFn T tanh<T>::operator()(const T x) const
51 {
52 return std::tanh(x);
53 }
54
55 template<typename T>
56 cudaFn T sin<T>::operator()(const T x) const
57 {
58 return std::sin(x);
59 }
60
61 template<typename T>
62 cudaFn T cos<T>::operator()(const T x) const
63 {
64 return std::cos(x);
65 }
66
67 template<typename T>
68 cudaFn T tan<T>::operator()(const T x) const
69 {
70 return std::tan(x);
71 }
72 }
73}
T operator()(const T x) const
Functor call.
Definition activations.inl:62
T operator()(const T x) const
Functor call.
Definition activations.inl:22
T operator()(const T x) const
Functor call.
Definition activations.inl:16
T operator()(const T x) const
Functor call.
Definition activations.inl:44
T operator()(const T x) const
Functor call.
Definition activations.inl:56
T operator()(const T x) const
Functor call.
Definition activations.inl:68
T operator()(const T x) const
Functor call.
Definition activations.inl:50
Main namespace.
Definition common.hpp:33