def bostondemo(wst = None):
        
    bostonData = sio.loadmat('./boston.mat')
    TRANSNAME='sigmoid'
    ROUNDS=200  #default 200
    ITER=50  #default 50
    STEPSIZE=0.01  #default 0.01
    
    xTr = bostonData['xTr']
    xTe = bostonData['xTe']
    
    if wst == None:
        wst = np.array([1,20,20,20,np.shape(xTr)[0]])
    #print(wst)
    
    
    [xTr,xTe, _, _] = preprocess(xTr,xTe)
    
    w = initweights(wst)
        
    plt.figure()
    
    yTr = np.array(bostonData['yTr'])
    yTe = np.array(bostonData['yTe'])
    
    # Sort X and Y
    itr = np.argsort(yTr).flatten();
    ite = np.argsort(yTe).flatten();
    xTr = xTr[:, itr]
    xTe = xTe[:, ite]
    yTr = yTr[:, itr]
    yTe = yTe[:, ite]
    
    xtr = np.arange(0,np.shape(yTr)[1])
    xte = np.arange(0,np.shape(yTe)[1])
    plt.subplot(1,3,1);plt.title('TRAIN')
    plt.plot(xtr, yTr.flatten(), 'r', linewidth=5)
    linePredTr = plt.plot(xtr, np.ones(len(xtr)), 'k.')
    plt.subplot(1,3,2);plt.title('TEST')
    plt.plot(xte,yTe.flatten(), 'r', linewidth=5)
    linePredTe = plt.plot(xte, np.ones(len(xte)), 'k.')
    errTr = []
    errTe = []
    f = lambda w: deepnet(w, xTr, yTr, wst, TRANSNAME)
    for i in range(ROUNDS):
        w = grdescent(f, w, STEPSIZE, ITER, 1e-8)
        predTr=deepnet(w,xTr,[],wst,TRANSNAME)
        predTe=deepnet(w,xTe,[],wst,TRANSNAME)
        errTr.append(np.sqrt(np.mean((predTr-yTr)**2)));
        errTe.append(np.sqrt(np.mean((predTe-yTe)**2)));
        linePredTr[0].set_data(xtr, predTr.flatten())
        linePredTe[0].set_data(xte, predTe.flatten())
        plt.subplot(1,3,3);plt.title('RMSE Errors')
        lineErrTr = plt.plot(errTr,'g', label = 'train')
        lineErrTe = plt.plot(errTe,'b', label = 'test')
        plt.legend(handles=[lineErrTr[0], lineErrTe[0]])
        plt.pause(0.05)
        
    plt.show()
    print('Lowest train score: {}'.format(np.min(errTr)));
    print('Lowest test score: {}'.format(np.min(errTe)));
Ejemplo n.º 2
0
def bostontest():
    bostonData = sio.loadmat('./boston.mat')
    #boston_secretData = sio.loadmat('./boston_secret.mat')

    with open('best_parameters.pickle', 'rb') as f:
        best_parameters = pickle.load(f)

    xTr = bostonData['xTr']
    yTr = bostonData['yTr']
    # xTe_secret = boston_secretData['xTe_secret']
    # yTe_secret = boston_secretData['yTe_secret']
    xTe_secret = bostonData['xTe']
    yTe_secret = bostonData['yTe']

    TRANSNAME = best_parameters['TRANSNAME']
    ROUNDS = best_parameters['ROUNDS']
    ITER = best_parameters['ITER']
    STEPSIZE = best_parameters['STEPSIZE']
    wst = best_parameters['wst'].astype(np.int)

    runs = 5
    times = np.zeros((1, runs))
    errors = np.zeros((1, runs))

    for r in range(runs):
        t1 = time.time()
        xTr, xTe_secret, _, _ = preprocess(xTr, xTe_secret)

        # Do training
        w = initweights(wst)
        err = []

        f = lambda w: deepnet(w, xTr, yTr, wst, TRANSNAME)
        for i in range(ROUNDS):
            w = grdescent(f, w, STEPSIZE, ITER, 1e-8)
            predTes = deepnet(w, xTe_secret, [], wst, TRANSNAME)
            err.append(np.sqrt(np.mean((predTes - yTe_secret)**2)))
        times[0, r] = time.time() - t1
        errors[0, r] = err[-1]

    final_time = np.min(times)
    final_err = np.min(errors)

    print(times)
    print(errors)

    if final_time > 500:
        time_score = 0
    elif final_time > 200:
        time_score = 1
    elif final_time > 100:
        time_score = 2
    elif final_time > 50:
        time_score = 3
    elif final_time > 30:
        time_score = 4
    elif final_time > 20:
        time_score = 5
    elif final_time > 15:
        time_score = 6
    elif final_time > 10:
        time_score = 7
    elif final_time > 5:
        time_score = 8
    elif final_time > 2:
        time_score = 9
    else:
        time_score = 10

    if final_err > 10:
        error_score = 0
    elif final_err > 4:
        error_score = 1
    elif final_err > 3.7:
        error_score = 2
    elif final_err > 3.5:
        error_score = 3
    elif final_err > 3.3:
        error_score = 4
    elif final_err > 3.2:
        error_score = 5
    elif final_err > 3.1:
        error_score = 6
    elif final_err > 3.0:
        error_score = 7
    elif final_err > 2.9:
        error_score = 8
    elif final_err > 2.8:
        error_score = 9
    else:
        error_score = 10
    return time_score, error_score