def __init__(self, parents: Tuple[Gaussian], child: Gaussian): VMPFactor.__init__(self) self._deterministic = True self.parents = parents self.child = child self.message_to_child = Gaussian.uniform(self.child.shape) self.message_to_parents = tuple( Gaussian.uniform(p.shape) for p in self.parents)
def __init__(self, parent: Gaussian, children: Tuple[Gaussian]): VMPFactor.__init__(self) self._deterministic = True self.parent = parent self.children = children self._chunks = tuple(c.shape[1] for c in children) self.message_to_children = tuple( Gaussian.uniform(c.shape) for c in children) self.message_to_parent = Gaussian.uniform(self.parent.shape)
def __init__(self, parent: Gaussian, index: torch.Tensor, child: Gaussian): VMPFactor.__init__(self) self._deterministic = True self.parent = parent self.index = index self.child = child self.which = torch.stack( [self.index == i for i in range(self.parent.shape[0])]) self.message_to_child = Gaussian.uniform(self.child.shape) self.message_to_parent = Gaussian.uniform(self.child.shape) self.message_to_parent_sum = Gaussian.uniform(self.parent.shape)
def __init__(self, parent: Gaussian, child: Gaussian, variance: torch.Tensor): # TODO: variance as tensor, variance in log scale VMPFactor.__init__(self) self._deterministic = False self._observed = False self._prior = False self.log_var = nn.Parameter(variance) self.parent = parent self.child = child self.message_to_child = Gaussian.uniform(self.shape) self.message_to_parent = Gaussian.uniform(self.shape)
def __init__(self, parent: Gaussian, child: Gaussian): VMPFactor.__init__(self) self._deterministic = True self.parent = parent self.child = child self.in_dim = self.parent.shape[1] self.out_dim = self.child.shape[1] dtype = self.parent.precision.dtype self.weight = nn.Parameter( torch.randn((self.in_dim, self.out_dim), dtype=dtype)) self.bias = nn.Parameter(torch.randn((self.out_dim, ), dtype=dtype)) self.message_to_child = Gaussian.uniform(self.child.shape) self.message_to_parent_sum = Gaussian.uniform(self.parent.shape) self.message_to_parent = Gaussian.uniform( (self.parent.shape[0], self.in_dim, self.out_dim))
def __init__(self, parent: Gaussian, child: Bernoulli): VMPFactor.__init__(self) self._deterministic = False self._observed = False self.parent = parent self.child = child self.message_to_child = Bernoulli.uniform(self.shape) self.message_to_parent = Gaussian.uniform(self.shape)
def __init__(self, positions: Gaussian, index: torch.Tensor, X_cts: torch.Tensor, X_bin: torch.Tensor): f.VMPFactor.__init__(self) self._deterministic = False p_cts = X_cts.shape[1] p_bin = X_bin.shape[1] p = p_cts + p_bin n = index.shape[0] K = positions.shape[1] self._positions = Gaussian.uniform((n, K)) self._mean = Gaussian.uniform((n, p)) self._mean_cts = Gaussian.uniform((n, p_cts)) self._mean_bin = Gaussian.uniform((n, p_bin)) self._select = f.Select(positions, index, self._positions) self._linear = f.Linear(self._positions, self._mean) self._split = f.Split(self._mean, (self._mean_cts, self._mean_bin)) self._gaussian = f.GaussianFactor.observed(self._mean_cts, X_cts, torch.zeros(p_cts)) self._logistic = f.Logistic.observed(self._mean_bin, X_bin)
def __init__(self, parents: Tuple[Gaussian], child: Gaussian): VMPFactor.__init__(self) self._deterministic = True self.parents = parents self.child = child self._products = Gaussian.uniform(self.parents[0].shape) self._product = Product(parents, self._products) self._linear = Linear(self._products, child) self._linear.weight.data = torch.ones_like(self._linear.weight.data) self._linear.weight.requires_grad = False self._linear.bias.data = torch.zeros_like(self._linear.bias.data) self._linear.bias.requires_grad = False
def __init__(self, positions: Gaussian, heterogeneity: Gaussian, indices: Tuple[torch.Tensor], links: torch.Tensor): f.VMPFactor.__init__(self) self._deterministic = False n = indices[0].shape[0] K = positions.shape[1] self._positions = tuple(Gaussian.uniform((n, K)) for _ in range(2)) self._heterogeneity = tuple(Gaussian.uniform((n, 1)) for _ in range(2)) self._inner_products = Gaussian.uniform((n, 1)) self._logits = Gaussian.uniform((n, 1)) self._select_positions = tuple( f.Select(positions, i, p) for i, p in zip(indices, self._positions)) self._select_heterogeneity = tuple( f.Select(heterogeneity, i, p) for i, p in zip(indices, self._heterogeneity)) self._inner_product = f.InnerProduct(self._positions, self._inner_products) self._sum = f.Sum((self._inner_products, *self._heterogeneity), self._logits) self._logistic = f.Logistic.observed(self._logits, links)
self # ----------------------------------------------------------------------------- # Select import torch import numpy as np from NNVI.vmp.gaussian import Gaussian from NNVI.vmp.factors import Select N = 5 n = N * (N - 1) // 2 K = 3 parent = Gaussian.from_array(torch.randn((N, K)), torch.ones((N, K))) index = torch.randint(0, N, (n, )) child = Gaussian.uniform((n, K)) self = Select(parent, index, child) self self.forward() self.backward() self # ----------------------------------------------------------------------------- # Product import torch import numpy as np from NNVI.vmp.gaussian import Gaussian from NNVI.vmp.factors import Product