[GitHub] [incubator-singa] nudles commented on a change in pull request #524: SINGA-474 prelu, add, equal, selu, elu operator

2019-08-24 Thread GitBox
nudles commented on a change in pull request #524: SINGA-474 
prelu,add,equal,selu,elu operator
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317377067
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -608,21 +608,177 @@ def backward(self, dy):
 def reshape(a,shape):
 return Reshape(shape)(a)[0]
 
+class PRelu(Operation):
+
+def __init__(self):
+super(PRelu, self).__init__()
+
+def forward(self, x, slope):
+mask0 = singa.LTFloat(x, 0.0)
+if training:
+self.input = x
+self.slope = slope
+self.mask0 = mask0
+x1 = singa.__mul__(x, mask0)
+x1 *= slope
+x2 = singa.ReLU(x)
+x1 += x2
+return x1
+
+def backward(self, dy):
+dx1mask = singa.GEFloat(self.input, 0.0)
+dx2 = singa.__mul__(self.mask0, self.slope)
+dx = singa.__add__(dx1mask, dx2)
+return singa.__mul__(dy, dx), singa.__mul__(dy,
+singa.__mul__(
+self.mask0, 
self.input))
+
+
+def prelu(x, slope):
+return PRelu()(x, slope)[0]
 
 class Add(Operation):
 def __init__(self):
 super(Add, self).__init__()
 
 def forward(self, a, b):
+#up till now, the dimensions of tensor a and b should less than 3
+self.shape0=list(a.shape())
+self.shape1=list(b.shape())
+assert(len(self.shape0) <= 2 and len(self.shape1) <= 2),"up till now, 
the dimensions of tensor a and b should less than 3"
 return singa.__add__(a, b)
 
 def backward(self, dy):
-return dy, dy
+if(type(dy)==float):return dy,dy
+db=CTensor(list(dy.shape()), dy.device())
+db.CopyData(dy)
+for i in range(len(self.shape0)-len(self.shape1)):
+db=singa.Sum(db, 0)
+return dy, db
 
 
 def add(a, b):
 return Add()(a, b)[0]
 
+class Elu(Operation):
+def __init__(self,alpha=1):
+super(Elu, self).__init__()
+self.alpha=alpha
+
+def forward(self, x):
+"""Do forward propgation.
+Store the x if requires gradient.
+Args:
+x (CTensor): matrix
+Returns:
+a CTensor for the result
+"""
+#f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0
+if training:
+self.input = x
+x1 = singa.LTFloat(x, 0.0)
+x1 = singa.__mul__(x, x1)
+x1 = singa.MultFloat(singa.SubFloat(singa.Exp(x1),1.0),self.alpha)
+x2 = singa.ReLU(x)
+x1 = singa.__add__(x1, x2)
 
 Review comment:
   can use ```x1+=x2```?


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-singa] nudles commented on a change in pull request #524: SINGA-474 prelu, add, equal, selu, elu operator

2019-08-24 Thread GitBox
nudles commented on a change in pull request #524: SINGA-474 
prelu,add,equal,selu,elu operator
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317377059
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -608,21 +608,177 @@ def backward(self, dy):
 def reshape(a,shape):
 return Reshape(shape)(a)[0]
 
+class PRelu(Operation):
+
+def __init__(self):
+super(PRelu, self).__init__()
+
+def forward(self, x, slope):
+mask0 = singa.LTFloat(x, 0.0)
+if training:
+self.input = x
+self.slope = slope
+self.mask0 = mask0
+x1 = singa.__mul__(x, mask0)
+x1 *= slope
+x2 = singa.ReLU(x)
+x1 += x2
+return x1
+
+def backward(self, dy):
+dx1mask = singa.GEFloat(self.input, 0.0)
+dx2 = singa.__mul__(self.mask0, self.slope)
+dx = singa.__add__(dx1mask, dx2)
+return singa.__mul__(dy, dx), singa.__mul__(dy,
+singa.__mul__(
+self.mask0, 
self.input))
+
+
+def prelu(x, slope):
+return PRelu()(x, slope)[0]
 
 class Add(Operation):
 def __init__(self):
 super(Add, self).__init__()
 
 def forward(self, a, b):
