Repository: incubator-singa Updated Branches: refs/heads/dev e857bc38a -> c50255259
SINGA-186 Create Python Tensor class - Format acording to pep8 - Update unittest_python.py Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/164fc2ea Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/164fc2ea Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/164fc2ea Branch: refs/heads/dev Commit: 164fc2eaaf921980d7d50dc3851d3f00f3a64b31 Parents: dc013f3 Author: chonho <[email protected]> Authored: Sat Jul 2 01:45:56 2016 +0800 Committer: chonho <[email protected]> Committed: Sat Jul 2 01:45:56 2016 +0800 ---------------------------------------------------------------------- src/python/device.py | 1 + src/python/tensor.py | 35 ++--------------------------------- test/python/unittest_python.py | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/164fc2ea/src/python/device.py ---------------------------------------------------------------------- diff --git a/src/python/device.py b/src/python/device.py index 68e0f6a..92a92e7 100644 --- a/src/python/device.py +++ b/src/python/device.py @@ -80,3 +80,4 @@ class CudaGPU(Device): def __init__(self, id=0, num_executors=1, scheduler='sync', vm='gc-only'): super(CudaGPU, self).__init__(id, num_executors, scheduler, vm, 'gpu') + http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/164fc2ea/src/python/tensor.py ---------------------------------------------------------------------- diff --git a/src/python/tensor.py b/src/python/tensor.py index 19af454..fc95d37 100644 --- a/src/python/tensor.py +++ b/src/python/tensor.py @@ -62,33 +62,6 @@ class Tensor(object): self.device = device self.dtype = dtype - def to_array(self): - # TODO(chonho): depreciated (will be deleted later) - idx = self.singa_tensor.data_type() - if idx == kFloat32: - data_array = singa.floatArray_frompointer( - self.singa_tensor.floatData()) - dt = np.float32 - elif idx == kFloat16: - print 'not implemented yet' - return - elif idx == kInt: - data_array = singa.intArray_frompointer( - self.singa_tensor.intData()) - dt = np.int32 - elif idx == kChar: - data_array = singa.charArray_frompointer( - self.singa_tensor.charData()) - dt = np.int8 - elif idx == kDouble: - data_array = singa.doubleArray_frompointer( - self.singa_tensor.doubleData()) - dt = np.float64 - - data = [data_array[i] for i in range(self.singa_tensor.Size())] - data = np.array(data, dtype=dt).reshape(self.tuple_shape) - return data - 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 @@ -411,6 +384,7 @@ def add(lhs, rhs, ret=None): singa.Add_Tf_out(lhs.singa_tensor, rhs, ret.singa_tensor) return ret + def sub(lhs, rhs, ret=None): if ret is None: # call Tensor.__sub__() @@ -461,12 +435,6 @@ 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) @@ -541,3 +509,4 @@ def _call_singa_func(_singa_func, *args): new_t.device = new_t.singa_tensor.device() new_t.dtype = new_t.singa_tensor.data_type() return new_t + http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/164fc2ea/test/python/unittest_python.py ---------------------------------------------------------------------- diff --git a/test/python/unittest_python.py b/test/python/unittest_python.py index 86830a0..5a3e9c2 100644 --- a/test/python/unittest_python.py +++ b/test/python/unittest_python.py @@ -114,8 +114,23 @@ class TestTensorMethods(unittest.TestCase): self.assertEqual(to_numpy(a)[0, 0], 1) def test_tensor_manipulation(self): - # TODO(chonho) - pass + t = self.t + arr = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.float32) + t.copy_from_numpy(arr) + s = Tensor((3,1)) + arr = np.array([7, 8, 9], dtype=np.float32) + s.copy_from_numpy(arr) + t.add_column(s) + self.assertEqual(to_numpy(t)[0, 0], 1+7) + self.assertEqual(to_numpy(t)[1, 0], 3+8) + self.assertEqual(to_numpy(t)[1, 1], 4+8) + + arr = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.float32) + t.copy_from_numpy(arr) + add_column(2, s, 3, t) + self.assertEqual(to_numpy(t)[0, 0], 3*1+2*7) + self.assertEqual(to_numpy(t)[1, 0], 3*3+2*8) + self.assertEqual(to_numpy(t)[1, 1], 3*4+2*8) def test_random_operations(self): # TODO(chonho)
