From dev-return-5786-archive-asf-public=cust-asf.ponee.io@singa.apache.org Tue Jun 16 07:14:01 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 9C475180621 for ; Tue, 16 Jun 2020 09:14:01 +0200 (CEST) Received: (qmail 32985 invoked by uid 500); 16 Jun 2020 07:14:01 -0000 Mailing-List: contact dev-help@singa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@singa.apache.org Delivered-To: mailing list dev@singa.apache.org Received: (qmail 32972 invoked by uid 99); 16 Jun 2020 07:14:01 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Jun 2020 07:14:01 +0000 From: =?utf-8?q?GitBox?= To: dev@singa.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bsinga=5D_joddiy_commented_on_a_change_in_pull_requ?= =?utf-8?q?est_=23736=3A_Add_expand_operator?= Message-ID: <159229164092.8807.3300358739485088337.asfpy@gitbox.apache.org> Date: Tue, 16 Jun 2020 07:14:00 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit In-Reply-To: References: 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