예제 #1
0
def test_rev_mul():
    x = Rev_Var(0.5)
    y = Rev_Var(4.2)
    z = x * y
    z.grad_value = 1.0
    assert z.value == 2.1
    assert x.grad() == pytest.approx(y.value)
예제 #2
0
def test_rev_add():
    x = Rev_Var(0.5)
    y = Rev_Var(4.2)
    z = x + y
    z.grad_value = 1.0
    assert z.value == 4.7
    assert y.grad() == 1
예제 #3
0
def test_truediv():
    x = Rev_Var(0.5)
    y = Rev_Var(4.2)
    z = x / y
    z.grad_value = 1.0
    assert z.value == pytest.approx(0.11904761904761904)
    assert x.grad() == pytest.approx(0.23809523809523808)
    assert y.grad() == pytest.approx(-0.028344671201814057)
예제 #4
0
def test_rev_grad():
    x = Rev_Var(0.5)
    y = Rev_Var(4.2)
    z = x * y + Rev_Var.sin(x)
    z.grad_value = 1.0

    assert z.value == pytest.approx(2.579425538604203)
    assert x.grad() == pytest.approx(4.2 + np.cos(x.value))
    assert y.grad() == pytest.approx(0.5)
예제 #5
0
def test_rev_sub():
    x = Rev_Var(0.5)
    y = Rev_Var(4.2)
    a = 3

    z1 = y - x
    z1.grad_value = 1.0
    assert z1.value == 3.7
    assert x.grad() == -1
    assert y.grad() == 1

    z2 = y - 3
    assert z2.value == pytest.approx(1.2)
예제 #6
0
def test_rev_pow():
    x1 = Rev_Var(0.5)
    x2 = Rev_Var(0.5)
    y = Rev_Var(4.2)
    a = 3
    z1 = x1**y
    z1.grad_value = 1
    z2 = x2**3
    z2.grad_value = 1

    assert z1.value == pytest.approx(0.05440941020600775)
    assert x1.grad() == pytest.approx(0.4570390457304651)
    assert y.grad() == pytest.approx(-0.03771372928022378)
    assert z2.value == pytest.approx(0.125)
    assert x2.grad() == pytest.approx(0.75)
예제 #7
0
def test_rtruediv():
    x = Rev_Var(0.5)
    y = 5
    z = x / y
    z.grad_value = 1.0
    assert z.value == 0.1
    assert x.grad() == 0.2
예제 #8
0
def test_rev_rpow():
    x = Rev_Var(0.5)
    a = 3
    z = 3**x
    z.grad_value = 1
    assert z.value == pytest.approx(1.7320508075688772)
    assert x.grad() == pytest.approx(1.902852301792692)
예제 #9
0
def test_cosh():
    x = Rev_Var(2.0)
    z = Rev_Var.cosh(x)
    z.grad_value = 1
    z3 = Rev_Var.cosh(2)
    assert z.value == pytest.approx(3.7621956910836314)
    assert z3 == pytest.approx(3.7621956910836314)
    assert x.grad() == pytest.approx(3.626860407847019)
예제 #10
0
def test_logistic():
    x = Rev_Var(3.0)
    z = Rev_Var.logistic(x)
    z.grad_value = 1
    z2 = Rev_Var.logistic(3.0)
    assert z.value == pytest.approx(1 / (1 + np.exp(-3)))
    assert x.grad() == pytest.approx(np.exp(3) / ((1 + np.exp(3))**2))
    assert z2 == 1 / (1 + np.exp(-3.0))
예제 #11
0
def test_sqrt():
    x = Rev_Var(2.0)
    z1 = Rev_Var.sqrt(x)
    z1.grad_value = 1
    z2 = Rev_Var.sqrt(2)
    assert z1.value == pytest.approx(1.4142135623730951)
    assert x.grad() == pytest.approx(0.3535533905932738)
    assert z2 == np.sqrt(2)
예제 #12
0
def test_tahh():
    x = Rev_Var(2.0)
    z = Rev_Var.tanh(x)
    z.grad_value = 1
    z4 = Rev_Var.tanh(2)
    assert z.value == pytest.approx(0.964027580075817)
    assert z4 == (np.exp(2) - np.exp(-2)) / (np.exp(2) + np.exp(-2))
    assert x.grad() == pytest.approx(0.07065082485316432)
예제 #13
0
def test_arcsin():
    x = Rev_Var(0.5)
    y = 0.3
    z = Rev_Var.arcsin(x)
    z.grad_value = 1
    assert z.value == pytest.approx(0.5235987755982988)
    assert x.grad() == pytest.approx(1.1547005383792517)
    assert Rev_Var.arcsin(y) == np.arcsin(y)
예제 #14
0
def test_cos():
    x = Rev_Var(2.0)
    y = 2
    z = Rev_Var.cos(x)
    z.grad_value = 1
    assert z.value == pytest.approx(-0.4161468365471424)
    assert x.grad() == pytest.approx(-0.9092974268256817)
    assert Rev_Var.cos(y) == np.cos(y)
