def test_gp(plot=False, method='full'): """ Compares model prediction with an exact GP (without optimisation) """ # note that this test fails without latent noise in the case of full Gaussian np.random.seed(111) num_input_samples = 10 num_samples = 10000 gaussian_sigma = .2 X, Y, kernel = DataSource.normal_generate_samples(num_input_samples, gaussian_sigma, 1) kernel = [GPy.kern.RBF(1, variance=1., lengthscale=np.array((1.,)))] if method == 'full': m = SAVIGP_SingleComponent(X, Y, num_input_samples, UnivariateGaussian(np.array(gaussian_sigma)), kernel, num_samples, None, 0.001, True, True) if method == 'diag': m = SAVIGP_Diag(X, Y, num_input_samples, 1, UnivariateGaussian(np.array(gaussian_sigma)), kernel, num_samples, None, 0.001, True, True) # update model using optimal parameters # gp = SAVIGP_Test.gpy_prediction(X, Y, gaussian_sigma, kernel[0]) # gp_mean, gp_var = gp.predict(X, full_cov=True) # m.MoG.m[0,0] = gp_mean[:,0] # m.MoG.update_covariance(0, gp_var - gaussian_sigma * np.eye(10)) try: folder_name = 'test' + '_' + ModelLearn.get_ID() logger = ModelLearn.get_logger(folder_name, logging.DEBUG) Optimizer.optimize_model(m, 10000, logger, ['mog']) except KeyboardInterrupt: pass sa_mean, sa_var = m.predict(X) gp = SAVIGP_Test.gpy_prediction(X, Y, gaussian_sigma, deepcopy(kernel[0])) gp_mean, gp_var = gp.predict(X) mean_error = (np.abs(sa_mean - gp_mean)).sum() / sa_mean.shape[0] var_error = (np.abs(sa_var - gp_var)).sum() / gp_var.T.shape[0] if mean_error < 0.1: print bcolors.OKBLUE, "passed: mean gp prediction ", mean_error else: print bcolors.WARNING, "failed: mean gp prediction ", mean_error print bcolors.ENDC if var_error < 0.1: print bcolors.OKBLUE, "passed: var gp prediction ", var_error else: print bcolors.WARNING, "failed: var gp prediction ", var_error print bcolors.ENDC if plot: plot_fit(m) gp.plot() show(block=True)
def creep_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.creep_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'creep' scaler = preprocessing.StandardScaler().fit(Xtrain) Xtrain = scaler.transform(Xtrain) Xtest = scaler.transform(Xtest) kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, True) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = WarpLL(np.array([3.8715, 3.8898, 2.8759]), np.array([1.5925, -1.3360, -2.0289]), np.array([0.7940, -4.1855, -3.0289]), np.log(0.01)) names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp', 'll'], MinTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 25, 'hyp': 25, 'll': 25}, max_iter=200))
def boston_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.boston_data() d = data[config['run_id'] - 1] names = [] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'boston' kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, True) # gaussian_sigma = np.var(Ytrain)/4 + 1e-4 gaussian_sigma = 1.0 # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) cond_ll = UnivariateGaussian(np.array(gaussian_sigma)) num_samples = 2000 names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['hyp', 'mog', 'll'], MeanTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 25, 'hyp': 25, 'll': 25}, max_iter=200)) return names
def wisconsin_breast_cancer_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.wisconsin_breast_cancer_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'breast_cancer' # uncomment these lines to use softmax # kernel = Experiments.get_kernels(Xtrain.shape[1], 2, False) # Ytrain = np.array([(Ytrain[:,0] + 1) / 2, (-Ytrain[:,0] + 1) / 2]).T # Ytest = np.array([(Ytest[:,0] + 1) / 2, (-Ytest[:,0] + 1) / 2]).T # cond_ll = SoftmaxLL(2) # uncomment these lines to use logistic cond_ll = LogisticLL() kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp'], IdentityTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 25, 'hyp': 25, 'll': 25}, max_iter=200)) return names
def mining_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.mining_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'mining' kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = LogGaussianCox(math.log(191. / 811)) kernel[0].variance = 1.0 kernel[0].lengthscale = 13516. names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog'], IdentityTransformation, True, config['log_level'], True, latent_noise=0.001, opt_per_iter={'mog': 15000}, max_iter=1)) return names
def abalone_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.abalone_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'abalone' kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = WarpLL(np.array([-2.0485, 1.7991, 1.5814]), np.array([2.7421, 0.9426, 1.7804]), np.array([0.1856, 0.7024, -0.7421]), np.log(0.1)) names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp', 'll'], MinTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 25, 'hyp': 25, 'll': 25}, max_iter=200))
def wisconsin_breast_cancer_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.wisconsin_breast_cancer_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'breast_cancer' # uncomment these lines to use softmax # kernel = Experiments.get_kernels(Xtrain.shape[1], 2, False) # Ytrain = np.array([(Ytrain[:,0] + 1) / 2, (-Ytrain[:,0] + 1) / 2]).T # Ytest = np.array([(Ytest[:,0] + 1) / 2, (-Ytest[:,0] + 1) / 2]).T # cond_ll = SoftmaxLL(2) # uncomment these lines to use logistic cond_ll = LogisticLL() kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp'], IdentityTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 25, 'hyp': 25, 'll': 25 }, max_iter=200)) return names
def sarcos_all_joints_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.sarcos_all_joints_data() names = [] d = data[0] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'sarcos_all_joints' scaler = preprocessing.StandardScaler().fit(Xtrain) Xtrain = scaler.transform(Xtrain) Xtest = scaler.transform(Xtest) kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 8, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = CogLL(0.1, 7, 1) if 'n_thread' in config.keys(): n_threads = config['n_thread'] else: n_threads = 1 if 'partition_size' in config.keys(): partition_size = config['partition_size'] else: partition_size = 3000 image = None if 'image' in config.keys(): image = config['image'] names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'll', 'hyp'], MeanStdYTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 50, 'hyp': 10, 'll': 10}, max_iter=200, partition_size=partition_size, ftol=10, n_threads=n_threads, model_image_file=image))
def creep_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.creep_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'creep' scaler = preprocessing.StandardScaler().fit(Xtrain) Xtrain = scaler.transform(Xtrain) Xtest = scaler.transform(Xtest) kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, True) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = WarpLL(np.array([3.8715, 3.8898, 2.8759]), np.array([1.5925, -1.3360, -2.0289]), np.array([0.7940, -4.1855, -3.0289]), np.log(0.01)) names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp', 'll'], MinTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 25, 'hyp': 25, 'll': 25 }, max_iter=200))
def USPS_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.USPS_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'USPS' kernel = [ ExtRBF(Xtrain.shape[1], variance=2, lengthscale=np.array((4., )), ARD=False) for j in range(3) ] # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = SoftmaxLL(3) names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp'], IdentityTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 25, 'hyp': 25, 'll': 25 }, max_iter=300))
def abalone_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.abalone_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'abalone' kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = WarpLL(np.array([-2.0485, 1.7991, 1.5814]), np.array([2.7421, 0.9426, 1.7804]), np.array([0.1856, 0.7024, -0.7421]), np.log(0.1)) names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp', 'll'], MinTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 25, 'hyp': 25, 'll': 25 }, max_iter=200))
def mining_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.mining_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'mining' kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = LogGaussianCox(math.log(191. / 811)) kernel[0].variance = 1.0 kernel[0].lengthscale = 13516. names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog'], IdentityTransformation, True, config['log_level'], True, latent_noise=0.001, opt_per_iter={ 'mog': 15000, 'hyp': 25, 'll': 25 }, max_iter=1)) return names
def boston_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.boston_data() d = data[config['run_id'] - 1] names = [] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'boston' kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 1, True) # gaussian_sigma = np.var(Ytrain)/4 + 1e-4 gaussian_sigma = 1.0 # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) cond_ll = UnivariateGaussian(np.array(gaussian_sigma)) num_samples = 2000 names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['hyp', 'mog', 'll'], MeanTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 25, 'hyp': 25, 'll': 25 }, max_iter=200)) return names
def USPS_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.USPS_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'USPS' kernel = [ExtRBF(Xtrain.shape[1], variance=2, lengthscale=np.array((4.,)), ARD=False) for j in range(3)] # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = SoftmaxLL(3) names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp'], IdentityTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 25, 'hyp': 25, 'll': 25}, max_iter=300))
def test_model_learn(config): """ Compares the model output with exact GP """ method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) names = [] num_input_samples = 20 gaussian_sigma = .2 X, Y, kernel = DataSource.normal_generate_samples(num_input_samples, gaussian_sigma) train_n = int(0.5 * num_input_samples) Xtrain = X[:train_n, :] Ytrain = Y[:train_n, :] Xtest = X[train_n:, :] Ytest = Y[train_n:, :] kernel1 = ModelLearn.get_kernels(Xtrain.shape[1], 1, True) kernel2 = ModelLearn.get_kernels(Xtrain.shape[1], 1, True) gaussian_sigma = 1.0 #number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 10000 cond_ll = UnivariateGaussian(np.array(gaussian_sigma)) n1, _ = ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel1, method, 'test_' + ModelLearn.get_ID(), 'test', num_inducing, num_samples, sparsify_factor, ['mog', 'll', 'hyp'], IdentityTransformation, True, logging.DEBUG, True) n2, _ =ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel2, 'gp', 'test_' + ModelLearn.get_ID(), 'test', num_inducing, num_samples, sparsify_factor, ['mog', 'll', 'hyp'], IdentityTransformation) PlotOutput.plot_output('test', ModelLearn.get_output_path(), [n1, n2], None, False)
def sarcos_all_joints_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.sarcos_all_joints_data() names = [] d = data[0] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'sarcos_all_joints' scaler = preprocessing.StandardScaler().fit(Xtrain) Xtrain = scaler.transform(Xtrain) Xtest = scaler.transform(Xtest) kernel = ExperimentSetup.get_kernels(Xtrain.shape[1], 8, False) # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = CogLL(0.1, 7, 1) if 'n_thread' in config.keys(): n_threads = config['n_thread'] else: n_threads = 1 if 'partition_size' in config.keys(): partition_size = config['partition_size'] else: partition_size = 3000 image = None if 'image' in config.keys(): image = config['image'] names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'll', 'hyp'], MeanStdYTransformation, True, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 50, 'hyp': 10, 'll': 10 }, max_iter=200, partition_size=partition_size, ftol=10, n_threads=n_threads, model_image_file=image))
def MNIST_binary_inducing_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.mnist_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain_full = d['train_Y'] Xtest = d['test_X'] Ytest_full = d['test_Y'] name = 'mnist_binary' # uncomment these lines to change the resolution # res = 13 # current_res = int(np.sqrt(Xtrain.shape[1])) # X_train_resized = np.empty((Xtrain.shape[0], res * res)) # X_test_resized = np.empty((Xtest.shape[0], res * res)) # for n in range(Xtrain.shape[0]): # im = Image.fromarray(Xtrain[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_train_resized[n] = np.array(im).flatten() # # for n in range(Xtest.shape[0]): # im = Image.fromarray(Xtest[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_test_resized[n] = np.array(im).flatten() # # # Xtrain = X_train_resized # Xtest = X_test_resized Ytrain = np.apply_along_axis( lambda x: x[1:10:2].sum() - x[0:10:2].sum(), 1, Ytrain_full).astype(int)[:, np.newaxis] Ytest = np.apply_along_axis( lambda x: x[1:10:2].sum() - x[0:10:2].sum(), 1, Ytest_full).astype(int)[:, np.newaxis] kernel = [ ExtRBF(Xtrain.shape[1], variance=11, lengthscale=np.array((9., )), ARD=False) for j in range(1) ] # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = LogisticLL() if 'n_thread' in config.keys(): n_threads = config['n_thread'] else: n_threads = 1 if 'partition_size' in config.keys(): partition_size = config['partition_size'] else: partition_size = 3000 image = None if 'image' in config.keys(): image = config['image'] names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp', 'inducing'], IdentityTransformation, False, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 60, 'hyp': 15, 'inducing': 6 }, max_iter=9, n_threads=n_threads, ftol=10, model_image_file=image, partition_size=partition_size))
# plots for an specific experiment # PlotOutput.plot_output_all('abalone_graph', Experiments.get_output_path(), # lambda x: x['experiment'] == 'abalone', False) def run_config(config): try: logger.info('started config: ' + str(config)) getattr(ExperimentSetup, config['method_to_run'])(config) logger.info('finished config: ' + str(config)) except Exception as e: logger.exception(config) if __name__ == '__main__': logger = ModelLearn.get_logger(ModelLearn.get_logger_path(), 'general_' + ModelLearn.get_ID(), logging.DEBUG) # uncomment to run experiments in parallel # ExperimentRunner.run_parallel(3) # runs an individual configuration # ExperimentRunner.boston_experiment() # ExperimentRunner.wisconsin_breast_experiment() # ExperimentRunner.USPS_experiment() # ExperimentRunner.mining_experiment() # ExperimentRunner.abalone_experiment() # ExperimentRunner.mnist_binary_inducing_experiment() ExperimentRunner.mnist_binary_experiment() # ExperimentRunner.sarcos_all_joins_experiment() # ExperimentRunner.sarcos_experiment()
def MNIST_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.mnist_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'mnist' # uncomment these lines to delete unused features # features_rm = np.array([]) # for n in range(Xtrain.shape[1]): # if Xtrain[:, n].sum() ==0: # features_rm = np.append(features_rm, n) # Xtrain = np.delete(Xtrain, features_rm.astype(int), 1) # Xtest = np.delete(Xtest, features_rm.astype(int), 1) # uncomment these lines to change the resolution # res = 13 # current_res = int(np.sqrt(Xtrain.shape[1])) # X_train_resized = np.empty((Xtrain.shape[0], res * res)) # X_test_resized = np.empty((Xtest.shape[0], res * res)) # for n in range(Xtrain.shape[0]): # im = Image.fromarray(Xtrain[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_train_resized[n] = np.array(im).flatten() # # for n in range(Xtest.shape[0]): # im = Image.fromarray(Xtest[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_test_resized[n] = np.array(im).flatten() # # # Xtrain = X_train_resized # Xtest = X_test_resized kernel = [ExtRBF(Xtrain.shape[1], variance=11, lengthscale=np.array((9.,)), ARD=False) for j in range(10)] # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = SoftmaxLL(10) if 'n_thread' in config.keys(): n_threads = config['n_thread'] else: n_threads = 1 if 'partition_size' in config.keys(): partition_size = config['partition_size'] else: partition_size = 3000 image = None if 'image' in config.keys(): image = config['image'] names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp'], IdentityTransformation, False, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 50, 'hyp': 10}, max_iter=300, n_threads=n_threads, ftol=10, model_image_file=image, partition_size=partition_size))
def MNIST_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.mnist_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain = d['train_Y'] Xtest = d['test_X'] Ytest = d['test_Y'] name = 'mnist' # uncomment these lines to delete unused features # features_rm = np.array([]) # for n in range(Xtrain.shape[1]): # if Xtrain[:, n].sum() ==0: # features_rm = np.append(features_rm, n) # Xtrain = np.delete(Xtrain, features_rm.astype(int), 1) # Xtest = np.delete(Xtest, features_rm.astype(int), 1) # uncomment these lines to change the resolution # res = 13 # current_res = int(np.sqrt(Xtrain.shape[1])) # X_train_resized = np.empty((Xtrain.shape[0], res * res)) # X_test_resized = np.empty((Xtest.shape[0], res * res)) # for n in range(Xtrain.shape[0]): # im = Image.fromarray(Xtrain[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_train_resized[n] = np.array(im).flatten() # # for n in range(Xtest.shape[0]): # im = Image.fromarray(Xtest[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_test_resized[n] = np.array(im).flatten() # # # Xtrain = X_train_resized # Xtest = X_test_resized kernel = [ ExtRBF(Xtrain.shape[1], variance=11, lengthscale=np.array((9., )), ARD=False) for j in range(10) ] # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = SoftmaxLL(10) if 'n_thread' in config.keys(): n_threads = config['n_thread'] else: n_threads = 1 if 'partition_size' in config.keys(): partition_size = config['partition_size'] else: partition_size = 3000 image = None if 'image' in config.keys(): image = config['image'] names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp'], IdentityTransformation, False, config['log_level'], False, latent_noise=0.001, opt_per_iter={ 'mog': 50, 'hyp': 10 }, max_iter=300, n_threads=n_threads, ftol=10, model_image_file=image, partition_size=partition_size))
def MNIST_binary_inducing_data(config): method = config['method'] sparsify_factor = config['sparse_factor'] np.random.seed(12000) data = DataSource.mnist_data() names = [] d = data[config['run_id'] - 1] Xtrain = d['train_X'] Ytrain_full = d['train_Y'] Xtest = d['test_X'] Ytest_full = d['test_Y'] name = 'mnist_binary' # uncomment these lines to change the resolution # res = 13 # current_res = int(np.sqrt(Xtrain.shape[1])) # X_train_resized = np.empty((Xtrain.shape[0], res * res)) # X_test_resized = np.empty((Xtest.shape[0], res * res)) # for n in range(Xtrain.shape[0]): # im = Image.fromarray(Xtrain[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_train_resized[n] = np.array(im).flatten() # # for n in range(Xtest.shape[0]): # im = Image.fromarray(Xtest[n, :].reshape((current_res, current_res))) # im = im.resize((res, res)) # X_test_resized[n] = np.array(im).flatten() # # # Xtrain = X_train_resized # Xtest = X_test_resized Ytrain = np.apply_along_axis(lambda x: x[1:10:2].sum() - x[0:10:2].sum(), 1, Ytrain_full).astype(int)[:, np.newaxis] Ytest = np.apply_along_axis(lambda x: x[1:10:2].sum() - x[0:10:2].sum(), 1, Ytest_full).astype(int)[:, np.newaxis] kernel = [ExtRBF(Xtrain.shape[1], variance=11, lengthscale=np.array((9.,)), ARD=False) for j in range(1)] # number of inducing points num_inducing = int(Xtrain.shape[0] * sparsify_factor) num_samples = 2000 cond_ll = LogisticLL() if 'n_thread' in config.keys(): n_threads = config['n_thread'] else: n_threads = 1 if 'partition_size' in config.keys(): partition_size = config['partition_size'] else: partition_size = 3000 image = None if 'image' in config.keys(): image = config['image'] names.append( ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernel, method, name, d['id'], num_inducing, num_samples, sparsify_factor, ['mog', 'hyp', 'inducing'], IdentityTransformation, False, config['log_level'], False, latent_noise=0.001, opt_per_iter={'mog': 60, 'hyp': 15, 'inducing': 6}, max_iter=9, n_threads=n_threads, ftol=10, model_image_file=image, partition_size=partition_size))
def plot(): PlotOutput.plot_output_all('boston', ModelLearn.get_output_path(), lambda x: x['method'] == 'full', False)
ModelLearn.run_model(Xtest, Xtrain, Ytest, Ytrain, cond_ll, kernels, method, name, d['id'], num_inducing, num_samples, num_inducing / Xtrain.shape[0], # optimise hyper-parameters (hyp), posterior parameters (mog), and likelihood parameters (ll) ['hyp', 'mog', 'll'], # Transform data before training MeanTransformation, # place inducting points on training data. If False, they will be places using clustering True, # level of logging logging.DEBUG, # do not export training data into csv files False, # add a small latent noise to the kernel for stability of numerical computations latent_noise=0.001, # for how many iterations each set of parameters will be optimised opt_per_iter={'mog': 25, 'hyp': 25, 'll': 25}, # total number of global optimisations max_iter=200, # number of threads n_threads=1, # size of each partition of data partition_size=3000)
def plot(): PlotOutput.plot_output_all('boston', ModelLearn.get_output_path(), lambda x: x['method'] == 'full', False)
# PlotOutput.plot_output_all('abalone_graph', Experiments.get_output_path(), # lambda x: x['experiment'] == 'abalone', False) def run_config(config): try: logger.info('started config: ' + str(config)) getattr(ExperimentSetup, config['method_to_run'])(config) logger.info('finished config: ' + str(config)) except Exception as e: logger.exception(config) if __name__ == '__main__': logger = ModelLearn.get_logger(ModelLearn.get_logger_path(), 'general_' + ModelLearn.get_ID(), logging.DEBUG) # uncomment to run experiments in parallel # ExperimentRunner.run_parallel(3) # runs an individual configuration ExperimentRunner.boston_experiment() # ExperimentRunner.wisconsin_breast_experiment() # ExperimentRunner.USPS_experiment() # ExperimentRunner.mining_experiment() # ExperimentRunner.abalone_experiment() # ExperimentRunner.mnist_binary_inducing_experiment() # ExperimentRunner.mnist_binary_experiment() # ExperimentRunner.sarcos_all_joins_experiment() # ExperimentRunner.sarcos_experiment()