示例#1
0
def test_cosh():
    """Test of cosh method."""
    # Test for sin with Rnode objects

    x = Rnode(1.0)
    z = Elem.cosh(x)
    z.grad_value = 1.0
    try:
        assert z.value == np.cosh(x.value)
        assert x.grad() == np.sinh(x.value)

    except AssertionError as e:
        print(e)
    # Test for cosh with two Dual objects
    val = Dual(3, [4, 1])
    z = Elem.cosh(val)

    try:
        assert z.val == np.cosh(val.val)
        assert z.der[0] == np.sinh(val.val) * val.der[0]
        assert z.der[1] == np.sinh(val.val) * val.der[1]

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for cosh with int,
    x = 3
    fx = Elem.cosh(x)
    try:
        assert fx == np.cosh(x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#2
0
def test_tanh():
    """Test of tanh method."""
    # Test for sin with Rnode objects

    x = Rnode(1.0)
    z = Elem.tanh(x)
    z.grad_value = 1.0
    try:
        assert z.value == np.tanh(x.value)
        assert x.grad() == 1 / np.cosh(x.value)**2

    except AssertionError as e:
        print(e)
    # Test for tan with two Dual objects
    val = Dual(3, [4, 1])
    z = Elem.tanh(val)
    der = val.der / (np.cosh(val.val))**2

    try:
        assert z.val == np.tanh(val.val)
        assert np.all(z.der == der)

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for tanh with int,
    x = 3
    fx = Elem.tanh(x)
    try:
        assert fx == np.tanh(x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#3
0
def test_add():
    """Test of addition special method (__add__) of Rnode class."""
    # Test for addition with scalar Rnode object and float value
    x = Rnode(0.11)
    z = x**2 + x
    z.grad_value = 1.0

    try:
        assert z.value == x.value**2 + x.value
        assert x.grad() == sum(weight * var.grad()
                               for weight, var in x.children)
    except AssertionError as e:
        print(e)
示例#4
0
def relu6(
        x: Union[Rnode, Dual,
                 float]) -> Union[Rnode, Dual, float, List[float]]:
    """Calculates the output of the relu6 function on the input.

    Parameters:
    x : array_like, Rnode object, or Dual Object.

    Returns:
    y : array_like, Rnode object, or Dual Object. The output  of the relu6 function on each element of x.
    """
    try:

        a = float(max(0, x.value))
        b = np.where(0.0 < a < 6.0, 1, 0)
        if a > 6.0:  # clip output to a maximum of 6
            a = 6.0
        z = Rnode(a)
        x.children.append((b, z))
        return z
    except AttributeError:
        try:
            a = float(max(0, x.val))
            b = np.where(0.0 < a < 6.0, 1, 0)
            if a > 6.0:  # clip output to a maximum of 6
                a = 6.0
            return Dual(a, b * x.der)
        except AttributeError:
            return min(max(0, x), 6)
示例#5
0
def arccos(
        x: Union[Rnode, Dual,
                 float]) -> Union[Rnode, Dual, float, List[float]]:
    """Calculates the inverse cosine of the input.

    Parameters:
    x : array_like, Rnode object, or Dual Object.

    Returns:
    y : array_like, Rnode object, or Dual Object. The inverse cosine of each element of x.
    """
    try:
        z = Rnode(np.arccos(x.value))
        temp = 1 - x.value**2
        # print("temp is " + str(temp))
        if temp <= 0:
            raise ValueError('Domain of sqrt is {x >= 0}')

        x.children.append((-1 / np.sqrt(temp), z))
        return z
    except AttributeError:
        try:
            return Dual(np.arccos(x.val),
                        -1 / np.sqrt(1 - x.val**2) * np.asarray(x.der))
        except AttributeError:
            return np.arccos(x)
示例#6
0
def relu(
        x: Union[Rnode, Dual,
                 float]) -> Union[Rnode, Dual, float, List[float]]:
    """Calculates the output of the relu function on the input.

    Parameters:
    x : array_like, Rnode object, or Dual Object.

    Returns:
    y : array_like, Rnode object, or Dual Object. The output  of the relu function on each element of x.
    """
    try:

        a = max(0, x.value)
        b = np.where(a > 0, 1, 0)
        z = Rnode(a)
        x.children.append((b, z))
        return z
    except AttributeError:
        try:
            a = max(0, x.val)
            b = np.where(a > 0, 1, 0)
            return Dual(a, b * x.der)
        except AttributeError:
            return max(0, x)
示例#7
0
def test_relu6():
    """Test of relu6 method."""
    # Test for sin with Rnode objects

    x = Rnode(7.0)
    z = Elem.relu6(x)
    z.grad_value = 1.0

    a = max(0, x.value)
    b = np.where(0.0 < a < 6.0, 1, 0)
    if a > 6.0:  # clip output to a maximum of 6
        a = 6.0
    try:
        assert z.value == a
        assert x.grad() == b

    except AssertionError as e:
        print(e)
    # Test for relu6 with two Dual objects
    x = Dual(8, [4, 1])
    z = Elem.relu6(x)

    a = max(0, x.val)
    b = np.where(0.0 < a < 6.0, 1, 0)
    print(b)
    if a > 6:  # clip output to a maximum of 6
        a = 6
    result = Dual(a, b * x.der)

    try:
        assert z.val == result.val
        assert np.all(z.der == result.der)

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for tanh with int,
    x = 3
    fx = Elem.relu6(x)
    try:
        assert fx == min(max(0, x), 6)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#8
0
def test_pow():
    """Test of exponent special method (__pow__) of Rnode class."""
    # Test for exponent with scalar Rnode object and float value
    x = Rnode(0.11)
    z = x**2
    z.grad_value = 1.0

    try:
        assert z.value == x.value**2
        assert x.grad() == x.value**2 * np.log(x.value)
        # assert x.children == (x.value ** 2 * np.log(x.value), z)
    except AssertionError as e:
        print(e)


# Test for exponent with two scalar Rnode object
    x = Rnode(0.11)
    y = Rnode(0.2)
    z = x**y
    z.grad_value = 1.0

    try:
        assert z.value == x.value**y.value
        assert x.grad() == x.value**y.value * np.log(x.value)
    except AssertionError as e:
        print(e)
示例#9
0
def test_str():
    """Test of the string special method (__str__) of Rnode class."""
    # Test for string special method with scalar Rnode objects
    x = Rnode(1.0)
    try:
        assert str(x) == 'Reverse-mode Rnode Object ( Values: 1.0 )'
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#10
0
def test_rsub():
    """Test of reverse subtraction special method (__rsub__) of Rnode class."""
    # Test for reverse subtraction with scalar Rnode object and float value
    x = Rnode(0.5)
    z = 0.1 - x
    try:
        assert z.value == x.value - 0.1
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#11
0
def test_pos():
    """Test of the positive special method (__pos__) of Rnode class."""
    # Test for positive special method with scalar Rnode object
    x = Rnode(5)
    z = +x
    try:
        assert z.value == 1 * x.value
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#12
0
def test_rtruediv():
    """Test of the reverse division special method (__rtruediv__) of Rnode class."""
    # Test for reverse division with scalar Rnode object and float value
    x = Rnode(5.0)
    z = 1 / x
    try:
        assert z.value == 1 / x.value
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#13
0
def test_rmul():
    """Test of reverse multiplication special method (__add__) of Rnode class."""
    # Test for reverse multiplication with scalar Rnode object and float value
    x = Rnode(0.11)
    z = 0.5 * x

    try:
        assert z.value == x.value * 0.5
    except AssertionError as e:
        print(e)
示例#14
0
def test_neg():
    """Test of the negation special method (__neg__) of Rnode class."""
    # Test for negation with scalar Rnode object
    x = Rnode(5.0)
    z = -x
    try:
        assert z.value == -1 * x.value
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#15
0
def test_arcsin():
    """Test of arcsin method."""
    # Test for arcsin with Rnode objects

    x = Rnode(0.11)
    z = Elem.arcsin(x)
    z.grad_value = 1.0
    temp = 1 - x.value**2
    if temp <= 0:
        raise ValueError('Domain of sqrt is {x >= 0}')
    try:
        assert z.value == np.arcsin(x.value)
        assert x.grad() == 1 / np.sqrt(temp)
    except AssertionError as e:
        print(e)

    # Test for arcsin with invalid Rnode objects
    with pytest.raises(ValueError, match=r".* sqrt .*"):
        Elem.arcsin(Rnode(1.0))

    # Test for arcsin with two Dual objects
    # arsin() input (-1,1)
    x = Dual(0.2, [0.4, 0.1])
    z = Elem.arcsin(x)
    print(z)
    der = 1 / np.sqrt(1 - x.val**2) * np.asarray(x.der)
    try:
        assert z.val == np.arcsin(x.val)
        assert np.all(z.der == der)

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for arcsin with int
    x = 0.1
    fx = Elem.arcsin(x)
    try:
        assert fx == np.arcsin(x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#16
0
def test_radd():
    """Test of reverse addition special method (__add__) of Rnode class."""
    # Test for reverse addition with scalar Rnode object and float value
    x = Rnode(0.11)
    z = 0.5 + x
    z.grad_value = 1.0

    try:
        assert z.value == x.value + 0.5
    except AssertionError as e:
        print(e)
示例#17
0
def test_relu():
    """Test of relu method."""
    # Test for sin with Rnode objects

    x = Rnode(1.0)
    z = Elem.relu(x)
    z.grad_value = 1.0

    a = max(0, x.value)
    b = np.where(a > 0, 1, 0)
    try:
        assert z.value == a
        assert x.grad() == b

    except AssertionError as e:
        print(e)
    # Test for relu with two Dual objects
    x = Dual(3, [4, 1])
    z = Elem.relu(x)

    a = max(0, x.val)
    b = np.where(a > 0, 1, 0)
    result = Dual(a, b * x.der)

    try:
        assert z.val == result.val
        assert np.all(z.der == result.der)

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for tanh with int,
    x = 3
    fx = Elem.relu(x)
    try:
        assert fx == max(0, x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#18
0
def test_log2():
    """Test of log2 method."""
    # Test for sin with Rnode objects

    x = Rnode(1.0)
    z = Elem.log2(x)
    z.grad_value = 1.0
    try:
        assert z.value == np.log2(x.value)
        assert x.grad() == 1 / (x.value * np.log(2))

    except AssertionError as e:
        print(e)
    # Test for log2 with two Dual objects
    val1 = Dual(3, [4, 1])
    val2 = Dual(2, [3, 1])
    val = val1 * val2
    z = Elem.log2(val)

    try:
        assert z.val == np.log2(val.val)
        assert z.der[0] == 1 / (val.val * np.log(2)) * val.der[0]
        assert z.der[1] == 1 / (val.val * np.log(2)) * val.der[1]

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for log2 with invalid Dual objects
    with pytest.raises(ValueError, match=r".* logarithm .*"):
        Elem.log2(Dual(-1, [4, 1]))

    # Test for log2 with int,
    x = 3
    fx = Elem.log2(x)
    try:
        assert fx == np.log2(x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#19
0
def test_rpow():
    """Test of reversed exponent special method (__pow__) of Rnode class."""
    # Test for reversed exponent with scalar Rnode object and float value
    x = Rnode(0.11)
    z = 2**x
    z.grad_value = 1.0

    try:
        assert z.value == 2**x.value
        # assert x.grad() == x.value ** 2 * np.log(x.value)
    except AssertionError as e:
        print(e)
示例#20
0
def test_neq():
    """Test of the not equal special method (__neq__) of Rnode class."""
    # Test for not equal special method with scalar Rnode object and float value
    x = Rnode(2.0)
    try:
        assert (x != 2) == False
        assert (x != 1) == True
    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for equality special method with two scalar Dual object
    x = Rnode(2.0)
    y = Rnode(2.0)
    z = Rnode(1.0)
    try:
        assert (x != y) == False
        assert (x != z) == True
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#21
0
def test_mul():
    """Test of multiplication special method (__mul__) of Rnode class."""
    # Test for multiplication with scalar Rnode object and float value
    x = Rnode(0.11)
    y = Rnode(0.5)
    z = x * y

    try:
        assert z.value == x.value * y.value
        # assert x.grad() == sum(weight * var.grad()
        #                           for weight, var in x.children)
    except AssertionError as e:
        print(e)
    # Test for subtraction with scalar Rnode object and float value
    x = Rnode(0.5)
    z = x * 0.1
    try:
        assert z.value == x.value * 0.1
        # assert x.grad() == sum(weight * var.grad()
        #                           for weight, var in x.children)
    except AssertionError as e:
        print(e)
示例#22
0
def test_sin():
    """Test of sin method."""
    # Test for sin with Rnode objects

    x = Rnode(1.0)
    z = Elem.sin(x)
    z.grad_value = 1.0
    try:
        assert z.value == np.sin(x.value)
        assert x.grad() == np.cos(x.value)

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for sin with two Dual objects
    val1 = Dual(3, [4, 1])
    val2 = Dual(2, [3, 1])
    val = val1 + val2
    z = Elem.sin(val)

    try:
        assert z.val == np.sin(val.val)
        assert z.der[0] == np.cos(val.val) * val.der[0]
        assert z.der[1] == np.cos(val.val) * val.der[1]

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for sin with int
    x = 3
    fx = Elem.sin(x)
    try:
        assert fx == np.sin(x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#23
0
def test_lt():
    """Test of the less than special method (__lt__) of Rnode class."""
    # Test for less than special method with scalar Rnode object and float value
    x = Rnode(2.0)
    try:
        assert (x < 3) == True
        assert (x < 1) == False
    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for less than special method with two Rnode Dual object
    a = Rnode(2.0)
    b = Rnode(2.0)
    c = Rnode(1.0)
    d = Rnode(1.0)
    try:
        assert (a < b) == False
        assert (a < c) == False
        assert (d < a) == True
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#24
0
def test_truediv():
    """Test of the division special method (__truediv__) of Rnode class."""
    # Test for division with scalar Rnode object and float value
    x = Rnode(0.11)
    z = x / 4
    z.grad_value = 1.0

    try:
        assert z.value == x.value / 4
    except AssertionError as e:
        print(e)


# Test for division with scalar Rnode objects
    x = Rnode(0.11)
    y = Rnode(0.5)
    z = x / y
    z.grad_value = 1.0

    try:
        assert z.value == x.value / y.value
    except AssertionError as e:
        print(e)
示例#25
0
def test_gt():
    """Test of the greater than special method (__gt__) of Rnode class."""
    # Test for greater than special method with scalar Rnode object and float value
    x = Rnode(2.0)
    try:
        assert (x > 3) == False
        assert (x > 1) == True
    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for greater than special method with two scalar Rnode object
    a = Rnode(2.0)
    b = Rnode(2.0)
    c = Rnode(1.0)
    d = Rnode(1.0)
    try:
        assert (a > b) == False
        assert (a > c) == True
        assert (a > d) == True
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#26
0
def test_ge():
    """Test of the greater than or equal to special method (__ge__) of Rnode class."""
    # Test for greater than or equal to special method with scalar Rnode object and float value
    x = Rnode(2.0)
    try:
        assert (x >= 3) == False
        assert (x >= 1) == True
    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for greater than or equal to special method with two scalar Rnode object
    a = Rnode(2.0)
    b = Rnode(2.0)
    c = Rnode(1.0)
    d = Rnode(1.0)
    try:
        assert (a >= b) == True
        assert (a >= c) == True
        assert (d >= a) == False
    except AssertionError as e:
        print(e)
        raise AssertionError
示例#27
0
def test_sub():
    """Test of subtraction special method (__sub__) of Rnode class."""
    # Test for subtraction with Rnode object
    x = Rnode(0.11)
    y = Rnode(0.5)
    z = x - y
    z.grad_value = 1.0

    try:
        assert z.value == x.value - y.value
        # assert x.grad() == sum(weight * var.grad()
        #                           for weight, var in x.children)
    except AssertionError as e:
        print(e)
    # Test for subtraction with scalar Rnode object and float value
    x = Rnode(0.5)
    z = x - 0.1
    try:
        assert z.value == x.value - 0.1
        # assert x.grad() == sum(weight * var.grad()
        #                           for weight, var in x.children)
    except AssertionError as e:
        print(e)
示例#28
0
def test_logistic():
    """Test of logistic method."""
    # Test for sin with Rnode objects

    x = Rnode(1.0)
    z = Elem.logistic(x)
    z.grad_value = 1.0

    nominator = np.exp(x.value)
    denominator = (1 + np.exp(x.value))**2
    try:
        assert z.value == 1 / (1 + np.exp(-x.value))
        assert x.grad() == nominator / denominator

    except AssertionError as e:
        print(e)
    # Test for logistic with two Dual objects
    x = Dual(3, [4, 1])
    z = Elem.logistic(x)
    result = (1 / (1 + np.exp(-x.val)),
              np.exp(x.val) / ((1 + np.exp(x.val))**2))
    try:
        assert z == result

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for logistic with int
    x = 3
    fx = Elem.logistic(x)
    try:
        assert fx == 1 / (1 + np.exp(-x))

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#29
0
def test_sqrt():
    """Test of sqrt method."""
    # Test for sqrt with Rnode objects

    x = Rnode(1.0)
    z = Elem.sqrt(x)
    z.grad_value = 1.0

    try:
        assert z.value == x.value**0.5
        assert x.grad() == 0.5 * x.value**(-0.5)

    except AssertionError as e:
        print(e)
    # Test for sqrt with two Dual objects
    x = Dual(3, [4, 1])
    z = Elem.sqrt(x)
    result = x**0.5

    try:
        assert z.val == np.sqrt(x.val)
        assert z.der[0] == result.der[0]
        assert z.der[1] == result.der[1]

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for sqrt with double
    x = 3.0
    fx = Elem.sqrt(x)
    try:
        assert fx == np.sqrt(x)

    except AssertionError as e:
        print(e)
        raise AssertionError
示例#30
0
def test_arctan():
    """Test of arctan method."""
    # Test for arctan with Rnode objects

    x = Rnode(0.11)
    z = Elem.arctan(x)
    z.grad_value = 1.0

    try:
        assert z.value == np.arctan(x.value)
        assert x.grad() == 1 / (1 + x.value**2)
    except AssertionError as e:
        print(e)

    # Test for arctan with two Dual objects
    # arctan() input (-1,1)
    x = Dual(0.2, [0.4, 0.1])
    z = Elem.arctan(x)
    der = 1 / (1 + x.val**2) * np.asarray(x.der)
    try:
        assert z.val == np.arctan(x.val)
        assert np.all(z.der == der)

    except AssertionError as e:
        print(e)
        raise AssertionError

    # Test for arctan with int
    x = 0.1
    fx = Elem.arctan(x)
    try:
        assert fx == np.arctan(x)

    except AssertionError as e:
        print(e)
        raise AssertionError