[arrayfire] 132/408: FEAT, TEST: Adding sigmoid function for all backends

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:11:37 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit d22c0bb0701a926c8099e21318ec96ef762392bd
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Fri Jul 10 10:18:22 2015 -0400

    FEAT,TEST: Adding sigmoid function for all backends
---
 examples/machine_learning/deep_belief_net.cpp     |  6 ------
 examples/machine_learning/logistic_regression.cpp |  6 ------
 examples/machine_learning/neural_network.cpp      |  6 ------
 examples/machine_learning/perceptron.cpp          |  6 ------
 examples/machine_learning/rbm.cpp                 |  6 ------
 include/af/arith.h                                | 19 +++++++++++++++++++
 src/api/c/optypes.hpp                             |  2 ++
 src/api/c/unary.cpp                               |  1 +
 src/api/cpp/unary.cpp                             |  1 +
 src/backend/cpu/unary.hpp                         |  7 +++++++
 src/backend/cuda/JIT/exp.cu                       | 11 +++++++++++
 src/backend/cuda/unary.hpp                        |  1 +
 src/backend/opencl/kernel/jit.cl                  |  1 +
 src/backend/opencl/unary.hpp                      |  1 +
 test/math.cpp                                     |  7 +++++++
 15 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/examples/machine_learning/deep_belief_net.cpp b/examples/machine_learning/deep_belief_net.cpp
index 89fb06b..4dfac9e 100644
--- a/examples/machine_learning/deep_belief_net.cpp
+++ b/examples/machine_learning/deep_belief_net.cpp
@@ -26,12 +26,6 @@ float accuracy(const array& predicted, const array& target)
     return 100 * count<float>(plabels == tlabels) / tlabels.elements();
 }
 
-// Activation function
-array sigmoid(const array &val)
-{
-    return 1 / (1 + exp(-val));
-}
-
 // Derivative of the activation function
 array deriv(const array &out)
 {
diff --git a/examples/machine_learning/logistic_regression.cpp b/examples/machine_learning/logistic_regression.cpp
index e7b4b2d..58281f0 100644
--- a/examples/machine_learning/logistic_regression.cpp
+++ b/examples/machine_learning/logistic_regression.cpp
@@ -31,12 +31,6 @@ float abserr(const array& predicted, const array& target)
     return 100 * sum<float>(abs(predicted - target)) / predicted.elements();
 }
 
-// Activation function
-array sigmoid(const array &val)
-{
-    return 1 / (1 + exp(-val));
-}
-
 // Predict based on given parameters
 array predict(const array &X, const array &Weights)
 {
diff --git a/examples/machine_learning/neural_network.cpp b/examples/machine_learning/neural_network.cpp
index ef27d30..b249fd0 100644
--- a/examples/machine_learning/neural_network.cpp
+++ b/examples/machine_learning/neural_network.cpp
@@ -26,12 +26,6 @@ float accuracy(const array& predicted, const array& target)
     return 100 * count<float>(plabels == tlabels) / tlabels.elements();
 }
 
-// Activation function
-array sigmoid(const array &val)
-{
-    return 1 / (1 + exp(-val));
-}
-
 // Derivative of the activation function
 array deriv(const array &out)
 {
diff --git a/examples/machine_learning/perceptron.cpp b/examples/machine_learning/perceptron.cpp
index 374edd0..7b5a579 100644
--- a/examples/machine_learning/perceptron.cpp
+++ b/examples/machine_learning/perceptron.cpp
@@ -26,12 +26,6 @@ float accuracy(const array& predicted, const array& target)
     return 100 * count<float>(plabels == tlabels) / tlabels.elements();
 }
 
