singa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wang...@apache.org
Subject [2/6] incubator-singa git commit: SINGA-186 Create Python Tensor class
Date Fri, 01 Jul 2016 08:24:45 GMT
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/50f67226/src/python/tensor.py
----------------------------------------------------------------------
diff --git a/src/python/tensor.py b/src/python/tensor.py
index d382aae..b8abb5c 100644
--- a/src/python/tensor.py
+++ b/src/python/tensor.py
@@ -28,7 +28,6 @@ to call singa::Tensor and its methods
 import sys
 import os
 import numpy as np
-import ctypes
 
 sys.path.append(os.path.join(os.path.dirname(__file__),
                              '../../build/lib'))
@@ -64,7 +63,7 @@ class Tensor(object):
             self.dtype = dtype
 
     def to_array(self):
-        # TODO(chonho) - need to think more efficient way to convert???
+        # TODO(chonho): depreciated (will be deleted later)
         idx = self.singa_tensor.data_type()
         if idx == kFloat32:
             data_array = singa.floatArray_frompointer(
@@ -73,9 +72,6 @@ class Tensor(object):
         elif idx == kFloat16:
             print 'not implemented yet'
             return
-            # data_array = singa.floatArray_frompointer(
-            #                  self.singa_tensor.floatData())
-            # dt = np.float16
         elif idx == kInt:
             data_array = singa.intArray_frompointer(
                              self.singa_tensor.intData())
@@ -93,20 +89,28 @@ class Tensor(object):
         data = np.array(data, dtype=dt).reshape(self.tuple_shape)
         return data
 
-    def from_array(self, np_array):
-        # TODO(chonho)
-        test = np.array([[1,2],[3,4]], dtype=np.float32)
-        test = test.flatten()
-        self.singa_tensor.floatCopyData(test, 4, 0)
+    def copy_to_numpy(self):
+        ''' this method gets the values of tensor data and
+            returns it as numpy array
         '''
-        c_float_ptr = ctypes.POINTER(ctypes.c_float)
-        data_ptr = test.ctypes.data_as(c_float_ptr)
-        self.singa_tensor.floatCopyData(data_ptr, 4, 0)
-        '''
-        '''
-        d = [1.0, 2.0, 3.0, 4.0]
-        self.singa_tensor.floatCopyData(d, 4, 0)
+        if self.dtype == kFloat32:
+            np_array = self.singa_tensor.floatGetValue(int(self.size()))
+        else:
+            print 'Not implemented yet for ', self.dtype
+        return np_array.reshape(self.tuple_shape)
+
+    def copy_from_numpy(self, np_array, offset=0):
+        ''' this method stores the values of numpy array into tensor data
+            from the position of offset
         '''
+        assert np_array.size == self.size(), 'tensor shape should be the same'
+        if not np_array.ndim == 1:
+            np_array = np_array.flatten()
+        dt = np_array.dtype
+        if dt == np.float32:
+            self.singa_tensor.floatCopyDataFromHostPtr(np_array, offset)
+        else:
+            print 'Not implemented yet for ', dt
 
     def data_type(self):
         return self.singa_tensor.data_type()
@@ -306,9 +310,19 @@ def reshape(t, s):
     return _call_singa_func(singa.Reshape, t.singa_tensor, s)
 
 
-def copy_data_to_from(dst, src, size, src_offset=0, dst_offset=0):
+def copy_data_to_from(dst, src, size, dst_offset=0, src_offset=0):
     singa.CopyDataToFrom(dst.singa_tensor, src.singa_tensor, size,
-                         src_offset, dst_offset)
+                         dst_offset, src_offset)
+
+
+def from_numpy(np_array):
+    ret = Tensor(np_array.shape)
+    ret.copy_from_numpy(np_array)
+    return ret
+
+
+def to_numpy(t):
+    return t.copy_to_numpy()
 
 
 def abs(t):
@@ -426,6 +440,9 @@ def eltwise_mult(lhs, rhs, ret=None):
 
 
 def mult(A, B, C=None, alpha=1.0, beta=0.0):
+    '''
+    This function returns C = alpha * A * B + beta * C
+    '''
     if C is None:
         return _call_singa_func(singa.Mult, A.singa_tensor, B.singa_tensor)
     else:
