You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@singa.apache.org by GitBox <gi...@apache.org> on 2020/06/15 10:25:17 UTC

[GitHub] [singa] joddiy opened a new pull request #736: Add expand operator

joddiy opened a new pull request #736:
URL: https://github.com/apache/singa/pull/736


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [singa] nudles merged pull request #736: Add expand operator

Posted by GitBox <gi...@apache.org>.
nudles merged pull request #736:
URL: https://github.com/apache/singa/pull/736


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [singa] joddiy commented on a change in pull request #736: Add expand operator

Posted by GitBox <gi...@apache.org>.
joddiy commented on a change in pull request #736:
URL: https://github.com/apache/singa/pull/736#discussion_r440637288



##########
File path: python/singa/autograd.py
##########
@@ -4698,6 +4698,103 @@ def cossim(a, b):
     return CosSim()(a, b)[0]
 
 
+class Expand(Operator):
+    """
+    Init a expand operator

Review comment:
       I've added an example usage at the comment.
   
   The `expand` duplicates the data along with the axis. It looks like `upsample` but still has some differences.
   >
       Example usage::
       data = [[1.], [2.], [3.]]
   
       # dim_changed
       shape = [2, 1, 6]
       output = [[[1., 1., 1., 1., 1., 1.], 
                  [2., 2., 2., 2., 2., 2.],
                  [3., 3., 3., 3., 3., 3.]],
                 [[1., 1., 1., 1., 1., 1.],
                  [2., 2., 2., 2., 2., 2.],
                  [3., 3., 3., 3., 3., 3.]]]
   
       # dim_unchanged
       shape = [3, 4]
       output = [[1., 1., 1., 1.],
                 [2., 2., 2., 2.],
                 [3., 3., 3., 3.]]
   >
   
   For upsample
   >
       Example usage::
       data = [[[[1, 2],
                 [3, 4],]]]
   
       # nearest
       scales = [1.0, 1.0, 2.0, 3.0]
       output = [[[[1, 1, 1, 2, 2, 2],
                   [1, 1, 1, 2, 2, 2],
                   [3, 3, 3, 4, 4, 4],
                   [3, 3, 3, 4, 4, 4],]]]
   >




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [singa] nudles commented on a change in pull request #736: Add expand operator

Posted by GitBox <gi...@apache.org>.
nudles commented on a change in pull request #736:
URL: https://github.com/apache/singa/pull/736#discussion_r440535951



##########
File path: python/singa/autograd.py
##########
@@ -4698,6 +4698,103 @@ def cossim(a, b):
     return CosSim()(a, b)[0]
 
 
+class Expand(Operator):
+    """
+    Init a expand operator

Review comment:
       You can explain what this operator does?
   Does it reshape the tensor with more dimensions?

##########
File path: python/singa/autograd.py
##########
@@ -4698,6 +4698,103 @@ def cossim(a, b):
     return CosSim()(a, b)[0]
 
 
