def load_data(dataset):
    '''
    load the data from the mauna_loa set to create a GLM from
    '''
    if dataset == 'rosenbrock':
        x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('rosenbrock', n_train=1000, d=2)
    else:
        x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(str(dataset))
    return x_train, x_valid, x_test, y_train, y_valid, y_test
def load_data(dataset):
    '''
    param dataset: str, the name of the dataset to be loaded for this iteration of the model
    '''
    if dataset not in possible_datasets:
        return 0, 0, 0, 0, 0, 0
    elif dataset == 'rosenbrock':
        x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(
            'rosenbrock', n_train=1000, d=2)
    else:
        x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(
            str(dataset))
    return x_train, x_valid, x_test, y_train, y_valid, y_test
def load_data(dataset):
    '''
    load the data using the function from data_utils.py
    '''
    if dataset not in possible_datasets:
        return 0,0,0,0,0,0
    x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('mnist_small')
    return x_train, x_valid, x_test, y_train, y_valid, y_test
def load_data(dataset, d):
    '''
    load the rosenbrock data to test the vectorization
    '''
    if dataset in possible_datasets:
        x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(dataset, n_train=5000, d=d)
        return x_train, x_valid, x_test, y_train, y_valid, y_test
    else:
        return 0,0,0,0,0,0 
def run_example():
    """
    This example demonstrates computation of the negative log likelihood (nll) as
    well as the gradient of the nll with respect to all weights and biases of the
    neural network. We will use 50 neurons per hidden layer and will initialize all 
    weights and biases to zero.
    """
    # load the MNIST_small dataset
    x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('mnist_small')
    
    # initialize the weights and biases of the network
    M = 50 # 50 neurons per hidden layer
    W1 = np.zeros((M, 784)) # weights of first (hidden) layer
    W2 = np.zeros((M, M)) # weights of second (hidden) layer
    W3 = np.zeros((10, M)) # weights of third (output) layer
    b1 = np.zeros((M, 1)) # biases of first (hidden) layer
    b2 = np.zeros((M, 1)) # biases of second (hidden) layer
    b3 = np.zeros((10, 1)) # biases of third (output) layer
    
    # considering the first 250 points in the training set, 
    # compute the negative log likelihood and its gradients
    (nll, (W1_grad, W2_grad, W3_grad, b1_grad, b2_grad, b3_grad)) = \
        nll_gradients(W1, W2, W3, b1, b2, b3, x_train[:250], y_train[:250])
    print("negative log likelihood: %.5f" % nll)
Beispiel #6
0
logger = utils.get_logger(logpath=os.path.join(opt.log_dir, 'logs'),
                          filepath=__file__)
logger.info(opt)

# store cmd
cmd = utils.store_cmd(opt=opt)

# set seed
random.seed(opt.seed)
torch.manual_seed(opt.seed)
torch.cuda.manual_seed_all(opt.seed)
logger.info("[*] Random Seed: %d" % opt.seed)
# setups ends here

# setup datasets
train_data, test_data = data_utils.load_dataset(opt)
train_generator = data_utils.get_data_generator(train_data,
                                                train=True,
                                                opt=opt)
test_dl_generator = data_utils.get_data_generator(test_data,
                                                  train=False,
                                                  dynamic_length=True,
                                                  opt=opt)

if opt.dataset == 'h36m':
    from human36m import Skeleton3DVisualizer, STD_SCALE
    visualizer = Skeleton3DVisualizer(
        train_data.skeleton.parents(),
        plot_3d_limit=[-2 * STD_SCALE, 2 * STD_SCALE],
        show_joint=False,
        show_ticks=False,
Beispiel #7
0
def graph_test(x_test, y_test, y_predict):
    plt.figure()
    plt.plot(x_test, y_test, x_test, y_predict)
    plt.xlabel('x_test')
    plt.ylabel('y_test')
    plt.title('Linear Regression Algorithm Mauna Loa Test Set Prediction')
    plt.legend(['Ground Truth', 'Prediction'])
    plt.grid()
    plt.show()

if __name__ == "__main__":
    print('Linear Regression Algorithm\n\nPerforming prediction on test set by minimizing the least-squares loss function using SVD:')
    for data in datasets:
        if data == 'mauna_loa' or data == 'rosenbrock' or data == 'pumadyn32nm':
            if data == 'rosenbrock':
                x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(data, n_train=5000, d=2)
            else:
                x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(data)
            type = 0 #type = 0 for regression datasets
        else:
            x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(data)
            type = 1 #type = 1 for classification datasets
        x_train = np.vstack([x_valid, x_train])
        y_train = np.vstack([y_valid, y_train])
        if not type:
            y_predict = linear_regression(x_train, y_train, x_test, type)
            rmse = mean_squared_error(y_test, y_predict, squared=False)
            print('The test RMSE value for the', data, 'dataset is', rmse)
            if data == 'mauna_loa':
                graph_test(x_test, y_test, y_predict)
        else:
def load_data():
    '''
    load the iris dataset using the specified method
    '''
    x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('iris')
    return x_train, x_valid, x_test, y_train, y_valid, y_test
Beispiel #9
0
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
import sys
sys.path.append('../')
from data.data_utils import load_dataset

x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('mauna_loa')
# x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('rosenbrock', n_train=1000, d=2)
# x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset('pumadyn32nm')
x_train = np.vstack([x_valid, x_train])
y_train = np.vstack([y_valid, y_train])
np.random.seed(5)
np.random.shuffle(x_train)
np.random.seed(5)
np.random.shuffle(y_train)

def l1_dist(x_train, x_test):
    return np.sum(np.abs(x_train-x_test), axis=1)

def l2_dist(x_train, x_test):
    return np.sqrt(np.sum(np.square(x_train-x_test), axis=1))

def knn_regressor(x_train, y_train, x_test, k):
    dist = l2_dist(x_train, x_test) # Calculate Distance
    i_nn = np.argpartition(dist, kth=k)[:k] # Obtain Indices of Nearest Neighbours
    y_test = np.average(y_train[i_nn]) # Calculate Predicted Value by Taking Average of Neighbours
    return y_test

def knn_validate(x_train, y_train, k, v):
Beispiel #10
0
    plt.ylabel('k-NN Regression Run-Time (s)')
    plt.title('k-NN Regression Algorithm Run-Time Performance')
    plt.legend(['Brute-Force', 'K-D Tree'])
    plt.grid()
    plt.show()


if __name__ == "__main__":
    print(
        'k-NN Regression Algorithm using the K-D Tree Data Structure\n\nPerforming prediction on rosenbrock test set using chosen k value of 5:'
    )
    elapsed_bf = []
    elapsed_kdt = []
    d = range(2, 33)
    for i in d:
        x_train, x_valid, x_test, y_train, y_valid, y_test = load_dataset(
            'rosenbrock', n_train=5000, d=i)
        y_predict_bf = []
        print('Running the brute-force approach for d =', i, '...')
        start = time()
        for j in range(len(x_test)):
            y_predict_bf.append(
                knn_regression_bf(x_train, y_train, x_test[j], 5))
        finish = time()
        elapsed_bf.append(finish - start)
        print('Running the K-D tree algorithm for d =', i, '...')
        start = time()
        y_predict_kdt = knn_regression_kdt(x_train, y_train, x_test, 5)
        finish = time()
        elapsed_kdt.append(finish - start)
    graph_time(d, elapsed_bf, elapsed_kdt)