%pip install mlai %pip install notutils %pip install daft import matplotlib # Comment for google colab (no latex available) #matplotlib.rc('text', usetex=True) #matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"] import mlai.plot as plot #plot.deep_nn(diagrams='./deepgp/') from IPython.lib.display import YouTubeVideo YouTubeVideo('WXuK6gekU1Y') from IPython.lib.display import YouTubeVideo YouTubeVideo('iWGhXof45zI') import mlai %load -n mlai.Kernel # %load -n mlai.Kernel class Kernel(): """Covariance function :param function: covariance function :type function: function :param name: name of covariance function :type name: string :param shortname: abbreviated name of covariance function :type shortname: string :param formula: latex formula of covariance function :type formula: string :param function: covariance function :type function: function :param \**kwargs: See below :Keyword Arguments: * """ def __init__(self, function, name=None, shortname=None, formula=None, **kwargs): self.function=function self.formula = formula self.name = name self.shortname = shortname self.parameters=kwargs def K(self, X, X2=None): """Compute the full covariance function given a kernel function for two data points.""" if X2 is None: X2 = X K = np.zeros((X.shape[0], X2.shape[0])) for i in np.arange(X.shape[0]): for j in np.arange(X2.shape[0]): K[i, j] = self.function(X[i, :], X2[j, :], **self.parameters) return K def diag(self, X): """Compute the diagonal of the covariance function""" diagK = np.zeros((X.shape[0], 1)) for i in range(X.shape[0]): diagK[i] = self.function(X[i, :], X[i, :], **self.parameters) return diagK def _repr_html_(self): raise NotImplementedError import mlai %load -n mlai.eq_cov # %load -n mlai.eq_cov def eq_cov(x, x_prime, variance=1., lengthscale=1.): """Exponentiated quadratic covariance function.""" diffx = x - x_prime return variance*np.exp(-0.5*np.dot(diffx, diffx)/lengthscale**2) kernel = Kernel(function=eq_cov, name='Exponentiated Quadratic', shortname='eq', lengthscale=0.25) import numpy as np np.random.seed(10) import mlai.plot as plot plot.rejection_samples(kernel=kernel, diagrams='./gp') import notutils as nu from ipywidgets import IntSlider nu.display_plots('gp_rejection_sample{sample:0>3}.png', directory='./gp', sample=IntSlider(1,1,5,1)) import mlai.plot as plot plot.low_rank_approximation(diagrams='.') import mlai.plot as plot plot.deep_nn_bottleneck(diagrams='./deepgp') from matplotlib import rc rc("font", **{'family':'sans-serif','sans-serif':['Helvetica'],'size':30}) rc("text", usetex=True) pgm = plot.horizontal_chain(depth=5) pgm.render().figure.savefig("./deepgp/deep-markov.svg", transparent=True) from matplotlib import rc rc("font", **{'family':'sans-serif','sans-serif':['Helvetica'], 'size':15}) rc("text", usetex=True) pgm = plot.vertical_chain(depth=5) pgm.render().figure.savefig("./deepgp/deep-markov-vertical.svg", transparent=True) import daft pgm = plot.vertical_chain(depth=5, shape=[2, 7]) pgm.add_node(daft.Node('y_2', r'$\mathbf{y}_2$', 1.5, 3.5, observed=True)) pgm.add_edge('f_2', 'y_2') pgm.render().figure.savefig("./deepgp/deep-markov-vertical-side.svg", transparent=True) %pip install camb %pip install healpy %config IPython.matplotlib.backend = 'retina' %config InlineBackend.figure_format = 'retina' import matplotlib import matplotlib.pyplot as plt from matplotlib import rc from cycler import cycler import numpy as np rc("font", family="serif", size=14) rc("text", usetex=False) matplotlib.rcParams['lines.linewidth'] = 2 matplotlib.rcParams['patch.linewidth'] = 2 matplotlib.rcParams['axes.prop_cycle'] =\ cycler("color", ['k', 'c', 'm', 'y']) matplotlib.rcParams['axes.labelsize'] = 16 import healpy as hp import camb from camb import model, initialpower nside = 512 # Healpix parameter, giving 12*nside**2 equal-area pixels on the sphere. lmax = 3*nside # band-limit. Should be 2*nside < lmax < 4*nside to get information content. # Mostly following http://camb.readthedocs.io/en/latest/CAMBdemo.html with parameters from https://en.wikipedia.org/wiki/Lambda-CDM_model pars = camb.CAMBparams() pars.set_cosmology(H0=67.74, ombh2=0.0223, omch2=0.1188, mnu=0.06, omk=0, tau=0.066) pars.InitPower.set_params(ns=0.96, r=0) pars.set_for_lmax(lmax, lens_potential_accuracy=0); results = camb.get_results(pars) powers = results.get_cmb_power_spectra(pars) totCL = powers['total'] unlensedCL = powers['unlensed_scalar'] ells = np.arange(totCL.shape[0]) Dells = totCL[:, 0] Cells = Dells * 2*np.pi / ells / (ells + 1) # change of convention to get C_ell Cells[0:2] = 0 cmbmap = hp.synfast(Cells, nside, lmax=lmax, mmax=None, alm=False, pol=False, pixwin=False, fwhm=0.0, sigma=None, new=False, verbose=True) hp.mollview(cmbmap) fig = plt.gcf() mlai.write_figure('mollweide-sample-cmb.png', directory='./physics/') %pip install gpy %pip install --upgrade git+https://github.com/SheffieldML/PyDeepGP.git # Late bind setup methods to DeepGP object from mlai.deepgp_tutorial import initialize from mlai.deepgp_tutorial import staged_optimize from mlai.deepgp_tutorial import posterior_sample from mlai.deepgp_tutorial import visualize from mlai.deepgp_tutorial import visualize_pinball import deepgp deepgp.DeepGP.initialize=initialize deepgp.DeepGP.staged_optimize=staged_optimize deepgp.DeepGP.posterior_sample=posterior_sample deepgp.DeepGP.visualize=visualize deepgp.DeepGP.visualize_pinball=visualize_pinball %pip install pods import numpy as np import pods data = pods.datasets.olympic_marathon_men() x = data['X'] y = data['Y'] offset = y.mean() scale = np.sqrt(y.var()) yhat = (y - offset)/scale import matplotlib.pyplot as plt import mlai.plot as plot import mlai xlim = (1875,2030) ylim = (2.5, 6.5) fig, ax = plt.subplots(figsize=plot.big_wide_figsize) _ = ax.plot(x, y, 'r.',markersize=10) ax.set_xlabel('year', fontsize=20) ax.set_ylabel('pace min/km', fontsize=20) ax.set_xlim(xlim) ax.set_ylim(ylim) mlai.write_figure(filename='olympic-marathon.svg', directory='./datasets') import GPy m_full = GPy.models.GPRegression(x,yhat) _ = m_full.optimize() # Optimize parameters of covariance function xt = np.linspace(1870,2030,200)[:,np.newaxis] yt_mean, yt_var = m_full.predict(xt) yt_sd=np.sqrt(yt_var) import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax = plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m_full, scale=scale, offset=offset, ax=ax, xlabel="year", ylabel="pace min/km", fontsize=20, portion=0.2) ax.set_xlim(xlim) ax.set_ylim(ylim) mlai.write_figure(figure=fig, filename="olympic-marathon-gp.svg", directory = "./gp", transparent=True, frameon=True) x_clean=np.vstack((x[0:2, :], x[3:, :])) y_clean=np.vstack((yhat[0:2, :], yhat[3:, :])) m_clean = GPy.models.GPRegression(x_clean,y_clean) _ = m_clean.optimize() import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax = plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m_clean, scale=scale, offset=offset, ax=ax, xlabel='year', ylabel='pace min/km', fontsize=20, portion=0.2) ax.set_xlim(xlim) ax.set_ylim(ylim) mlai.write_figure(figure=fig, filename='./gp/olympic-marathon-gp.svg', transparent=True, frameon=True) import GPy import deepgp hidden = 1 m = deepgp.DeepGP([y.shape[1],hidden,x.shape[1]],Y=yhat, X=x, inits=['PCA','PCA'], kernels=[GPy.kern.RBF(hidden,ARD=True), GPy.kern.RBF(x.shape[1],ARD=True)], # the kernels for each layer num_inducing=50, back_constraint=False) # Call the initalization m.initialize() for layer in m.layers: layer.likelihood.variance.constrain_positive(warning=False) m.optimize(messages=True,max_iters=10000) m.staged_optimize(messages=(True,True,True)) import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax = plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m, scale=scale, offset=offset, ax=ax, xlabel='year', ylabel='pace min/km', fontsize=20, portion=0.2) ax.set_xlim(xlim) ax.set_ylim(ylim) mlai.write_figure(figure=fig, filename='./deepgp/olympic-marathon-deep-gp.svg', transparent=True, frameon=True) fig, ax = plt.subplots(figsize=plot.big_wide_figsize) plot.model_sample(m, scale=scale, offset=offset, samps=10, ax=ax, xlabel='year', ylabel='pace min/km', portion = 0.225) ax.set_xlim(xlim) ax.set_ylim(ylim) mlai.write_figure(figure=fig, filename='./deepgp/olympic-marathon-deep-gp-samples.svg', transparent=True, frameon=True) m.visualize(scale=scale, offset=offset, xlabel='year', ylabel='pace min/km',xlim=xlim, ylim=ylim, dataset='olympic-marathon', diagrams='./deepgp') import notutils as nu nu.display_plots('olympic-marathon-deep-gp-layer-{sample:0>1}.svg', './deepgp', sample=(0,1)) fig, ax = plt.subplots(figsize=plot.big_wide_figsize) m.visualize_pinball(ax=ax, scale=scale, offset=offset, points=30, portion=0.1, xlabel='year', ylabel='pace km/min', vertical=True) mlai.write_figure(figure=fig, filename='./deepgp/olympic-marathon-deep-gp-pinball.svg', transparent=True, frameon=True) num_low=25 num_high=25 gap = -.1 noise=0.0001 x = np.vstack((np.linspace(-1, -gap/2.0, num_low)[:, np.newaxis], np.linspace(gap/2.0, 1, num_high)[:, np.newaxis])) y = np.vstack((np.zeros((num_low, 1)), np.ones((num_high,1)))) scale = np.sqrt(y.var()) offset = y.mean() yhat = (y-offset)/scale fig, ax = plt.subplots(figsize=plot.big_wide_figsize) _ = ax.plot(x, y, 'r.',markersize=10) _ = ax.set_xlabel('$x$', fontsize=20) _ = ax.set_ylabel('$y$', fontsize=20) xlim = (-2, 2) ylim = (-0.6, 1.6) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(figure=fig, filename='./datasets/step-function.svg', transparent=True, frameon=True) m_full = GPy.models.GPRegression(x,yhat) _ = m_full.optimize() # Optimize parameters of covariance function fig, ax=plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m_full, scale=scale, offset=offset, ax=ax, fontsize=20, portion=0.5) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(figure=fig,filename='./gp/step-function-gp.svg', transparent=True, frameon=True) layers = [y.shape[1], 1, 1, 1,x.shape[1]] inits = ['PCA']*(len(layers)-1) kernels = [] for i in layers[1:]: kernels += [GPy.kern.RBF(i)] m = deepgp.DeepGP(layers,Y=yhat, X=x, inits=inits, kernels=kernels, # the kernels for each layer num_inducing=20, back_constraint=False) m.initialize() m.staged_optimize() fig, ax=plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m, scale=scale, offset=offset, ax=ax, fontsize=20, portion=0.5) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(filename='./deepgp/step-function-deep-gp.svg', transparent=True, frameon=True) import mlai.plot as plot fig, ax=plt.subplots(figsize=plot.big_wide_figsize) plot.model_sample(m, scale=scale, offset=offset, samps=10, ax=ax, portion = 0.5) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(figure=fig, filename='./deepgp/step-function-deep-gp-samples.svg', transparent=True, frameon=True) m.visualize(offset=offset, scale=scale, xlim=xlim, ylim=ylim, dataset='step-function', diagrams='./deepgp') import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax=plt.subplots(figsize=plot.big_wide_figsize) m.visualize_pinball(offset=offset, ax=ax, scale=scale, xlim=xlim, ylim=ylim, portion=0.1, points=50) mlai.write_figure(figure=fig, filename='./deepgp/step-function-deep-gp-pinball.svg', transparent=True, frameon=True, ax=ax) import pods data = pods.datasets.mcycle() x = data['X'] y = data['Y'] scale=np.sqrt(y.var()) offset=y.mean() yhat = (y - offset)/scale import matplotlib.pyplot as plt import mlai import mlai.plot as plot fig, ax = plt.subplots(figsize=plot.big_wide_figsize) _ = ax.plot(x, y, 'r.',markersize=10) _ = ax.set_xlabel('time', fontsize=20) _ = ax.set_ylabel('acceleration', fontsize=20) xlim = (-20, 80) ylim = (-175, 125) ax.set_xlim(xlim) ax.set_ylim(ylim) mlai.write_figure(filename='motorcycle-helmet.svg', directory='./datasets/', transparent=True, frameon=True) m_full = GPy.models.GPRegression(x,yhat) _ = m_full.optimize() # Optimize parameters of covariance function import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax=plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m_full, scale=scale, offset=offset, ax=ax, xlabel='time', ylabel='acceleration/$g$', fontsize=20, portion=0.5) xlim=(-20,80) ylim=(-180,120) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(figure=fig,filename='./gp/motorcycle-helmet-gp.svg', transparent=True, frameon=True) import deepgp layers = [y.shape[1], 1, x.shape[1]] inits = ['PCA']*(len(layers)-1) kernels = [] for i in layers[1:]: kernels += [GPy.kern.RBF(i)] m = deepgp.DeepGP(layers,Y=yhat, X=x, inits=inits, kernels=kernels, # the kernels for each layer num_inducing=20, back_constraint=False) m.initialize() m.staged_optimize(iters=(1000,1000,10000), messages=(True, True, True)) import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax=plt.subplots(figsize=plot.big_wide_figsize) plot.model_output(m, scale=scale, offset=offset, ax=ax, xlabel='time', ylabel='acceleration/$g$', fontsize=20, portion=0.5) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(filename='./deepgp/motorcycle-helmet-deep-gp.svg', transparent=True, frameon=True) import mlai.plot as plot import mlai fig, ax=plt.subplots(figsize=plot.big_wide_figsize) plot.model_sample(m, scale=scale, offset=offset, samps=10, ax=ax, xlabel='time', ylabel='acceleration/$g$', portion = 0.5) ax.set_ylim(ylim) ax.set_xlim(xlim) mlai.write_figure(figure=fig, filename='./deepgp/motorcycle-helmet-deep-gp-samples.svg', transparent=True, frameon=True) m.visualize(xlim=xlim, ylim=ylim, scale=scale,offset=offset, xlabel="time", ylabel="acceleration/$g$", portion=0.5, dataset='motorcycle-helmet', diagrams='./deepgp') fig, ax=plt.subplots(figsize=plot.big_wide_figsize) m.visualize_pinball(ax=ax, xlabel='time', ylabel='acceleration/g', points=50, scale=scale, offset=offset, portion=0.1) mlai.write_figure(figure=fig, filename='./deepgp/motorcycle-helmet-deep-gp-pinball.svg', transparent=True, frameon=True) from sklearn.datasets import fetch_openml mnist = fetch_openml('mnist_784') import numpy as np np.random.seed(0) digits = [0,1,2,3,4] N_per_digit = 100 Y = [] labels = [] for d in digits: imgs = mnist['data'][mnist['target']==str(d)] Y.append(imgs.loc[np.random.permutation(imgs.index)[:N_per_digit]]) labels.append(np.ones(N_per_digit)*d) Y = np.vstack(Y).astype(np.float64) labels = np.hstack(labels) Y /= 255 import deepgp import GPy num_latent = 2 num_hidden_2 = 5 m = deepgp.DeepGP([Y.shape[1],num_hidden_2,num_latent], Y, kernels=[GPy.kern.RBF(num_hidden_2,ARD=True), GPy.kern.RBF(num_latent,ARD=False)], num_inducing=50, back_constraint=False, encoder_dims=[[200],[200]]) m.obslayer.likelihood.variance[:] = Y.var()*0.01 for layer in m.layers: layer.kern.variance.fix(warning=False) layer.likelihood.variance.fix(warning=False) m.optimize(messages=False,max_iters=100) for layer in m.layers: layer.kern.variance.constrain_positive(warning=False) m.optimize(messages=False,max_iters=100) for layer in m.layers: layer.likelihood.variance.constrain_positive(warning=False) m.optimize(messages=True,max_iters=10000) import matplotlib.pyplot as plt import mlai.plot as plot import mlai from matplotlib import rc rc("font", **{'family':'sans-serif','sans-serif':['Helvetica'],'size':20}) fig, ax = plt.subplots(figsize=plot.big_figsize) for d in digits: ax.plot(m.layer_1.X.mean[labels==d,0],m.layer_1.X.mean[labels==d,1],'.',label=str(d)) _ = plt.legend() mlai.write_figure(figure=fig, filename="./deepgp/mnist-digits-subsample-latent.svg", transparent=True) m.obslayer.kern.lengthscale import matplotlib.pyplot as plt import mlai.plot as plot import mlai fig, ax = plt.subplots(figsize=plot.big_figsize) for i in range(5): for j in range(i): dims=[i, j] ax.cla() for d in digits: ax.plot(m.obslayer.X.mean[labels==d,dims[0]], m.obslayer.X.mean[labels==d,dims[1]], '.', label=str(d)) plt.legend() plt.xlabel('dimension ' + str(dims[0])) plt.ylabel('dimension ' + str(dims[1])) mlai.write_figure(figure=fig, filename="./deepgp/mnist-digits-subsample-hidden-" + str(dims[0]) + '-' + str(dims[1]) + '.svg', transparent=True) rows = 10 cols = 20 t=np.linspace(-1, 1, rows*cols)[:, None] kern = GPy.kern.RBF(1,lengthscale=0.05) cov = kern.K(t, t) x = np.random.multivariate_normal(np.zeros(rows*cols), cov, num_latent).T import matplotlib.pyplot as plt import mlai.plot as plot import mlai yt = m.predict(x) fig, axs = plt.subplots(rows,cols,figsize=(10,6)) for i in range(rows): for j in range(cols): #v = np.random.normal(loc=yt[0][i*cols+j, :], scale=np.sqrt(yt[1][i*cols+j, :])) v = yt[0][i*cols+j, :] axs[i,j].imshow(v.reshape(28,28), cmap='gray', interpolation='none', aspect='equal') axs[i,j].set_axis_off() mlai.write_figure(figure=fig, filename="./deepgp/digit-samples-deep-gp.svg", transparent=True)