def __init__(self, N, mean=0., variance=1.): self.N = N self.mean = mean self.variance = variance self.prior = Prior(GaussianArray.from_shape((N, 1), mean, variance)) self.marginal = GaussianArray.uniform((N, 1)) self.from_adjacency = GaussianArray.uniform((N, 1))
import numpy as np from models.distributions.gaussianarray import GaussianArray from models.distributions.bernoulliarray import BernoulliArray # ----------------------------------------------------------------------------- # PRIOR from models.vmp.vmp_factors2 import Prior child = GaussianArray.uniform((3, 3)) self = Prior(child, 0., 1.) self.forward() self.to_elbo() # ----------------------------------------------------------------------------- # ADD VARIANCE from models.vmp.vmp_factors2 import AddVariance parent = GaussianArray.from_shape((3, 3), 0., 1.) child = GaussianArray.from_shape((3, 3), 0., 1.) self = AddVariance(child, parent, 1.) self.to_child() print(child) self.to_parent() print(parent) self.to_elbo() # ----------------------------------------------------------------------------- # PROBIT from models.vmp.vmp_factors2 import Probit parent = GaussianArray.from_shape((5, 5), 0., 1.) A = tf.where(tf.random.normal((5, 5)) > 0., 1., 0.)
a_u = GaussianArray( tf.tile(tf.expand_dims(alpha.precision(), 0), [N, 1, 1]), tf.tile(tf.expand_dims(alpha.mean_times_precision(), 0), [N, 1, 1])) a_v = GaussianArray( tf.tile(tf.expand_dims(alpha.precision(), 1), [1, N, 1]), tf.tile(tf.expand_dims(alpha.mean_times_precision(), 1), [1, N, 1])) s_uv = GaussianArray.from_array(tf.random.normal((N, N, K), 0., 1.), 0.1 * tf.ones((N, N, K))) x = {"a_u": a_u, "a_v": a_v, "s_uv": s_uv} v = GaussianArray.from_array(tf.random.normal((N, N, K + 2), 0., 1.), 0.1 * tf.ones((N, N, K + 2))) self.to_v(x) self.to_x(v) # ---------- GaussianArray.from_shape((N, N, N + 2), 0.0, 1.0) self.factors["concatenate"].to_x( GaussianArray.from_shape((N, N, N + 2), 0.0, 1.0)) for k, m in self.factors["concatenate"].message_to_x.items(): print(m.shape()) self._update_latent_variable() product = self.nodes["product"] product.mean() b = self.nodes["latent_variable"] b.mean()
import tensorflow as tf import numpy as np from models.distributions.gaussianarray import GaussianArray from models.vmp.vmp_factors2 import Product N = 3 K = 1 parent = GaussianArray.from_shape((N, K), 1.414, 1.) upper = tf.linalg.band_part(tf.ones((N, N)), -1, 0) == 0 mean = tf.where(tf.expand_dims(upper, 2), 2., 0.) variance = tf.where(tf.expand_dims(upper, 2), 5., np.inf) child = GaussianArray.from_array(mean, variance) self = Product(child, parent) print(parent) self.forward() self.backward() print(parent) print(child) self.to_child() print(child.mean()) self.to_elbo()