def test_load_save(self): cov_main = GPCov(wfn_params=[1.0,], dfn_params=[ 2.5,], wfn_str="compact0", dfn_str="euclidean") cov_fic = GPCov(wfn_params=[1.0,], dfn_params=[ 1.5,], wfn_str="se", dfn_str="euclidean", Xu = self.u) noise_var = 1.0 gp1 = GP(X=self.X, y=self.y1, noise_var = noise_var, cov_main = cov_main, cov_fic = cov_fic, compute_ll=True, sparse_threshold=0, build_tree=False) gp1.save_trained_model("test_csfic.npz") gp2 = GP(fname="test_csfic.npz", build_tree=False) pts = np.reshape(np.linspace(-5, 5, 20), (-1, 1)) p1 = gp1.predict(pts) v1 = gp1.variance(pts) p2 = gp2.predict(pts) v2 = gp2.variance(pts) self.assertTrue((p1 == p2).all()) self.assertTrue((v1 == v2).all())
def trained_gp(dataset_name, model_name, n=None, tag=None, **kwargs): fname = gp_fname(dataset_name, model_name, n=n, tag=tag) if os.path.exists(fname): return GP(fname=fname, **kwargs) X_train, y_train = training_data(dataset_name, n=n) cov_main, cov_fic, noise_var = load_hparams(dataset_name, model_name, tag=tag) if model_name=="se": kwargs['sparse_invert'] = False kwargs['build_tree'] = False "print training GP for", dataset_name, model_name sgp = GP(X=X_train, y=y_train, noise_var=noise_var, cov_main=cov_main, cov_fic=cov_fic, sparse_threshold=1e-8, **kwargs) sgp.save_trained_model(fname) print "saved trained GP to", fname stats_fname = fname[:-3] + "_stats.txt" with open(stats_fname, 'w') as f: f.write("main hyperparams: %s\n" % cov_main) f.write("fic hyperparams: %s\n" % cov_fic) if scipy.sparse.issparse(sgp.K): nzr, nzc = sgp.K.nonzero() f.write("K nonzero: %d/%d = %.3f%%\n" % (len(nzr), sgp.K.shape[0]**2, float(len(nzr))/sgp.K.shape[0]**2)) if scipy.sparse.issparse(sgp.Kinv): nzr, nzc = sgp.Kinv.nonzero() f.write("Kinv nonzero: %d/%d = %.3f%%\n" % (len(nzr), sgp.Kinv.shape[0]**2, float(len(nzr))/sgp.Kinv.shape[0]**2)) for k in sgp.timings: f.write("timing %s: %.3f\n" % (k, sgp.timings[k])) print "saved stats to", stats_fname return sgp