def build_likelihood(self):
Fmean, Fvar = self.build_predict(self.X, full_cov=False, S=self.num_samples)
S, N, D = shape_as_list(Fmean)
Y = tile_over_samples(self.Y, self.num_samples)
f = lambda a: self.likelihood.variational_expectations(a[0], a[1], a[2])
var_exp = tf.map_fn(f, (Fmean, Fvar, Y), dtype=float_type)
var_exp = tf.stack(var_exp) #SN
var_exp = tf.reduce_mean(var_exp, 0) # S,N -> N. Average over samples
L = tf.reduce_sum(var_exp) # N -> scalar. Sum over data (minibatch)
KL = 0.
for layer in self.layers:
KL += layer.KL()
scale = tf.cast(self.num_data, float_type)
scale /= tf.cast(tf.shape(self.X)[0], float_type) # minibatch size
return L * scale - KL
评论列表
文章目录