singa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-singa] joddiy commented on a change in pull request #528: SINGA 475 - add logical operator: and, or, xor, not & negative, reciprocal
Date Tue, 17 Sep 2019 11:52:09 GMT
joddiy commented on a change in pull request #528: SINGA 475 - add logical operator: and, or,
xor, not & negative, reciprocal
URL: https://github.com/apache/incubator-singa/pull/528#discussion_r325123236
 
 

 ##########
 File path: python/singa/autograd.py
 ##########
 @@ -2522,3 +2523,144 @@ def backward(self, dy):
 def max(a,b):
     return Max()(a,b)[0]
 
+
+class And(Operation):
+    def __init__(self):
+        super(And, self).__init__()
+
+    def forward(self, a, b):
+        m = singa.__div__(a,b)
+
+        mask0 = singa.GEFloat(m,1)
+        mask1 = singa.LEFloat(m,1)
+        cur = singa.__mul__(mask0,mask1)
+
+        return cur
+
+    def backward(self, dy):
+        assert 0,('no gradient')
+        return None
+
+def _and(a,b):
+    return And()(a,b)[0]
+
+
+class Or(Operation):
+    def __init__(self):
+        super(Or, self).__init__()
+
+    def forward(self, a, b):
+        #find equal element-wise
+        m = singa.__sub__(a,b)
+        m0 = singa.GEFloat(m,0)
+        m1 = singa.LEFloat(m,0)
+        mask0 = singa.__mul__(m0, m1)
+
+        #find 0 element-wise
+        n = singa.__add__(a,b)
+        n0 = singa.GEFloat(n,0)
+        n1 = singa.LEFloat(n,0)
+        mask1 = singa.__mul__(n0, n1)
+
+        #find equal 0 element-wise
+        n = singa.__mul__(mask0, mask1)
+        cur = singa.LEFloat(n, 0)
+
+        return cur
+
+    def backward(self, dy):
+        assert 0,('no gradient for backward function')
+        return None
+
+
+def _or(a,b):
+    return Or()(a,b)[0]
+
+
+class Not(Operation):
+    def __init__(self):
+        super(Not, self).__init__()
+
+    def forward(self, x):
+        mask0 = singa.GEFloat(x,0)
+        mask1 = singa.LEFloat(x,0)
+        cur = singa.__mul__(mask0,mask1)
+
+        return cur
+
+    def backward(self, dy):
+        assert 0,('no gradient for backward function')
+        return None
+
+def _not(x):
+    return Not()(x)[0]
+
+
+class Xor(Operation):
+    def __init__(self):
+        super(Xor, self).__init__()
+
+    def forward(self, a, b):
+        #find element with value =0
+        m0 = singa.__mul__(a,b)
 
 Review comment:
   not clear, plz use this:
   ```
           m = singa.__sub__(singa.PowFloat(singa.Sign(a), 2.0), singa.PowFloat(singa.Sign(b),
2.0))
           cur = singa.PowFloat(singa.Sign(m), 2.0)
   ```

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