def backward_algorithm(): wordHMMs = {} isolated = get_isolated(prondict) plot_p_color_mesh(example['logbeta'], 'example beta matrix') # verify implementation wordHMMs['o'] = concatHMMs(phoneHMMsAll, isolated['o']) log_st_prob = np.log(wordHMMs['o']['startprob']) log_transmat = np.log(wordHMMs['o']['transmat']) beta_matrix = backward(example['obsloglik'], log_st_prob, log_transmat) plot_p_color_mesh(beta_matrix, "hmms all output example beta matrix")
def EM(lmfcc, obsloglik, model): log_st_prob = np.log(model['startprob']) log_transmat = np.log(model['transmat']) # E step beta_matrix = backward(obsloglik, log_st_prob, log_transmat) alpha_matrix = forward(obsloglik, log_st_prob, log_transmat) posterior = statePosteriors(alpha_matrix, beta_matrix) #M step new_mean, new_var = updateMeanAndVar(lmfcc, posterior) model['means'] = new_mean model['covars'] = new_var return model
def retrain(feature, model, num_iters=20, threshold=1): # extract params means = model['means'] covars = model['covars'] transmat = model['transmat'][:-1, :-1] startprob = model['startprob'][:-1] log_pi = np.log(startprob) log_trans = np.log(transmat) # calculate the emission obsloglik = log_multivariate_normal_density_diag(feature, means, covars) # EM algorithm loglik_old = -np.inf for iter_ in range(num_iters): # E-step log_alpha = forward(obsloglik, log_pi, log_trans) log_beta = backward(obsloglik, log_pi, log_trans) log_gamma = statePosteriors(log_alpha, log_beta) # M-step means, covars = updateMeanAndVar(feature, log_gamma) # update obsloglik = log_multivariate_normal_density_diag( feature, means, covars) loglik = logsumexp(log_alpha[-1]) print("Iter {}: The log likelihood in EM:".format(iter_), loglik) # check if terminate EM if (loglik - loglik_old) < threshold or np.isnan(loglik): print("Terminating the EM") break else: loglik_old = loglik
# load data data = np.load('data/lab2_data.npz')['data'] example = np.load('data/lab2_example.npz')['example'].item() phoneHMMs = np.load('data/lab2_models_onespkr.npz')['phoneHMMs'].item() # Build hmm wordHMMs = {} wordHMMs['o'] = concatHMMs(phoneHMMs, isolated['o']) trans_mat = wordHMMs['o']['transmat'][:-1, :-1] pi_vec = wordHMMs['o']['startprob'][:-1] log_startprob = np.log(pi_vec) log_transmat = np.log(trans_mat) log_emlik = example['obsloglik'] # ===================================================== log_beta = backward(log_emlik, log_startprob, log_transmat) log_alpha = forward(log_emlik, log_startprob, log_transmat) # caculate the log gamma log_gamma = statePosteriors(log_alpha, log_beta) # print(np.allclose(example['loggamma'], log_gamma)) # check if sum to one in linear / sum to zero in log domain ntime_steps = log_gamma.shape[0] zeros = np.zeros((ntime_steps)) # print(np.allclose(logsumexp(log_gamma, axis=1), zeros)) # ==================================================================== # See notes, just do the normalization to get the state posterior of GMM logZ = logsumexp(log_emlik, axis=1) # the normalization factor in log space