Пример #1
0
def test_functions():
    value = random.randint(-100, 100)
    value2 = random.randint(1, 100)
    assert round(c.sin(value)) == round(math.sin(
        value)), "Sin function is not working, you studied this in 10.."
    assert round(c.cos(value)) == round(math.cos(
        value)), "Cos function is not working, you studied this in 10.."
    assert round(c.tan(value)) == round(math.tan(
        value)), "Tan function is not working, you studied this in 10.."
    assert round(c.tanh(value)) == round(math.tanh(
        value)), "Tanh function is not working, you studied this in 10.."
    assert round(c.sigmoid(value)) == round(
        (1 / (1 + math.exp(-value))
         )), "Sigmoid function is not working, this is the basic of DNN.."
    assert round(c.relu(value)) == round(max(
        0, value)), "Relu function is not working, this is the basic of DNN,,"
    assert round(c.log(value2)) == round(math.log(
        value2)), "Log function is not working, you studied this in 10.."
    assert round(c.e(value)) == round(math.exp(
        value)), "Exp function is not working, you studied this in 10.."
    assert [
        0.0003282279149649954, 0.0024252944769113903, 0.01792063694632493,
        0.0008922159768423478, 0.9784336246849563
    ] == c.softmax([1, 3, 5, 2,
                    9]), "Softmax not working bro.. how will you make NN then"
def test_sigmoid():
    """
    Test the method sigmoid in the calculator package
    :return: None
    """
    output = calculator.sigmoid(0)
    assert output == 0.5
    output = derivatives.derivative_sigmoid(0)
    assert output == 0.25
Пример #3
0
def test_all_function_check():
    import calculator
    assert calculator.sin(math.pi / 2) == 1
    assert calculator.cos(math.pi) == -1
    assert calculator.tan(0) == 0
    assert calculator.tanh(0) == 0
    assert calculator.softmax([1,
                               2]) == [0.2689414213699951, 0.7310585786300049]
    assert calculator.e(0) == 1
    assert calculator.log(10, base=10) == 1
    assert calculator.relu(10) == 10
    assert calculator.relu(-10) == 0
    assert calculator.sigmoid(0) == 0.5
Пример #4
0
def test():

    abs_tol = 0.001
    rel_tol = 0.001

    test_value = 25

    # =================  testing sin =====================

    assert math.isclose(
        calculator.sin(test_value), -0.132, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.sin(test_value)}',
                           f'expected : {-0.132}')

    assert math.isclose(
        derivatives.sin(test_value), 0.991, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.sin(test_value)}',
                           f'expected : {0.991}')

    # =================  testing cosine =====================

    assert math.isclose(
        calculator.cos(test_value), 0.991, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.cos(test_value)}',
                           f'expected : {0.991}')

    assert math.isclose(
        derivatives.cos(test_value), 0.132, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.cos(test_value)}',
                           f'expected : {0.132}')

    # =================  testing exponential =====================

    assert math.isclose(
        calculator.exp(test_value),
        72004899337,
        rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.exp(test_value)}',
                           f'expected : {72004899337}')

    assert math.isclose(
        derivatives.exp(test_value),
        72004899337,
        rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.exp(test_value)}',
                           f'expected : {72004899337}')

    # =================  testing log =====================
    assert math.isclose(
        calculator.log(test_value), 3.218, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.log(test_value)}',
                           f'expected : {3.218}')

    assert math.isclose(
        derivatives.log(test_value), 0.04, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.log(test_value)}',
                           f'expected : {0.04}')
    # =================  testing relu =====================
    assert math.isclose(
        calculator.relu(test_value), 25, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.relu(test_value)}',
                           f'expected : {25}')

    assert math.isclose(
        derivatives.relu(test_value), 1, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.relu(test_value)}',
                           f'expected : {1}')

    # =================  testing sigmoid =====================

    assert math.isclose(
        calculator.sigmoid(test_value),
        0.999,
        rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.sigmoid(test_value)}',
                           f'expected : {0.999}')

    assert math.isclose(
        derivatives.sigmoid(test_value),
        1.388794386457827062508E-11,
        rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.sigmoid(test_value)}',
                           f'expected : {1.388794386457827062508E-11}')

    # =================  testing Softmax =====================

    assert math.isclose(
        calculator.softmax(test_value), 1, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.softmax(test_value)}',
                           f'expected : {1}')

    # assert math.isclose(derivatives.softmax(test_value),  0.04 ,
    #                     rel_tol=abs_tol,
    #                     abs_tol=abs_tol),(f'actual : {derivatives.softmax(test_value)}',
    #                     f'expected : {0.04}' )

    # =================  testing tan =====================

    assert math.isclose(
        calculator.tan(test_value), -0.133, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {calculator.tan(test_value)}',
                           f'expected : {-0.133}')

    assert math.isclose(
        derivatives.tan(test_value), 2.017, rel_tol=abs_tol,
        abs_tol=abs_tol), (f'actual : {derivatives.tan(test_value)}',
                           f'expected : {2.017}')
Пример #5
0
def test_sigmoid_import():
    sigmoid_zero = calculator.sigmoid(0)
    assert sigmoid_zero == 0.5, "sigmoid function in calculor has wrong implementation"
