Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
yajiedesign committed Nov 10, 2015
1 parent 4348575 commit c2200be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions python/mxnet/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __add__(self, other):
if isinstance(other, Symbol):
return Symbol._Plus(self, other)
if isinstance(other, Number):
return Symbol._PlusScalar(self, other)
return Symbol._PlusScalar(self, value=other)
else:
raise TypeError('type %s not supported' % str(type(other)))

Expand All @@ -45,13 +45,13 @@ def __sub__(self, other):
if isinstance(other, Symbol):
return Symbol._Minus(self, other)
if isinstance(other, Number):
return Symbol._MinusScalar(self,value=other)
return Symbol._MinusScalar(self, value=other)
else:
raise TypeError('type %s not supported' % str(type(other)))

def __rsub__(self, other):
if isinstance(other, Number):
return Symbol._MinusScalar(self,value=other,right=True)
return Symbol._MinusScalar(self, value=other, right=True)
else:
raise TypeError('type %s not supported' % str(type(other)))

Expand All @@ -76,13 +76,16 @@ def __div__(self, other):

def __rdiv__(self, other):
if isinstance(other, Number):
return Symbol._DivScalar(self, value=other,right=True)
return Symbol._DivScalar(self, value=other, right=True)
else:
raise TypeError('type %s not supported' % str(type(other)))

def __truediv__(self, other):
return self.__div__(other)

def __rtruediv__(self, other):
return self.__rdiv__(other)

def __del__(self):
check_call(_LIB.MXSymbolFree(self.handle))

Expand Down
16 changes: 16 additions & 0 deletions tests/python/unittest/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,27 @@ def test_python_op():
exec1.backward(dy)
assert reldiff(dy.asnumpy(), dx.asnumpy()) < 1e-5

def test_scalarop():
data = mx.symbol.Variable('data')
shape = (3, 4)
data_tmp = np.ones(shape)
data_tmp[:]=5
arr_data = mx.nd.array(data_tmp)

test = (1+data+1)*2/5-0.2+3*data+5/data-data
exe_test = test.bind(mx.cpu(), args=[arr_data])
exe_test.forward()
out = exe_add.outputs[0].asnumpy()

npout = (1+data_tmp +1)*2/5-0.2+3*data_tmp+5/data_tmp-data_tmp
assert reldiff(out, npout) < 1e-6

if __name__ == '__main__':
test_elementwise_sum()
test_concat()
test_slice_channel()
test_regression()
test_python_op()
test_scalarop();
#check_softmax_with_shape((3,4), mx.cpu())
#check_multi_softmax_with_shape((3,4,5), mx.cpu())

0 comments on commit c2200be

Please sign in to comment.