-
Notifications
You must be signed in to change notification settings - Fork 23
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
Conversation
examples/alexnet.cpp
Outdated
} | ||
} | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you send this in a different PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or edit perceptron.cpp to use ReLU instead of sigmoid.
@@ -10,6 +10,7 @@ | |||
|
|||
#include <af/autograd/Variable.hpp> | |||
#include <af/nn/Modules/Module.hpp> | |||
#include <float.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary ? If it is use #include <cfloat>
@@ -30,5 +31,12 @@ namespace af | |||
|
|||
autograd::Variable forward(const autograd::Variable &input); | |||
}; | |||
class ReLU : public Module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
src/autograd/Functions.cpp
Outdated
auto result = lhs.array() > rhs.array(); | ||
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) { | ||
inputs[0].addGrad(grad_output); | ||
inputs[1].addGrad(grad_output); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is right. May be this function doesnt need to have a gradient?
src/autograd/Functions.cpp
Outdated
inputs[0].addGrad(grad_output); | ||
inputs[1].addGrad(grad_output); | ||
}; | ||
return Variable(result, {lhs, rhs}, grad_func); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are removing grad_func, then return Variable(result, false);
This tells the backward pass that this variable is not differentiable.
src/autograd/Functions.cpp
Outdated
inputs[0].addGrad(relu(grad_output) > 0); | ||
}; | ||
return Variable(result, {input}, grad_func); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be simplified like this:
Variable relu(const Variable &input)
{
auto mask = input > 0;
auto result = mask.array() * input.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output * inputs[1]);
};
return Variable(result, {input, mask}, grad_func);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is inputs[1]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are sending mask as second input for backward pass. You can use that instead of comparing inputs[0] with 0 again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are sending mask as second input for backward pass. You can use that instead of comparing inputs[0] with 0 again
src/nn/Modules/Activations.cpp
Outdated
Variable ReLU::forward(const Variable &input) | ||
{ | ||
return relu(input); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@plavin Instead of having a You can then call |
This commit adds ReLU and LeakyRelu modules, as well as a few helper functions, max and !.
I'm getting a segfault when I run this, haven't figured out why yet. I'll look more later.
Error: