ShichengChen commented on a change in pull request #524: SINGA-474 
prelu,add,equal,selu,elu operator
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317383901
 
 

 ##########
 File path: python/singa/autograd.py
 ##########
 @@ -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
+            self.mask0 = mask0
+        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)
+        dx = singa.__add__(dx1mask, dx2)
+        return singa.__mul__(dy, dx), singa.__mul__(dy,
+                                                    singa.__mul__(
+                                                        self.mask0, 
self.input))
+
+
+def prelu(x, slope):
+    return PRelu()(x, slope)[0]
 
 class Add(Operation):
     def __init__(self):
         super(Add, self).__init__()
 
     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"
         return singa.__add__(a, b)
 
     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
 
 
 def add(a, b):
     return Add()(a, b)[0]
 
+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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to