-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEWLayer.hpp
More file actions
145 lines (132 loc) · 3.97 KB
/
Copy pathEWLayer.hpp
File metadata and controls
145 lines (132 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include <algorithm>
#include <cmath>
#include <string>
#include <type_traits>
#include <utility>
#include "layers/Layer.hpp"
namespace it_lab_ai {
template <typename T>
T relu(const T& value) {
if (value > T(0)) {
return value;
}
return T(0);
}
class EWLayer : public Layer {
public:
EWLayer() : Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}
EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
: Layer(kElementWise),
func_(std::move(function)),
alpha_(alpha),
beta_(beta) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override {
std::vector<int> v = {0};
Tensor a = make_tensor(v);
return a;
}
#endif
private:
std::string func_;
float alpha_;
float beta_;
};
template <typename ValueType>
class EWLayerImpl : public LayerImpl<ValueType> {
public:
EWLayerImpl() = delete;
EWLayerImpl(const Shape& shape, std::string function, float alpha = 0.0F,
float beta = 0.0F,
ParBackend parallel_backend = ParBackend::kSeq);
EWLayerImpl(const EWLayerImpl& c) = default;
EWLayerImpl& operator=(const EWLayerImpl& c) = default;
std::vector<ValueType> run(
const std::vector<ValueType>& input) const override;
private:
std::string func_;
float alpha_;
float beta_;
ParBackend parallel_backend_;
};
template <typename ValueType>
EWLayerImpl<ValueType>::EWLayerImpl(const Shape& shape, std::string function,
float alpha, float beta,
ParBackend parallel_backend)
: LayerImpl<ValueType>(shape, shape),
func_(std::move(function)),
alpha_(alpha),
beta_(beta),
parallel_backend_(parallel_backend) {}
template <typename ValueType>
std::vector<ValueType> EWLayerImpl<ValueType>::run(
const std::vector<ValueType>& input) const {
std::vector<ValueType> res(this->outputShape_.count());
parallel::Options options;
options.backend = parallel_backend_;
if (func_ == "relu") {
parallel::parallel_for(
input.size(),
[&](std::size_t i) {
res[i] = input[i] > ValueType(0) ? input[i] : ValueType(0);
},
options);
} else if (func_ == "tanh") {
parallel::parallel_for(
input.size(),
[&](std::size_t i) {
res[i] = static_cast<ValueType>(std::tanh(input[i]));
},
options);
} else if (func_ == "sin") {
parallel::parallel_for(
input.size(),
[&](std::size_t i) {
res[i] = static_cast<ValueType>(std::sin(input[i]));
},
options);
} else if (func_ == "minus") {
parallel::parallel_for(
input.size(), [&](std::size_t i) { res[i] = -input[i]; }, options);
} else if (func_ == "linear") {
parallel::parallel_for(
input.size(),
[&](std::size_t i) {
res[i] = input[i] * static_cast<ValueType>(alpha_) +
static_cast<ValueType>(beta_);
},
options);
} else if (func_ == "sigmoid") {
if constexpr (std::is_integral_v<ValueType>) {
parallel::parallel_for(
input.size(),
[&](std::size_t i) {
auto x_float = static_cast<float>(input[i]);
float result = 1.0F / (1.0F + std::exp(-x_float));
res[i] = static_cast<ValueType>(std::round(result));
},
options);
} else {
parallel::parallel_for(
input.size(),
[&](std::size_t i) {
ValueType x = input[i];
if (x >= ValueType(0)) {
ValueType z = std::exp(-x);
res[i] = ValueType(1) / (ValueType(1) + z);
} else {
ValueType z = std::exp(x);
res[i] = z / (ValueType(1) + z);
}
},
options);
}
} else {
throw std::invalid_argument("No such function for EWLayer");
}
return res;
}
} // namespace it_lab_ai