-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathrelu_layer.hpp
More file actions
55 lines (48 loc) · 1.64 KB
/
Copy pathrelu_layer.hpp
File metadata and controls
55 lines (48 loc) · 1.64 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
// Copyright 2016, Tobias Hermann.
// https://github.com/Dobiasd/frugally-deep
// Distributed under the MIT License.
// (See accompanying LICENSE file or at
// https://opensource.org/licenses/MIT)
#pragma once
#include "fdeep/layers/activation_layer.hpp"
#include <algorithm>
#include <limits>
#include <string>
namespace fdeep {
namespace internal {
class relu_layer : public activation_layer {
public:
explicit relu_layer(const std::string& name,
const float_type max_value,
const float_type negative_slope,
const float_type threshold)
: activation_layer(name)
, max_value_(max_value)
, negative_slope_(negative_slope)
, threshold_(threshold)
{
}
protected:
tensor transform_input(const tensor& in_vol) const override
{
if (negative_slope_ == static_cast<float_type>(0) && threshold_ == static_cast<float_type>(0) && max_value_ == std::numeric_limits<float_type>::max()) {
return transform_tensor([](float_type x) -> float_type {
return std::max(static_cast<float_type>(0), x);
},
in_vol);
}
return transform_tensor([&](float_type x) -> float_type {
if (x >= max_value_)
return max_value_;
if (threshold_ <= x && x < max_value_)
return x;
return negative_slope_ * (x - threshold_);
},
in_vol);
}
float_type max_value_;
float_type negative_slope_;
float_type threshold_;
};
}
}