+#up till now, the dimensions of tensor a and b should less than 3
+self.shape0=list(a.shape())
+self.shape1=list(b.shape())
+assert(len(self.shape0) <= 2 and len(self.shape1) <= 2),"up till now, 
the dimensions of tensor a and b should less than 3"
 return singa.__add__(a, b)
 
 def backward(self, dy):
-return dy, dy
+if(type(dy)==float):return dy,dy
 
 Review comment:
   this implementation assumes that the a has the same shape as dy?
   then need to assert it.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-singa] nudles commented on a change in pull request #524: SINGA-474 prelu, add, equal, selu, elu operator

2019-08-24 Thread GitBox
nudles commented on a change in pull request #524: SINGA-474 
prelu,add,equal,selu,elu operator
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317377086
 
 

 ##
 File path: test/python/test_operation.py
 ##
 @@ -1587,25 +1587,148 @@ def test_min_gpu(self):
 
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)),
 DX1, decimal=5)
 
 
-def test_HardSigmoid(self):
-def test_helper(gpu=False):
-x = np.random.randn(3, 2)
-#y = max(0, min(1, alpha * x + gamma))
-a=0.2
-g=0.5
-y = np.clip(x * 0.2 + 0.5, 0, 1)
-grad=(0<(np.clip(x * 0.2 + 0.5, 0, 1)) * (np.clip(x * 0.2 + 0.5, 0, 
1)<1))*0.2
-x = tensor.from_numpy(x)
-if(gpu):
-x.to_device(gpu_dev)
-result = autograd.hardsigmoid(x,a,g)
-dy = tensor.from_numpy(np.random.randn((3,2)).astype(np.float32))
-dx = result.creator.backward(dy.data)
-np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
-
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
-test_helper(False)
-test_helper(True)
+def test_HardSigmoid(self):
+def test_helper(gpu=False):
+x = np.random.randn(3, 2)
+#y = max(0, min(1, alpha * x + gamma))
+a=0.2
+g=0.5
+y = np.clip(x * 0.2 + 0.5, 0, 1)
+dy=np.random.randn(3,2)
+grad=(0<(np.clip(x * 0.2 + 0.5, 0, 1)) * (np.clip(x * 0.2 + 0.5, 
0, 1)<1))*0.2 * dy
+x = tensor.from_numpy(x)
+dy = tensor.from_numpy(dy)
+if(gpu):
+x.to_device(gpu_dev)
+dy.to_device(gpu_dev)
+result = autograd.hardsigmoid(x,a,g)
+dx = result.creator.backward(dy.data)
+np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
+test_helper(False)
+test_helper(True)
+
+@unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+def test_prelu(self):
+def hepler(gpu):
+x = np.random.randn(3, 2)
+slope = np.random.randn(3, 2)
+y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope
+dy = np.random.randn(3, 2)
+x0=x.copy()
+x0[x0>0]=1
+x0[x0<1]=0
+grad0=(x0+(1-x0)*slope)*dy
+grad1 = (1-x0)*x*dy
+x = tensor.from_numpy(x)
+slope = tensor.from_numpy(slope)
+dy = tensor.from_numpy(dy)
+if(gpu):
+x.to_device(gpu_dev)
+slope.to_device(gpu_dev)
+dy.to_device(gpu_dev)
+result = autograd.prelu(x,slope)
+dx0,dx1 = result.creator.backward(dy.data)
+np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx0)),
 grad0, decimal=5)
+
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)),
 grad1, decimal=5)
+hepler(False)
+hepler(True)
+
+@unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+def test_SeLU(self):
+def test_helper(gpu):
+x = np.random.randn(3, 2)
+a=0.2
+g=0.3
+y = np.clip(x, 0, np.inf) * g + (np.exp(np.clip(x, -np.inf, 0)) - 
1) * a * g
+dy=np.random.randn(3, 2)
+grad = (np.exp(np.clip(x, -np.inf, 0))) * g
+grad[x<=0]=grad[x<=0]*a
+grad*=dy
+x = tensor.from_numpy(x)
+
 
