Repository: incubator-singa
Updated Branches:
  refs/heads/master a36291824 -> 76779be72


SINGA-382 Implement concat operation for autograd

- develop concat operation and test it backward function.

- its forward function has one bug in singa.ConcatOn(). the function cannot 
understand vector of CTensor corr  ectly.


Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/054f303a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/054f303a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/054f303a

Branch: refs/heads/master
Commit: 054f303af7b881b0dea98e39a77f52c3d41357ba
Parents: a362918
Author: xuewanqi <xue_wa...@outlook.com>
Authored: Thu Jul 12 08:08:49 2018 +0000
Committer: Wang Wei <wangwei...@gmail.com>
Committed: Fri Jul 13 14:15:06 2018 +0800

----------------------------------------------------------------------
 include/singa/core/tensor.h |  2 +-
 python/singa/autograd.py    | 33 ++++++++++++++++++++++++++++++++-
 src/api/core_tensor.i       |  3 +++
 src/core/tensor/tensor.cc   |  3 ++-
 4 files changed, 38 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/054f303a/include/singa/core/tensor.h
----------------------------------------------------------------------
diff --git a/include/singa/core/tensor.h b/include/singa/core/tensor.h
old mode 100644
new mode 100755
index a73821c..905da27
--- a/include/singa/core/tensor.h
+++ b/include/singa/core/tensor.h
@@ -563,7 +563,7 @@ Tensor SliceColumns(const Tensor &in, const size_t start, 
const size_t end);
 /// tensor in 'in' is a 2D tensor. Values are copied, no memory sharing.
 Tensor ConcatenateRows(const vector<Tensor> &in);
 /// Return a tensor concatenated of the input tensors along the give axis.
-Tensor ConcatOn(const vector<Tensor> &in, int axis);
+Tensor ConcatOn(const std::vector<Tensor> &in, int axis);
 /// Alias name for function ConcatenateRows
 Tensor ConcatRows(const vector<Tensor> &in);
 /// Return a tensor which is horizontally stacked from tensors in 'in'. Each

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/054f303a/python/singa/autograd.py
----------------------------------------------------------------------
diff --git a/python/singa/autograd.py b/python/singa/autograd.py
index 16d7f82..c3986f3 100755
--- a/python/singa/autograd.py
+++ b/python/singa/autograd.py
@@ -485,7 +485,7 @@ def ctensor2numpy(x):
 
 class Flatten(Operation):
 
-    def __init(self, start_axis=1):
+    def __init__(self, start_axis=1):
         # flatten all axis after (inclusive) start_axis
         self.start_axis = start_axis
         assert start_axis == 1, 'must flatten into 2d array not'
@@ -545,6 +545,37 @@ class Linear(Layer):
         return y
 
 
+class Concat(Operation):
+
+    def __init__(self, axis=0):
+        self.axis = axis
+
+    def forward(self, *xs):
+        if training:
+            offset = 0
+            self.slice_point = []
+            for t in xs:
+                offset += t.shape()[self.axis]
+                self.slice_point.append(offset)
+        return singa.ConcatOn(xs, self.axis)
+
+    def backward(self, dy):
+        assert hasattr(
+            self, 'slice_point'), 'Please set training as True before do BP. '
+        assert self.slice_point[-1] == dy.shape()[self.axis], 'Shape 
dismatched.'
+        dxs = []
+        last_offset = 0
+        for p in self.slice_point:
+            dxs.append(singa.SliceOn(dy, last_offset, p, self.axis))
+            last_offset = p
+        return tuple(dxs)
+
+
+def concat(*xs):
+    # TODO changable axis
+    return Concat()(*xs)
+
+
 class _Conv2D(Operation):
 
     def __init__(self, handle):

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/054f303a/src/api/core_tensor.i
----------------------------------------------------------------------
diff --git a/src/api/core_tensor.i b/src/api/core_tensor.i
old mode 100644
new mode 100755
index 9427b11..e98590c
--- a/src/api/core_tensor.i
+++ b/src/api/core_tensor.i
@@ -227,6 +227,9 @@ namespace singa{
   template <typename DType> Tensor operator>=(const Tensor &t, const DType x);
   %template(opge) operator>= <float>;
 
+  Tensor ConcatOn(const std::vector<Tensor> &in, int axis);
+  Tensor SliceOn(const Tensor&in, const size_t start, const size_t end, int 
axis);
+
 
   /* ========== Arithmetic operations ========== */
   %rename(__add__) operator+(const Tensor &lhs, const Tensor &rhs);

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/054f303a/src/core/tensor/tensor.cc
----------------------------------------------------------------------
diff --git a/src/core/tensor/tensor.cc b/src/core/tensor/tensor.cc
index 1ac1b42..720ef90 100755
--- a/src/core/tensor/tensor.cc
+++ b/src/core/tensor/tensor.cc
@@ -23,6 +23,7 @@
 #include <utility>
 #include <algorithm>
 
+
 #define Noaxis 9999
 
 namespace singa {
@@ -871,7 +872,7 @@ void DivColumn(const Tensor &v, Tensor *M) {
   MultColumn(inv, M);
 }
 
-Tensor ConcatOn(const vector<Tensor> &in, int axis) {
+Tensor ConcatOn(const std::vector<Tensor> &in, int axis) {
   vector<Tensor> tmp;
   Shape out_shape = in[0].shape();
   size_t dim = in[0].shape().size();

Reply via email to