Commit 560a887f authored by Devanshu Agrawal's avatar Devanshu Agrawal
Browse files

Add script to make digures.

parent 492c23de
Loading
Loading
Loading
Loading
+99 −0
Original line number Diff line number Diff line
import os
import json
import numpy as np
import pandas as pd

import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
#matplotlib.rc("xtick", labelsize=14)
#matplotlib.rc("ytick", labelsize=14)


def heat_maps(dataset, depths, widths):
	dir = os.path.join("figs", dataset, "hm")
	os.makedirs(dir, exist_ok=True)
	quants = ["loss", "v_b", "v_w", "v_n"]
	titles = ["Loss", "$v_b$", "$v_w$", "$v_n$"]
	ims = {q: [] for q in quants}
	for d in depths:
		ims_d = {q: [] for q in quants}
		for w in widths:
			with open( os.path.join("../results", dataset, "depths1_{}_widths{}.json".format(d, w)), "r") as fp:
				results = json.load(fp)
			for q in quants:
				ims_d[q].append( results["test"][q] )
		for q in quants:
			ims[q].append( ims_d[q] )
	ims = {q: np.array(v) for (q, v) in ims.items()}
	for (q, title) in zip(quants, titles):
		plt.figure()
		plt.imshow(ims[q], cmap="YlGnBu_r")
		plt.colorbar()
		plt.xticks(np.arange(len(widths))+1, widths)
		plt.yticks(np.arange(len(depths))+1, depths)
		plt.xlabel("Width", fontsize=12)
		plt.ylabel("Depth", fontsize=12)
		plt.title(title, fontsize=16)
		plt.tight_layout()
		filename = os.path.join(dir, "{}.png".format(q))
		plt.savefig(filename, box_inches="tight")
		plt.close()


def learning_curves(dataset, depth, width):
	with open( os.path.join("../results", dataset, "depths1_{}_widths{}.json".format(depth, width)), "r") as fp:
		results = json.load(fp)
		dir = os.path.join("figs", dataset, "lc", "d{}w{}".format(depth, width))
	os.makedirs(dir, exist_ok=True)
	# loss
	iters = np.arange(1, len(results["train"]["loss"])+1)
	plt.figure()
	plt.plot(iters, results["train"]["loss"], color="red", label="Before update")
	plt.plot(iters, results["train"]["loss_new"], color="blue", label="After update")
	plt.legend(loc="upper right", bbox_to_anchor=(1, 1), fancybox=True, fontsize=10)
	plt.xlabel("Iteration", fontsize=12)
	plt.title("Loss", fontsize=16)
	plt.tight_layout()
	filename = os.path.join(dir, "loss.png")
	plt.savefig(filename, box_inches="tight")
	plt.close()
	# delta loss
	plt.figure()
	plt.plot(iters, results["train"]["delta_loss"], color="black")
	plt.xlabel("Iteration", fontsize=12)
	plt.ylabel("(%)", fontsize=12)
	plt.title("Change in loss", fontsize=16)
	plt.tight_layout()
	filename = os.path.join(dir, "delta_loss.png")
	plt.savefig(filename, box_inches="tight")
	plt.close()
	# parameters
	iters = np.arange(len(results["train"]["loss"])+1)
	plt.figure()
	for (param, color) in zip(["v_b", "v_w", "v_n"], ["red", "green", "blue"]):
		plt.plot(iters, results["train"][param], color=color, label="${}$".format(param))
	plt.legend(loc="upper right", bbox_to_anchor=(1, 1), fancybox=True, fontsize=10)
	plt.xlabel("Iteration", fontsize=12)
	plt.title("Hyperparameters", fontsize=16)
	plt.tight_layout()
	filename = os.path.join(dir, "params.png")
	plt.savefig(filename, box_inches="tight")
	plt.close()


if __name__ == "__main__":
	dataset = "boston"

	hm_depths = [10*i for i in range(1, 10)]
	hm_widths = [4, 16, 64, 256]
	heat_maps(dataset, hm_depths, hm_widths)

	lc_depths = [10, 30, 50, 70, 90]
	lc_widths = [4, 16, 64, 256]
	for d in lc_depths:
		for w in lc_widths:
			learning_curves(dataset, d, w)


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