aux = NormalWishart(**basis_hypparams) basis_prior.append(aux) models_hypparams = dict(M=np.zeros((target_dim, nb_params)), K=K * np.eye(nb_params), nu=target_dim + 1, psi=np.eye(target_dim) * psi_mnw) aux = MatrixNormalWishart(**models_hypparams) models_prior.append(aux) gating_hypparams = dict(K=args.nb_models, gammas=np.ones((args.nb_models, )), deltas=np.ones((args.nb_models, )) * args.alpha) gating_prior = TruncatedStickBreaking(**gating_hypparams) ilr = BayesianMixtureOfLinearGaussians( gating=CategoricalWithStickBreaking(gating_prior), basis=[ GaussianWithNormalWishart(basis_prior[i]) for i in range(args.nb_models) ], models=[ LinearGaussianWithMatrixNormalWishart(models_prior[i], affine=args.affine) for i in range(args.nb_models) ]) import copy from sklearn.utils import shuffle
def _job(kwargs): args = kwargs.pop('arguments') seed = kwargs.pop('seed') input = kwargs.pop('train_input') target = kwargs.pop('train_target') input_dim = input.shape[-1] target_dim = target.shape[-1] # set random seed np.random.seed(seed) nb_params = input_dim if args.affine: nb_params += 1 basis_prior = [] models_prior = [] # initialize Normal psi_nw = 1e0 kappa = 1e-2 # initialize Matrix-Normal psi_mnw = 1e0 K = 1e-3 for n in range(args.nb_models): basis_hypparams = dict(mu=np.zeros((input_dim, )), psi=np.eye(input_dim) * psi_nw, kappa=kappa, nu=input_dim + 1) aux = NormalWishart(**basis_hypparams) basis_prior.append(aux) models_hypparams = dict(M=np.zeros((target_dim, nb_params)), K=K * np.eye(nb_params), nu=target_dim + 1, psi=np.eye(target_dim) * psi_mnw) aux = MatrixNormalWishart(**models_hypparams) models_prior.append(aux) # define gating if args.prior == 'stick-breaking': gating_hypparams = dict(K=args.nb_models, gammas=np.ones((args.nb_models, )), deltas=np.ones( (args.nb_models, )) * args.alpha) gating_prior = TruncatedStickBreaking(**gating_hypparams) ilr = BayesianMixtureOfLinearGaussians( gating=CategoricalWithStickBreaking(gating_prior), basis=[ GaussianWithNormalWishart(basis_prior[i]) for i in range(args.nb_models) ], models=[ LinearGaussianWithMatrixNormalWishart(models_prior[i], affine=args.affine) for i in range(args.nb_models) ]) else: gating_hypparams = dict(K=args.nb_models, alphas=np.ones( (args.nb_models, )) * args.alpha) gating_prior = Dirichlet(**gating_hypparams) ilr = BayesianMixtureOfLinearGaussians( gating=CategoricalWithDirichlet(gating_prior), basis=[ GaussianWithNormalWishart(basis_prior[i]) for i in range(args.nb_models) ], models=[ LinearGaussianWithMatrixNormalWishart(models_prior[i], affine=args.affine) for i in range(args.nb_models) ]) ilr.add_data(target, input, whiten=True) # Gibbs sampling ilr.resample(maxiter=args.gibbs_iters, progprint=args.verbose) for _ in range(args.super_iters): if args.stochastic: # Stochastic meanfield VI ilr.meanfield_stochastic_descent(maxiter=args.svi_iters, stepsize=args.svi_stepsize, batchsize=args.svi_batchsize) if args.deterministic: # Meanfield VI ilr.meanfield_coordinate_descent(tol=args.earlystop, maxiter=args.meanfield_iters, progprint=args.verbose) ilr.gating.prior = ilr.gating.posterior for i in range(ilr.likelihood.size): ilr.basis[i].prior = ilr.basis[i].posterior ilr.models[i].prior = ilr.models[i].posterior return ilr
import numpy as np import numpy.random as npr import scipy as sc from scipy import stats from mimo.distributions import TruncatedStickBreaking alpha = 10 K = 100 sb = TruncatedStickBreaking(K, np.ones(K), np.ones(K) * alpha) weights = np.vstack([sb.rvs() for _ in range(10000)]) weights = np.mean(weights, axis=0) import matplotlib.pyplot as plt plt.axis([0, K + 1, 0, np.max(weights)]) plt.bar(range(1, K + 1), weights) plt.show() h = sc.stats.norm omega = h.rvs(size=(10, K)) x = np.linspace(-3, 3, 200) sample_cdfs = (weights[..., np.newaxis] * np.less.outer(omega, x)).sum(axis=1) fig, ax = plt.subplots(figsize=(8, 6)) ax.plot(x, sample_cdfs[1:].T, c='gray', alpha=0.75)