Ejemplo n.º 1
0
    pyro.sample("prediction", Delta(model(x_data, y_data)))


for name, value in pyro.get_param_store().items():
    print(name, pyro.param(name))

for name, value in pyro.get_param_store().items():
    print(name, pyro.param(name).cpu().detach().numpy().mean())

posterior = svi.run(Xtrain, Ytrain)

# Break
#pdb.set_trace()

# posterior predictive distribution we can get samples from
trace_pred = TracePredictive(wrapped_model, posterior, num_samples=100)
post_pred = trace_pred.run(Xtrain, None)  #inputing pca components?
post_summary = summary(post_pred, sites=['prediction', 'obs'])
meuw = post_summary["prediction"]
y = post_summary["obs"]
meuw.insert(0, 'true', np.array(Ytrain.cpu()))
y.insert(0, 'true', np.array(Ytrain.cpu()))

print("sample meuw data:")
print(meuw.head(10))
#What's the difference between mu and y? Means are the same but sigma is very different.
print("sample y data:")
print(y.head(10))

df = pd.DataFrame(meuw)
nx = df.reset_index()  #insert a first row in Dataframe for index
        marginal_site = pd.DataFrame(marginal[:, i]).transpose()
        describe = partial(pd.Series.describe,
                           percentiles=[.05, 0.25, 0.5, 0.75, 0.95])
        site_stats[site_name] = marginal_site.apply(describe, axis=1) \
            [["mean", "std", "5%", "25%", "50%", "75%", "95%"]]
    return site_stats


def wrapped_model(x_data, y_data):
    pyro.sample("prediction", Delta(model(x_data, y_data)))


posterior = svi.run(data[0], data[1][:, -1])

# posterior predictive distribution we can get samples from
trace_pred = TracePredictive(wrapped_model, posterior, num_samples=1000)
post_pred = trace_pred.run(data[0], None)
post_summary = summary(post_pred, sites=['prediction', 'obs'])
mu = post_summary["prediction"]
y = post_summary["obs"]
y.insert(0, 'true', data[1].cpu().numpy())

print("sample y data:")
print(y.head(10))

print("mu_mean")
print(mu["mean"])
print("mu_5_pct")
print(mu["5%"])
print("mu_50_pct")
print(mu["50%"])
Ejemplo n.º 3
0
    try:
        site = new_sites[i]
        sns.distplot(svi_samples[site], ax=ax, label="Posterior Distribution")
        ax.set_title(site, fontsize=12)
    except IndexError:
        break
handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper right')
plt.savefig("posterior_dists.png")

# sense check with standard package implementation of logistic regression
sklearn_model = LogisticRegression(solver="lbfgs")
sklearn_model.fit(X_train_scaled, y_train)

trace_pred = TracePredictive(predictive_model,
                             svi_meanfield_posterior,
                             num_samples=1000)
X_tensor = torch.tensor(X_test_scaled, dtype=torch.float32)
y_tensor = torch.tensor(y_test.values, dtype=torch.float32)
posterior_predictive = trace_pred.run(X_tensor, None)
sites = ["prediction", "obs"]
posterior_predictive_samples = \
    {site: EmpiricalMarginal(posterior_predictive, sites=site)
     .enumerate_support().detach().cpu() for site in sites}

subset = posterior_predictive_samples["prediction"][:, 10:20]
y_pred_sklearn = sklearn_model.predict(X_test_scaled)
subset_sklearn = sklearn_model.predict_proba(X_test_scaled)[10:20, 1]

fig, axs = plt.subplots(nrows=5,
                        ncols=2,
        site_stats[site_name] = marginal_site.apply(describe, axis=1) \
            [["mean", "std", "5%", "25%", "50%", "75%", "95%"]]
    return site_stats


def wrapped_model(x_data, y_data):
    pyro.sample("prediction", Delta(model(x_data, y_data)))


#moved up  posterior = svi.run(data_train[0], data_train[1][:,-1])

# Break
# import Ipython; Ipython.embed()

# posterior predictive distribution we can get samples from
trace_pred = TracePredictive(wrapped_model, posterior, num_samples=100)
post_pred = trace_pred.run(data_train[0], None)  #inputing pca components?
post_summary = summary(post_pred, sites=['prediction', 'obs'])
meuw = post_summary["prediction"]
y = post_summary["obs"]
meuw.insert(0, 'true', data_train[1].cpu().numpy())
y.insert(0, 'true', data_train[1].cpu().numpy())

print("sample meuw data:")
print(meuw.head(10))
#What's the difference between meuw and y? Means are the same but sigma is very different.
print("sample y data:")
print(y.head(10))

df = pd.DataFrame(meuw)
nx = df.reset_index()  #insert a first row in Dataframe for index
        site_stats[site_name] = marginal_site.apply(describe, axis=1) \
            [["mean", "std", "5%", "25%", "50%", "75%", "95%"]]
    return site_stats

def wrapped_model(x_data, y_data):
    pyro.sample("prediction", Delta(model(x_data, y_data)))




posterior = svi.run(data_train[0], data_train[1][:,-1])


# posterior predictive distribution we can get samples from
trace_pred = TracePredictive(wrapped_model,
                             posterior,
                             num_samples=1000)
post_pred = trace_pred.run(data_train[0], None)  #check Why data_train[0] ?
post_summary = summary(post_pred, sites= ['prediction', 'obs'])
mu = post_summary["prediction"]
y = post_summary["obs"]
y.insert(0, 'true', data_train[1].cpu().numpy())

print("sample y data:")
print(y.head(10))

df = pd.DataFrame(y) 
nx = df.reset_index()  #insert a first row in Dataframe for index
nx = nx.values  #Convert Dataframe to array
fig = plt.figure(dpi=100, figsize=(5, 4))
plt.scatter(nx[:,0],nx[:,1], c='b')