def meanfieldupdate(self, bias_model, weight_model, impulse_model): """ Perform the mean field update. :param bias_model: :param weight_model: :param impulse_model: :return: """ # If necessary, initialize arrays for mean field parameters if self.EZ is None: self.EZ = np.zeros((self.T, self.K, self.K, self.B)) if self.EZ0 is None: self.EZ0 = np.copy(self.S).astype(np.float) # Uncomment this line to use python # self._mf_update_Z_python(bias_model, weight_model, impulse_model) # Use Cython exp_E_log_lambda0 = np.exp(bias_model.expected_log_lambda0()) exp_E_log_W = np.exp(weight_model.expected_log_W()) exp_E_log_g = np.exp(impulse_model.expected_log_g()) mf_update_Z(self.EZ0, self.EZ, self.S, exp_E_log_lambda0, exp_E_log_W, exp_E_log_g, self.F)
def test_parallel_parent_updates(): """ Just run the parent updates in a parallel loop and watch htop to make sure that all the cores are being used. :return: """ T = 10000 K = 100 B = 3 S = np.random.poisson(2.0, size=((T,K))).astype(np.int) EZ0 = np.zeros((T,K)) EZ = np.zeros((T,K,K,B)) exp_E_log_lambda0 = np.random.gamma(1.0, 1.0, size=(K)) exp_E_log_W = np.random.gamma(1.0, 1.0, size=(K,K)) exp_E_log_g = np.random.gamma(1.0, 1.0, size=(K,K,B)) F = np.random.gamma(1.0, 1.0, size=(T,K,B)) for itr in xrange(1000): if itr % 10 == 0: print "Iteration\t", itr mf_update_Z(EZ0, EZ, S, exp_E_log_lambda0, exp_E_log_W, exp_E_log_g, F)
def _mf_update_Z(self): """ Update the mean field parameters for the latent parents :return: """ bias_model, weight_model, impulse_model = \ self.model.bias_model, self.model.weight_model, self.model.impulse_model exp_E_log_lambda0 = np.exp(bias_model.expected_log_lambda0()) exp_E_log_W = np.exp(weight_model.expected_log_W()) exp_E_log_g = np.exp(impulse_model.expected_log_g()) for k2, (Sk, Fk, EZk) in enumerate(zip(self.Ss, self.Fs, self.EZ)): mf_update_Z(k2, EZk, Sk, exp_E_log_lambda0, exp_E_log_W, exp_E_log_g, Fk) self._check_EZ()
def _mf_update_Z(self): """ Update the mean field parameters for the latent parents :return: """ bias_model, weight_model, impulse_model = \ self.model.bias_model, self.model.weight_model, self.model.impulse_model K, B = self.K, self.B exp_E_log_lambda0 = np.exp(bias_model.expected_log_lambda0()) exp_E_log_W = np.exp(weight_model.expected_log_W()) exp_E_log_g = np.exp(impulse_model.expected_log_g()) for k2, (Sk, Fk, EZk) in enumerate(zip(self.Ss, self.Fs, self.EZ)): mf_update_Z(k2, EZk, Sk, exp_E_log_lambda0, exp_E_log_W, exp_E_log_g, Fk) self._check_EZ()