def expected_theta(self, obs_wordidsd, obs_wordctsd, lambda_v, nu_v): ''' Return expected theta under a variational distribution Args: self : use all the parameters initialized before lambda_v : variational parameter lambda nu_v : variational parameter nu Returns: val : the expected theta ''' nsamples = 100 eta = np.zeros(self._K) theta = eta[:] # initialize e_theta e_theta = -1.0 * np.ones(self._K) # for each sample nterms = len(obs_wordidsd) for n in range(nterms): # sample eta from q(\eta) for i in range(self._K): eta[i] = random.gauss(0, np.sqrt(nu_v[i])) + lambda_v[i] # compute p(w | \eta) - q(\eta) log_prob = self.sample_term(eta, lambda_v, nu_v, obs_wordidsd, obs_wordctsd) # compute theta theta = eta[:] sum_t = np.sum(np.exp(eta)) theta = np.divide(theta, sum_t) # update e_theta for i in range(self._K): e_theta[i] = math_utli.log_sum(e_theta[i], log_prob + math_utli.safe_log(theta[i])) # normalize e_theta and set return vector sum_et = -1.0 for i in range(self._K): e_theta[i] -= np.log(nsamples) sum_et = math_utli.log_sum(sum_et, e_theta[i]) e_theta = np.exp(np.subtract(e_theta, sum_et)) return e_theta
def opt_phi(self, ntermd, lambda_v, log_phi_v): logger.info("calculating variational parameter PHI") # optimize phi log_sum_temp = 0 phi_v = np.zeros_like(log_phi_v) for i in range(self._K): log_sum_temp = 0.0 for j in range(ntermd): log_phi_v[i, j] = lambda_v[i] + self.log_beta[i, j] if i == 0: log_sum_temp = log_phi_v[i, j] else: log_sum_temp = math_utli.log_sum(log_sum_temp, log_phi_v[i, j]) log_phi_v[i, j] -= log_sum_temp phi_v[i, j] = np.exp(log_phi_v[i, j]) return (phi_v, log_phi_v)