-// Activation function
-array sigmoid(const array &val)
-{
-    return 1 / (1 + exp(-val));
-}
-
 // Predict based on given parameters
 array predict(const array &X, const array &Weights)
 {
diff --git a/examples/machine_learning/rbm.cpp b/examples/machine_learning/rbm.cpp
index 77ad611..d6b68f3 100644
--- a/examples/machine_learning/rbm.cpp
+++ b/examples/machine_learning/rbm.cpp
@@ -26,12 +26,6 @@ float accuracy(const array& predicted, const array& target)
     return 100 * count<float>(plabels == tlabels) / tlabels.elements();
 }
 
-// Activation function
-array sigmoid(const array &val)
-{
-    return 1 / (1 + exp(-val));
-}
-
 // Derivative of the activation function
 array deriv(const array &out)
 {
diff --git a/include/af/arith.h b/include/af/arith.h
index 2a3d9f4..d8e6bb2 100644
--- a/include/af/arith.h
+++ b/include/af/arith.h
@@ -387,6 +387,14 @@ namespace af
     /// @}
 
 
+    /// C++ Interface for calculating sigmoid function of an array
+    ///
+    /// \param[in] in is input
+    /// \return the sigmoid of \p in
+    ///
+    /// \ingroup arith_func_sigmoid
+    AFAPI array sigmoid (const array &in);
+
     /// C++ Interface for exponential of an array
     ///
     /// \param[in] in is exponent
@@ -1166,6 +1174,17 @@ extern "C" {
     AFAPI af_err af_exp     (af_array *out, const af_array in);
 
     /**
+       C Interface for calculating sigmoid function of an array
+
+       \param[out] out will contain the sigmoid of \p in
+       \param[in] in is input
+       \return \ref AF_SUCCESS if the execution completes properly
+
+       \ingroup arith_func_sigmoid
+    */
+    AFAPI af_err af_sigmoid (af_array *out, const af_array in);
+
+    /**
        C Interface for exponential of an array minus 1
 
        \param[out] out will contain the exponential of \p in - 1
diff --git a/src/api/c/optypes.hpp b/src/api/c/optypes.hpp
index b9514da..c468ac5 100644
--- a/src/api/c/optypes.hpp
+++ b/src/api/c/optypes.hpp
@@ -89,5 +89,7 @@ typedef enum {
     af_isinf_t,
     af_isnan_t,
 
+    af_sigmoid_t,
+
     af_noop_t
 } af_op_t;
diff --git a/src/api/c/unary.cpp b/src/api/c/unary.cpp
index f1e86ff..a3833c77 100644
--- a/src/api/c/unary.cpp
+++ b/src/api/c/unary.cpp
@@ -86,6 +86,7 @@ UNARY(floor)
 UNARY(ceil)
 
 UNARY(exp)
+UNARY(sigmoid)
 UNARY(expm1)
 UNARY(erf)
 UNARY(erfc)
diff --git a/src/api/cpp/unary.cpp b/src/api/cpp/unary.cpp
index 5b3a236..4b4ab66 100644
--- a/src/api/cpp/unary.cpp
+++ b/src/api/cpp/unary.cpp
@@ -59,6 +59,7 @@ namespace af
     INSTANTIATE(expm1 )
     INSTANTIATE(erf   )
     INSTANTIATE(erfc  )
+    INSTANTIATE(sigmoid)
 
     INSTANTIATE(log   )
     INSTANTIATE(log1p )
diff --git a/src/backend/cpu/unary.hpp b/src/backend/cpu/unary.hpp
index 970b151..3cf45ac 100644
--- a/src/backend/cpu/unary.hpp
+++ b/src/backend/cpu/unary.hpp
@@ -17,6 +17,12 @@ namespace cpu
 {
 #define sign(in) std::signbit(in)
 
+template<typename T>
+T sigmoid(T in)
+{
+    return (1.0) / (1 + std::exp(-in));
+}
+
 #define UNARY_FN(op)                            \
     template<typename T>                        \
     struct UnOp<T, T, af_##op##_t>              \
@@ -50,6 +56,7 @@ UNARY_FN(floor)
 UNARY_FN(ceil)
 
 UNARY_FN(exp)
+UNARY_FN(sigmoid)
 UNARY_FN(expm1)
 UNARY_FN(erf)
 UNARY_FN(erfc)
diff --git a/src/backend/cuda/JIT/exp.cu b/src/backend/cuda/JIT/exp.cu
index 9568342..202c03c 100644
--- a/src/backend/cuda/JIT/exp.cu
+++ b/src/backend/cuda/JIT/exp.cu
@@ -9,6 +9,16 @@
 
 #include "types.h"
 
+__device__ double sigmoid(double in)
+{
+    return (1.0) / (1 + exp(-in));
+}
+
+__device__ float sigmoidf(float in)
+{
+    return (1.0) / (1 + expf(-in));
+}
+
 #define MATH_BASIC(fn, T)                       \
     __device__ T ___##fn(T a)                   \
     {                                           \
@@ -32,6 +42,7 @@ MATH(exp)
 MATH(expm1)
 MATH(erf)
 MATH(erfc)
+MATH(sigmoid)
 
 MATH(log)
 MATH(log10)
diff --git a/src/backend/cuda/unary.hpp b/src/backend/cuda/unary.hpp
index 290e7c0..85597cc 100644
--- a/src/backend/cuda/unary.hpp
+++ b/src/backend/cuda/unary.hpp
@@ -57,6 +57,7 @@ UNARY_FN(acosh)
 UNARY_FN(atanh)
 
 UNARY_FN(exp)
+UNARY_FN(sigmoid)
 UNARY_FN(expm1)
 UNARY_FN(erf)
 UNARY_FN(erfc)
diff --git a/src/backend/opencl/kernel/jit.cl b/src/backend/opencl/kernel/jit.cl
index 74b776e..b34bbcd 100644
--- a/src/backend/opencl/kernel/jit.cl
+++ b/src/backend/opencl/kernel/jit.cl
@@ -37,6 +37,7 @@
 #define __cimag(in) ((in).y)
 #define __cabs2(in) ((in).x * (in).x + (in).y * (in).y)
 #define __cabs(in) sqrt(__cabs2(in))
+#define __sigmoid(in) (1.0/(1 + exp(-(in))))
 
 float2 __cconjf(float2 in)
 {
diff --git a/src/backend/opencl/unary.hpp b/src/backend/opencl/unary.hpp
index 4d6796e..5a2cc9e 100644
--- a/src/backend/opencl/unary.hpp
+++ b/src/backend/opencl/unary.hpp
@@ -44,6 +44,7 @@ UNARY_FN(acosh)
 UNARY_FN(atanh)
 
 UNARY_FN(exp)
+UNARY_DECL(sigmoid, "__sigmoid")
 UNARY_FN(expm1)
 UNARY_FN(erf)
 UNARY_FN(erfc)
diff --git a/test/math.cpp b/test/math.cpp
index e5dbe76..1147c3e 100644
--- a/test/math.cpp
+++ b/test/math.cpp
@@ -19,6 +19,12 @@ const int num = 10000;
 const float flt_err = 1e-3;
 const double dbl_err = 1e-10;
 
+template<typename T>
+T sigmoid(T in)
+{
+    return 1.0 / (1.0 + std::exp(-in));
+}
+
 #define MATH_TESTS_LIMITS(Ti, To, func, err, lo, hi)            \
     TEST(MathTests, Test_##func##_##Ti)                         \
     {                                                           \
@@ -55,6 +61,7 @@ MATH_TESTS_FLOAT(tanh)
 MATH_TESTS_FLOAT(sqrt)
 
 MATH_TESTS_FLOAT(exp)
+MATH_TESTS_FLOAT(sigmoid)
 MATH_TESTS_FLOAT(log)
 MATH_TESTS_FLOAT(log10)
 MATH_TESTS_FLOAT(log2)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list