+class Expand(Operator):
+    """
+    Init a expand operator
+    """
+
+    def __init__(self, shape):
+        """
+        Args:
+            shape (list[int]: indicates the shape you want to expand to, 
+                following the broadcast rule
+        """
+        super(Expand, self).__init__()
+        self.shape = shape
+
+    def forward(self, x):
+        """
+        forward of Expand
+        Args:
+            x (CTensor): input tensor.
+        Returns:
+            the output CTensor.
+        """
+        if isinstance(self.shape, np.ndarray):
+            self.shape = self.shape.tolist()
+        else:
+            self.shape = list(self.shape)
+        self.dim_changed = True
+        self.x_shape = list(x.shape())
+        x_shape = self.x_shape.copy()
+        for s_1, s_2 in zip(self.shape[::-1], x_shape[::-1]):
+            if s_1 != 1 and s_2 !=1:
+                if len(self.shape)!=len(x_shape):
+                    assert False, ('not support dim_unchanged mode')
+                self.dim_changed = False
+                break
+        if self.dim_changed:
+            tmp_tensor = singa.Tensor(self.shape, x.device())
+            tmp_tensor.SetFloatValue(1.)
+            x = singa.__mul__(x, tmp_tensor)
+        else:
+            for axis, s_1, s_2 in zip(range(len(self.shape)), self.shape, x_shape):
+                if s_1 == s_2:
+                    continue
+                xs = [x] * (s_1//s_2)
+                x = singa.VecTensor(xs)
+                x = singa.ConcatOn(x, axis)
+        return x
+
+    def backward(self, dy):
+        """
+        backward of Expand
+        Args:f
+            dy (CTensor), gradient tensor.
+        Return:
+            the gradient tensor over input tensor.
+        """
+        x_shape = self.x_shape
+        if self.dim_changed:
+            dy = tensor.from_raw_tensor(dy)
+            if len(self.shape) > len(x_shape):
+                x_shape = [1] * (len(self.shape) - len(x_shape))+ x_shape 
+            for axis, s in zip(range(len(self.shape))[::-1], x_shape[::1]):
+                if s == 1:
+                    dy = tensor.sum(dy, axis)
+            dy = dy.data
+        else:
+            for axis, s_1, s_2 in zip(range(len(self.shape))[::-1], self.shape[::-1], x_shape[::-1]):
+                if s_1 > s_2:
+                    duplic = s_1//s_2
+                    dxs = []
+                    for i in range(s_2):
+                        tmp_tensor = None
+                        for j in range(duplic):
+                            if not tmp_tensor:
+                                tmp_tensor = singa.SliceOn(dy, j*s_2+i, j*s_2+i+1, axis)
+                            else:
+                                tmp_tensor += singa.SliceOn(dy, j*s_2+i, j*s_2+i+1, axis)
+                        dxs.append(tmp_tensor)
+                    dxs = singa.VecTensor(dxs)
+                    dy = singa.ConcatOn(dxs, axis)
+        dy = singa.Reshape(dy, self.x_shape)
+        return dy
+
+
+def expand(x, shape):
+    """
+    Produces a cos similarity operator

Review comment:
       expand and cos similarity are related??

##########
File path: python/singa/autograd.py
##########
@@ -4698,6 +4698,103 @@ def cossim(a, b):
     return CosSim()(a, b)[0]
 
 
+class Expand(Operator):
+    """
+    Init a expand operator

Review comment:
       what's the difference to reshape?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [singa] joddiy commented on a change in pull request #736: Add expand operator

Posted by GitBox <gi...@apache.org>.
joddiy commented on a change in pull request #736:
URL: https://github.com/apache/singa/pull/736#discussion_r440633751



##########
File path: python/singa/autograd.py
##########
@@ -4698,6 +4698,103 @@ def cossim(a, b):
     return CosSim()(a, b)[0]
 
 
+class Expand(Operator):
+    """
+    Init a expand operator
+    """
+
+    def __init__(self, shape):
+        """
+        Args:
+            shape (list[int]: indicates the shape you want to expand to, 
+                following the broadcast rule
+        """
+        super(Expand, self).__init__()
+        self.shape = shape
+
+    def forward(self, x):
+        """
+        forward of Expand
+        Args:
+            x (CTensor): input tensor.
+        Returns:
+            the output CTensor.
+        """
+        if isinstance(self.shape, np.ndarray):
+            self.shape = self.shape.tolist()
+        else:
+            self.shape = list(self.shape)
+        self.dim_changed = True
+        self.x_shape = list(x.shape())
+        x_shape = self.x_shape.copy()
+        for s_1, s_2 in zip(self.shape[::-1], x_shape[::-1]):
+            if s_1 != 1 and s_2 !=1:
+                if len(self.shape)!=len(x_shape):
+                    assert False, ('not support dim_unchanged mode')
+                self.dim_changed = False
+                break
+        if self.dim_changed:
+            tmp_tensor = singa.Tensor(self.shape, x.device())
+            tmp_tensor.SetFloatValue(1.)
+            x = singa.__mul__(x, tmp_tensor)
+        else:
+            for axis, s_1, s_2 in zip(range(len(self.shape)), self.shape, x_shape):
+                if s_1 == s_2:
+                    continue
+                xs = [x] * (s_1//s_2)
+                x = singa.VecTensor(xs)
+                x = singa.ConcatOn(x, axis)
+        return x
+
+    def backward(self, dy):
+        """
+        backward of Expand
+        Args:f
+            dy (CTensor), gradient tensor.
+        Return:
+            the gradient tensor over input tensor.
+        """
+        x_shape = self.x_shape
+        if self.dim_changed:
+            dy = tensor.from_raw_tensor(dy)
+            if len(self.shape) > len(x_shape):
+                x_shape = [1] * (len(self.shape) - len(x_shape))+ x_shape 
+            for axis, s in zip(range(len(self.shape))[::-1], x_shape[::1]):
+                if s == 1:
+                    dy = tensor.sum(dy, axis)
+            dy = dy.data
+        else:
+            for axis, s_1, s_2 in zip(range(len(self.shape))[::-1], self.shape[::-1], x_shape[::-1]):
+                if s_1 > s_2:
+                    duplic = s_1//s_2
+                    dxs = []
+                    for i in range(s_2):
+                        tmp_tensor = None
+                        for j in range(duplic):
+                            if not tmp_tensor:
+                                tmp_tensor = singa.SliceOn(dy, j*s_2+i, j*s_2+i+1, axis)
+                            else:
+                                tmp_tensor += singa.SliceOn(dy, j*s_2+i, j*s_2+i+1, axis)
+                        dxs.append(tmp_tensor)
+                    dxs = singa.VecTensor(dxs)
+                    dy = singa.ConcatOn(dxs, axis)
+        dy = singa.Reshape(dy, self.x_shape)
+        return dy
+
+
+def expand(x, shape):
+    """
+    Produces a cos similarity operator

Review comment:
       just a typo, fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org