def gaussian_exhaustive_test(): model = random_model(3) obs_distns = [ d.Gaussian(mu=np.random.randn(2),sigma=np.eye(2)), d.Gaussian(mu=np.random.randn(2),sigma=np.eye(2)), d.Gaussian(mu=np.random.randn(2),sigma=np.eye(2))] stateseq = np.random.randint(3,size=10) data = np.vstack([obs_distns[a].rvs() for a in stateseq]) target_val = compute_likelihood_enumeration(obs_distns=obs_distns,data=data,**model) likelihood_check(target_val=target_val,data=data,obs_distns=obs_distns,**model)
def _random_variant_model(): N = 4 obs_dim = 2 obs_hypparams = { 'mu_0': np.zeros(obs_dim), 'sigma_0': np.eye(obs_dim), 'kappa_0': 0.05, 'nu_0': obs_dim + 5 } obs_distns = [d.Gaussian(**obs_hypparams) for state in range(N)] dur_distns = \ [d.NegativeBinomialIntegerRVariantDuration( np.r_[0,0,0,0,0,1.,1.,1.], # discrete distribution uniform over {6,7,8} alpha_0=9,beta_0=1, # average geometric success probability 1/(9+1) ) for state in range(N)] model = m.HSMMIntNegBinVariant(init_state_concentration=10., alpha=6., gamma=6., obs_distns=obs_distns, dur_distns=dur_distns) return model
def likelihood_exhaustive_tests(): ### discrete data for i in range(2): model = random_model(2) obs_distns = [ d.Categorical(K=3, alpha_0=1.), d.Categorical(K=3, alpha_0=1.) ] stateseq = np.random.randint(2, size=10) data = np.array([obs_distns[a].rvs() for a in stateseq]) target_val = compute_likelihood_enumeration(obs_distns=obs_distns, data=data, **model) yield make_nose_tuple(_likelihood_helper, target_val=target_val, data=data, obs_distns=obs_distns, **model) # Gaussian data for i in range(2): model = random_model(3) obs_distns = [ d.Gaussian(mu=np.random.randn(2), sigma=np.eye(2)), d.Gaussian(mu=np.random.randn(2), sigma=np.eye(2)), d.Gaussian(mu=np.random.randn(2), sigma=np.eye(2)) ] stateseq = np.random.randint(3, size=10) data = np.vstack([obs_distns[a].rvs() for a in stateseq]) target_val = compute_likelihood_enumeration(obs_distns=obs_distns, data=data, **model) yield make_nose_tuple(_likelihood_helper, target_val=target_val, data=data, obs_distns=obs_distns, **model)
import numpy as np from matplotlib import pyplot as plt from pyhsmm import models, distributions np.random.seed(0) np.seterr(invalid='raise') obs_hypparams = dict(mu_0=np.zeros(2), sigma_0=np.eye(2), kappa_0=0.05, nu_0=5) ### generate data num_modes = 3 true_obs_distns = [ distributions.Gaussian(**obs_hypparams) for i in range(num_modes) ] data = np.concatenate( [true_obs_distns[i % num_modes].rvs(25) for i in range(25)]) ## inference! hmm = models.HMM(obs_distns=[ distributions.Gaussian(**obs_hypparams) for i in range(num_modes * 3) ], alpha=3., init_state_concentration=1.) hmm.add_data(data) hmm.meanfield_coordinate_descent_step() scores = [hmm.meanfield_coordinate_descent_step() for i in range(50)] scores = np.array(scores)
allseqs = np.array_split(alldata, 250) datas, heldout = hold_out(allseqs, 0.05) training_size = sum(data.shape[0] for data in datas) print('...done!') print('%d total frames' % sum(data.shape[0] for data in alldata)) print('split into %d training and %d test sequences' % (len(datas), len(heldout))) ### inference! Nmax = 20 obs_hypparams = dict(mu_0=np.zeros(2), sigma_0=np.eye(2), kappa_0=0.2, nu_0=5) hmm = models.HMM( obs_distns=[distributions.Gaussian(**obs_hypparams) for i in range(Nmax)], alpha=10., init_state_concentration=1.) scores = [] sgdseq = sgd_passes(tau=0, kappa=0.7, datalist=datas) for t, (data, rho_t) in progprint(enumerate(sgdseq)): hmm.meanfield_sgdstep(data, data.shape[0] / training_size, rho_t) if t % 10 == 0: scores.append(hmm.log_likelihood(heldout)) plt.figure() plt.plot(scores) plt.show()