Repository: OpenMined/PySyft
Pull request #183 (2017-08-24 08:25:35)

Zero_: new method for TensorBase
This is supposed to address [https://github.com/OpenMined/PySyft/issues/109](https://github.com/OpenMined/PySyft/issues/109)


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index f5bf716aa4..485cae8734 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -163,7 +163,7 @@ class TensorBase(object):
             return NotImplemented
 
         tensor = _ensure_tensorbase(tensor)
-        self.data /= tensor.data
+        self.data = self.data / tensor.data
         return self
 
     def __getitem__(self, position):
@@ -234,7 +234,7 @@ class TensorBase(object):
         """Returns the ceilling of the input tensor elementwise."""
         if self.encrypted:
             return NotImplemented
-        self.data = syft.math.ceil(self.data)
+        self.data = syft.math.ceil(self.data).data
         return self
 
     def floor_(self):
@@ -244,6 +244,14 @@ class TensorBase(object):
         self.data = syft.math.floor(self.data).data
         return self
 
+    def zero_(self):
+        """Replaces tensor values with zeros"""
+        if self.encrypted:
+            return NotImplemented
+
+        self.data.fill(0)
+        return self.data
+
     def addmm(self, tensor2, mat, beta=1, alpha=1):
         """Performs ((Mat*Beta)+((Tensor1@Tensor2)*Alpha)) and  returns the
         result as a Tensor


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 5b9eb12f5b..148a0258c7 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -28,10 +28,25 @@ class AddTests(unittest.TestCase):
 
 
 class CeilTests(unittest.TestCase):
+    def testCeil(self):
+        t = TensorBase(np.array([1.4, 2.7, 6.2]))
+        tdash = t.ceil()
+        self.assertTrue(syft.equal(tdash.data, TensorBase([2, 3, 7])))
+        self.assertTrue(syft.equal(t.data, TensorBase([1.4, 2.7, 6.2])))
+
     def testCeil_(self):
         t = TensorBase(np.array([1.4, 2.7, 6.2]))
-        t.ceil_()
-        self.assertTrue(syft.equal(t.data, TensorBase([2, 3, 7])))
+        self.assertTrue(syft.equal(t.ceil_(), [2, 3, 7]))
+        self.assertTrue(syft.equal(t.data, [2, 3, 7]))
+
+
+class ZeroTests(unittest.TestCase):
+    def testZero(self):
+        t = TensorBase(np.array([13, 42, 1024]))
+        self.assertTrue(syft.equal(t.zero_(), [0, 0, 0]))
+
+        t = TensorBase(np.array([13.1, 42.2, 1024.4]))
+        self.assertTrue(syft.equal(t.zero_(), [0.0, 0.0, 0.0]))
 
     def testCeil(self):
         t = TensorBase(np.array([1.4, 2.7, 6.2]))
@@ -42,8 +57,6 @@ class CeilTests(unittest.TestCase):
 class FloorTests(unittest.TestCase):
     def testFloor_(self):
         t = TensorBase(np.array([1.4, 2.7, 6.2]))
-
-        # Now we test in-place
         self.assertTrue(syft.equal(t.floor_(), [1., 2., 6.]))
         self.assertTrue(syft.equal(t.data, [1., 2., 6.]))
 
@@ -84,9 +97,13 @@ class DivTests(unittest.TestCase):
         self.assertTrue(syft.equal(t / np.array([2, 2, 2]), [1, 2, 4]))
 
     def testInplace(self):
-        t = TensorBase(np.array([1, 2, 3]))
-        t *= np.array([1, 2, 3])
-        self.assertTrue(syft.equal(t.data, [1, 4, 9]))
+        t = TensorBase(np.array([2, 4, 8]))
+        t /= np.array([2, 2, 2])
+        self.assertTrue(syft.equal(t.data, [1, 2, 4]))
+
+        t = TensorBase(np.array([1, 7, 11]))
+        t /= np.array([3, 2, 9])
+        self.assertTrue(syft.equal(t, [1 / 3, 7 / 2, 11 / 9]))
 
     def testScalar(self):
         t = TensorBase(np.array([2, 4, 6]))
@@ -443,5 +460,4 @@ class logTests(unittest.TestCase):
 
 
 if __name__ == "__main__":
-    if __name__ == '__main__':
-        unittest.main()
+    unittest.main()