[GitHub] [incubator-singa] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309704143
 
 

 ##
 File path: test/python/test_operation.py
 ##
 @@ -322,6 +336,34 @@ def test_LeakyRelu(self):
 np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT)
 self.check_shape(dx.shape(), (3, 2))
 
+def test_Elu_cpu(self):
+#f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0
+x = np.array([-0.9, -0.3, -0.1, 0.1, 0.5, 0.9]).reshape(3, 
2).astype(np.float32)/10
+lossf = lambda x : np.sum(np.clip(x, 0, np.inf) + (np.exp(np.clip(x, 
-np.inf, 0)) - 1) * 1.0)
 
 Review comment:
   lambda x : alpha * (np.exp(x) -1) if x < 0 else x


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309766405
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -358,6 +358,48 @@ def relu(x):
 return ReLU()(x)[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.SubFloat(singa.Exp(x1),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.GTFloat(self.input, 0.0)
 
 Review comment:
   should be GEFloat


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309766714
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -358,6 +358,48 @@ def relu(x):
 return ReLU()(x)[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.SubFloat(singa.Exp(x1),self.alpha)
 
 Review comment:
   I think this formula doesn't correspond to alpha * (exp(x) - 1.)


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309687031
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -358,6 +358,48 @@ def relu(x):
 return ReLU()(x)[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.SubFloat(singa.Exp(x1),self.alpha)
+x2 = singa.ReLU(x)
+x1 = singa.__add__(x1, x2)
+return x1
 
 Review comment:
   It's a little weird to call ReLU here, can we implement it directly?


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309702428
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -358,6 +358,48 @@ def relu(x):
 return ReLU()(x)[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.SubFloat(singa.Exp(x1),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)
 
 Review comment:
   is this formula correct?


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309687031
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -358,6 +358,48 @@ def relu(x):
 return ReLU()(x)[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.SubFloat(singa.Exp(x1),self.alpha)
+x2 = singa.ReLU(x)
+x1 = singa.__add__(x1, x2)
+return x1
 
 Review comment:
   It's a little weird to call ReLU here, can we implement it directly?


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309704143
 
 

 ##
 File path: test/python/test_operation.py
 ##
 @@ -322,6 +336,34 @@ def test_LeakyRelu(self):
 np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT)
 self.check_shape(dx.shape(), (3, 2))
 
+def test_Elu_cpu(self):
+#f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0
+x = np.array([-0.9, -0.3, -0.1, 0.1, 0.5, 0.9]).reshape(3, 
2).astype(np.float32)/10
+lossf = lambda x : np.sum(np.clip(x, 0, np.inf) + (np.exp(np.clip(x, 
-np.inf, 0)) - 1) * 1.0)
 
 Review comment:
   lambda x : alpha * (np.exp(x) -1) if x < 0 else x


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] joddiy commented on a change in pull request #480: SINGA-474 ELU operator

2019-08-01 Thread GitBox
joddiy commented on a change in pull request #480: SINGA-474 ELU operator
URL: https://github.com/apache/incubator-singa/pull/480#discussion_r309702428
 
 

 ##
 File path: python/singa/autograd.py
 ##
 @@ -358,6 +358,48 @@ def relu(x):
 return ReLU()(x)[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.SubFloat(singa.Exp(x1),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)
 
 Review comment:
   is this formula correct?


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