Commit 1642c051 authored by Devanshu Agrawal's avatar Devanshu Agrawal
Browse files

Add Iris.

parent 358d4269
Loading
Loading
Loading
Loading

log_lik/iris.py

0 → 100644
+57 −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 Iris data
X, y = datasets.load_iris(return_X_y=True)

# one-hot encode
Y = np.zeros((len(y), 3))
for i in range(3):
	Y[y==i, i] = 1

# 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/iris", exist_ok=True)
filename = "results/iris/depth"+"-".join(map(str, depths))+".csv"
data.to_csv(filename, index=False)

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