@@ -434,22 +451,6 @@ def mult(A, B, C=None, alpha=1.0, beta=0.0):
         return C
 
 
-'''
-TODO(chonho) combined into the above
-                delete later
-def mult(A, B, C=None):
-    if C is None:
-        return _call_singa_func(singa.Mult, A.singa_tensor, B.singa_tensor)
-    else:
-        singa_Mult(A.singa_tensor, B.singa_tensor, C.singa_tensor)
-        return C
-
-def axypbz(alpha, A, B, b, C):
-    singa.floatMult(alpha, A.singa_tensor, B.singa_tensor, b, C.singa_tensor)
-    return C
-'''
-
-
 def div(lhs, rhs, ret=None):
     if ret is None:
         # call Tensor.__div__()
@@ -462,6 +463,12 @@ def div(lhs, rhs, ret=None):
         return ret
 
 
+def axypbz(alpha, A, B, b, C):
+    # TODO(chonho): depreciated (will be deleted later)
+    singa.floatMult(alpha, A.singa_tensor, B.singa_tensor, b, C.singa_tensor)
+    return C
+
+
 def axpy(alpha, x, y):
     if type(alpha) == float:
         singa.floatAxpy(alpha, x.singa_tensor, y.singa_tensor)

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/50f67226/test/python/unittest_python.py
----------------------------------------------------------------------
diff --git a/test/python/unittest_python.py b/test/python/unittest_python.py
index e1f4b64..569a7ad 100644
--- a/test/python/unittest_python.py
+++ b/test/python/unittest_python.py
@@ -54,86 +54,99 @@ class TestTensorMethods(unittest.TestCase):
         self.assertEqual(t.size(), 2*3)
         self.assertEqual(t.memsize(), 2*3*sizeof(kFloat32))
         self.assertFalse(t.is_transpose())
+        print 'Done tensor fields'
 
     def test_unary_operators(self):
         t = self.t
-        self.assertAlmostEqual(t.toarray()[0,0], 0.0)
+        arr = np.array([[1.0,2.0],[2.0,3.0],[3.0,4.0]], dtype=np.float32)
+        t.copy_data_from(arr)
+        self.assertAlmostEqual(t.to_array()[0,0], 1.0)
+        self.assertAlmostEqual(t.to_array()[0,1], 2.0)
         t += 1.23
-        self.assertAlmostEqual(t.toarray()[0,0], 1.23)
+        self.assertAlmostEqual(t.to_array()[0,0], 1.0+1.23)
         t -= 0.23
-        self.assertAlmostEqual(t.toarray()[0,0], 1.23-0.23)
+        self.assertAlmostEqual(t.to_array()[0,0], 2.23-0.23)
         t *= 2.5
-        self.assertAlmostEqual(t.toarray()[0,0], (1.23-0.23)*2.5)
+        self.assertAlmostEqual(t.to_array()[0,0], (2.23-0.23)*2.5)
         t /= 2
-        self.assertAlmostEqual(t.toarray()[0,0], (1.23-0.23)*2.5/2)
+        self.assertAlmostEqual(t.to_array()[0,0], (2.23-0.23)*2.5/2)
+        print 'Done unary_operators'
 
     def test_binary_operators(self):
         t = self.t
-        t += 3.2
+        arr = np.array([[1.0,2.0],[2.0,3.0],[3.0,4.0]], dtype=np.float32)
+        t.copy_data_from(arr)
         s = self.s
-        s += 2.1
+        arr = np.array([[4.0,3.0],[3.0,2.0],[2.0,1.0]], dtype=np.float32)
+        s.from_array(arr)
         a = t + s
-        self.assertAlmostEqual(a.toarray()[0,0], 3.2+2.1, 5)
+        self.assertAlmostEqual(a.to_array()[0,0], 1.0+4.0)
         a = t - s
-        self.assertAlmostEqual(a.toarray()[0,0], 3.2-2.1, 5)
+        self.assertAlmostEqual(a.to_array()[0,0], 1.0-4.0)
         a = t * s
