コード例 #1
0
def testNonLinear(X, Y):
    sigmoide = Non_linear.Sigmoide()
    tanh = Non_linear.Tanh()
    coche1 = Linear(2, 5)
    coche2 = Linear(5, 1)
    mse = MSE()
    res_sigmoide = None
    loss = []
    maxIter = 100
    for _ in range(maxIter):
        #forward
        res_lin1 = coche1.forward(X)
        res_tanh = tanh.forward(res_lin1)
        res_lin2 = coche2.forward(res_tanh)
        res_sigmoide = sigmoide.forward(res_lin2)
        #loss
        res = np.array([[1 if res_sigmoide[i] > 0.5 else 0]
                        for i in range(len(res_sigmoide))])
        # res = np.array([res_sigmoide[i] > 0 for i in range(len(res_sigmoide))])
        # print("res",res.shape)
        loss.append(sum(mse.forward(Y.reshape(-1, 1), res)))
        #retro-propager
        res_mse = mse.backward(Y.reshape(-1, 1), res)
        # print("mse",res_mse.shape)
        delta_sig = sigmoide.backward_delta(res_lin2, res_mse)
        coche2.zero_grad()
        coche2.backward_update_gradient(res_tanh, delta_sig)
        coche2.update_parameters(0.05)

        delta_lin2 = coche2.backward_delta(X, delta_sig)
        delta_tanh = tanh.backward_delta(res_lin1, delta_lin2)
        coche1.zero_grad()
        coche1.backward_update_gradient(X, delta_tanh)
        coche1.update_parameters(0.05)
    return loss, "Nonlinear", maxIter
コード例 #2
0
def testlinear(datax, datay):
    ## Lineaire et MSE
    linear = Linear(10, 1)
    linear.zero_grad()
    mse = MSE()
    # print(datax,datay)
    l_mse = []
    for _ in range(100):
        res_lin = linear.forward(datax)
        res_mse = mse.forward(datay, res_lin)
        delta_mse = mse.backward(datay, res_lin)
        l_mse.append(sum(res_mse))
        linear.zero_grad()
        linear.backward_update_gradient(datax, delta_mse)
        linear.update_parameters()
    return l_mse, "Linear", 100