nswamy closed pull request #9858: softsign activation function
URL: https://github.com/apache/incubator-mxnet/pull/9858
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/operator/contrib/softsign.cc b/src/operator/contrib/softsign.cc
new file mode 100644
index 0000000000..64bdf94d51
--- /dev/null
+++ b/src/operator/contrib/softsign.cc
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file softsign.cc
+ * \brief CPU Implementation of softsign function.
+ */
+#include "../tensor/elemwise_unary_op.h"
+#include "../tensor/elemwise_binary_op.h"
+
+namespace mxnet {
+namespace op {
+// softsign
+MXNET_OPERATOR_REGISTER_UNARY(_contrib_softsign)
+  .describe(R"code(Computes softsign of x element-wise.
+
+.. math::
+   y = x / (1 + abs(x))
+
+)code" ADD_FILELINE)
+  .set_attr<nnvm::FGradient>("FGradient", 
ElemwiseGradUseIn{"_backward_contrib_softsign"})
+  .set_attr<FCompute>("FCompute<cpu>",
+                      UnaryLaunch<cpu, kernel_launch_op::softsign>);
+
+
+MXNET_OPERATOR_REGISTER_BINARY(_backward_contrib_softsign)
+.set_attr<FCompute>("FCompute<cpu>",
+                    BinaryLaunch<cpu, kernel_launch_op::softsign_grad>);
+}  // namespace op
+}  // namespace mxnet
diff --git a/src/operator/contrib/softsign.cu b/src/operator/contrib/softsign.cu
new file mode 100644
index 0000000000..7623688475
--- /dev/null
+++ b/src/operator/contrib/softsign.cu
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file softsign.cu
+ * \brief CPU Implementation of softsign function.
+ */
+#include "../tensor/elemwise_unary_op.h"
+#include "../tensor/elemwise_binary_op.h"
+
+namespace mxnet {
+namespace op {
+// softsign
+NNVM_REGISTER_OP(_contrib_softsign)
+.set_attr<FCompute>("FCompute<gpu>", UnaryLaunch<gpu, 
kernel_launch_op::softsign>);
+
+NNVM_REGISTER_OP(_backward_contrib_softsign)
+.set_attr<FCompute>("FCompute<gpu>", BinaryLaunch<gpu, 
kernel_launch_op::softsign_grad>);
+}  // namespace op
+}  // namespace mxnet
+
+
diff --git a/src/operator/mshadow_op.h b/src/operator/mshadow_op.h
index f7815d2f8d..0c4f5bc19b 100644
--- a/src/operator/mshadow_op.h
+++ b/src/operator/mshadow_op.h
@@ -186,6 +186,19 @@ struct softrelu_grad {
     return -DType(expm1f(-a));
   }
 };
+/*! \brief softsign unit */
+struct softsign {
+  template<typename DType>
+  MSHADOW_XINLINE static DType Map(DType a) {
+    return DType(a / (DType(1.0f) + fabsf(a)));
+  }
+};
+struct softsign_grad {
+  template<typename DType>
+  MSHADOW_XINLINE static DType Map(DType a) {
+    return DType(1.0f / powf((DType(1.0f) + fabsf(a)), 2.0f) );
+  }
+};
 
 struct exp {
   template<typename DType>
diff --git a/src/operator/tensor/elemwise_unary_op.h 
b/src/operator/tensor/elemwise_unary_op.h
index b6994844e0..fc03f08361 100644
--- a/src/operator/tensor/elemwise_unary_op.h
+++ b/src/operator/tensor/elemwise_unary_op.h
@@ -170,6 +170,21 @@ struct relu_grad {
     out[i] = out_grad[i] * DType(in[i] > DType(0.0f) ? DType(1.0f) : 
DType(0.0f));
   }
 };
+/*! \brief softsign unit */
+struct softsign {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, DType *out,
+                                  const DType *in) {
+    out[i] = DType(DType(in[i]) / (DType(1.0f) + fabsf(in[i])));
+  }
+};
+struct softsign_grad {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, DType *out,
+                                  const DType *out_grad, const DType *in) {
+    out[i] = DType(DType(1.0f) / powf(DType(1.0f) + fabsf(in[i]), 2.0f));
+  }
+};
 }  // namespace kernel_launch_op
 
 #define MXNET_OPERATOR_REGISTER_UNARY(name)                         \
diff --git a/tests/python/unittest/test_operator.py 
b/tests/python/unittest/test_operator.py
index a33cb039c8..06e0c3501e 100644
--- a/tests/python/unittest/test_operator.py
+++ b/tests/python/unittest/test_operator.py
@@ -398,6 +398,20 @@ def fsigmoid(a):
     check_symbolic_forward(y, [xa], [ya])
     check_symbolic_backward(y, [xa], [np.ones(shape)], [ya * (1 - ya)])
 
+def test_softsign():
+    def fsoftsign(a):
+        return np.divide(a, (1.0 + np.abs(a)))
+    def fsoftsign_grad(a):
+        return np.divide(1.0, np.square((1.0 + np.abs(a))))
+    shape = (3, 4)
+    x = mx.symbol.Variable("x")
+    y = mx.contrib.symbol.softsign(x)
+    xa = np.random.uniform(low=-1.0,high=1.0,size=shape)
+    ya = fsoftsign(xa)
+    ya_grad = fsoftsign_grad(xa)
+    check_symbolic_forward(y, [xa], [ya])
+    check_symbolic_backward(y, [xa], [np.zeros(shape)], [ya_grad])
+
 def test_binary_logic():
     def _inner_test(forward_gt, logic_sym, x_shape, y_shape, test_scalar=True):
         x = mx.symbol.Variable("x")


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to