def _compute_mixture_posteriors(self, X, y, lengths): ''' Computes the posterior log-probability of each mixture component given the observations X, y. Inputs: X - np.array of size (n_samples, n_features). y - np.int of size n_sequences, whose entries are in the range [0, n_nodes-1]. lengths - list containing the lengths of each individual sequence in X, with size n_sequences. Outputs: logmixpost - np.array of size (n_sequences, mix_dim). ''' N = len(lengths) transitions = [] #means = [] logmixpost = np.zeros((N, self.mix_dim)) for m in range(self.mix_dim): ll_m = np.zeros(N) for seq_idx, (i, j) in enumerate(iter_from_X_lengths(X, lengths)): ll_m[seq_idx] = self.mixModels[m].score(X[i:j, :]) transitions = np.append(transitions, self.mixModels[m].transmat_) #means = np.append(means, self.mixModels[m].means_) logmixpost[:, m] = ll_m + np.log(self.mixCoef[y, m] + .000000001) log_normalize(logmixpost, axis=1) return logmixpost, transitions #, means
def _accumulate_sufficient_statistics(self, stats, X, framelogprob, post_comp, fwdlattice, bwdlattice): # TODO: support multiple frames super()._accumulate_sufficient_statistics(stats, X, framelogprob, post_comp, fwdlattice, bwdlattice) n_samples, _ = X.shape stats['n_samples'] = n_samples stats['samples'] = X post_mix = np.zeros((n_samples, self.n_components, self.n_mix)) for p in range(self.n_components): log_denses = self._compute_log_weighted_gaussian_densities(X, p) log_normalize(log_denses, axis=-1) with np.errstate(under="ignore"): post_mix[:, p, :] = np.exp(log_denses) with np.errstate(under="ignore"): post_comp_mix = post_comp[:, :, None] * post_mix stats['post_comp_mix'] = post_comp_mix stats['post_mix_sum'] = np.sum(post_comp_mix, axis=0) stats['post_sum'] = np.sum(post_comp, axis=0) stats['centered'] = X[:, None, None, :] - self.means_
def _compute_posteriors(self, fwdlattice, bwdlattice): # gamma is guaranteed to be correctly normalized by logprob at # all frames, unless we do approximate inference using pruning. # So, we will normalize each frame explicitly in case we # pruned too aggressively. log_gamma = fwdlattice + bwdlattice log_normalize(log_gamma, axis=1) return np.exp(log_gamma)
def get_expectation(self): # HMM: fwd * bwd / prob(observations) # Directed Graph: fwd * bwd q = self.fwd + self.bwd # log normalization utils.log_normalize(q, axis=1) with np.errstate(under="ignore"): return np.exp(np.swapaxes(q, 0, 1))
def _accumulate_sufficient_statistics(self, stats, X, framelogprob, posteriors, fwdlattice, bwdlattice): log_gamma = fwdlattice + bwdlattice log_normalize(log_gamma, axis=1) super()._accumulate_sufficient_statistics(stats, X, framelogprob, posteriors, fwdlattice, bwdlattice) if 'o' in self.params: stats['post'] += posteriors.sum(axis=0) stats['obs'] += np.dot(posteriors.T, X) return 0
def _accumulate_sufficient_statistics(self, stats, X, framelogprob, posteriors, fwdlattice, bwdlattice, run_lengths): stats['nobs'] += 1 if 's' in self.params: first_posterior = get_log_init_posterior( np.log(self.startprob_) + framelogprob[0], bwdlattice[0], run_lengths[0], log_mask_zero(self.transmat_), framelogprob[0]) log_normalize(first_posterior) stats['start'] += np.exp(first_posterior) if 't' in self.params: n_samples, n_components = framelogprob.shape if n_samples <= 1: return full_fwdlattice = np.vstack( (np.log(self.startprob_) + framelogprob[0], fwdlattice)) log_xi_sum = compute_log_xi_sum(full_fwdlattice, log_mask_zero(self.transmat_), bwdlattice, framelogprob, run_lengths) with np.errstate(under="ignore"): stats['trans'] += np.exp(log_xi_sum)