From 1613c4a241639792b711b3c0c8dd3ae9d8f03a19 Mon Sep 17 00:00:00 2001 From: Pavan Yalamanchili Date: Sun, 23 Jul 2017 23:01:26 -0700 Subject: [PATCH] Adding abs function to autograd --- include/af/autograd/Functions.hpp | 2 +- src/autograd/Functions.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/af/autograd/Functions.hpp b/include/af/autograd/Functions.hpp index 182aba0..119d9c0 100644 --- a/include/af/autograd/Functions.hpp +++ b/include/af/autograd/Functions.hpp @@ -73,6 +73,6 @@ namespace af { Variable matmulTN(const Variable &lhs, const Variable &rhs); Variable matmulNT(const Variable &lhs, const Variable &rhs); - + Variable abs(const Variable &val); } } diff --git a/src/autograd/Functions.cpp b/src/autograd/Functions.cpp index d754689..09c14ef 100644 --- a/src/autograd/Functions.cpp +++ b/src/autograd/Functions.cpp @@ -375,5 +375,17 @@ namespace af { }; return Variable(result, {lhs, rhs}, grad_func); } + + Variable abs(const Variable &val) + { + auto result = af::abs(val.array()); + auto grad_func = [](std::vector &inputs, const Variable &grad_output) { + // af::sign returns signbit + // Convert it into -1, 1 + auto sign = Variable(1 - 2 * af::sign(inputs[0].array()), false); + inputs[0].addGrad(sign * grad_output); + }; + return Variable(result, {val}, grad_func); + } } }