Ejemplo n.º 1
0
def model_sample(cll=None):
    # wrap params for use in model -- required
    # decoder = pyro.module("decoder", pt_decode)

    # sample from prior
    z_mu, z_sigma = Variable(torch.zeros(
        [1, 20])), Variable(torch.ones([1, 20]))

    # sample
    z = pyro.sample("latent", DiagNormal(z_mu, z_sigma))

    alpha = Variable(torch.ones([1, 10]) / 10.)

    if cll.data.cpu().numpy() is None:
        bb()
        cll = pyro.sample('class', Categorical(alpha))
        print('sampling class')

    # decode into size of imgx1 for mu
    img_mu = pt_decode.forward(z, cll)
    # bb()
    # img=Bernoulli(img_mu).sample()
    # score against actual images
    img = pyro.sample("sample", Bernoulli(img_mu))
    # return img
    return img, img_mu
Ejemplo n.º 2
0
def run_pyro_nuts(data, pfile, n_samples, params):

    # import model, transformed_data functions (if exists) from pyro module

    model = import_by_string(pfile + ".model")
    assert model is not None, "model couldn't be imported"
    transformed_data = import_by_string(pfile + ".transformed_data")
    if transformed_data is not None:
        transformed_data(data)

    nuts_kernel = NUTS(model, step_size=0.0855)
    mcmc_run = MCMC(nuts_kernel,
                    num_samples=n_samples,
                    warmup_steps=int(n_samples / 2))
    posteriors = {k: [] for k in params}

    for trace, _ in mcmc_run._traces(data, params):
        for k in posteriors:
            posteriors[k].append(trace.nodes[k]['value'])

    #posteriors["sigma"] = list(map(torch.exp, posteriors["log_sigma"]))
    #del posteriors["log_sigma"]

    posterior_means = {
        k: torch.mean(torch.stack(posteriors[k]), 0)
        for k in posteriors
    }
    bb()
    return posterior_means
Ejemplo n.º 3
0
def render_update(full_geo, viewport, print_render=False):

    # get our init state with some info
    init_state, geo_states = full_geo[0], full_geo[1:]
    img_size = init_state["img_size"]
    start_viewport = init_state["viewport"]

    vine_json = {
        "depth": init_state["depth"],
        "pos": {"x": init_state["pos"].x.item(), "y": init_state["pos"].y.item()},
        "width": init_state["width"].item(),
        "angle": init_state["angle"].item(),
        "img_size": list(init_state['img_size']),
        "viewport": {"xmin": start_viewport.xmin, "xmax": start_viewport.xmax,
                     "ymin": start_viewport.ymin, "ymax": start_viewport.ymax},
        "vines": [geo_to_json(state) for state in geo_states]
    }
    # bb()
    vv = PCGVineView()
    ss = ProtoScreenshot()
    from json import dumps

    # parse our json into the actual object
    Parse(dumps(vine_json), vv)

    queue_name = 'task_queue'
    print("Getting class information from Unity")
    client = BasicClient(queue_name).connect()

    # from IPython import embed
    # embed()

    cs_response = client.sync_call(client.get_message(vv, ss))
    bb()
Ejemplo n.º 4
0
def main(args):
    policies = init(args.num_players)
    model(policies)
    bb()
    lr = 1e-2
    inference = SVI(model, guide, Adam({"lr": lr}), 'ELBO')
    for i in range(args.num_samples):
        loss = inference.step()
        print("loss = {}".format(loss))
    print(pyro.get_param_store()._params)
Ejemplo n.º 5
0
def compare_models(data, sfile, pfile, n_samples=2000, model_cache=None):
    validate_data_def, init_params, model, transformed_data = get_fns_pyro(
        pfile)

    s_site_means = run_stan_nuts(data,
                                 sfile,
                                 n_samples=n_samples,
                                 model_cache=model_cache)
    # breakpoint
    initialized_params = {}
    tensorize_data(data)
    transformed_data(data)
    init_params(data, initialized_params)
    p_site_means = run_pyro_advi(data, validate_data_def, initialized_params,
                                 model)

    #TODO: check that these means are close to each other for each parameter

    bb()
Ejemplo n.º 6
0
            self.connection.process_data_events()

        return self.msg_wrap.decode_to_proto(self.response)


if __name__ == '__main__':

    import sys

    queue_name = 'task_queue'

    print("Getting class information from Unity")
    client = BasicClient(queue_name).connect()

    # lets say hello
    print("Class info achieved, saying hello")
    hello_friend = ProtoHello()
    hello_friend.proto_message = 'hi friend from python'

    # empty call to serve, wait for response
    cs_response = client.sync_call(client.get_message(hello_friend))
    print("simple call and respond {}".format(cs_response))

    ss_for_me = ProtoScreenShot()
    ss_for_me.width = 32
    ss_for_me.height = 32
    ss_response = client.sync_call(client.get_message(ss_for_me))
    np_ss = screenshot_to_np(ss_response[0][1])

    bb()