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

WIP Indexing autograd and tests, new loss functions #47

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

syurkevi
Copy link

@syurkevi syurkevi commented Oct 1, 2018

Adds new indexing functions to address #43. Theses are needed to implement Losses ans SVM from #34 and #6 those changes are next in line.
Operator overloading currently avoided in favor of new select_index () and set_index functions(). Will change to preferred solution as part of review.
Also brings in google test framework for testing. examples/autograd.cpp has been converted to (non-comprehensive) tests.

@@ -44,6 +44,8 @@ namespace af {
Variable operator <=(const Variable &lhs, const double &rhs);

Variable operator !(const Variable &input);
Variable select_index(const Variable &input, const Variable &idx);
Variable set_index(const Variable &input, const Variable &idx, const Variable &vals);
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 lookup and assign are better. Or go withgather vs scatter like tensorflow.


auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
auto grad = inputs[2].array();
auto grad_mask = af::where(grad);
Copy link
Member

Choose a reason for hiding this comment

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

Why are you converting idx into a mask and converting it back into an index again? This is making unnecessary copies.


inputs[0].addGrad(Variable(grad, false));
};
return Variable(result, {input, idx, Variable(mask, false)}, 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.

You only need idx or mask. Not both.

auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(inputs[3] * grad_output);
};
return Variable(result, {input, idx, vals, Variable(mask, false)}, 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.

It is better to use just index and do the masking inside the grad_func. No reason to do the masking op until it is necessary.

if(PACKAGE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
Copy link
Member

Choose a reason for hiding this comment

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

You can avoid creating a separate option PACKAGE_TESTS. gtest automatically addes BUILD_TESTING which you can use.

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