def log_prob(self, xs, zs): """Return scalar, the log joint density log p(xs, zs).""" x = xs['x'] pi, mus, sigmas = zs['pi'], zs['mu'], zs['sigma'] log_prior = dirichlet.logpdf(pi, self.alpha) log_prior += tf.reduce_sum(norm.logpdf(mus, 0.0, self.c)) log_prior += tf.reduce_sum(invgamma.logpdf(sigmas, self.a, self.b)) # log-likelihood is # sum_{n=1}^N log sum_{k=1}^K exp( log pi_k + log N(x_n; mu_k, sigma_k) ) # Create a K x N matrix, whose entry (k, n) is # log pi_k + log N(x_n; mu_k, sigma_k). N = get_dims(x)[0] matrix = [] for k in range(self.K): matrix += [tf.ones(N) * tf.log(pi[k]) + multivariate_normal_diag.logpdf(x, mus[(k * self.D):((k + 1) * self.D)], sigmas[(k * self.D):((k + 1) * self.D)])] matrix = tf.pack(matrix) # log_sum_exp() along the rows is a vector, whose nth # element is the log-likelihood of data point x_n. vector = log_sum_exp(matrix, 0) # Sum over data points to get the full log-likelihood. log_lik = tf.reduce_sum(vector) return log_prior + log_lik
def log_prob(self, xs, zs): """Return scalar, the log joint density log p(xs, zs).""" x = xs["x"] pi, mus, sigmas = zs["pi"], zs["mu"], zs["sigma"] log_prior = dirichlet.logpdf(pi, self.alpha) log_prior += tf.reduce_sum(norm.logpdf(mus, 0.0, self.c)) log_prior += tf.reduce_sum(invgamma.logpdf(sigmas, self.a, self.b)) # log-likelihood is # sum_{n=1}^N log sum_{k=1}^K exp( log pi_k + log N(x_n; mu_k, sigma_k) ) # Create a K x N matrix, whose entry (k, n) is # log pi_k + log N(x_n; mu_k, sigma_k). N = get_dims(x)[0] matrix = [] for k in range(self.K): matrix += [ tf.ones(N) * tf.log(pi[k]) + multivariate_normal_diag.logpdf( x, mus[(k * self.D) : ((k + 1) * self.D)], sigmas[(k * self.D) : ((k + 1) * self.D)] ) ] matrix = tf.pack(matrix) # log_sum_exp() along the rows is a vector, whose nth # element is the log-likelihood of data point x_n. vector = log_sum_exp(matrix, 0) # Sum over data points to get the full log-likelihood. log_lik = tf.reduce_sum(vector) return log_prior + log_lik
def log_prob(self, xs, zs): """ Return scalar, the log joint density log p(xs, zs). Given n_minibatch data points, n_samples of variables Summing over the datapoints makes sense since the joint is the only place in the estiamtion of the gradient that has the data points, and its the log, so we can sum over them BUT summing over the variables doenst make sense,its supposed to be one at a time """ x = xs['x'] pi, mus, sigmas = zs['pi'], zs['mu'], zs['sigma'] # print(get_dims(x)) #[n_minibatch, D] # print(get_dims(pi)) #[K] # print(get_dims(mus)) #[K*D] # print(get_dims(sigmas)) #[K*D] log_prior = dirichlet.logpdf(pi, self.alpha) log_prior += tf.reduce_sum(norm.logpdf(mus, 0.0, self.c)) log_prior += tf.reduce_sum(invgamma.logpdf(sigmas, self.a, self.b)) # log-likelihood is # sum_{n=1}^N log sum_{k=1}^K exp( log pi_k + log N(x_n; mu_k, sigma_k) ) # Create a K x N matrix, whose entry (k, n) is # log pi_k + log N(x_n; mu_k, sigma_k). n_minibatch = get_dims(x)[ 0] #this is [n_minibatch, D], with [0] its just n_minibatch #OH I think they compute the matrix so that they can do log sum exp, since they need to find the max value matrix = [] for k in range(self.K): # bbbb = tf.log(pi[k]) # print(get_dims(bbbb)) # aaaa= multivariate_normal_diag.logpdf(x, mus[(k * self.D):((k + 1) * self.D)], sigmas[(k * self.D):((k + 1) * self.D)]) # print(get_dims(aaaa)) # fadad matrix += [ tf.ones(n_minibatch) * tf.log(pi[k]) + multivariate_normal_diag.logpdf( x, mus[(k * self.D):((k + 1) * self.D)], sigmas[(k * self.D):((k + 1) * self.D)]) ] matrix = tf.pack(matrix) # log_sum_exp() along the rows is a vector, whose nth # element is the log-likelihood of data point x_n. vector = log_sum_exp(matrix, 0) # Sum over data points to get the full log-likelihood. log_lik = tf.reduce_sum(vector) return log_prior + log_lik
def predict(self, xs, zs): """Calculate a K x N matrix of log-likelihoods, per-cluster and per-data point.""" x = xs['x'] pi, mus, sigmas = zs['pi'], zs['mu'], zs['sigma'] matrix = [] for k in range(self.K): matrix += [multivariate_normal_diag.logpdf(x, mus[(k * self.D):((k + 1) * self.D)], sigmas[(k * self.D):((k + 1) * self.D)])] return tf.pack(matrix)