-
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
WIP Indexing autograd and tests, new loss functions #47
base: master
Are you sure you want to change the base?
Changes from 4 commits
0410583
54fd74f
88bee36
eaa986e
eea9572
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "extern/googletest"] | ||
path = extern/googletest | ||
url = https://github.com/google/googletest.git |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
Variable negate(const Variable &input); | ||
Variable reciprocal(const Variable &input); | ||
|
@@ -54,6 +56,7 @@ namespace af { | |
Variable cos(const Variable &input); | ||
Variable tanh(const Variable &input); | ||
Variable sigmoid(const Variable &input); | ||
Variable softmax(const Variable &input); | ||
|
||
Variable max(const Variable &lhs, const Variable &rhs); | ||
Variable max(const Variable &lhs, const double &rhs); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,35 @@ namespace af { | |
return Variable(result, false); | ||
} | ||
|
||
Variable select_index(const Variable &input, const Variable &idx) | ||
{ | ||
af::array result = input.array()(idx.array()); | ||
af::array mask = af::constant(0, input.dims()); | ||
mask(idx.array()) = 1; | ||
|
||
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) { | ||
auto grad = inputs[2].array(); | ||
auto grad_mask = af::where(grad); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you converting |
||
grad(grad_mask) *= grad_output.array(); | ||
|
||
inputs[0].addGrad(Variable(grad, false)); | ||
}; | ||
return Variable(result, {input, idx, Variable(mask, false)}, grad_func); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only need |
||
} | ||
|
||
Variable set_index(const Variable &input, const Variable &idx, const Variable &vals) | ||
{ | ||
af::array result = input.array(); | ||
result(idx.array()) = vals.array(); | ||
af::array mask = af::constant(1, input.dims(), s32); | ||
mask(idx.array()) = 0; | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
Variable max(const Variable &lhs, const Variable &rhs) | ||
{ | ||
auto mask = lhs > rhs; | ||
|
@@ -241,6 +270,24 @@ namespace af { | |
return Variable(result, {input}, grad_func); | ||
} | ||
|
||
Variable softmax(const Variable &input) | ||
{ | ||
//todo: add axis to apply? | ||
auto exps = exp(input.array()); | ||
auto result = exps / tile(sum(exps, 0), exps.dims(0)); | ||
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) { | ||
auto exps = exp(inputs[0]); | ||
auto tmp = exps / tileAs(sum(exps, {0}), exps); | ||
|
||
auto ps_j = tile(tmp, { 1, (int)tmp.dims()[0] } ); | ||
auto ps_i = transpose(tile(tmp, {1,(int)tmp.dims()[0] } )); | ||
Variable I(identity((int)tmp.dims()[0], (int)tmp.dims()[0]), false); | ||
auto jac = (sum(ps_i * (I - ps_j), { 1 })); | ||
inputs[0].addGrad(grad_output * jac); | ||
}; | ||
return Variable(result, {input}, grad_func); | ||
} | ||
|
||
Variable transpose(const Variable &input) | ||
{ | ||
auto result = transpose(input.array()); | ||
|
@@ -281,7 +328,7 @@ namespace af { | |
|
||
Variable tile(const Variable &input, const std::vector<int> &repeats) | ||
{ | ||
dim4 dims; | ||
dim4 dims(0); | ||
for (size_t i = 0; i < repeats.size(); i++) { | ||
dims[i] = repeats[i]; | ||
} | ||
|
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 can avoid creating a separate option
PACKAGE_TESTS
. gtest automatically addesBUILD_TESTING
which you can use.