def test_function_dsigmoid():
    assert derivatives.dsigmoid(calculator.sigmoid(num)) == calculator.sigmoid(num)*(1-calculator.sigmoid(num)), 'dSigmoid implementaion failed'
def test_function_sigmoid():
    assert calculator.sigmoid(0) == 0.5, 'Sigmoid implementaion failed'
Пример #8
0
def test_sigmoid():
    assert calc.sigmoid(0) == 0.5, 'Check sigmoid function'
def test_type_error():

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.sin("60")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.cos("-45")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.tan("4.5")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.tanh("50")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.relu("1.3")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.sigmoid("2.4")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.euler("4")

    with pytest.raises(TypeError, match=r".*invalid type*"):
        calculator.softmax([10, "2", "3"])

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.log("5", 10)

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        calculator.log(3, "10")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_sin("45")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_cos("4.5")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_tan("45")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_tanh([1, 3])

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_euler("10")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_sigmoid("3")

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_relu("-5")

    with pytest.raises(TypeError, match=r".*invalid type*"):
        derivatives.d_softmax([20, "3", 5])

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_log("5", 20)

    with pytest.raises(TypeError, match=r".*Input value of invalid type*"):
        derivatives.d_log(5, "20")
def test_sigmoid_module():

    assert 0.6224593312018546 == calculator.sigmoid(
        0.5), "sigmoid_func module from cal package is not working as expected"
    assert 0.2350037122015945 == derivatives.d_sigmoid(
        0.5), "sigmoid_func module from cal package is not working as expected"
Пример #11
0
def Build():
    (dataTrain , dataTest , dataValid ) = lib_reader.getData()
    X_train, Y_train = dataTrain
    X_test, Y_test = dataTest
    X_valid, Y_valid = dataValid

    print("================= Data-set-information ===============")
    print(" Number of dataTrain samples : ",len(Y_train))
    print(" Number of dataTest  samples : ",len(Y_test))
    print(" Number of dataValid samples : ",len(Y_valid))
    print(" Number of features          : ",len(X_train[0]) )
    print(" Number of classes           :  2")
    print("======================================================")

    # init model :w1 , w2, nEpochs = ??
    alpha = 0.001
    hSize = 32
    np.random.seed(7)
    W1 = np.random.random((X_train.shape[1] , hSize)) #dxh 30x15
    W2 = np.random.random((hSize , 1))                #hx1 15x1
    nEpochs = 10000
    historyTrain = []
    historyValid = []
    historyTest  = []

    # ==========================================TRAINING=======================================
    for epoch in range(nEpochs):
        layer0 = X_train                                   # input_layer  30 features x 457 samples : 457x30
        layer1 = lib_calc.sigmoid ( np.dot(layer0 , W1))   # hidden_layer h = 15 neurals x n = 457 samples : 457x15
        layer2 = lib_calc.sigmoid ( np.dot(layer1 , W2))   # output_layer c = 1 value x  n = 457 samples   : 457x1

        # caculate loss + test --> plot
        if (epoch%100 == 0):
            print("Epoch ",epoch, ":  ==============")
            historyTrain.append( lib_calc.caculate_and_show("training-set   :", layer2, Y_train)[0])
            historyValid.append( lib_calc.caculate_and_show("validation-set :", getOutput(X_valid , W1,W2), Y_valid)[0])
            historyTest.append( lib_calc.caculate_and_show("test-set       :", getOutput(X_test  , W1,W2), Y_test)[0])
            print("========================")



        # update model
        layer2_error = layer2 - Y_train
        layer2_delta = layer2_error * lib_calc.sigmoid_derivative(layer2)
        W2_delta     = layer1.T.dot(layer2_delta)

        layer1_error = layer2_delta.dot(W2.T)
        layer1_delta = layer1_error * lib_calc.sigmoid_derivative(layer1)
        W1_delta     = layer0.T.dot(layer1_delta)

        W1 -= alpha * W1_delta
        W2 -= alpha * W2_delta

    # plot to graph
    plt.plot(historyTrain)
    plt.plot(historyTest)
    plt.plot(historyValid)
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test' , 'valid'], loc='upper left')
    plt.show()
Пример #12
0
def getOutput(X,W1,W2):
    layer0 = X  # input_layer  30 features x 457 samples : 457x30
    layer1 = lib_calc.sigmoid(np.dot(layer0, W1))  # hidden_layer h = 15 neurals x n = 457 samples : 457x15
    layer2 = lib_calc.sigmoid(np.dot(layer1, W2))
    return layer2
Пример #13
0
def test_sigmoid_len():
    assert calculator.sigmoid(mmatrix_single) == 'ERROR!!, more than one value is required'
Пример #14
0
def test_sigmoid():
    print(calculator.sigmoid(mmatrix))
Пример #15
0
def test_sigmoid():
    assert calc.sigmoid(7) == (1 / (1 + np.exp(-7)))
Пример #16
0
 def test_sigmoid(self):
     expect = calculator.sigmoid(np.array([0, 2]))
     self.assertEqual(0.5, expect[0])
     self.assertEqual(0.88079707797788231, expect[1])