Ky = kern.white(1, 0.01) m2 = MOHGP(X, Kf, Ky, Y, K=Nclust, prior_Z='DP', alpha=alpha) m2.ensure_default_constraints() m2.checkgrad(verbose=1) m2.randomize() m2.optimize() m2.systematic_splits() m2.systematic_splits() m2.plot(1, 1, 1, 0, 0, 1) #construct a MOG model (can't recover the clusters) Y_ = Y.copy() Y_ -= Y_.mean(0) Y_ /= Y_.std(0) m3 = MOG(Y_, prior_Z='DP', alpha=alpha) m3.randomize() m3.optimize() m3.systematic_splits() m3.systematic_splits() pb.figure() pb.subplot(2, 2, 1) pb.imshow(ground_truth_phi, aspect='auto', cmap=pb.cm.gray) pb.title('ground truth') pb.subplot(2, 2, 2) pb.imshow(m.phi, aspect='auto', cmap=pb.cm.gray) pb.title('structured GP-DP') pb.subplot(2, 2, 3) pb.imshow(m2.phi, aspect='auto', cmap=pb.cm.gray) pb.title('unstructured GP-DP') pb.subplot(2, 2, 4)
Nclust = 15 dim = 2 Nmin = 25 Nmax = 50 Ndata = np.random.randint(Nmin, Nmax, Nclust) means = np.random.randn(Nclust, dim) * 5 aa = [np.random.randn(dim, dim + 1) for i in range(Nclust)] Sigmas = [np.dot(a, a.T) for a in aa] X = np.vstack([ np.random.multivariate_normal(mu, cov, (n, )) for mu, cov, n in zip(means, Sigmas, Ndata) ]) / 100 Nrestarts = 3 Nclust = 15 m = MOG(X, Nclust, prior_Z='DP') #starts = [np.random.randn(m.N*m.K) for i in range(Nrestarts)] from scipy.cluster import vq starts = [] for i in range(Nrestarts): means = X[np.random.permutation(X.shape[0])[:Nclust]] dists = np.square(X[:, :, None] - means.T[None, :, :]).sum(1) starts.append(dists) for method in ['steepest', 'PR', 'FR', 'HS']: for st in starts: m.set_vb_param(st) m.optimize(method=method, maxiter=1e4) m.plot_tracks()