Commit 961d9df6 authored by Devanshu Agrawal's avatar Devanshu Agrawal
Browse files

Add Boston experiment.

parent 69a0ebc5
Loading
Loading
Loading
Loading

log_lik/boston.py

0 → 100644
+60 −0
Original line number Diff line number Diff line
import os
import argparse
import time
import pickle
import random
import itertools
import numpy as np
import pandas as pd
from sklearn import datasets
import models

random.seed(123)
np.random.seed(456)

parser = argparse.ArgumentParser(description="Convergence of bottleneck NNGP to NNGP in log-likelihood.")
parser.add_argument("--depths", "-d", dest="depths", type=lambda s: [int(i) for i in s.split(",")], default="1,1", help="comma-separated depths of bottleneck components.")
args = parser.parse_args()

# load Boston data
X, Y = datasets.load_boston(return_X_y=True)
Y = Y.reshape((-1, 1))
#Y = np.tile(Y, [1, 2])

# take smaller sample
#indices = np.arange(X.shape[0])
#np.random.shuffle(indices)
#X = X[indices[:200]]
#Y = Y[indices[:200]]

# standardize
X = (X - np.mean(X, axis=0, keepdims=True))/np.std(X, axis=0, keepdims=True)
Y = (Y - np.mean(Y, axis=0, keepdims=True))/np.std(Y, axis=0, keepdims=True)

n_samples = 10**3
widths = [1, 2, 3, 4, 5, 10, 50, 100, 500, np.inf]
depths = args.depths

# variance hyperparameters
v_b = 1.0
v_w = 1.0
v_n = 1e-4

liks = []
for width in widths:
	print("width:", width)
	ds = [sum(depths)+len(depths)-1] if width == np.inf else depths
	ws = [] if width == np.inf else [width]*(len(depths)-1)
	prior = models.bottleneck_nngp_prior(X, output_dims=Y.shape[1], v_b=v_b, v_w=v_w, v_n=v_n, depths=ds, widths=ws, bottleneck_activation=True)
	l = prior.log_likelihood(Y, n_samples=n_samples)/X.shape[0]
	liks.append(l)

liks = list(map(lambda x: np.round(x, 3), liks))
data = pd.DataFrame.from_dict({"width": widths, "lik": liks})[["width", "lik"]]

# write to CSV
os.makedirs("results/boston", exist_ok=True)
filename = "results/boston/depth"+"-".join(map(str, depths))+".csv"
data.to_csv(filename, index=False)

print("Done!")
 No newline at end of file