+result = autograd.selu(x,a,g)
+dy = tensor.from_numpy(dy)
+if(gpu):
+dy.to_device(gpu_dev)
+x.to_device(gpu_dev)
+dx = result.creator.backward(dy.data)
+
+np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
+test_helper(False)
+test_helper(True)
+
+
+@unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+def test_Equal(self):
+def test_helper(gpu):
+x0 = np.random.randn(3, 2)
+x1 = np.random.randn(3, 2)
+y = np.equal(x0,x1)
+x0 = tensor.from_numpy(x0)
+x1 = tensor.from_numpy(x1)
+if(gpu):
+x0.to_device(gpu_dev)
+x1.to_device(gpu_dev)
+
+result = autograd.equal(x0,x1)
+
+np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+test_helper(False)
+test_helper(True)
+
+@unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enab

[GitHub] [incubator-singa] nudles commented on a change in pull request #524: SINGA-474 prelu, add, equal, selu, elu operator

2019-08-24 Thread GitBox
nudles commented on a change in pull request #524: SINGA-474 
prelu,add,equal,selu,elu operator
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317377077
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -608,21 +608,177 @@ def backward(self, dy):
 def reshape(a,shape):
 return Reshape(shape)(a)[0]
 
+class PRelu(Operation):
+
+def __init__(self):
+super(PRelu, self).__init__()
+
+def forward(self, x, slope):
+mask0 = singa.LTFloat(x, 0.0)
+if training:
+self.input = x
+self.slope = slope
+self.mask0 = mask0
+x1 = singa.__mul__(x, mask0)
+x1 *= slope
+x2 = singa.ReLU(x)
+x1 += x2
+return x1
+
+def backward(self, dy):
+dx1mask = singa.GEFloat(self.input, 0.0)
+dx2 = singa.__mul__(self.mask0, self.slope)
+dx = singa.__add__(dx1mask, dx2)
+return singa.__mul__(dy, dx), singa.__mul__(dy,
+singa.__mul__(
+self.mask0, 
self.input))
+
+
+def prelu(x, slope):
+return PRelu()(x, slope)[0]
 
 class Add(Operation):
 def __init__(self):
 super(Add, self).__init__()
 
 def forward(self, a, b):
+#up till now, the dimensions of tensor a and b should less than 3
+self.shape0=list(a.shape())
+self.shape1=list(b.shape())
+assert(len(self.shape0) <= 2 and len(self.shape1) <= 2),"up till now, 
the dimensions of tensor a and b should less than 3"
 return singa.__add__(a, b)
 
 def backward(self, dy):
-return dy, dy
+if(type(dy)==float):return dy,dy
+db=CTensor(list(dy.shape()), dy.device())
+db.CopyData(dy)
+for i in range(len(self.shape0)-len(self.shape1)):
+db=singa.Sum(db, 0)
+return dy, db
 
 
 def add(a, b):
 return Add()(a, b)[0]
 
+class Elu(Operation):
+def __init__(self,alpha=1):
+super(Elu, self).__init__()
+self.alpha=alpha
+
+def forward(self, x):
+"""Do forward propgation.
+Store the x if requires gradient.
+Args:
+x (CTensor): matrix
+Returns:
+a CTensor for the result
+"""
+#f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0
+if training:
+self.input = x
+x1 = singa.LTFloat(x, 0.0)
+x1 = singa.__mul__(x, x1)
+x1 = singa.MultFloat(singa.SubFloat(singa.Exp(x1),1.0),self.alpha)
+x2 = singa.ReLU(x)
+x1 = singa.__add__(x1, x2)
+return x1
+
+def backward(self, dy):
+"""
+Args:
+dy (CTensor): data for the dL / dy, L is the loss
+Returns:
+a tuple for dx
+"""
+dx1mask = singa.LTFloat(self.input, 0.0)
+dx1 = singa.MultFloat(singa.Exp(self.input), self.alpha)
+dx1 = singa.__mul__(dx1mask, dx1)
+
+dx2mask = singa.GEFloat(self.input, 0.0)
+
+dx = singa.__add__(dx1, dx2mask)
+return singa.__mul__(dy, dx)
 
 Review comment:
   can use dx *= dy?


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:
us...@infra.apache.org


With regards,
Apache Git Services