backtracking

mail@pastecode.io avatar
unknown
python
2 years ago
967 B
2
Indexable
Never
import numpy as np
def steep_desc_backtracking(theta,alpha,c,p,tol,data):
    (n,m)=data.shape
    (n1) = theta.shape
    theta31 = np.copy(theta)
    #alpha1w = alpha
    #alpha1b = alpha
    w1 = theta31[0:2]
    b1 = theta31[2]
    nitmax = 100000
    err=1
    nit=0#number of iterartion
    while err > tol and nit < nitmax:
        nit=nit+1
        theta30 = np.copy(theta31)
        w0 = np.copy(w1)
        b0 = np.copy(b1)
        w1 = np.copy(w0)
        b1 = np.copy(b0)
        for k in range(n):
            yk = neuron(w0,b0,data[k,0]) 
            w1 = w1 - c*alpha*(yk-data[k,1])*np.asarray(data[k,0])*(1-yk**2)/2.0
            b1 = b1 - c*alpha*(yk-data[k,1])*(1-yk**2)/2.0
            # update alpha wrt the function
            alpha = p*alpha
            print("Alpha ",alpha)
        theta31[0:2] = w1
        theta31[2] = b1
        err =  np.max(abs(theta31-theta30)/(1+abs(theta30)))
      
    print('nit',nit,err,w1,b1)
    return theta31