def _forward_backward(self, itraj): """ Estimation step: Runs the forward-back algorithm on trajectory with index itraj Parameters ---------- itraj : int index of the observation trajectory to process Results ------- logprob : float The probability to observe the observation sequence given the HMM parameters gamma : ndarray(T,N, dtype=float) state probabilities for each t count_matrix : ndarray(N,N, dtype=float) the Baum-Welch transition count matrix from the hidden state trajectory """ # get parameters A = self._hmm.transition_matrix pi = self._hmm.initial_distribution obs = self._observations[itraj] T = len(obs) # compute output probability matrix # t1 = time.time() self._hmm.output_model.p_obs(obs, out=self._pobs) # t2 = time.time() # self._fbtimings[0] += t2-t1 # forward variables logprob = hidden.forward(A, self._pobs, pi, T=T, alpha_out=self._alpha)[0] # t3 = time.time() # self._fbtimings[1] += t3-t2 # backward variables hidden.backward(A, self._pobs, T=T, beta_out=self._beta) # t4 = time.time() # self._fbtimings[2] += t4-t3 # gamma hidden.state_probabilities(self._alpha, self._beta, T=T, gamma_out=self._gammas[itraj]) # t5 = time.time() # self._fbtimings[3] += t5-t4 # count matrix hidden.transition_counts(self._alpha, self._beta, A, self._pobs, T=T, out=self._Cs[itraj]) # t6 = time.time() # self._fbtimings[4] += t6-t5 # return results return logprob
def run_gamma(self, i, out): gamma = None hidden.set_implementation(self.kernel) time1 = time.time() for k in range(self.nrep): gamma = hidden.state_probabilities(self.alpha[i], self.beta[i], gamma_out=out) # compare time2 = time.time() d = (time2-time1)/(1.0*self.nrep) return (gamma, d)
def run_gamma(self, i, out): gamma = None hidden.set_implementation(self.kernel) time1 = time.time() for k in range(self.nrep): gamma = hidden.state_probabilities(self.alpha[i], self.beta[i], gamma_out=out) # compare time2 = time.time() d = (time2-time1)/(1.0*self.nrep) return gamma, d
def run_gamma(self, i, kernel, out): nrep = max(1, int(10000/self.T[i])) gamma = None hidden.set_implementation(kernel) time1 = time.time() for k in range(nrep): gamma = hidden.state_probabilities(self.alpha[i], self.beta[i], gamma_out=out) # compare time2 = time.time() d = (time2-time1) / (1.0*nrep) return gamma, d
def run_all_mem(self, A, pobs, pi): T = pobs.shape[0] N = A.shape[0] alpha = np.zeros((T, N)) beta = np.zeros((T, N)) gamma = np.zeros((T, N)) C = np.zeros((N, N)) logprob, alpha = hidden.forward(A, pobs, pi, alpha_out=alpha) # backward hidden.backward(A, pobs, beta_out=beta) # gamma hidden.state_probabilities(alpha, beta, gamma_out=gamma) # state counts statecount = hidden.state_counts(gamma, T) # transition counts hidden.transition_counts(alpha, beta, A, pobs, out=self.C) # viterbi path vpath = hidden.viterbi(A, pobs, pi) # return return logprob, alpha, beta, gamma, statecount, C, vpath
def run_all_mem(self, A, pobs, pi): T = pobs.shape[0] N = A.shape[0] alpha = np.zeros((T, N)) beta = np.zeros((T, N)) gamma = np.zeros((T, N)) C = np.zeros((N, N)) logprob, alpha = hidden.forward(A, pobs, pi, alpha_out=alpha) # backward hidden.backward(A, pobs, beta_out=beta) # gamma hidden.state_probabilities(alpha, beta, gamma_out=gamma) # state counts statecount = hidden.state_counts(gamma, T) # transition counts hidden.transition_counts(alpha, beta, A, pobs, out=self.C) # viterbi path vpath = hidden.viterbi(A, pobs, pi) # return return (logprob, alpha, beta, gamma, statecount, C, vpath)
def run_gamma(self, i, kernel, out): nrep = max(1, int(10000 / self.T[i])) gamma = None hidden.set_implementation(kernel) time1 = time.time() for k in range(nrep): gamma = hidden.state_probabilities(self.alpha[i], self.beta[i], gamma_out=out) # compare time2 = time.time() d = (time2 - time1) / (1.0 * nrep) return (gamma, d)
def run_all(self, A, pobs, pi): # forward logprob, alpha = hidden.forward(A, pobs, pi) # backward beta = hidden.backward(A, pobs) # gamma gamma = hidden.state_probabilities(alpha, beta) # state counts T = pobs.shape[0] statecount = hidden.state_counts(gamma, T) # transition counts C = hidden.transition_counts(alpha, beta, A, pobs) # viterbi path vpath = hidden.viterbi(A, pobs, pi) # return return logprob, alpha, beta, gamma, statecount, C, vpath
def run_all(self, A, pobs, pi): # forward logprob, alpha = hidden.forward(A, pobs, pi) # backward beta = hidden.backward(A, pobs) # gamma gamma = hidden.state_probabilities(alpha, beta) # state counts T = pobs.shape[0] statecount = hidden.state_counts(gamma, T) # transition counts C = hidden.transition_counts(alpha, beta, A, pobs) # viterbi path vpath = hidden.viterbi(A, pobs, pi) # return return (logprob, alpha, beta, gamma, statecount, C, vpath)