-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathActivations.cpp
More file actions
89 lines (73 loc) · 2.07 KB
/
Activations.cpp
File metadata and controls
89 lines (73 loc) · 2.07 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
/*******************************************************
* Copyright (c) 2017, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/autograd/Functions.hpp>
#include <af/nn/Modules/Activations.hpp>
#include <af/nn/Init.hpp>
namespace af
{
namespace nn
{
using namespace autograd;
Sigmoid::Sigmoid() {}
Variable Sigmoid::forward(const Variable &input)
{
return sigmoid(input);
}
Tanh::Tanh() {}
Variable Tanh::forward(const Variable &input)
{
return tanh(input);
}
ReLU::ReLU() {}
Variable ReLU::forward(const Variable &input)
{
return max(input, 0.0);
}
LeakyReLU::LeakyReLU(double slope) :
m_slope(slope)
{
}
Variable LeakyReLU::forward(const Variable &input)
{
return max(input, m_slope * input);
}
PReLU::PReLU(int size, double value)
{
auto w = nn::constant(value, size, 1);
setParams({w});
}
PReLU::PReLU(const Variable &w) :
Module({w})
{
}
Variable PReLU::forward(const Variable &input)
{
auto mask = input >= 0.0;
return (input * mask) + (input * !mask * tileAs(m_parameters[0], input));
}
ELU::ELU(double alpha) :
m_alpha(alpha)
{
}
Variable ELU::forward(const Variable &input)
{
auto mask = input >= 0.0;
return (mask * input) + (!mask * m_alpha * (exp(input)-1));
}
ThresholdReLU::ThresholdReLU(double threshold) :
m_threshold(threshold)
{
}
Variable ThresholdReLU::forward(const Variable &input)
{
auto mask = input >= m_threshold;
return input * mask;
}
}
}