def test_models_pickle(): models = ( bb, bnb, gp, nich, dd(4), bbnc, niw(3), dm(5), ) for model in models: bstr = pickle.dumps(model) model1 = pickle.loads(bstr) assert_equals(model.name(), model1.name()) if model.name() == 'dd': assert_equals(len(model.default_hyperparams()['alphas']), len(model1.default_hyperparams()['alphas'])) elif model.name() == 'niw': assert_equals(len(model.default_hyperparams()['mu']), len(model1.default_hyperparams()['mu'])) elif model.name() == 'dm': assert_equals(model.py_desc().get_np_dtype().shape, model1.py_desc().get_np_dtype().shape)
def test_model_definition_pickle(): defn = model_definition(10, [bb, niw(3)]) bstr = pickle.dumps(defn) defn1 = pickle.loads(bstr) assert_equals(defn.n(), defn1.n()) assert_equals(len(defn.models()), len(defn1.models())) for a, b in zip(defn.models(), defn1.models()): assert_equals(a.name(), b.name())
def test_runner_default_kernel_grid(): models = [bb, nich, niw(3)] def kc_fn(defn): return list(it.chain( runner.default_assign_kernel_config(defn), runner.default_grid_feature_hp_kernel_config(defn))) _test_runner_kernel_config(kc_fn, models)
def test_sample_sanity(): # just a sanity check defn = model_definition(10, [bb, bnb, gp, nich, dd(5), niw(4)]) clusters, samplers = sample(defn) assert_equals(len(clusters), len(samplers)) for cluster in clusters: assert_true(len(cluster) > 0) for v in cluster: assert_equals(len(v), len(defn.models()))
def test_model_definition_pickle(): defn = model_definition([10, 12], [((0, 0), bb), ((1, 0), niw(3))]) bstr = pickle.dumps(defn) defn1 = pickle.loads(bstr) assert_list_equal(defn.domains(), defn1.domains()) assert_equals(defn.relations(), defn1.relations()) zipped_models = zip(defn.relation_models(), defn1.relation_models()) for model, model1 in zipped_models: assert_equals(model.name(), model1.name())
def test_model_definition_copy(): defn = model_definition(10, [bb, niw(3)]) defn_shallow = copy.copy(defn) defn_deep = copy.deepcopy(defn) assert_is_not(defn, defn_shallow) assert_is_not(defn, defn_deep) assert_is_not(defn._models, defn_deep._models) assert_equals(defn.n(), defn_shallow.n()) assert_equals(defn.n(), defn_deep.n())
def test_runner_multyvac(): defn = model_definition(10, [bb, nich, niw(3)]) Y = toy_dataset(defn) view = numpy_dataview(Y) kc = runner.default_kernel_config(defn) prng = rng() latents = [model.initialize(defn, view, prng) for _ in xrange(2)] runners = [runner.runner(defn, view, latent, kc) for latent in latents] r = parallel.runner(runners, backend='multyvac', layer='perf', core='f2') r.run(r=prng, niters=1000) r.run(r=prng, niters=1000)
def test_runner_multiprocessing(): defn = model_definition(10, [bb, nich, niw(3)]) Y = toy_dataset(defn) view = numpy_dataview(Y) kc = runner.default_kernel_config(defn) prng = rng() latents = [model.initialize(defn, view, prng) for _ in xrange(mp.cpu_count())] runners = [runner.runner(defn, view, latent, kc) for latent in latents] r = parallel.runner(runners) # check it is restartable r.run(r=prng, niters=10) r.run(r=prng, niters=10)
def _test_multivariate_models(initialize_fn, dataview, bind, gibbs_assign, R): # XXX: this test only checks that the operations don't crash mu = np.ones(3) kappa = 0.3 Q = random_orthonormal_matrix(3) psi = np.dot(Q, np.dot(np.diag([1.0, 0.5, 0.2]), Q.T)) nu = 6 N = 10 def genrow(): return tuple([ np.random.choice([False, True]), [np.random.uniform(-3.0, 3.0) for _ in xrange(3)] ]) X = np.array([genrow() for _ in xrange(N)], dtype=[('', bool), ('', float, (3, ))]) view = dataview(X) defn = model_definition(N, [bb, niw(3)]) s = initialize_fn(defn, view, cluster_hp={'alpha': 2.}, feature_hps=[{ 'alpha': 2., 'beta': 2. }, { 'mu': mu, 'kappa': kappa, 'psi': psi, 'nu': nu }], r=R) bound_s = bind(s, view) for _ in xrange(10): gibbs_assign(bound_s, R)
def _test_multivariate_models(initialize_fn, dataview, bind, gibbs_assign, R): # XXX: this test only checks that the operations don't crash mu = np.ones(3) kappa = 0.3 Q = random_orthonormal_matrix(3) psi = np.dot(Q, np.dot(np.diag([1.0, 0.5, 0.2]), Q.T)) nu = 6 N = 10 def genrow(): return tuple( [np.random.choice([False, True]), [np.random.uniform(-3.0, 3.0) for _ in xrange(3)]]) X = np.array([genrow() for _ in xrange(N)], dtype=[('', bool), ('', float, (3,))]) view = dataview(X) defn = model_definition(N, [bb, niw(3)]) s = initialize_fn( defn, view, cluster_hp={'alpha': 2.}, feature_hps=[ {'alpha': 2., 'beta': 2.}, {'mu': mu, 'kappa': kappa, 'psi': psi, 'nu': nu} ], r=R) bound_s = bind(s, view) for _ in xrange(10): gibbs_assign(bound_s, R)
def test_default_parameters(): models = [bb, bnb, gp, nich, dd(5), bbnc, niw(10)] for m in models: typ = m.py_desc()._model_module s = typ.Shared() s.load(m.default_hyperparams())
def test_runner_default_kernel_config_nonconj(): models = [bbnc, nich, niw(3)] _test_runner_kernel_config(runner.default_kernel_config, models)
def test_copy_state(): defn = model_definition(10, [bb, niw(3)]) _test_copy_state(defn, cxx_initialize, bind)
def test_models_dtype(): assert_equals(niw(3).py_desc().get_np_dtype().shape, (3,)) assert_equals(niw(5).py_desc().get_np_dtype().shape, (5,))
def test_models_dtype(): assert_equals(niw(3).py_desc().get_np_dtype().shape, (3, )) assert_equals(niw(5).py_desc().get_np_dtype().shape, (5, ))