Skip to content

Commit

Permalink
Merge pull request #486 from pinpom/Sqrt
Browse files Browse the repository at this point in the history
SINGA -475 add Sqrt operator to singa
  • Loading branch information
nudles committed Aug 9, 2019
2 parents 5af541f + a62a683 commit fc1f3bc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
19 changes: 18 additions & 1 deletion python/singa/autograd.py
Expand Up @@ -1829,6 +1829,24 @@ def backward(self, dy):
def leakyrelu(x, a=0.01):
return LeakyRelu(a)(x)[0]

class Sqrt(Operation):
def __init__(self):
super(Sqrt, self).__init__()

def forward(self, x):
if training:
self.input = x
return singa.Sqrt(x)
def backward(self, dy):
dx = singa.PowFloat(self.input,-0.5)
dx = singa.MultFloat(dx,0.5)
dx = singa.__mul__(dy, dx)
return dx

def sqrt(x):
return Sqrt()(x)[0]

class Sub(Operation):
def __init__(self):
Expand All @@ -1845,4 +1863,3 @@ def backward(self, dy):

def sub(a, b):
return Sub()(a,b)[0]

38 changes: 38 additions & 0 deletions test/python/test_operation.py
Expand Up @@ -824,6 +824,44 @@ def test_Sub_gpu(self):
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx0)), DX0, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)), DX1, decimal=5)

def test_Sqrt_cpu(self):
X = np.array([0.1,1.0,0.4,4.0,0.9,9.0]).reshape(3,2).astype(np.float32)
XT = np.sqrt(X)
DY = np.ones((3, 2), dtype = np.float32)

x = tensor.from_numpy(X)
dy = tensor.from_numpy(DY)
x.to_device(cpu_dev)
dy.to_device(cpu_dev)

result = autograd.sqrt(x)
dx = result.creator.backward(dy.data)

G = 0.5 * np.power(X, -0.5)
DX = np.multiply(G, DY)

np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)), DX, decimal=5)

def test_Sqrt_gpu(self):
X = np.array([0.1,1.0,0.4,4.0,0.9,9.0]).reshape(3,2).astype(np.float32)
XT = np.sqrt(X)
DY = np.ones((3, 2), dtype = np.float32)

x = tensor.from_numpy(X)
dy = tensor.from_numpy(DY)
x.to_device(gpu_dev)
dy.to_device(gpu_dev)

result = autograd.sqrt(x)
dx = result.creator.backward(dy.data)

G = 0.5 * np.power(X, -0.5)
DX = np.multiply(G, DY)

np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)), DX, decimal=5)


if __name__ == '__main__':
unittest.main()

0 comments on commit fc1f3bc

Please sign in to comment.