def compute_bethe_average_energy_MLP(self, factor_beliefs, factor_potentials, batch_factors, debug=False): ''' Equation (37) in: https://www.cs.princeton.edu/courses/archive/spring06/cos598C/papers/YedidaFreemanWeiss2004.pdf ''' assert(factor_potentials.shape == factor_beliefs.shape), (factor_potentials.shape, factor_beliefs.shape) if debug: print() print('!!!!!!!') print("debugging compute_bethe_average_energy") print("torch.exp(factor_beliefs):", torch.exp(factor_beliefs)) print("neg_inf_to_zero(factor_potentials):", neg_inf_to_zero(factor_potentials)) pooled_fac_beleifPotentials = global_add_pool(torch.exp(factor_beliefs)*neg_inf_to_zero(factor_potentials), batch_factors) #keep 1st dimension for # of factors, but flatten remaining dimensions for belief_repeats and each factor pooled_fac_beleifPotentials = pooled_fac_beleifPotentials.view(pooled_fac_beleifPotentials.shape[0], -1) if debug: factor_beliefs_shape = factor_beliefs.shape pooled_fac_beleifPotentials_orig = torch.sum((torch.exp(factor_beliefs)*neg_inf_to_zero(factor_potentials)).view(factor_beliefs_shape[0], -1), dim=0) print("original pooled_fac_beleifPotentials_orig:", pooled_fac_beleifPotentials_orig) print("pooled_fac_beleifPotentials:", pooled_fac_beleifPotentials) print("factor_beliefs.shape:", factor_beliefs.shape) print("pooled_fac_beleifPotentials_orig.shape:", pooled_fac_beleifPotentials_orig.shape) print("pooled_fac_beleifPotentials.shape:", pooled_fac_beleifPotentials.shape) print("(torch.exp(factor_beliefs)*neg_inf_to_zero(factor_potentials)).view(factor_beliefs_shape[0], -1).shape:", (torch.exp(factor_beliefs)*neg_inf_to_zero(factor_potentials)).view(factor_beliefs_shape[0], -1).shape) return pooled_fac_beleifPotentials #negate and sum to get average bethe energy
def compute_bethe_entropy_MLP(self, factor_beliefs, var_beliefs, numVars, var_degrees, batch_factors, batch_vars, debug=False): ''' Equation (38) in: https://www.cs.princeton.edu/courses/archive/spring06/cos598C/papers/YedidaFreemanWeiss2004.pdf ''' pooled_fac_beliefs = -global_add_pool(torch.exp(factor_beliefs)*neg_inf_to_zero(factor_beliefs), batch_factors) #keep 1st dimension for # of factors, but flatten remaining dimensions for belief_repeats and each factor pooled_fac_beliefs = pooled_fac_beliefs.view(pooled_fac_beliefs.shape[0], -1) if debug: factor_beliefs_shape = factor_beliefs.shape pooled_fac_beliefs_orig = -torch.sum((torch.exp(factor_beliefs)*neg_inf_to_zero(factor_beliefs)).view(factor_beliefs_shape[0], -1), dim=0) print("pooled_fac_beliefs_orig:", pooled_fac_beliefs_orig) print("pooled_fac_beliefs:", pooled_fac_beliefs) var_beliefs_shape = var_beliefs.shape assert(var_beliefs_shape[0] == var_degrees.shape[0]) pooled_var_beliefs = global_add_pool(torch.exp(var_beliefs)*neg_inf_to_zero(var_beliefs)*(var_degrees.float() - 1).view(var_degrees.shape[0], 1, 1), batch_vars) #keep 1st dimension for # of factors, but flatten remaining dimensions for belief_repeats and variable states pooled_var_beliefs = pooled_var_beliefs.view(pooled_var_beliefs.shape[0], -1) if debug: pooled_var_beliefs_orig = torch.sum(torch.exp(var_beliefs)*neg_inf_to_zero(var_beliefs)*(var_degrees.float() - 1).view(var_beliefs_shape[0], -1), dim=0) print("pooled_var_beliefs_orig:", pooled_var_beliefs_orig) print("pooled_var_beliefs:", pooled_var_beliefs) # sleep(SHAPECHECK) return pooled_fac_beliefs, pooled_var_beliefs
def compute_bethe_average_energy(factor_beliefs, factor_potentials, debug=False): ''' Equation (37) in: https://www.cs.princeton.edu/courses/archive/spring06/cos598C/papers/YedidaFreemanWeiss2004.pdf ''' assert(factor_potentials.shape == factor_beliefs.shape) if debug: print() print('!!!!!!!') print("debugging compute_bethe_average_energy") print("torch.exp(factor_beliefs):", torch.exp(factor_beliefs)) print("neg_inf_to_zero(factor_potentials):", neg_inf_to_zero(factor_potentials)) bethe_average_energy = -torch.sum(torch.exp(factor_beliefs)*neg_inf_to_zero(factor_potentials)) #elementwise multiplication, then sum # print("bethe_average_energy:", bethe_average_energy) return bethe_average_energy
def compute_bethe_entropy(self, factor_beliefs, var_beliefs): ''' Equation (38) in: https://www.cs.princeton.edu/courses/archive/spring06/cos598C/papers/YedidaFreemanWeiss2004.pdf ''' bethe_entropy = -torch.sum(torch.exp(factor_beliefs)*neg_inf_to_zero(factor_beliefs)) #elementwise multiplication, then sum assert(var_beliefs.shape == torch.Size([self.numVars, 2])), (var_beliefs.shape, [self.numVars, 2]) # sum_{x_i} b_i(x_i)*ln(b_i(x_i)) inner_sum = torch.einsum('ij,ij->i', [torch.exp(var_beliefs), neg_inf_to_zero(var_beliefs)]) # sum_{i=1}^N (d_i - 1)*inner_sum outer_sum = torch.sum((self.var_degrees.float() - 1) * inner_sum) # outer_sum = torch.einsum('i,i->', [self.var_degrees - 1, inner_sum]) bethe_entropy += outer_sum # print("bethe_entropy:", bethe_entropy) return bethe_entropy