Repository: OpenMined/PySyft
Pull request #201 (2017-09-01 11:21:53)

Implementation of uniform, fill, topk, tolist, trace functions for TensorBase #44, #106, #104
Added uniform, uniform_ , fill_ , tolist, topk and trace functions for TensorBase along with tests.


Patch:
diff --git a/syft/tensor.py b/syft/tensor.py
index 66e99246d0..6963de3675 100644
--- a/syft/tensor.py
+++ b/syft/tensor.py
@@ -621,6 +621,61 @@ class TensorBase(object):
         self.data = np.random.lognormal(mean, stdev, self.shape())
         return self
 
+    def uniform_(self, low=0, high=1):
+        """Fills the tensor in-place with numbers sampled unifromly
+        over the half-open interval [low,high) or from the uniform distribution"""
+        if self.encrypted:
+            return NotImplemented
+        self.data = np.random.uniform(low=low, high=high, size=self.shape())
+        return self
+
+    def uniform(self, low=0, high=1):
+        """Returns a new tensor filled with numbers sampled unifromly
+        over the half-open interval [low,high) or from the uniform distribution"""
+        if self.encrypted:
+            return NotImplemented
+        out = np.random.uniform(low=low, high=high, size=self.shape())
+        return TensorBase(out)
+
+    def fill_(self, value):
+        """Fills the tensor in-place with the specified value"""
+        if self.encrypted:
+            return NotImplemented
+        self.data.fill(value)
+        return self
+
+    def tolist(self):
+        """Returns a new tensor as (possibly a nested) list"""
+        if self.encrypted:
+            return NotImplemented
+        out = self.data.tolist()
+        return out
+
+    def topk(self, k, largest=True):
+        """Returns a new tensor with the sorted k largest (or smallest) values"""
+        if self.encrypted:
+            return NotImplemented
+        out_sort = np.sort(self.data)
+        if self.data.ndim > 1:
+            out = np.partition(out_sort, kth=k)
+            out = out[:, -k:] if largest else out[:, :k]
+        else:
+            out = np.partition(out_sort, kth=k)
+            out = out[-k:] if largest else out[:k]
+        return TensorBase(out)
+
+    def trace(self, axis1=None, axis2=None):
+        """Returns a new tenosr with the sum along diagonals of a 2D tensor.
+           Axis1 and Axis2 are used to extract 2D subarray for sum calculation
+           along diagonals, if tensor has more than two dimensions. """
+        if self.encrypted:
+            return NotImplemented
+        if axis1 is not None and axis2 is not None and self.data.ndim > 2:
+            out = np.trace(a=self.data, axis1=axis1, axis2=axis2)
+        else:
+            out = np.trace(a=self.data)
+        return TensorBase(out)
+
     def view(self, *args):
         """View the tensor."""
         if self.encrypted:


Test patch:
diff --git a/tests/test_tensor.py b/tests/test_tensor.py
index 1ba6c36006..3142b516d3 100644
--- a/tests/test_tensor.py
+++ b/tests/test_tensor.py
@@ -469,5 +469,44 @@ class logTests(unittest.TestCase):
         self.assertTrue(np.allclose((t1.log1p_()).data, [0.69314718, 1.09861229, 1.38629436]))
 
 
+class uniformTests(unittest.TestCase):
+    def testUniform(self):
+        t1 = TensorBase(np.zeros(4))
+        out = t1.uniform(low=0, high=3)
+        self.assertTrue(np.all(out.data > 0) and np.all(out.data < 3))
+
+    def testUniform_(self):
+        t1 = TensorBase(np.zeros(4))
+        t1.uniform_(low=0, high=3)
+        self.assertTrue(np.all(t1.data > 0) and np.all(t1.data < 3))
+
+
+class fillTests(unittest.TestCase):
+    def testFill_(self):
+        t1 = TensorBase(np.array([1, 2, 3, 4]))
+        t1.fill_(5)
+        self.assertTrue(np.all(t1.data == 5))
+
+
+class topkTests(unittest.TestCase):
+    def testTopK(self):
+        t1 = TensorBase(np.array([[900, 800, 1000, 2000, 5, 10, 20, 40, 50], [10, 11, 12, 13, 5, 6, 7, 8, 9], [30, 40, 50, 10, 8, 1, 2, 3, 4]]))
+        t2 = t1.topk(3, largest=True)
+        self.assertTrue(np.array_equal(t2.data, np.array([[900, 1000, 2000], [11, 12, 13], [30, 40, 50]])))
+
+
+class tolistTests(unittest.TestCase):
+    def testToList(self):
+        t1 = TensorBase(np.array([200, 300, 90, 100, 600]))
+        t1_list = t1.tolist()
+        self.assertTrue(isinstance(t1_list, list))
+
+
+class traceTests(unittest.TestCase):
+    def testTrace(self):
+        t1 = TensorBase(np.arange(1, 10).reshape(3, 3))
+        self.assertTrue(np.equal(t1.trace().data, 15))
+
+
 if __name__ == "__main__":
     unittest.main()