template = sampleTemplate(groundgraph, numtemplate) if 'WORKHASH' in os.environ: jobhash = os.environ['WORKHASH'] if not r.hexists('jobs:grounds', jobhash): r.hset('jobs:grounds', jobhash, zlib.compress(cPickle.dumps(groundgraph))) random.seed() np.random.seed() datasizes = [4, 16, 32, 64, 128, 256] temps = [1.0, 1.0, 2.0, 2.0, 5.0, 5.0] for temperature, numdata in zip(temps, datasizes): data = generateData(groundgraph, joint, numdata) groundbnet = BayesNetCPD(states, data, limparent=3) groundbnet.set_cpds(joint) obj = BayesNetCPD(states, data, limparent=3) b = BayesNetSampler(obj, template, groundbnet, priorweight) s = SAMCRun(b, burn, stepscale, refden, thin) s.sample(iters, temperature) s.compute_means() if 'WORKHASH' in os.environ: r.lpush('jobs:done:' + jobhash, s.read_db()) r.lpush('custom:%s:samplesize=%d' % (jobhash, numdata), s.db.root.computed.means._v_attrs['kld']) s.db.close()
#time() #s1.sample(iters, temperature) #time() #s1.compute_means() #if 'WORKHASH' in os.environ: #r.lpush('jobs:done:' + jobhash, s1.read_db()) #s1.db.close() ############# bayesnetcpd ############ #import pstats, cProfile joint = utils.graph_to_joint(groundgraph) states = np.ones(len(joint.dists),dtype=np.int32)*2 groundbnet = BayesNetCPD(states, data) groundbnet.set_cpds(joint) obj = BayesNetCPD(states, data) b2 = BayesNetSampler(obj, template, groundbnet, priorweight) s2 = SAMCRun(b2,burn,stepscale,refden,thin) time() #cProfile.runctx("s2.sample(iters, temperature)", globals(), locals(), "prof.prof") s2.sample(iters, temperature) time() s2.compute_means() #s2.compute_means(cummeans=False) if 'WORKHASH' in os.environ: r.lpush('jobs:done:' + jobhash, s2.read_db()) s2.db.close() #######################################
random.seed() np.random.seed() ############### TreeNet ############## groundtree = TreeNet(N, data=data, graph=groundgraph) b1 = TreeNet(N, data, template, priorweight, groundtree) #s1 = SAMCRun(b1,burn,stepscale,refden,thin) #s1.sample(iters, temperature) ############## bayesnetcpd ############ joint = utils.graph_to_joint(groundgraph) states = np.ones(len(joint.dists),dtype=np.int32)*2 groundbnet = BayesNetCPD(states, data) groundbnet.set_cpds(joint) obj = BayesNetCPD(states, data) b2 = BayesNetSampler(obj, template, groundbnet, priorweight) #s2 = SAMCRun(b2,burn,stepscale,refden,thin) #s2.sample(iters, temperature) ####################################### def test(): def close_enough(n1,n2): return (n1-n2) < 1e-5 return (n1-n2) < np.finfo(float).eps assert close_enough(groundtree.kld(b1), groundbnet.kld(b2.bayesnet))
random.seed() np.random.seed() ############### TreeNet ############## groundtree = TreeNet(N, data=data, graph=groundgraph) b1 = TreeNet(N, data, template, priorweight, groundtree) #s1 = SAMCRun(b1,burn,stepscale,refden,thin) #s1.sample(iters, temperature) ############## bayesnetcpd ############ joint = utils.graph_to_joint(groundgraph) states = np.ones(len(joint.dists), dtype=np.int32) * 2 groundbnet = BayesNetCPD(states, data) groundbnet.set_cpds(joint) obj = BayesNetCPD(states, data) b2 = BayesNetSampler(obj, template, groundbnet, priorweight) #s2 = SAMCRun(b2,burn,stepscale,refden,thin) #s2.sample(iters, temperature) ####################################### def test(): def close_enough(n1, n2): return (n1 - n2) < 1e-5 return (n1 - n2) < np.finfo(float).eps
random.seed(123456) np.random.seed(123456) groundgraph = generateHourGlassGraph(nodes=N) #joint, states = generateJoint(groundgraph, method='dirichlet') joint, states = generateJoint(groundgraph, method='noisylogic') data = generateData(groundgraph, joint, numdata) template = sampleTemplate(groundgraph, numtemplate) print "Joint:" print joint random.seed() np.random.seed() ground = BayesNetCPD(states, data) ground.set_cpds(joint) obj = BayesNetCPD(states, data) b = BayesNetSampler(obj, template, ground, priorweight) s = SAMCRun(b,burn,stepscale,refden,thin) time() s.sample(iters, temperature) time() s.compute_means() #fname = '/tmp/test.h5' #fid = open(fname, 'w') #fid.write(zlib.decompress(s.read_db()))