def tan(x): try: return Dual(np.tan(x.val), x.der * (1 / np.cos(x.val))**2, (1 / np.cos(x.val)**2) * (x.der2 + 2 * x.der**2 * np.tan(x.val))) except: return np.tan(x)
def relu(x): try: u = np.maximum(0, x.val) v = np.where(u > 0, 1, 0) return Dual(u, x.der * v, 0) except: return np.maximum(0, x)
def test_sin(): assert (el.sin(3) == pytest.approx(0.1411, 0.001)) x = Dual(3, 3, 1) sinx = el.sin(x) assert (sinx.val == pytest.approx(0.1411, 0.001)) assert (sinx.der == pytest.approx(-2.9699, 0.001)) assert (sinx.der2 == pytest.approx(-2.260, 0.001))
def test_arctanh(): assert (el.arctanh(0.6) == pytest.approx(0.6931, 0.001)) x = Dual(0.5, 2, 3) arctanhx = el.arctanh(x) assert (arctanhx.val == pytest.approx(0.5493, 0.001)) assert (arctanhx.der == pytest.approx(1.333 * 2, 0.001)) assert (arctanhx.der2 == pytest.approx(11.11111, 0.001))
def test_exp2(): assert (el.exp2(0) == pytest.approx(1)) assert (el.exp2(4) == pytest.approx(16)) x = Dual(4, 4, 1) x2 = el.exp2(x) assert (x2.val == pytest.approx(16)) assert (x2.der == pytest.approx(44.361, 0.001))
def arctanh(x): try: return Dual(np.arctanh(x.val), -1 * x.der * 1 / (x.val**2 - 1), (-1 * x.val**2 * x.der2 + x.der2 + 2 * x.val * x.der**2) / (1 - x.val**2)**2) except: return np.arctanh(x)
def test_neg(): x = Dual(10, [1, 0], [1, 0]) z = -x assert(z.val == -10) assert(all(z.der == [-1, 0])) assert(all(z.der2 == [-1, 0]))
def test_log(): assert (el.log(27) == pytest.approx(3.2958, 0.001)) x = Dual(3, 5, 2) lnx = el.log(x) assert (lnx.val == pytest.approx(1.098, 0.001)) assert (lnx.der == pytest.approx(1.6666, 0.001)) assert (lnx.der2 == pytest.approx(-2.11111, 0.001))
def test_arccosh(): assert (el.arccosh(2) == pytest.approx(1.3169, 0.001)) x = Dual(3, 2, 1) arccoshx = el.arccosh(x) assert (arccoshx.val == pytest.approx(1.7627, 0.001)) assert (arccoshx.der == pytest.approx(0.3535 * 2, 0.001)) assert (arccoshx.der2 == pytest.approx(-0.17677, 0.001))
def test_log2(): assert (el.log2(8) == pytest.approx(3)) x = Dual(4, 8, 3) log2x = el.log2(x) assert (log2x.val == pytest.approx(2)) assert (log2x.der == pytest.approx(2.8853, 0.001)) assert (log2x.der2 == pytest.approx(-4.68875, 0.001))
def arcsinh(x): try: return Dual(np.arcsinh(x.val), x.der * 1 / np.sqrt(x.val**2 + 1), (x.val**2 * x.der2 + x.der2 - x.val * x.der**2) / (x.val**2 + 1)**(3 / 2)) except: return np.arcsinh(x)
def tanh(x): try: return Dual(np.tanh(x.val), x.der * 1 / (np.cosh(x.val)**2), (1 / np.cosh(x.val))**2 * (x.der2 - 2 * x.der**2 * np.tanh(x.val))) except: return np.tanh(x)
def arctan(x): try: return Dual(np.arctan(x.val), x.der * 1 / (x.val**2 + 1), ((x.val**2 + 1) * x.der2 - 2 * x.val * x.der**2) / (x.val**2 + 1)**2) except: return np.arctan(x)
def arcsin(x): try: return Dual(np.arcsin(x.val), x.der * 1 / (np.sqrt(1 - x.val**2)), (x.val * x.der**2 - (x.val**2 - 1) * x.der2) / (1 - x.val**2)**(3 / 2)) except: return np.arcsin(x)
def test_rmul(): x = 2 y = Dual(3, [0, 1], [0, 1]) z = x * y assert(z.val == 6) assert(all(z.der == [0, 2])) assert(all(z.der2 == [0, 2]))
def test_rsub(): x = 2 y = Dual(3, [0, 1], [0, 1]) z = x - y assert(z == -1) assert(all(z.der == [0, 1])) assert(all(z.der2 == [0, 1]))
def exp2(x): try: return Dual( np.exp2(x.val), np.exp2(x.val) * (x.der * np.log(2)), np.log(2) * np.exp2(x.val) * (x.der2 + np.log(2) * x.der**2)) except: return np.exp2(x)
def test_radd(): x = 2 y = Dual(3, [0, 1], [0, 1]) z = x + y assert(z.val == 5) assert(all(z.der == [0, 1])) assert(all(z.der2 == [0, 1]))
def arccos(x): try: return Dual( np.arccos(x.val), -1 * x.der * 1 / (np.sqrt(1 - x.val**2)), -1 * (x.val**2 * -1 * (x.der2) + x.der2 + x.val * x.der**2) / (1 - x.val**2)**(3 / 2)) except: return np.arccos(x)
def _eval(self, pos, wrt_variables = False): if isinstance(pos, collections.Iterable): if wrt_variables: params = [] for cursor, pos_i in enumerate(pos): der_partials = np.zeros(self.num_params) der_partials[cursor] = 1 params.append(Dual(pos_i, der_partials, np.zeros(self.num_params))) else: params = [Dual(pos_i,1,0) for pos_i in pos] return self.func(*params) else: if wrt_variables: params = Dual(pos,[1],[0]) else: params = Dual(pos,1,0) return self.func(params)
def test_exp(): assert (el.exp(0) == pytest.approx(1)) assert (el.exp(5)) == pytest.approx(148.413159, 0.0001) x = Dual(3, 4, 5) ex = el.exp(x) assert (ex.val == pytest.approx(20.0855369, 0.0001)) assert (ex.der == pytest.approx(80.3421476, 0.0001)) assert (ex.der2 == pytest.approx(421.7962, 0.001))
def arccosh(x): try: return Dual( np.arccosh(x.val), x.der * (1 / np.sqrt(x.val - 1) * 1 / (np.sqrt(x.val + 1))), (x.val**2 * x.der2 - x.der2 - x.val * x.der**2) / ((x.val - 1)**(3 / 2) * (x.val + 1)**(3 / 2))) except: return np.arccosh(x)
def _eval_func(func, args, der_attr): if isinstance(args, collections.Iterable): params = [] for cursor, arg in enumerate(args): der_partials = np.zeros(len(args)) der_partials[cursor] = 1 params.append(Dual(arg, der_partials, np.zeros(len(args)))) res = func(*params) if isinstance(res, collections.Iterable): return [getattr(i, der_attr) for i in res] else: return getattr(res, der_attr) else: args = Dual(args,[1],[0]) res = func(args) if isinstance(res, collections.Iterable): return [getattr(i, der_attr) for i in res] else: return getattr(res, der_attr)
def test_rpow(): x = 2 y = Dual(3, [0, 1], [0, 1]) z = x**y assert(z.val == 8) assert(z.der[0] == 0) assert(z.der[1] == pytest.approx(5.54517744)) assert(z.der2[0] == 0) assert(z.der2[1] == pytest.approx(9.38880156))
def test_rtruediv(): x = 6 y = Dual(3, [0, 1], [0, 1]) z = x / y assert(z.val == 2) assert(z.der[0] == 0) assert(z.der[1] == pytest.approx(-0.66666667)) assert(z.der2[0] == 0) assert(z.der2[1] == pytest.approx(-0.2222222))
def test_pow(): x = Dual(2, [1, 0], [1, 0]) y = Dual(3, [0, 1], [0, 1]) z1 = x**y assert(z1.val == 8) assert(z1.der[0] == 12) assert(z1.der[1] == pytest.approx(5.54517744)) assert(z1.der2[0] == 24) assert(z1.der2[1] == pytest.approx(9.38880156)) z2 = x**x assert(z2.val == 4) assert(z2.der[0] == pytest.approx(6.77258872)) assert(z2.der[1] == 0) assert(z2.der2[0] == pytest.approx(20.23957822)) assert(z2.der2[1] == 0)
def test_neq(): x = Dual(10, [1, 0], [1,0]) y = Dual(10, [0, 1], [1,0]) result = (x != y) assert(result == True)
def test_sigmoid(): assert (el.sigmoid(5) == pytest.approx(0.9933, 0.001)) x = Dual(2, 3, 1) sigmoidx = el.sigmoid(x) assert (sigmoidx.val == pytest.approx(0.8807, 0.001)) assert (sigmoidx.der == pytest.approx(0.10499 * 3, 0.001))
def test_repr(): x = Dual(10, [1, 0], [1, 0]) assert(repr(x) == '[10, [1 0], [1 0]]')
def test_eq(): x = Dual(10, [1, 0], [1,0]) y = Dual(10, [1, 0], [1,0]) result = (x == y) assert(result == True)