Skip to content

Commit 49bfb8e

Browse files
fohx13pavanky
authored andcommitted
Add PReLU layer
1 parent b30a3c8 commit 49bfb8e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

include/af/nn/Modules/Activations.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ namespace af
4848

4949
autograd::Variable forward(const autograd::Variable &input);
5050
};
51+
52+
class PReLU : public Module
53+
{
54+
public:
55+
PReLU(int size, double spread = 1.0);
56+
PReLU(const autograd::Variable &w);
57+
58+
autograd::Variable forward(const autograd::Variable &input);
59+
};
60+
5161
}
5262
}

src/nn/Modules/Activations.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include <af/autograd/Functions.hpp>
1111
#include <af/nn/Modules/Activations.hpp>
12-
12+
#include <af/nn/Types.hpp>
1313
namespace af
1414
{
1515
namespace nn
@@ -46,5 +46,26 @@ namespace af
4646
{
4747
return max(input, m_slope * input);
4848
}
49+
50+
PReLU::PReLU(int size, double spread)
51+
{
52+
auto w = nn::weight(size, 1, spread);
53+
setParams({w});
54+
}
55+
56+
PReLU::PReLU(const Variable &w) :
57+
Module({w})
58+
{
59+
}
60+
61+
Variable PReLU::forward(const Variable &input)
62+
{
63+
auto tmp = max(input, 0.0);
64+
auto res = expandAs(m_parameters[0],tmp) * tmp;
65+
//TODO: Determine if doing the max after the mul is preferable
66+
return res;
67+
68+
}
69+
4970
}
5071
}

0 commit comments

Comments
 (0)