def test_expm1(): def grad_grad_op(x): return nd.exp(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, nd.expm1, grad_grad_op)
def test_log1p(): def grad_grad_op(x): return -1 / ((1 + x)**2) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, nd.log1p, grad_grad_op)
def test_square(): def grad_grad_op(x): return nd.ones_like(x) * 2 for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, nd.square, grad_grad_op)
def test_arctanh(): def arctanh(x): return nd.arctanh(x) def grad_grad_op(x): return (2 * x) / ((1 - x**2)**2) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, arctanh, grad_grad_op)
def test_cos(): def cos(x): return nd.cos(x) def grad_grad_op(x): return -nd.cos(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, cos, grad_grad_op)
def test_sinh(): def sinh(x): return nd.sinh(x) def grad_grad_op(x): return sinh(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, sinh, grad_grad_op)
def test_reciprocal(): def reciprocal(x): return nd.reciprocal(x) def grad_grad_op(x): return 2 / x**3 for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, reciprocal, grad_grad_op)
def test_log10(): def log10(x): return nd.log10(x) def grad_grad_op(x): return -1 / ((x**2) * math.log(10)) arrays = random_arrays((2, 2), (2, 3), (4, 5, 2), (3, 1, 4, 5)) for array in arrays: check_second_order_unary(array, log10, grad_grad_op)
def test_dropout(): def dropout(x): return nd.Dropout(x) def grad_grad_op(x): return nd.zeros_like(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, dropout, grad_grad_op)
def test_abs(): def abs(x): return nd.abs(x) def grad_grad_op(x): return nd.zeros_like(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, abs, grad_grad_op)
def test_log10(): def log10(x): return nd.log10(x) def grad_grad_op(x): return -1 / ((x**2) * math.log(10)) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, log10, grad_grad_op)
def test_arcsinh(): def arcsinh(x): return nd.arcsinh(x) def grad_grad_op(x): return x / nd.sqrt((nd.square(x) + 1)**3) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, arcsinh, grad_grad_op)
def test_clip(): def clip(x): a_min, a_max = sorted([random.random(), random.random()]) return nd.clip(x, a_min, a_max) def grad_grad_op(x): return nd.zeros_like(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, clip, grad_grad_op)
def test_arctan(): def arctan(x): return nd.arctan(x) def grad_grad_op(x): return (-2 * x) / ((1 + x**2)**2) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) # Domain of arctan is all real numbers. # Scale std_dev array *= random.randint(500, 10000) check_second_order_unary(array, arctan, grad_grad_op)
def test_tan(): def tan(x): return nd.tan(x) def grad_op(x): return 1 / nd.cos(x)**2 def grad_grad_op(x): return 2 * tan(x) * grad_op(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, tan, grad_grad_op)
def test_sigmoid(): def sigmoid(x): return nd.sigmoid(x) def grad_op(x): return sigmoid(x) * (1 - sigmoid(x)) def grad_grad_op(x): return grad_op(x) * (1 - 2 * sigmoid(x)) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, sigmoid, grad_grad_op)
def test_tanh(): def tanh(x): return nd.tanh(x) def grad_op(x): return 1 - tanh(x)**2 def grad_grad_op(x): return -2 * tanh(x) * grad_op(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_nth_order_unary(array, tanh, grad_op, 1, rtol=1e-6, atol=1e-6) check_second_order_unary( array, tanh, grad_grad_op, rtol=1e-6, atol=1e-5)
def test_arccos(): def arccos(x): return nd.arccos(x) def grad_grad_op(x): return -x / nd.sqrt((1 - x**2)**3) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) # Hack: Decrease std_dev to make # sure all elements # are in range -1 to 1 # i.e. Domain of arccos array *= 0.2 check_second_order_unary(array, arccos, grad_grad_op)
def test_log(): def log(x): return nd.log(x) def grad_op(x): return 1 / x def grad_grad_op(x): return -1 / (x**2) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, log, grad_grad_op) # TODO(kshitij12345): Remove check_nth_order_unary(array, log, [grad_op, grad_grad_op], [1, 2])
def test_cos(): def cos(x): return nd.cos(x) def grad_grad_op(x): return -nd.cos(x) def grad_grad_grad_op(x): return nd.sin(x) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, cos, grad_grad_op) # TODO(kshitij12345): Remove check_nth_order_unary(array, cos, [grad_grad_op, grad_grad_grad_op], [2, 3])
def test_rcbrt(): def rcbrt(x): return nd.rcbrt(x) def grad_grad_op(x): return 4 / (9 * nd.cbrt(x**7)) sigma = random.randint(25, 100) mu = random.randint(500, 1000) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) array = sigma * array + mu # Only positive numbers assert ((array > 0).all()) check_second_order_unary(array, rcbrt, grad_grad_op)
def test_sigmoid(): def sigmoid(x): return nd.sigmoid(x) def grad_op(x): return sigmoid(x) * (1 - sigmoid(x)) def grad_grad_op(x): return grad_op(x) * (1 - 2 * sigmoid(x)) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) check_second_order_unary(array, sigmoid, grad_grad_op) # TODO(kshitij12345): Remove check_nth_order_unary(array, sigmoid, [grad_op, grad_grad_op], [1, 2]) check_nth_order_unary(array, sigmoid, grad_grad_op, 2)
def test_arccosh(): def arccosh(x): return nd.arccosh(x) def grad_grad_op(x): return x / (nd.sqrt(x - 1) * nd.sqrt(x + 1) * (x + 1) * (x - 1)) sigma = random.randint(25, 100) mu = random.randint(500, 1000) for dim in range(1, 5): shape = rand_shape_nd(dim) array = random_arrays(shape) array = array * sigma + mu # Domain of arccosh 1 to infinity. assert ((array > 1).all()) check_second_order_unary(array, arccosh, grad_grad_op)