bw = experiment.m_vb.likelihood.binsize plt.plot(X,np.mean(np.exp(experiment.fsamples_vb)*bw,axis=1) ,'b' ,label='VB+Gaussian') plt.plot(X,np.mean(np.exp(experiment.fsamples_mc)*bw,axis=1) ,'r' ,label='VB+MCMC') plt.legend() plt.plot(X,np.percentile(np.exp(experiment.fsamples_vb)*bw, 5,axis=1), 'b-') plt.plot(X,np.percentile(np.exp(experiment.fsamples_vb)*bw,95,axis=1), 'b-') plt.plot(X,np.percentile(np.exp(experiment.fsamples_mc)*bw, 5,axis=1), 'r-') plt.plot(X,np.percentile(np.exp(experiment.fsamples_mc)*bw,95,axis=1), 'r-') #load and plot raw data X = load_coal() plt.plot(X, X*0, 'k|') plt.xlabel('time (years)') plt.ylabel('rate') plt.ylim(-.05, 1) plt.xlim(X.min(), X.max()) save_tikz('coal_rates.tikz',figurewidth='\\figurewidth', figureheight = '\\figureheight') plt.figure() #trans = GPy.core.parameterization.transformations.Logexp() trans = GPy.core.parameterization.transformations.Exponent() plt.hist(trans.f(experiment.samples[:,0]), 100, normed=True) plt.xlabel('signal varaince') #save_tikz('coal_variance.tikz',figurewidth='\\figurewidth', figureheight = '\\figureheight') np.savetxt('coal_var_samples',trans.f(experiment.samples[:,0])) plt.figure() plt.hist(trans.f(experiment.samples[:,1]), 100, normed=True) plt.xlabel('lengthscale') #save_tikz('coal_lengthscale.tikz',figurewidth='\\figurewidth', figureheight = '\\figureheight') np.savetxt('coal_ls_samples',trans.f(experiment.samples[:,1])) #plota scatter of variance, ls
samples[pointIndex,:] = means[currentPointCategory] + np.dot(Ls[currentPointCategory],rng.randn(2) ) categories[pointIndex] = currentPointCategory return samples, categories if __name__ == '__main__': nTrainingSamples = 750 nTestingSamples = 750 rng = np.random.RandomState( 1 ) X_train, Y_train = getSamples( nTrainingSamples, rng ) X_test, Y_test = getSamples( nTrainingSamples, rng ) from matplotlib import pylab as plt RTrain = np.array( [ X_train[index,:] for index in range(X_train.shape[0] ) if Y_train[index]== 0 ] ) GTrain = np.array( [ X_train[index,:] for index in range(X_train.shape[0] ) if Y_train[index]== 1 ] ) BTrain = np.array( [ X_train[index,:] for index in range(X_train.shape[0] ) if Y_train[index]== 2 ] ) plt.plot( RTrain[:,0], RTrain[:,1] ,'ro') plt.plot( GTrain[:,0], GTrain[:,1] , 'bo') plt.plot( BTrain[:,0], BTrain[:,1] , 'go') plt.xlim( [-6.,6.] ) plt.ylim( [-6.,6.] ) data = {'X_train':X_train,'Y_train':Y_train,'X_test':X_test,'Y_test':Y_test} cPickle.dump( data , open('data','w') ) from matplotlib2tikz import save as save_tikz save_tikz('simple_data.tikz')
m_mc.likelihood.delta.fix(1e-3) m_vb.Z.fix() m_vb.kern.fix() #m_vb.optimize('bfgs', max_iters=vb_frozen_iters, messages=True) #m_vb.Z.unfix() #m_vb.kern.constrain_positive() #m_vb.kern.white.fix(1e-3) # to keep the same as mcmc #m_vb.optimize('bfgs', max_iters=vb_max_iters, messages=True) m_vb = GPy.load('m_vb') drawPredictionSpace(m_vb) from matplotlib2tikz import save as save_tikz save_tikz('simple_vb.tikz') #m_mc.kern.rbf[:] = m_vb.kern.rbf[:]*1 m_mc.Z[:] = m_vb.Z[:] * 1 m_mc.Z.fix() L = GPy.util.choleskies.flat_to_triang(m_vb.q_u_chol) U = np.vstack( [np.dot(L[i, :, :], np.random.randn(m_mc.V.shape[0])) for i in range(3)]).T U = U + m_vb.q_u_mean K = m_mc.kern.K(m_mc.Z) L = GPy.util.linalg.jitchol(K) m_mc.V[:] = GPy.util.linalg.dtrtrs(L, U)[0] #samples mcmc def f(x):
epochs_test_loss=epochs_test_loss) plt.figure(0) plt.xlabel("Iteration") plt.ylabel("Loss") plt.plot(train_batches_loss, "g-", label="Loss", alpha=0.2) plt.plot(moving_average(train_batches_loss, moving_average_window_size, use_fraction=True), "g-", label="Loss MA") plt.legend() plt.tight_layout() plt.savefig(os.path.join(path, "train_batches_loss.png")) save_tikz(os.path.join(path, "train_batches_loss.tikz"), figureheight="\\figureheight", figurewidth="\\figurewidth") plt.figure(1) plt.xlabel("Iteration") plt.ylabel("Accuracy") plt.plot(train_batches_accuracy, "g-", label="Accuracy", alpha=0.2) plt.plot(moving_average(train_batches_accuracy, moving_average_window_size, use_fraction=True), "g-", label="Accuracy MA") plt.legend() plt.tight_layout() plt.savefig(os.path.join(path, "train_batches_accuracy.png")) save_tikz(os.path.join(path, "train_batches_accuracy.tikz"),
def output_current_plot(fileName): save_tikz(fileName, figurewidth='\\figurewidth', figureheight='\\figureheight')
'b-') plt.plot(X, np.percentile(np.exp(experiment.fsamples_vb) * bw, 95, axis=1), 'b-') plt.plot(X, np.percentile(np.exp(experiment.fsamples_mc) * bw, 5, axis=1), 'r-') plt.plot(X, np.percentile(np.exp(experiment.fsamples_mc) * bw, 95, axis=1), 'r-') #load and plot raw data X = load_coal() plt.plot(X, X * 0, 'k|') plt.xlabel('time (years)') plt.ylabel('rate') plt.ylim(-.05, 1) plt.xlim(X.min(), X.max()) save_tikz('coal_rates.tikz', figurewidth='\\figurewidth', figureheight='\\figureheight') plt.figure() #trans = GPy.core.parameterization.transformations.Logexp() trans = GPy.core.parameterization.transformations.Exponent() plt.hist(trans.f(experiment.samples[:, 0]), 100, normed=True) plt.xlabel('signal varaince') #save_tikz('coal_variance.tikz',figurewidth='\\figurewidth', figureheight = '\\figureheight') np.savetxt('coal_var_samples', trans.f(experiment.samples[:, 0])) plt.figure() plt.hist(trans.f(experiment.samples[:, 1]), 100, normed=True) plt.xlabel('lengthscale') #save_tikz('coal_lengthscale.tikz',figurewidth='\\figurewidth', figureheight = '\\figureheight') np.savetxt('coal_ls_samples', trans.f(experiment.samples[:, 1]))
size=12, ha="center", transform=ax[i].transAxes) ax[i].xaxis.set_major_locator(plt.NullLocator()) ax[i].yaxis.set_major_locator(plt.NullLocator()) ax[i].imshow(groups[i][0][:, :, [2, 1, 0]], cmap="gray", vmin=0, vmax=1) plt.tight_layout() if arg_write: plt.savefig( os.path.join(plot_path, "_".join([obj_name, "grouping.png"]))) save_tikz(os.path.join( plot_path, "_".join([obj_name, "grouping.tikz"])), figureheight="\\figureheight", figurewidth="\\figurewidth") if not arg_groups: plt.close() #plots saliency maps of each view if arg_saliency or arg_write: if scores.size and group_ids.size and group_weights.size: groups = sorted(zip(views, scores, group_ids, saliency), key=lambda x: (x[2], x[1])) saliency = [s[3] for s in groups] fig, ax = plt.subplots(rows_in_plot, int(arg_views / rows_in_plot), figsize=(6.2, 2), dpi=300) plt.subplots_adjust()
from matplotlib import pylab as plt RTrain = np.array([ X_train[index, :] for index in range(X_train.shape[0]) if Y_train[index] == 0 ]) GTrain = np.array([ X_train[index, :] for index in range(X_train.shape[0]) if Y_train[index] == 1 ]) BTrain = np.array([ X_train[index, :] for index in range(X_train.shape[0]) if Y_train[index] == 2 ]) plt.plot(RTrain[:, 0], RTrain[:, 1], 'ro') plt.plot(GTrain[:, 0], GTrain[:, 1], 'bo') plt.plot(BTrain[:, 0], BTrain[:, 1], 'go') plt.xlim([-6., 6.]) plt.ylim([-6., 6.]) data = { 'X_train': X_train, 'Y_train': Y_train, 'X_test': X_test, 'Y_test': Y_test } cPickle.dump(data, open('data', 'w')) from matplotlib2tikz import save as save_tikz save_tikz('simple_data.tikz')
m_vb.likelihood.delta.fix(1e-3) m_mc.likelihood.delta.fix(1e-3) m_vb.Z.fix() m_vb.kern.fix() #m_vb.optimize('bfgs', max_iters=vb_frozen_iters, messages=True) #m_vb.Z.unfix() #m_vb.kern.constrain_positive() #m_vb.kern.white.fix(1e-3) # to keep the same as mcmc #m_vb.optimize('bfgs', max_iters=vb_max_iters, messages=True) m_vb = GPy.load('m_vb') drawPredictionSpace( m_vb ) from matplotlib2tikz import save as save_tikz save_tikz('simple_vb.tikz') #m_mc.kern.rbf[:] = m_vb.kern.rbf[:]*1 m_mc.Z[:] = m_vb.Z[:]*1 m_mc.Z.fix() L = GPy.util.choleskies.flat_to_triang(m_vb.q_u_chol) U = np.vstack([np.dot(L[i,:,:], np.random.randn(m_mc.V.shape[0])) for i in range(3)]).T U = U + m_vb.q_u_mean K = m_mc.kern.K(m_mc.Z) L = GPy.util.linalg.jitchol(K) m_mc.V[:] = GPy.util.linalg.dtrtrs(L, U)[0] #samples mcmc def f(x): return (-a for a in m_mc._objective_grads(x)) #m_mc.kern.fix()