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 #502: SINGA-474 Clip operator
Date Wed, 14 Aug 2019 08:59:56 GMT
nudles commented on a change in pull request #502: SINGA-474 Clip operator
URL: https://github.com/apache/incubator-singa/pull/502#discussion_r313772729
 
 

 ##########
 File path: python/singa/autograd.py
 ##########
 @@ -357,6 +359,46 @@ def backward(self, dy):
 def relu(x):
     return ReLU()(x)[0]
 
+class Clip(Operation):
+    def __init__(self,min,max):
+        super(Clip, self).__init__()
+        self.max=max
+        self.min=min
+
+    def forward(self, x):
+        """
+        Args:
+            x(CTensor): input tensor, min: float, max:float
+        Returns:
+            np.clip(x,min,max)
+        """
+
+        mask0 = singa.LTFloat(x, self.min)
+        mask1 = singa.GTFloat(x, self.max)
+        mask00 = singa.MultFloat(mask0,self.min)
+        mask11 = singa.MultFloat(mask1,self.max)
+
+        mask2 = singa.LEFloat(x, self.max)
+        mask3 = singa.GEFloat(x, self.min)
+        maskm = singa.__mul__(mask2,mask3)
+
+        if training:
+            self.mask = maskm
+
+        return singa.__add__(singa.__add__(singa.__mul__(maskm,x),mask00),mask11)
+
+    def backward(self, dy):
+        """
 
 Review comment:
   the comments are misleading..

----------------------------------------------------------------
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