Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt at ReLU #31

Merged
merged 1 commit into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/af/autograd/Functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ namespace af {
Variable cos(const Variable &input);
Variable tanh(const Variable &input);
Variable sigmoid(const Variable &input);


Variable max(const Variable &lhs, const Variable &rhs);
Variable max(const Variable &lhs, const double &rhs);
Variable max(const double &lhs, const Variable &rhs);

Variable transpose(const Variable &input);
Variable expandAs(const Variable &input, const Variable &reference);
Variable reduceAs(const Variable &input, const Variable &reference);

Variable matmul(const Variable &lhs, const Variable &rhs);
Variable matmulTN(const Variable &lhs, const Variable &rhs);
Variable matmulNT(const Variable &lhs, const Variable &rhs);


}
}
18 changes: 18 additions & 0 deletions include/af/nn/Modules/Activations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,23 @@ namespace af

autograd::Variable forward(const autograd::Variable &input);
};

class ReLU : public Module
{
public:
ReLU();

autograd::Variable forward(const autograd::Variable &input);
};

class LeakyReLU : public Module
{
private:
double m_slope;
public:
LeakyReLU(double slope = 0.0);

autograd::Variable forward(const autograd::Variable &input);
};
}
}
58 changes: 57 additions & 1 deletion src/autograd/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ namespace af {
};
return Variable(result, {lhs, rhs}, grad_func);
}

Variable operator >(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() > rhs.array();
return Variable(result, false);
}

Variable operator <=(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() <= rhs.array();
return Variable(result, false);
}

#define INSTANTIATE_OPERATOR(OP) \
Variable operator OP(const double &lhs_val, const Variable &rhs) \
Expand All @@ -78,10 +90,54 @@ namespace af {
INSTANTIATE_OPERATOR(-)
INSTANTIATE_OPERATOR(*)
INSTANTIATE_OPERATOR(/)
INSTANTIATE_OPERATOR(>)
INSTANTIATE_OPERATOR(<=)

#undef INSTANTIATE_OPERATOR

Variable negate(const Variable &input)
Variable operator !(const Variable &input)
{
auto result = !input.array();
return Variable(result, false);
}

Variable max(const Variable &lhs, const Variable &rhs)
{
auto mask = lhs > rhs;
auto result = max(lhs.array(), rhs.array());

auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad( inputs[2] * grad_output);
inputs[1].addGrad(!inputs[2] * grad_output);
};
return Variable(result, {lhs, rhs, mask}, grad_func);
}

#define INSTANTIATE_FUNCTION(FN) \
Variable FN(const double &lhs_val, const Variable &rhs) \
{ \
auto lhs = Variable( \
af::constant(lhs_val, \
rhs.array().dims(), \
rhs.array().type()), \
false); \
return FN(lhs,rhs); \
} \
Variable FN(const Variable &lhs, const double &rhs_val) \
{ \
auto rhs = Variable( \
af::constant(rhs_val, \
lhs.array().dims(), lhs.array().type()), \
false); \
return FN(lhs, rhs); \
}


INSTANTIATE_FUNCTION(max);

#undef INSTANTIATE_FUNCTION

Variable negate(const Variable &input)
{
auto result = 0.0 - input.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
Expand Down
17 changes: 17 additions & 0 deletions src/nn/Modules/Activations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,22 @@ namespace af
{
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);
}
}
}