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()
#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() ####################################### ###
burn = 1 stepscale=10 temperature = 1.0 thin = 1 refden = 0.0 random.seed(123456) np.random.seed(123456) groundgraph = generateTree(N, comps) data = generateData(groundgraph,numdata) #template = sampleTemplate(groundgraph, numtemplate) random.seed() np.random.seed() ground = TreeNet(N, graph=groundgraph) b = TreeNet(N, data=data, ground=ground) s = SAMCRun(b,burn,stepscale,refden,thin,verbose=True) s.sample(iters, temperature) s.compute_means() # All to exercise cde deps tmp = s.read_db() import cPickle txt = zlib.compress(cPickle.dumps([1,2,3])) if 'WORKHASH' in os.environ: r.lpush('jobs:done:'+os.environ['WORKHASH'], s.read_db())
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())) #fid.close() #db = t.openFile(fname, 'r') #if 'WORKHASH' in os.environ: #r.lpush('jobs:done:'+os.environ['WORKHASH'], s.read_db())
if cpd: p_cpd = p_struct else: p_cpd = 0.0 random.seed(12345) np.random.seed(12345) obj = BayesNetCPD(states, data, limparent=3) template = sampleTemplate(groundgraph, numtemplate) random.seed() np.random.seed() b = BayesNetSampler(obj, template, groundbnet, p_struct=p_struct, p_cpd=p_cpd) s = SAMCRun(b,burn,stepscale,refden,thin) s.sample(iters, temperature) s.compute_means(cummeans=False) if 'WORKHASH' in os.environ: r.lpush('jobs:done:' + jobhash, s.read_db()) r.lpush('custom:%s:p_struct=%d:ntemplate=%d:p_cpd=%d' % (jobhash, int(p_struct*10), numtemplate, int(p_cpd*10)), s.db.root.computed.means._v_attrs['kld'] ) s.db.close()
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())) #fid.close() #db = t.openFile(fname, 'r') #if 'WORKHASH' in os.environ: #r.lpush('jobs:done:'+os.environ['WORKHASH'], s.read_db())