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:
[email protected]


Reply via email to