예제 #15
0
def test_logk():
    x = Rev_Var(2.0)
    z2 = Rev_Var.logk(x, 3.0)
    z2.grad_value = 1
    z3 = Rev_Var.logk(2.0, 3.0)
    assert z2.value == np.log(2) / np.log(3)
    assert x.grad() == pytest.approx(0.45511961331341866)
    assert z3 == np.log(2) / np.log(3)
예제 #16
0
def test_arctan():
    x = Rev_Var(0.5)
    y = 0.25
    z = Rev_Var.arctan(x)
    z.grad_value = 1
    assert z.value == pytest.approx(0.46364760900080615)
    assert x.grad() == pytest.approx(0.8)
    assert Rev_Var.arctan(y) == np.arctan(y)
예제 #17
0
def test_arccos():
    x = Rev_Var(0.5)
    y = 0.3
    z = Rev_Var.arccos(x)
    z.grad_value = 1
    assert z.value == pytest.approx(1.0471975511965976)
    assert x.grad() == pytest.approx(-1.1547005383792517)
    assert Rev_Var.arccos(y) == np.arccos(y)
예제 #18
0
def test_tan():
    x = Rev_Var(2.0)
    y = 2
    z = Rev_Var.tan(x)
    z.grad_value = 1
    assert z.value == pytest.approx(-2.185039863261519)
    assert x.grad() == pytest.approx(5.774399204041917)
    assert Rev_Var.tan(y) == np.tan(y)
예제 #19
0
def test_log():
    x = Rev_Var(2.0)
    z1 = Rev_Var.log(x)
    z1.grad_value = 1
    z2 = Rev_Var.log(2.0)
    assert z1.value == pytest.approx(0.6931471805599453)
    assert z1.value == np.log(2)
    assert x.grad() == pytest.approx(0.5)
    assert z2 == np.log(2)
예제 #20
0
def test_exp():
    x = Rev_Var(2.0)
    z1 = Rev_Var.exp(x)
    y = 5
    z1.grad_value = 1
    z2 = Rev_Var.exp(y)
    assert z1.value == pytest.approx(7.38905609893065)
    assert z2 == pytest.approx(148.4131591025766)
    assert x.grad() == pytest.approx(7.38905609893065)
예제 #21
0
    def __init__(self, vals, ders, mode=AD_Mode.FORWARD):
        """
        init the AD class
        INPUT
        =======
        self: an AD class
        vals: a list of initial value for a list of variables
        ders: a list of initial derivative for a list of variables
        mode: the mode for auto-diff, ie, FORWARD or REVERSE
        
        RETURNS
        =======
        the output of automatic differentiation for the given vals and ders
        
        EXAMPLES
        =======
        >>> ad = AD(np.array([2, 2]), np.array([1, 1]), AD_Mode.FORWARD)
        >>> print(vars(ad.vars[0]), vars(ad.vars[1]))
        {'val': 2, 'der': array([1, 0])} {'val': 2, 'der': array([0, 1])}
        >>> ad = AD(np.array([2, 2]), np.array([1, 1]), AD_Mode.REVERSE)
        >>> print(vars(ad.vars[0]), vars(ad.vars[1]))
        {'value': 2, 'children': [], 'grad_value': None} {'value': 2, 'children': [], 'grad_value': None}
        """
        self.mode = mode
        if self.mode == AD_Mode.FORWARD:
            assert (len(vals) == len(ders))

            self.vars = []
            dimen = len(vals)
            cnt = 0
            for val, der in zip(vals, ders):
                der_list = np.array([0 for i in range(dimen)])
                der_list[cnt] = der
                self.vars.append(Var(val, der_list))
                cnt += 1
        else:
            self.vals = vals
            self.vars = []
            for val in self.vals:
                self.vars.append(Rev_Var(val))
예제 #22
0
def test_expk():
    x = Rev_Var(2.0)
    z = Rev_Var.expk(3.0, x)
    z.grad_value = 1
    assert z.value == pytest.approx(8)
    assert x.grad() == pytest.approx(12)
예제 #23
0
def test_pos():
    x = Rev_Var(0.5)
    assert +x.value == x.value
    assert +x.value == 0.5
예제 #24
0
def test_rev_rsub():
    x = Rev_Var(0.5)
    a = 3
    z = a - x
    assert z.value == pytest.approx(2.5)
예제 #25
0
def test_equal():
    x = Rev_Var(0.5)
    z = Rev_Var(0.5)
    y = 2
    assert x == z
    assert (x == y) == False
예제 #26
0
def test_notequal():
    x = Rev_Var(0.5)
    y = 2
    z = Rev_Var(4)
    assert x != z
    assert (x != y) == True
예제 #27
0
def test_rev_rmul():
    x = 3
    y = Rev_Var(4.2)
    z = x * y
    assert z.value == pytest.approx(12.6)
예제 #28
0
def test_neg():
    x = Rev_Var(0.5)
    y = Rev_Var(-0.5)
    assert -x.value == -0.5
    assert x.value == -y.value
예제 #29
0
 def clear(self):
     if self.mode == AD_Mode.REVERSE:
         self.vars = []
         for val in self.vals:
             self.vars.append(Rev_Var(val))
예제 #30
0
def test_rev_real_add():
    x = Rev_Var(0.5)
    y = 4.2
    z = x + y
    assert z.value == x.value + y