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

First attempt at ReLU #31

merged 1 commit into from
Jul 7, 2017

Conversation

plavin
Copy link
Contributor

@plavin plavin commented Jul 6, 2017

I'm getting a segfault when I run this, haven't figured out why yet. I'll look more later.

Error:

libc++abi.dylib: terminating with uncaught exception of type af::exception: ArrayFire Exception (Input types are not the same:205):
In function af_err af_matmul(af_array *, const af_array, const af_array, const af_mat_prop, const af_mat_prop)
In file src/api/c/blas.cpp:133
Type mismatch inputs

In function af::array af::matmulNT(const af::array &, const af::array &)
In file src/api/cpp/blas.cpp:28
Abort trap: 6

}
}
return 0;
}
Copy link
Member

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.

Copy link
Member

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>
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation

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);
Copy link
Member

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?

inputs[0].addGrad(grad_output);
inputs[1].addGrad(grad_output);
};
return Variable(result, {lhs, rhs}, grad_func);
Copy link
Member

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.

inputs[0].addGrad(relu(grad_output) > 0);
};
return Variable(result, {input}, grad_func);
}
Copy link
Member

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);
          }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is inputs[1]?

Copy link
Member

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

Copy link
Member

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

Variable ReLU::forward(const Variable &input)
{
return relu(input);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@pavanky
Copy link
Member

pavanky commented Jul 6, 2017

@plavin Instead of having a autograd::relu function, it is better to implement an autograd::max function.

You can then call autograd::max(var, 0) from nn::Relu::forward

This commit adds ReLU and LeakyRelu modules, as well as a few
helper functions, max and !.
@pavanky pavanky merged commit 0f170a9 into arrayfire:master Jul 7, 2017
@pavanky pavanky mentioned this pull request Jul 10, 2017
20 tasks
@pavanky pavanky modified the milestone: 0.1 Jul 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants