def expand_test2(): k1 = fk.MaskKernel(2, 0, fk.SqExpKernel(1, 1)) k2 = fk.MaskKernel(2, 1, fk.SqExpPeriodicKernel(2, 2, 2)) e = fk.SumKernel([k1, k2]) g = grammar.MultiDGrammar(2) print '' for f in grammar.expand(e, g): print f.pretty_print() print grammar.canonical(f).pretty_print() print print ' ***** duplicates removed *****' print kernels = grammar.expand(e, g) for f in grammar.remove_duplicates(kernels): print f.pretty_print() print print '%d originally, %d without duplicates' % ( len(kernels), len(grammar.remove_duplicates(kernels))) print 'expand_test complete'
def canonical(kernel): '''Sorts a kernel tree into a canonical form.''' if isinstance(kernel, fk.BaseKernel): return kernel.copy() elif isinstance(kernel, fk.MaskKernel): return fk.MaskKernel(kernel.ndim, kernel.active_dimension, canonical(kernel.base_kernel)) elif isinstance(kernel, fk.SumKernel): new_ops = [] for op in kernel.operands: op_canon = canonical(op) if isinstance(op, fk.SumKernel): new_ops += op_canon.operands else: new_ops.append(op_canon) return fk.SumKernel(sorted(new_ops)) elif isinstance(kernel, fk.ProductKernel): new_ops = [] for op in kernel.operands: op_canon = canonical(op) if isinstance(op, fk.ProductKernel): new_ops += op_canon.operands else: new_ops.append(op_canon) return fk.ProductKernel(sorted(new_ops)) else: raise RuntimeError('Unknown kernel class:', kernel.__class__)
def polish_to_kernel(polish_expr): if type(polish_expr) == tuple: if polish_expr[0] == '+': operands = [polish_to_kernel(e) for e in polish_expr[1:]] return fk.SumKernel(operands) elif polish_expr[0] == '*': operands = [polish_to_kernel(e) for e in polish_expr[1:]] return fk.ProductKernel(operands) else: raise RuntimeError('Unknown operator: %s' % polish_expr[0]) else: assert isinstance(polish_expr, fk.Kernel) return polish_expr
def call_gpml_test(): np.random.seed(0) k = fk.SumKernel([fk.SqExpKernel(0, 0), fk.SqExpKernel(0, 0)]) print k.gpml_kernel_expression() print k.pretty_print() print '[%s]' % k.param_vector() X, y = load_mauna() N_orig = X.shape[0] X = X[:N_orig // 3, :] y = y[:N_orig // 3, :] results = [] pylab.figure() for i in range(15): init_params = np.random.normal(size=k.param_vector().size) #kernel_hypers, nll, nlls = gpml.optimize_params(k.gpml_kernel_expression(), k.param_vector(), X, y, return_all=True) kernel_hypers, nll, nlls = gpml.optimize_params( k.gpml_kernel_expression(), init_params, X, y, return_all=True) print "kernel_hypers =", kernel_hypers print "nll =", nll k_opt = k.family().from_param_vector(kernel_hypers) print k_opt.gpml_kernel_expression() print k_opt.pretty_print() print '[%s]' % k_opt.param_vector() pylab.semilogx(range(1, nlls.size + 1), nlls) results.append((kernel_hypers, nll)) pylab.draw() print print results = sorted(results, key=lambda p: p[1]) for kernel_hypers, nll in results: print nll, kernel_hypers print "done"
def expand(kernel, grammar): result = expand_single_tree(kernel, grammar) if isinstance(kernel, fk.BaseKernel): pass elif isinstance(kernel, fk.MaskKernel): result += [ fk.MaskKernel(kernel.ndim, kernel.active_dimension, e) for e in expand(kernel.base_kernel, grammar) ] elif isinstance(kernel, fk.SumKernel): for i, op in enumerate(kernel.operands): for e in expand(op, grammar): new_ops = kernel.operands[:i] + [e] + kernel.operands[i + 1:] new_ops = [op.copy() for op in new_ops] result.append(fk.SumKernel(new_ops)) elif isinstance(kernel, fk.ProductKernel): for i, op in enumerate(kernel.operands): for e in expand(op, grammar): new_ops = kernel.operands[:i] + [e] + kernel.operands[i + 1:] new_ops = [op.copy() for op in new_ops] result.append(fk.ProductKernel(new_ops)) else: raise RuntimeError('Unknown kernel class:', kernel.__class__) return result