Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SINGA -475 add Sub operator implementation to singa #485

Merged
merged 6 commits into from Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions python/singa/autograd.py
Expand Up @@ -1828,3 +1828,21 @@ def backward(self, dy):

def leakyrelu(x, a=0.01):
return LeakyRelu(a)(x)[0]


class Sub(Operation):
def __init__(self):
super(Sub, self)._init_()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line1835:      super(Sub, self)._init_()  
should be
 super(Sub, self).__init__()  

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix the code


def forward(self, a, b):
if training:
self.input = (a, b)
return singa.__sub__(a, b)

def backward(self, dy):
return dy, singa.MultFloat(dy, -1.0)


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

15 changes: 15 additions & 0 deletions test/python/test_operation.py
Expand Up @@ -322,6 +322,21 @@ def test_LeakyRelu(self):
np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT)
self.check_shape(dx.shape(), (3, 2))

def test_Sub(self):
X=np.array([0.8,-1.2,3.3,-3.6,-0.5,0.5]).reshape(3,2).astype(np.float32)
Y=np.array([4.4,5.3,3.2,3.7,5.4,6.3]).reshape(3,2).astype(np.float32)
x = tensor.from_numpy(X)
y = tensor.from_numpy(Y)
x.to_device(gpu_dev)
y.to_device(gpu_dev)

result=autograd.sub(x,y)
dx=result.creator.backward(x.data)[0]

result_np=np.subtract(X,Y)
np.testing.assert_array_almost_equal(tensor.to_numpy(result), result_np)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the test case of backward propagation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added backward test.

self.check_shape(dx.shape(), (3, 2))

def test_Cos_cpu(self):
X = np.array([0.8, -1.2, 3.3, -3.6, -0.5, 0.5]).reshape(3, 2).astype(np.float32)
XT = np.cos(X)
Expand Down