singa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-singa] nudles commented on a change in pull request #524: SINGA-474 prelu, add, equal, selu, elu operator
Date Sat, 24 Aug 2019 23:12:55 GMT
nudles 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_r317377077
 
 

 ##########
 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)
+        return x1
+
+    def backward(self, dy):
+        """
+        Args:
+            dy (CTensor): data for the dL / dy, L is the loss
+        Returns:
+            a tuple for dx
+        """
+        dx1mask = singa.LTFloat(self.input, 0.0)
+        dx1 = singa.MultFloat(singa.Exp(self.input), self.alpha)
+        dx1 = singa.__mul__(dx1mask, dx1)
+
+        dx2mask = singa.GEFloat(self.input, 0.0)
+
+        dx = singa.__add__(dx1, dx2mask)
+        return singa.__mul__(dy, dx)
 
 Review comment:
   can use dx *= dy?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message