# [GitHub] [incubator-singa] ShichengChen commented on a change in pull request #524: SINGA-474 prelu, add, equal, selu, elu operator

```ShichengChen commented on a change in pull request #524: SINGA-474
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317383901

```
```
##########
##########
@@ -608,21 +608,177 @@ def backward(self, dy):
def reshape(a,shape):
return Reshape(shape)(a)[0]

+class PRelu(Operation):
+
+    def __init__(self):
+        super(PRelu, self).__init__()
+
+    def forward(self, x, slope):
+        mask0 = singa.LTFloat(x, 0.0)
+        if training:
+            self.input = x
+            self.slope = slope
+        x1 = singa.__mul__(x, mask0)
+        x1 *= slope
+        x2 = singa.ReLU(x)
+        x1 += x2
+        return x1
+
+    def backward(self, dy):
+        dx1mask = singa.GEFloat(self.input, 0.0)
+        dx2 = singa.__mul__(self.mask0, self.slope)
+        return singa.__mul__(dy, dx), singa.__mul__(dy,
+                                                    singa.__mul__(
self.input))
+
+
+def prelu(x, slope):
+    return PRelu()(x, slope)[0]

def __init__(self):

def forward(self, a, b):
+        #up till now, the dimensions of tensor a and b should less than 3
+        self.shape0=list(a.shape())
+        self.shape1=list(b.shape())
+        assert(len(self.shape0) <= 2 and len(self.shape1) <= 2),"up till now,
the dimensions of tensor a and b should less than 3"

def backward(self, dy):
-        return dy, dy
+        if(type(dy)==float):return dy,dy
+        db=CTensor(list(dy.shape()), dy.device())
+        db.CopyData(dy)
+        for i in range(len(self.shape0)-len(self.shape1)):
+            db=singa.Sum(db, 0)
+        return dy, db

+class Elu(Operation):
+    def __init__(self,alpha=1):
+        super(Elu, self).__init__()
+        self.alpha=alpha
+
+    def forward(self, x):
+        """Do forward propgation.
+        Store the x if requires gradient.
+        Args:
+            x (CTensor): matrix
+        Returns:
+            a CTensor for the result
+        """
+        #f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0
+        if training:
+            self.input = x
+        x1 = singa.LTFloat(x, 0.0)
+        x1 = singa.__mul__(x, x1)
+        x1 = singa.MultFloat(singa.SubFloat(singa.Exp(x1),1.0),self.alpha)
+        x2 = singa.ReLU(x)
+        x1 = singa.__add__(x1, x2)

Review comment:
yes

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.