def test_kernel_decompose_1d(): '''Checks that a kernel decomposes into a sum properly''' sk = fk.repr_string_to_kernel('ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=4.853529, output_variance=-0.648382, alpha=0.457387)), SumKernel([ ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=1.395371, period=-3.990523, output_variance=0.565365)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.000420, lengthscale=-0.120045)) ]), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.802417, lengthscale=3.350816)), SumKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=-3.899540, period=0.087011, output_variance=2.430187)), MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=3.865315, output_variance=4.028649, alpha=-5.060996)), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=3.251723, period=1.540000, output_variance=-2.497487)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=-1.424416, lengthscale=-1.732677)) ]) ]) ]) ]) ]), nll=558.339977, laplace_nle=-266.580399, bic_nle=1216.076221, noise=[ 1.66059002])') k = fk.strip_masks(sk.k_opt) correct_answer = ['RQ \\times Per \\times Lin', 'RQ \\times Lin \\times Per', 'RQ \\times Lin \\times RQ', 'RQ \\times Lin \\times Per \\times Lin'] kd = fk.break_kernel_into_summands(k) assert( [k.latex_print() for k in kd] == correct_answer )
def debug_laplace(): # Load data set X, y, D, Xtest, ytest = gpml.load_mat( '../data/kfold_data/r_concrete_500_fold_10_of_10.mat', y_dim=1) # Load the suspicious kernel sk = fk.repr_string_to_kernel( 'ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=8, active_dimension=0, base_kernel=CubicKernel(offset=1.757755, output_variance=7.084045)), MaskKernel(ndim=8, active_dimension=7, base_kernel=SqExpPeriodicKernel(lengthscale=-2.701080, period=-0.380918, output_variance=-0.071214)) ]), nll=6348.096611, laplace_nle=-184450132.068237, bic_nle=12720.630212, noise=[-1.77276072])' ) # Create some code to evaluate it if X.ndim == 1: X = X[:, nax] if y.ndim == 1: y = y[:, nax] ndata = y.shape[0] # Create data file data_file = cblparallel.create_temp_file('.mat') scipy.io.savemat(data_file, {'X': X, 'y': y}) # Save regression data # Move to fear cblparallel.copy_to_remote(data_file) scripts = [ gpml.OPTIMIZE_KERNEL_CODE % { 'datafile': data_file.split('/')[-1], 'writefile': '%(output_file)s', # N.B. cblparallel manages output files 'gpml_path': cblparallel.gpml_path(local_computation=False), 'kernel_family': sk.k_opt.gpml_kernel_expression(), 'kernel_params': '[ %s ]' % ' '.join(str(p) for p in sk.k_opt.param_vector()), 'noise': str(sk.noise), 'iters': str(300) } ] #### Need to be careful with % signs #### For the moment, cblparallel expects no single % signs - FIXME scripts[0] = re.sub('% ', '%% ', scripts[0]) # Test scripts[0] = re.sub('delta = 1e-6', 'delta = 1e-6', scripts[0]) #scripts[0] = re.sub('hyp.lik = [-1.77276072]', 'hyp.lik = [-0.77276072]', scripts[0]) output_file = cblparallel.run_batch_on_fear(scripts, language='matlab', max_jobs=600)[0] # Read in results output = gpml.read_outputs(output_file) result = ScoredKernel.from_matlab_output(output, sk.k_opt.family(), ndata) print result print output.hessian os.remove(output_file) # Remove temporary data file (perhaps on the cluster server) cblparallel.remove_temp_file(data_file, local_computation=False)
def test_kernel_decompose_1d(): '''Checks that a kernel decomposes into a sum properly''' sk = fk.repr_string_to_kernel( 'ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=4.853529, output_variance=-0.648382, alpha=0.457387)), SumKernel([ ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=1.395371, period=-3.990523, output_variance=0.565365)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.000420, lengthscale=-0.120045)) ]), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.802417, lengthscale=3.350816)), SumKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=-3.899540, period=0.087011, output_variance=2.430187)), MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=3.865315, output_variance=4.028649, alpha=-5.060996)), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=3.251723, period=1.540000, output_variance=-2.497487)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=-1.424416, lengthscale=-1.732677)) ]) ]) ]) ]) ]), nll=558.339977, laplace_nle=-266.580399, bic_nle=1216.076221, noise=[ 1.66059002])' ) k = fk.strip_masks(sk.k_opt) correct_answer = [ 'RQ \\times Per \\times Lin', 'RQ \\times Lin \\times Per', 'RQ \\times Lin \\times RQ', 'RQ \\times Lin \\times Per \\times Lin' ] kd = fk.break_kernel_into_summands(k) assert ([k.latex_print() for k in kd] == correct_answer)
def parse_results( results_filename, max_level=None ): ''' Returns the best kernel in an experiment output file as a ScoredKernel ''' # Read relevant lines of file lines = [] with open(results_filename) as results_file: for line in results_file: if line.startswith("ScoredKernel"): lines.append(line) elif (not max_level is None) and (len(re.findall('Level [0-9]+', line)) > 0): level = int(line.split(' ')[2]) if level > max_level: break #result_tuples = [fk.repr_string_to_kernel(line.strip()) for line in open(results_filename) if line.startswith("ScoredKernel")] result_tuples = [fk.repr_string_to_kernel(line.strip()) for line in lines] best_tuple = sorted(result_tuples, key=ScoredKernel.score)[0] return best_tuple
def debug_laplace(): # Load data set X, y, D, Xtest, ytest = gpml.load_mat('../data/kfold_data/r_concrete_500_fold_10_of_10.mat', y_dim=1) # Load the suspicious kernel sk = fk.repr_string_to_kernel('ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=8, active_dimension=0, base_kernel=CubicKernel(offset=1.757755, output_variance=7.084045)), MaskKernel(ndim=8, active_dimension=7, base_kernel=SqExpPeriodicKernel(lengthscale=-2.701080, period=-0.380918, output_variance=-0.071214)) ]), nll=6348.096611, laplace_nle=-184450132.068237, bic_nle=12720.630212, noise=[-1.77276072])') # Create some code to evaluate it if X.ndim == 1: X = X[:, nax] if y.ndim == 1: y = y[:, nax] ndata = y.shape[0] # Create data file data_file = cblparallel.create_temp_file('.mat') scipy.io.savemat(data_file, {'X': X, 'y': y}) # Save regression data # Move to fear cblparallel.copy_to_remote(data_file) scripts = [gpml.OPTIMIZE_KERNEL_CODE % {'datafile': data_file.split('/')[-1], 'writefile': '%(output_file)s', # N.B. cblparallel manages output files 'gpml_path': cblparallel.gpml_path(local_computation=False), 'kernel_family': sk.k_opt.gpml_kernel_expression(), 'kernel_params': '[ %s ]' % ' '.join(str(p) for p in sk.k_opt.param_vector()), 'noise': str(sk.noise), 'iters': str(300)}] #### Need to be careful with % signs #### For the moment, cblparallel expects no single % signs - FIXME scripts[0] = re.sub('% ', '%% ', scripts[0]) # Test scripts[0] = re.sub('delta = 1e-6', 'delta = 1e-6', scripts[0]) #scripts[0] = re.sub('hyp.lik = [-1.77276072]', 'hyp.lik = [-0.77276072]', scripts[0]) output_file = cblparallel.run_batch_on_fear(scripts, language='matlab', max_jobs=600)[0] # Read in results output = gpml.read_outputs(output_file) result = ScoredKernel.from_matlab_output(output, sk.k_opt.family(), ndata) print result print output.hessian os.remove(output_file) # Remove temporary data file (perhaps on the cluster server) cblparallel.remove_temp_file(data_file, local_computation=False)
def test_change_kernel_eval(): '''Tests whether we can take a string of a changepoint kernel.''' result_string = '''ChangeKernel(steepness=2.0, location=-1.2)''' k = fk.repr_string_to_kernel(result_string) k.pretty_print()
def test_kernel_eval(): '''Tests whether we can take a string and make a kernel out of it.''' result_string = '''ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=4, active_dimension=1, base_kernel=PP0Kernel(lengthscale=-3.776833, output_variance=-3.365662)), MaskKernel(ndim=4, active_dimension=2, base_kernel=CubicKernel(offset=-1.149225, output_variance=-0.604651)) ]), nll=4546.591426, laplace_nle=4531.678317, bic_nle=9108.234692, noise=[-2.26189631])''' k = fk.repr_string_to_kernel(result_string) k.pretty_print()