def testPriorAndPrefs(self): S5 = Shekel5() pX = lhcSample(S5.bounds, 100, seed=13) pY = [S5.f(x) for x in pX] prior = RBFNMeanPrior() prior.train(pX, pY, S5.bounds, k=10) hv = .1 hyper = [hv, hv, hv, hv] gkernel = GaussianKernel_ard(hyper) GP = PrefGaussianProcess(gkernel, prior=prior) X = [array([i + .5] * 4) for i in range(5)] valX = [x.copy() for x in X] prefs = [] for i in range(len(X)): for j in range(i): if S5.f(X[i]) > S5.f(X[j]): prefs.append((X[i], X[j], 0)) else: prefs.append((X[j], X[i], 0)) GP.addPreferences(prefs) opt, optx = maximizeEI(GP, S5.bounds)
def testFastGallery(self): tf = Hartman3() kernel = tf.createKernel(GaussianKernel_ard) X = lhcSample(tf.bounds, 10, seed=23) Y = [tf.f(x) for x in X] prefs = query2prefs(X, tf.f) GP = PrefGaussianProcess(kernel) GP.addPreferences(prefs) gallery = fastUCBGallery(GP, tf.bounds, 4) print 'gallery returned:' for x in gallery: print '\t', x GP.addPreferences(query2prefs(gallery, tf.f)) # make sure we don't return anything out of bounds bounds = copy(tf.bounds) bounds[0] = [0., 0.] gallery = fastUCBGallery(GP, bounds, 4) print 'gallery returned:' for x in gallery: print '\t', x for v, b in zip(x, bounds): self.failUnless(v>=b[0] and v<=b[1])
def testPriorAndPrefs(self): S5 = Shekel5() pX = lhcSample(S5.bounds, 100, seed=13) pY = [S5.f(x) for x in pX] prior = RBFNMeanPrior() prior.train(pX, pY, S5.bounds, k=10) hv = .1 hyper = [hv, hv, hv, hv] gkernel = GaussianKernel_ard(hyper) GP = PrefGaussianProcess(gkernel, prior=prior) X = [array([i+.5]*4) for i in xrange(5)] valX = [x.copy() for x in X] prefs = [] for i in xrange(len(X)): for j in xrange(i): if S5.f(X[i]) > S5.f(X[j]): prefs.append((X[i], X[j], 0)) else: prefs.append((X[j], X[i], 0)) GP.addPreferences(prefs) opt, optx = maximizeEI(GP, S5.bounds)
def demoPrefGallery(): """ A simpler demo, showing how to use a preference gallery. This demo is not interactive -- it uses the 6D Hartman test function to generate the preferences. """ N = 3 # gallery size # use the Hartman6 test function, which has a kernel and bounds predefined tf = Hartman6() bounds = tf.bounds kernel = tf.createKernel(GaussianKernel_ard) # set up a Preference Gaussian Process, in which the observations are # preferences, rather than scalars GP = PrefGaussianProcess(kernel) # initial preferences -- since we have no informative prior on the space, # the gallery will be a set of points that maximize variance gallery = fastUCBGallery(GP, bounds, N) # this is a utility function for automtically testing the preferences -- # given a test functions and some sample points, it will return a list of # preferences prefs = query2prefs(gallery, tf.f) # preferences have the form [r, c, degree], where r is preferred to c. # degree is degree of preference. Just leave degree at 0 for now. for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # add preferences to the model GP.addPreferences(prefs) # get another gallery, but with the first three dimensions fixed to .5 nbounds = deepcopy(bounds) nbounds[:3] = [[.5,.5]]*3 gallery = fastUCBGallery(GP, nbounds, N) prefs = query2prefs(gallery, tf.f) for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # get another gallery, but with the *last* three dimensions fixed to .5 nbounds = deepcopy(bounds) nbounds[3:] = [[.5,.5]]*3 gallery = fastUCBGallery(GP, nbounds, N) prefs = query2prefs(gallery, tf.f) for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # preferences don't have to come from the gallery r = array([0, 0, .5, 0, 1, .25]) c = array([1, 1, .75, 1, 0, .5]) pref = (r, c, 0) GP.addPreferences([pref])
def learn(request,pref=None,unpref=None,canvas_size=400): # set up if not request.session.has_key('session_id'): raise Exception('session_id not found in request.session') session = BanditProblemSession.objects.get(id=request.session['session_id']) problem = session.problem # push to database if needed if pref is not None and unpref is not None: pref = BanditContext.objects.get(id=pref) unpref = BanditContext.objects.get(id=unpref) pc = PairedComparison.objects.create( unpreferred_context=unpref, preferred_context=pref, session=session, ) # now pull historical preferences gp = PrefGaussianProcess(GaussianKernel_iso([problem.length_scale, problem.noise_magnitude])) pcs = session.comparisons.all() if pcs is not None and len(pcs) > 0: gp.addPreferences([ (pc.preferred_context.vector, pc.unpreferred_context.vector, 0) for pc in pcs ]) # generate art (it just sounds wrong!!!) gal_db = {} gallery = fastUCBGallery(gp, [[0,1]]*problem.dim(), 2, useBest=True, xi=problem.xi,passback=gal_db) ctx1,_ = BanditContext.objects.get_or_create(dimension=len(gallery[0]),vector=gallery[0].tolist()) ctx2,_ = BanditContext.objects.get_or_create(dimension=len(gallery[1]),vector=gallery[1].tolist()) left_choice = problem.generate(ctx1, id='left') right_choice = problem.generate(ctx2, id='right') plots = _make_plots_from_gallery_passback(problem,gal_db) # ... t = loader.get_template('compare.html') c = Context({ 'left_choice': left_choice, 'right_choice': right_choice, 'context_1': ctx1, 'context_2': ctx2, 'problem': problem, 'plots': json.dumps(plots), }) return HttpResponse(t.render(c))
def testFastGallery(self): tf = Hartman3() kernel = tf.createKernel(GaussianKernel_ard) X = lhcSample(tf.bounds, 10, seed=23) Y = [tf.f(x) for x in X] prefs = query2prefs(X, tf.f) GP = PrefGaussianProcess(kernel) GP.addPreferences(prefs) gallery = fastUCBGallery(GP, tf.bounds, 4) print('gallery returned:') for x in gallery: print('\t', x) GP.addPreferences(query2prefs(gallery, tf.f)) # make sure we don't return anything out of bounds bounds = copy(tf.bounds) bounds[0] = [0., 0.] gallery = fastUCBGallery(GP, bounds, 4) print('gallery returned:') for x in gallery: print('\t', x) for v, b in zip(x, bounds): self.failUnless(v >= b[0] and v <= b[1])
def demoPrefGallery(): """ A simpler demo, showing how to use a preference gallery. This demo is not interactive -- it uses the 6D Hartman test function to generate the preferences. """ N = 3 # gallery size # use the Hartman6 test function, which has a kernel and bounds predefined tf = Hartman6() bounds = tf.bounds kernel = tf.createKernel(GaussianKernel_ard) # set up a Preference Gaussian Process, in which the observations are # preferences, rather than scalars GP = PrefGaussianProcess(kernel) # initial preferences -- since we have no informative prior on the space, # the gallery will be a set of points that maximize variance gallery = fastUCBGallery(GP, bounds, N) # this is a utility function for automtically testing the preferences -- # given a test functions and some sample points, it will return a list of # preferences prefs = query2prefs(gallery, tf.f) # preferences have the form [r, c, degree], where r is preferred to c. # degree is degree of preference. Just leave degree at 0 for now. for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # add preferences to the model GP.addPreferences(prefs) # get another gallery, but with the first three dimensions fixed to .5 nbounds = deepcopy(bounds) nbounds[:3] = [[.5, .5]] * 3 gallery = fastUCBGallery(GP, nbounds, N) prefs = query2prefs(gallery, tf.f) for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # get another gallery, but with the *last* three dimensions fixed to .5 nbounds = deepcopy(bounds) nbounds[3:] = [[.5, .5]] * 3 gallery = fastUCBGallery(GP, nbounds, N) prefs = query2prefs(gallery, tf.f) for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # preferences don't have to come from the gallery r = array([0, 0, .5, 0, 1, .25]) c = array([1, 1, .75, 1, 0, .5]) pref = (r, c, 0) GP.addPreferences([pref])