def setUp(self): # Load data Y = np.loadtxt(os.path.join(base_path, self.outputfile)) X = np.loadtxt(os.path.join(base_path, self.inputfile)) m = deepgp.DeepGP([Y.shape[1], 5, X.shape[1]], Y, X=X, kernels=[ GPy.kern.RBF(5, ARD=True), GPy.kern.RBF(X.shape[1], ARD=True) ], num_inducing=2, back_constraint=False) if not os.path.exists(os.path.join(base_path, self.modelfile)): # Create the model file m.randomize() m._trigger_params_changed() m.save(os.path.join(base_path, self.modelfile)) with h5py.File(os.path.join(base_path, self.modelfile), 'r+') as f: L = f.create_dataset("L", (1, ), dtype=np.float) L[:] = m._log_marginal_likelihood f.close() # Load model parameters with tables.open_file(os.path.join(base_path, self.modelfile), 'r') as f: m.param_array[:] = f.root.param_array[:] L = float(f.root.L[:]) m._trigger_params_changed() f.close() self.model = m self.L = L
def deepGaussianProcessClassifier(self, testIndex): print("Deep Gaussian Process Classification") trainIndex = self.posMusic + self.negMusic features = self.newFactory.musicFiles.mtFeatures likes = self.newFactory.musicFiles.like X1 = numpy.zeros((len(trainIndex), len(features[0][:33]))) y1 = numpy.zeros((len(trainIndex), 1)) for i, mem in enumerate(trainIndex): X1[i, :] = squeeze(features[mem][:33]) if likes[mem] == True: y1[i] = 0 else: y1[i] = 1 Q = 2 ## Binary classification kern1 = GPy.kern.RBF(Q, ARD=True) + GPy.kern.Bias(Q) kern2 = GPy.kern.RBF(Q, ARD=False) + GPy.kern.Bias(X1.shape[1]) m = deepgp.DeepGP([2, 2, X1.shape[1]], y1, X_tr=X1, kernels=[kern1, kern2], num_inducing=10, back_constraint=False) m.optimize(max_iters=1500, messages=True) X2 = numpy.zeros((len(testIndex), len(features[0][:33]))) for i, mem in enumerate(testIndex): X2[i, :] = squeeze(features[mem][:33]) result = m.predict(X2) print(result[0], result[0].shape) for i in range(len(testIndex)): if likes[testIndex[i]] == True: print(i, "0") else: print(i, "1")
#--------- Model Construction ----------# # Number of latent dimensions per layer Q1, Q2 = 5, 4 # Type of kernel per layer kern1 = GPy.kern.RBF(Q1,ARD=True) + GPy.kern.Bias(Q1) kern2 = GPy.kern.RBF(Q2,ARD=True) + GPy.kern.Bias(Q2) # Number of inducing points per layer (can be set to different if given as list). num_inducing = 40 # Whether to use back-constraint for variational posterior back_constraint = False # Dimensions of the MLP back-constraint if set to true encoder_dims=[[300],[150]] m = deepgp.DeepGP([Y.shape[1],Q1,Q2],Y,kernels=[kern1,kern2], num_inducing=num_inducing, back_constraint=back_constraint, encoder_dims = encoder_dims) #--------- Optimization ----------# # Make sure initial noise variance gives a reasonable signal to noise ratio for i in range(len(m.layers)): output_var = m.layers[i].Y.var() if i==0 else m.layers[i].Y.mean.var() m.layers[i].Gaussian_noise.variance = output_var*0.01 m.optimize(max_iters=5000, messages=True) #--------- Inspection ----------# # Plot ARD scales per layer m.obslayer.kern.plot_ARD() m.layer_1.kern.plot_ARD()
# Number of latent dimensions (single hidden layer, since the top layer is observed) Q = 5 # Define what kernels to use per layer kern1 = GPy.kern.RBF(Q, ARD=True) + GPy.kern.Bias(Q) kern2 = GPy.kern.RBF(Q, ARD=False) + GPy.kern.Bias(X_tr.shape[1]) # Number of inducing points to use num_inducing = 40 # Whether to use back-constraint for variational posterior back_constraint = True # Dimensions of the MLP back-constraint if set to true encoder_dims = [[300], [150]] m = deepgp.DeepGP([Y_tr.shape[1], Q, X_tr.shape[1]], Y_tr, X_tr=X_tr, kernels=[kern1, kern2], num_inducing=num_inducing, back_constraint=back_constraint) #--------- Optimization ----------# # Make sure initial noise variance gives a reasonable signal to noise ratio. # Fix to that value for a few iterations to avoid early local minima for i in range(len(m.layers)): output_var = m.layers[i].Y.var() if i == 0 else m.layers[i].Y.mean.var() m.layers[i].Gaussian_noise.variance = output_var * 0.01 m.layers[i].Gaussian_noise.variance.fix() m.optimize(max_iters=800, messages=True) # Unfix noise variance now that we have initialized the model for i in range(len(m.layers)): m.layers[i].Gaussian_noise.variance.unfix()
# Number of latent dimensions (single hidden layer, since the top layer is observed) Q = 1 # Define what kernels to use per layer # 初始超参数选择 kern1 = GPy.kern.RBF(Q, ARD=True) + GPy.kern.Bias(Q) # Q表示隐结点个数 kern2 = GPy.kern.RBF(train_x.shape[1], ARD=True) + GPy.kern.Bias(train_x.shape[1]) # Number of inducing points to use num_inducing = 100 # Whether to use back-constraint for variational posterior back_constraint = False # Dimensions of the MLP back-constraint if set to true # encoder_dims = [[300], [150]] m = deepgp.DeepGP([norm_train_y.shape[1], Q, train_x.shape[1]], norm_train_y, X_tr=train_x, kernels=[kern1, kern2], num_inducing=num_inducing, back_constraint=back_constraint) # print(np.mean(m.layers[0].Y)) # exit() # --------- Optimization ----------# # Make sure initial noise variance gives a reasonable signal to noise ratio. # Fix to that value for a few iterations to avoid early local minima for i in range(len(m.layers)): output_var = m.layers[i].Y.var() if i == 0 else m.layers[i].Y.mean.var() m.layers[i].Gaussian_noise.variance = output_var * 0.01 m.layers[i].Gaussian_noise.variance.fix() m.optimize(max_iters=1000, messages=True) # # Unfix noise variance now that we have initialized the model # for i in range(len(m.layers)):
kern3 = GPy.kern.RBF(QX, ARD=False) + GPy.kern.Bias(QX) # Layer X -> H2 # Number of inducing points to use num_inducing = 40 # Whether to use back-constraint for variational posterior back_constraint = False # Dimensions of the MLP back-constraint if set to true encoder_dims = [[300], [150], [150]] # Collect the dimensions of [Y, H1, H2, X] dimensions = [Y_tr.shape[1], Q1, Q2, QX] # Create model m = deepgp.DeepGP(dimensions, Y_tr_scaled, X=X_tr_scaled, kernels=[kern1, kern2, kern3], num_inducing=num_inducing, back_constraint=back_constraint) # print(m) # This will print the model #--------- Optimization ----------# # Make sure initial noise variance gives a reasonable signal to noise ratio. # Fix to that value for a few iterations to avoid early local minima for i in range(len(m.layers)): output_var = m.layers[i].Y.var() if i == 0 else m.layers[i].Y.mean.var() m.layers[i].Gaussian_noise.variance = output_var * 0.01 m.layers[i].Gaussian_noise.variance.fix() m.optimize(max_iters=800, messages=True) # Unfix noise variance now that we have initialized the model