Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
tests compatible with nosetest. fixed linting in relevant files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Oct 31, 2016
1 parent b211739 commit 06fcee9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 101 deletions.
8 changes: 4 additions & 4 deletions src/operator/mshadow_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,22 @@ struct floor {
}
};

// rint
// rint
struct rint {
template<typename DType>
MSHADOW_XINLINE static DType Map(DType a) {
float floor = floorf(a);
float ceil = ceilf(a);
float ceil = ceilf(a);
return DType(fabsf(floor - a) < fabsf(ceil - a) ? floor : ceil);
}
};

// fix
// fix
struct fix {
template<typename DType>
MSHADOW_XINLINE static DType Map(DType a) {
float floor = floorf(a);
float ceil = ceilf(a);
float ceil = ceilf(a);
return DType(fabsf(floor - 0) < fabsf(ceil - 0) ? floor : ceil);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/operator/tensor/elemwise_unary_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ MXNET_OPERATOR_REGISTER_UNARY(floor)
.MXNET_DESCRIBE("Take floor of the src")
.set_attr<FCompute>("FCompute<cpu>", UnaryCompute<cpu, mshadow_op::floor>);

// rint
// rint
MXNET_OPERATOR_REGISTER_UNARY(rint)
.MXNET_DESCRIBE("Take nearest integer of the src")
.set_attr<FCompute>("FCompute<cpu>", UnaryCompute<cpu, mshadow_op::rint>);
Expand Down
195 changes: 99 additions & 96 deletions tests/python/unittest/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ def mathematical_core(name, context, forward_mxnet_call, forward_numpy_call, bac
assert reldiff(arr_grad, npout_grad) < 1e-6, "%s mathematical backward failed\n%s\n\n%s" % (
name, arr_grad, npout_grad)

def test_rounding(name, context, forward_mxnet_call, forward_numpy_call, data_init=5., grad_init=2.):
def rounding(name, context, forward_mxnet_call, forward_numpy_call, data_init=5., grad_init=2.):
data = mx.symbol.Variable('data')
shape = (3, 4)
data_tmp = np.ones(shape)
Expand All @@ -1549,105 +1549,109 @@ def test_rounding(name, context, forward_mxnet_call, forward_numpy_call, data_in
npout = forward_numpy_call(data_tmp)
assert reldiff(out, npout) < 1e-6, "%s mathematical forward failed\n%s\n\n%s" % (name, out, npout)

def test_mathematical(context):
# rsqrt
mathematical_core("rsqrt", context,
lambda x: mx.sym.rsqrt(x),
lambda x: 1 / np.sqrt(x),
lambda x: -(1.0 / (2.0 * x * np.sqrt(x))))
# tan
mathematical_core("tan", context, lambda x: mx.sym.tan(x), lambda x: np.tan(x), lambda x: np.tan(x) ** 2 + 1)
# arcsin
mathematical_core("arcsin", context, lambda x: mx.sym.arcsin(x), lambda x: np.arcsin(x),
lambda x: 1. / (1. - x ** 2) ** (1. / 2.), 0.5, 0.5)
# arccos
mathematical_core("arccos", context, lambda x: mx.sym.arccos(x), lambda x: np.arccos(x),
lambda x: -1. / (1. - x ** 2.) ** (1. / 2.), 0.5, 0.5)
# arctan
mathematical_core("arctan", context, lambda x: mx.sym.arctan(x), lambda x: np.arctan(x),
lambda x: 1. / (x ** 2. + 1.), 0.5, 0.5)
# hypot
mathematical_core_binary("hypot", context,
lambda x, y: mx.sym.hypot(x, y),
lambda x, y: np.hypot(x, y),
lambda x, y: x / np.hypot(x, y),
lambda x, y: y / np.hypot(x, y),
0.5, 0.5, 0.5)

# hypot scalar
mathematical_core("hypot scalar", context,
lambda x: mx.sym.hypot(x, 3),
lambda x: np.hypot(x, 3),
lambda x: x / np.hypot(x, 3),
0.5, 0.5)

# degrees
mathematical_core("degrees", context,
lambda x: mx.sym.degrees(x),
lambda x: np.degrees(x),
lambda x: 180./np.pi,
0.5, 0.5)
# radians
mathematical_core("radians", context,
lambda x: mx.sym.radians(x),
lambda x: np.radians(x),
lambda x: np.pi / 180.,
0.6, 1)
# sinh
mathematical_core("sinh", context, lambda x: mx.sym.sinh(x), lambda x: np.sinh(x), lambda x: np.cosh(x))

# cosh
mathematical_core("cosh", context, lambda x: mx.sym.cosh(x), lambda x: np.cosh(x), lambda x: np.sinh(x), 5, 5)

# tanh
mathematical_core("tanh", context, lambda x: mx.sym.tanh(x), lambda x: np.tanh(x), lambda x: 1. - np.tanh(x) ** 2, 0.5, 1)

# arcsinh
mathematical_core("arcsinh", context, lambda x: mx.sym.arcsinh(x), lambda x: np.arcsinh(x),
lambda x: 1./(x**2 + 1.)**(1./2.))

# arccosh
mathematical_core("arccosh", context, lambda x: mx.sym.arccosh(x), lambda x: np.arccosh(x),
lambda x: 1./(x**2 - 1.)**(1./2.))

# arctanh
mathematical_core("arctanh", context, lambda x: mx.sym.arctanh(x), lambda x: np.arctanh(x),
lambda x: -1./(x**2 - 1.), 0.5)

# log1p
mathematical_core("log1p", context, lambda x: mx.sym.log1p(x), lambda x: np.log1p(x),
lambda x: 1. / (1.0 + x), 0.5, 0.5)
# expm1
mathematical_core("expm1", context, lambda x: mx.sym.expm1(x), lambda x: np.expm1(x),
lambda x: np.exp(x), 0.5, 0.5)
# log10
mathematical_core("log10", context, lambda x: mx.sym.log10(x), lambda x: np.log10(x),
lambda x: (1 / x))

# log2
mathematical_core("log2", context, lambda x: mx.sym.log2(x), lambda x: np.log2(x),
lambda x: (1 / x))

# rint
test_rounding("rint", context, lambda x: mx.sym.rint(x), lambda x: np.rint(x))

# fix
test_rounding("fix", context, lambda x: mx.sym.fix(x), lambda x: np.fix(x))

def test_special_functions_using_scipy(context):
def test_mathematical():

for context in [mx.cpu(), mx.gpu()]:
# rsqrt
mathematical_core("rsqrt", context,
lambda x: mx.sym.rsqrt(x),
lambda x: 1 / np.sqrt(x),
lambda x: -(1.0 / (2.0 * x * np.sqrt(x))))
# tan
mathematical_core("tan", context, lambda x: mx.sym.tan(x), lambda x: np.tan(x), lambda x: np.tan(x) ** 2 + 1)
# arcsin
mathematical_core("arcsin", context, lambda x: mx.sym.arcsin(x), lambda x: np.arcsin(x),
lambda x: 1. / (1. - x ** 2) ** (1. / 2.), 0.5, 0.5)
# arccos
mathematical_core("arccos", context, lambda x: mx.sym.arccos(x), lambda x: np.arccos(x),
lambda x: -1. / (1. - x ** 2.) ** (1. / 2.), 0.5, 0.5)
# arctan
mathematical_core("arctan", context, lambda x: mx.sym.arctan(x), lambda x: np.arctan(x),
lambda x: 1. / (x ** 2. + 1.), 0.5, 0.5)
# hypot
mathematical_core_binary("hypot", context,
lambda x, y: mx.sym.hypot(x, y),
lambda x, y: np.hypot(x, y),
lambda x, y: x / np.hypot(x, y),
lambda x, y: y / np.hypot(x, y),
0.5, 0.5, 0.5)

# hypot scalar
mathematical_core("hypot scalar", context,
lambda x: mx.sym.hypot(x, 3),
lambda x: np.hypot(x, 3),
lambda x: x / np.hypot(x, 3),
0.5, 0.5)

# degrees
mathematical_core("degrees", context,
lambda x: mx.sym.degrees(x),
lambda x: np.degrees(x),
lambda x: 180./np.pi,
0.5, 0.5)
# radians
mathematical_core("radians", context,
lambda x: mx.sym.radians(x),
lambda x: np.radians(x),
lambda x: np.pi / 180.,
0.6, 1)
# sinh
mathematical_core("sinh", context, lambda x: mx.sym.sinh(x), lambda x: np.sinh(x), lambda x: np.cosh(x))

# cosh
mathematical_core("cosh", context, lambda x: mx.sym.cosh(x), lambda x: np.cosh(x), lambda x: np.sinh(x), 5, 5)

# tanh
mathematical_core("tanh", context, lambda x: mx.sym.tanh(x), lambda x: np.tanh(x), lambda x: 1. - np.tanh(x) ** 2, 0.5, 1)

# arcsinh
mathematical_core("arcsinh", context, lambda x: mx.sym.arcsinh(x), lambda x: np.arcsinh(x),
lambda x: 1./(x**2 + 1.)**(1./2.))

# arccosh
mathematical_core("arccosh", context, lambda x: mx.sym.arccosh(x), lambda x: np.arccosh(x),
lambda x: 1./(x**2 - 1.)**(1./2.))

# arctanh
mathematical_core("arctanh", context, lambda x: mx.sym.arctanh(x), lambda x: np.arctanh(x),
lambda x: -1./(x**2 - 1.), 0.5)

# log1p
mathematical_core("log1p", context, lambda x: mx.sym.log1p(x), lambda x: np.log1p(x),
lambda x: 1. / (1.0 + x), 0.5, 0.5)
# expm1
mathematical_core("expm1", context, lambda x: mx.sym.expm1(x), lambda x: np.expm1(x),
lambda x: np.exp(x), 0.5, 0.5)
# log10
mathematical_core("log10", context, lambda x: mx.sym.log10(x), lambda x: np.log10(x),
lambda x: (1 / x))

# log2
mathematical_core("log2", context, lambda x: mx.sym.log2(x), lambda x: np.log2(x),
lambda x: (1 / x))

# rint
rounding("rint", context, lambda x: mx.sym.rint(x), lambda x: np.rint(x))

# fix
rounding("fix", context, lambda x: mx.sym.fix(x), lambda x: np.fix(x))

def test_special_functions_using_scipy():
try:
from scipy import special as scipy_special
except:
print("Could not import scipy. Skipping unit tests for special functions")
return

# gamma
mathematical_core("gamma", context, lambda x: mx.sym.gamma(x), lambda x: scipy_special.gamma(x),
lambda x: scipy_special.gamma(x) * scipy_special.psi(x), 0.5, 0.5)
for context in [mx.cpu(), mx.gpu()]:

# gammaln
mathematical_core("gammaln", context, lambda x: mx.sym.gammaln(x), lambda x: scipy_special.gammaln(x),
lambda x: scipy_special.psi(x), 0.5, 0.5)
# gamma
mathematical_core("gamma", context, lambda x: mx.sym.gamma(x), lambda x: scipy_special.gamma(x),
lambda x: scipy_special.gamma(x) * scipy_special.psi(x), 0.5, 0.5)

# gammaln
mathematical_core("gammaln", context, lambda x: mx.sym.gammaln(x), lambda x: scipy_special.gammaln(x),
lambda x: scipy_special.psi(x), 0.5, 0.5)


if __name__ == '__main__':
Expand Down Expand Up @@ -1691,6 +1695,5 @@ def test_special_functions_using_scipy(context):
test_support_vector_machine_l1_svm()
test_support_vector_machine_l2_svm()
test_roipooling()
for ctx in [mx.cpu(), mx.gpu()]:
test_mathematical(ctx)
test_special_functions_using_scipy(ctx)
test_mathematical()
test_special_functions_using_scipy()

0 comments on commit 06fcee9

Please sign in to comment.