def run(verbose=True, Y=1, D=2, dataset=2, in_path=None, out_path=None, steps=2): ripl = s.make_puma_church_prime_ripl() total_birds = 1000 if dataset == 2 else 1000000 name = "%dx%dx%d-train" % (width, height, total_birds) params = { "name":name, "width":width, "height":height, "cells":cells, "dataset":dataset, "total_birds":total_birds, "years":range(Y), "days":[], "hypers":hypers, "in_path":in_path, "out_path":out_path, } model = Poisson(ripl, params) if verbose: print "Starting run" ripl.clear() model.loadAssumes() model.updateObserves(0) logs = [] t = [time.time()] # python :( def log(): dt = time.time() - t[0] #logs.append((ripl.get_global_logscore(), model.computeScoreDay(model.days[-2]), dt)) logs.append((ripl.get_global_logscore(), dt)) if verbose: print logs[-1] t[0] += dt for d in range(1, D): if verbose: print "Day %d" % d model.updateObserves(d) log() for i in range(steps): ripl.infer({"kernel":"mh", "scope":d-1, "block":"one", "transitions": Y*1000}) log() continue bird_locs = model.getBirdLocations(days=[d]) for y in range(Y): path = 'bird_moves%d/%d/%02d/' % (dataset, y, d) ensure(path) drawBirds(bird_locs[y][d], path + '%02d.png' % i, **params) #model.drawBirdLocations() model.writeBirdMoves() return logs
def run(verbose=True, Y=1, D=2, dataset=2, in_path=None, out_path=None, steps=2): ripl = s.make_puma_church_prime_ripl() total_birds = 1000 if dataset == 2 else 1000000 name = "%dx%dx%d-test" % (width, height, total_birds) params = { "name": name, "width": width, "height": height, "cells": cells, "dataset": dataset, "total_birds": total_birds, "years": range(Y), "days": [], "hypers": hypers, "in_path": in_path, "out_path": out_path, } model = Poisson(ripl, params) if verbose: print "Starting run" ripl.clear() model.loadAssumes() predictions = {} for d in range(D - 1): if verbose: print "Day %d" % d model.updateObserves(d) if d > 0: for i in range(5): ripl.infer({"kernel": "mh", "scope": d - 1, "block": "one", "transitions": Y * 1000}) last_day = d == D - 2 if last_day: bird_moves = ripl.predict("(get_birds_moving3 %d)" % d, label="predict%d" % d) else: bird_moves = [ripl.predict("(get_birds_moving3 %d)" % t, label="predict%d" % t) for t in [d, d + 1]] for y in range(Y): for i in range(cells): for j in range(cells): if last_day: predictions[(y, d - 1, i, j)] = [bird_moves[y][i][j], -1] else: predictions[(y, d - 1, i, j)] = [b[y][i][j] for b in bird_moves] for t in [d] if last_day else [d, d + 1]: ripl.forget("predict%d" % t) # model.drawBirdLocations() writePredictions(predictions, **params)
def run(y, days, runs, in_path=None): ripl = s.make_puma_church_prime_ripl() params = { "dataset":1, "name":name, "cells":cells, "years":[y], "days":days, "in_path":in_path } onebird = OneBird(ripl, params) def sweep(r, *args): for i in range(0): onebird.inferMove(ripl=r) onebird.inferHypers(ripl=r) history, _ = onebird.runFromConditional(D, runs=runs, infer=sweep, verbose=True) history.hypers = [avgFinalValue(history, 'hypers%d' % k) for k in range(num_features)] history.save(directory="%s/%d" % (name, y))
def run(y): ripl = s.make_puma_church_prime_ripl() params = {"name": name, "cells": cells, "years": [y], "days": range(D)} onebird = OneBird(ripl, params) def sweep(r, *args): for i in range(5): onebird.inferMove(ripl=r) #r.infer('(slice hypers one %d)' % num_features) r.infer('(mh hypers one %d)' % 5 * (1 + num_features)) history, ripl = onebird.runFromConditional(D, runs=runs, infer=sweep, verbose=True) history.hypers = [ avgFinalValue(history, 'hypers%d' % k) for k in range(num_features) ] #history.save(directory="%s/%d" % (name, y)) return history, ripl
def run(y): ripl = s.make_puma_church_prime_ripl() params = { "name":name, "cells":cells, "years":[y], "days":range(D) } onebird = OneBird(ripl, params) def sweep(r, *args): for i in range(5): onebird.inferMove(ripl=r) #r.infer('(slice hypers one %d)' % num_features) r.infer('(mh hypers one %d)' % 5 * (1 + num_features)) history, ripl = onebird.runFromConditional(D, runs=runs, infer=sweep, verbose=True) history.hypers = [avgFinalValue(history, 'hypers%d' % k) for k in range(num_features)] #history.save(directory="%s/%d" % (name, y)) return history,ripl
import time import venture.shortcuts as s ripl = s.make_puma_church_prime_ripl() from utils import * from model_param import Poisson, num_features def makeModel(dataset=2, D=3, Y=1, learnHypers=True, hyperPrior='(gamma 1 .1)',in_path=None, out_path=None): width,height = 10,10 cells = width * height total_birds = 1000 if dataset == 2 else 1000000 name = "%dx%dx%d-train" % (width, height, total_birds) runs = 1 hypers = [5, 10, 10, 10] if not learnHypers else [hyperPrior]*4 params = { "name":name, "width":width, "height":height, "cells":cells, "dataset":dataset, "total_birds":total_birds, "years":range(Y), "days":[], "hypers":hypers, "maxDay":D, "in_path":in_path, "out_path":out_path} model = Poisson(ripl,params)
def build(): return s.make_puma_church_prime_ripl()
self.assume("alpha_topic_word", "(gamma 1.0 1.0)") self.assume("get_document_topic_sampler", "(mem (lambda (doc) (make_sym_dir_mult alpha_document_topic topics)))") self.assume("get_topic_word_sampler", "(mem (lambda (topic) (make_sym_dir_mult alpha_topic_word vocab)))") self.assume("get_word", "(mem (lambda (doc pos) ((get_topic_word_sampler ((get_document_topic_sampler doc))))))") return def makeObserves(self): D = self.parameters['documents'] N = self.parameters['words_per_document'] for doc in range(D): for pos in range(N): self.observe("(get_word %d %d)" % (doc, pos), "atom<%d>" % 0) return if __name__ == '__main__': ripl = shortcuts.make_puma_church_prime_ripl() #parameters = {'topics' : 4, 'vocab' : 10, 'documents' : 8, 'words_per_document' : 12} #model = LDA(ripl, parameters) #history = model.runConditionedFromPrior(50, verbose=True) #history = model.runFromJoint(50, verbose=True) #history = model.sampleFromJoint(20, verbose=True) #sample_hist, infer_hist, klHistory = model.computeJointKL(200, 50, verbose=True) #history = model.runFromConditional(50) #klHistory.plot(fmt='png') parameters = {'topics' : 3, 'vocab' : 8, 'documents' : 5, 'words_per_document' : [7, 8]} mySweeps=10 run_count = 0 def runner(params):
import venture.shortcuts as s ripl = s.make_puma_church_prime_ripl() from model import OneBird, num_features from itertools import product name = "onebird" width = 4 height = 4 cells = width * height Y = 1 D = 20 params = {"name": name, "cells": cells, "years": [0], "days": range(D)} onebird = OneBird(ripl, params) onebird.loadAssumes() onebird.loadObserves() def checkHypers(hypers): print hypers for i in range(num_features): onebird.ripl.force('hypers%d' % i, hypers[i]) for i in range(5): onebird.inferMove() return onebird.ripl.get_global_logscore() def gridHypers(grid):