-        self.assertAlmostEqual(a.toarray()[0,0], 3.2*2.1, 5)
-        ''' not implemented yet
+        self.assertAlmostEqual(a.to_array()[0,0], 1.0*4.0)
         a = t / s
-        self.assertAlmostEqual(a.toarray()[0,0], 3.2/2.1, 5)
-        '''
+        self.assertAlmostEqual(a.to_array()[0,0], 1.0/4.0)
+        print 'Done binary_operators'
 
     def test_comparison_operators(self):
         t = self.t
         t += 3.45
         a = t < 3.45
-        self.assertEqual(a.toarray()[0,0], 0)
+        self.assertEqual(a.to_array()[0,0], 0)
         a = t <= 3.45
-        self.assertEqual(a.toarray()[0,0], 1)
+        self.assertEqual(a.to_array()[0,0], 1)
         a = t > 3.45
-        self.assertEqual(a.toarray()[0,0], 0)
+        self.assertEqual(a.to_array()[0,0], 0)
         a = t >= 3.45
-        self.assertEqual(a.toarray()[0,0], 1)
+        self.assertEqual(a.to_array()[0,0], 1)
         a = lt(t, 3.45)
-        self.assertEqual(a.toarray()[0,0], 0)
+        self.assertEqual(a.to_array()[0,0], 0)
         a = le(t, 3.45)
-        self.assertEqual(a.toarray()[0,0], 1)
+        self.assertEqual(a.to_array()[0,0], 1)
         a = gt(t, 3.45)
-        self.assertEqual(a.toarray()[0,0], 0)
+        self.assertEqual(a.to_array()[0,0], 0)
         a = ge(t, 3.45)
-        self.assertEqual(a.toarray()[0,0], 1)
+        self.assertEqual(a.to_array()[0,0], 1)
 
+    def test_tensor_manipulation(self):
+        #TODO(chonho)
+        pass
+
+    def test_random_operations(self):
+        #TODO(chonho)
+        pass
 
     def test_tensor_copy(self):
         t = Tensor((2,3))
         t += 1.23
-        self.assertAlmostEqual(t.toarray()[0,0], 1.23)
+        self.assertAlmostEqual(t.to_array()[0,0], 1.23)
         tc = t.copy()
         tdc = t.deepcopy()
-        self.assertAlmostEqual(tc.toarray()[0,0], 1.23)
-        self.assertAlmostEqual(tdc.toarray()[0,0], 1.23)
+        self.assertAlmostEqual(tc.to_array()[0,0], 1.23)
+        self.assertAlmostEqual(tdc.to_array()[0,0], 1.23)
         t += 1.23
-        self.assertAlmostEqual(t.toarray()[0,0], 2.46)
-        self.assertAlmostEqual(tc.toarray()[0,0], 2.46)
-        self.assertAlmostEqual(tdc.toarray()[0,0], 1.23)
+        self.assertAlmostEqual(t.to_array()[0,0], 2.46)
+        self.assertAlmostEqual(tc.to_array()[0,0], 2.46)
+        self.assertAlmostEqual(tdc.to_array()[0,0], 1.23)
 
     def test_copy_data(self):
         t = self.t
         t += 1.23
         s = self.s
         s += 5.43
-        self.assertAlmostEqual(t.toarray()[0,0], 1.23)
+        self.assertAlmostEqual(t.to_array()[0,0], 1.23)
         copy_data_to_from(t, s, 2)
-        self.assertAlmostEqual(t.toarray()[0,0], 5.43, 5)
-        self.assertAlmostEqual(t.toarray()[0,1], 5.43, 5)
-        self.assertAlmostEqual(t.toarray()[0,2], 1.23)
+        self.assertAlmostEqual(t.to_array()[0,0], 5.43, 5)
+        self.assertAlmostEqual(t.to_array()[0,1], 5.43, 5)
+        self.assertAlmostEqual(t.to_array()[0,2], 1.23)
 
 
     def test_global_method(self):
         t = self.t
         t += 12.34
         a = log(t)
-        self.assertAlmostEqual(a.toarray()[0,0], math.log(12.34))
+        self.assertAlmostEqual(a.to_array()[0,0], math.log(12.34))
 
 if __name__ == '__main__':
     unittest.main()


Mime
View raw message