Repository: OpenMined/PySyft
Pull request #221 (2017-09-07 15:30:05)

feat(ne implementation for TensorBase)
- ne function implementation
- ne_ inplace function implementation
- tests for ne and ne_

aradhyamathur: @iamtrask with reference to https://github.com/OpenMined/PySyft/issues/72

Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index de6eaff6dd..6ffbdbb107 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -927,20 +927,6 @@ class TensorBase(object):
         out = np.broadcast_to(self.data, shape)
         return TensorBase(out)
 
-    def neg(self):
-        """Returns negative of the elements of tensor"""
-        if self.encrypted:
-            return NotImplemented
-        out = -1 * np.array(self.data)
-        return TensorBase(out)
-
-    def neg_(self):
-        """Returns negative of the elements of tensor inplace"""
-        if self.encrypted:
-            return NotImplemented
-        self.data = -1 * np.array(self.data)
-        return self
-
     def normal(self, mu, sigma):
         """Returns a Tensor of random numbers drawn from separate
         normal distributions who’s mean and standard deviation are given."""
@@ -957,6 +943,30 @@ class TensorBase(object):
         self.data = np.random.normal(mu, sigma, self.data.shape)
         return self
 
+    def ne(self, tensor):
+        """Checks element-wise equality with the given tensor and returns
+        a boolean result with same dimension as the input matrix"""
+        if self.encrypted:
+            return NotImplemented
+        else:
+            if tensor.shape() == self.shape():
+
+                tensor2 = np.array([1 if x else 0 for x in np.equal(tensor.data.flatten(), self.data.flatten()).tolist()])
+                result = tensor2.reshape(self.data.shape)
+                return TensorBase(result)
+            else:
+                raise ValueError('inconsistent dimensions {} and {}'.format(self.shape(), tensor.shape()))
+
+    def ne_(self, tensor):
+        """
+         Checks in place element wise equality and updates the data matrix to the equality matrix
+        """
+        if self.encrypted:
+            return NotImplemented
+        else:
+            value = self.ne(tensor)
+            self.data = value.data
+
     def median(self, axis=1, keepdims=False):
         """Returns median of tensor as per specified axis. By default median is calculated along rows.
         axis=None can be used get median of whole tensor."""


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 63529701c3..1a93cc28c7 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -706,5 +706,18 @@ class expandAsTests(unittest.TestCase):
         self.assertTrue(np.array_equal(t2.data.shape, t3.data.shape))
 
 
+class notEqualTests(unittest.TestCase):
+    def testNe(self):
+        t1 = TensorBase([1, 2, 3, 4])
+        t2 = TensorBase([1., 2., 3., 5.])
+        self.assertEqual(t1.ne(t2), TensorBase([1, 1, 1, 0]))
+
+    def testNe_(self):
+        t1 = TensorBase([1, 2, 3, 4])
+        t2 = TensorBase([1., 2., 3., 5.])
+        t1.ne_(t2)
+        self.assertTrue(syft.equal(t1, TensorBase([1, 1, 1, 0])))
+
+
 if __name__ == "__main__":
     unittest.main()