Beispiel #1
0
 def __init__(self,
              parent: GaussianArray,
              child_cts: GaussianArray,
              child_bin: BernoulliArray,
              variance_cts=None,
              variance_bin=None,
              bin_model="NoisyProbit"):
     super().__init__()
     self._deterministic = False
     # nodes
     self.parent = parent
     self.child_cts = child_cts
     self.child_bin = child_bin
     # dimensions
     N, K = parent.shape()
     p_cts, p_bin = child_cts.shape()[1], child_bin.shape()[1]
     if variance_cts is None:
         variance_cts = tf.ones((1, p_cts))
     if variance_bin is None:
         variance_bin = tf.ones((1, p_bin))
     # hidden nodes
     self._lin_pred_cts = GaussianArray.uniform((N, p_cts))
     self._lin_pred_bin = GaussianArray.uniform((N, p_bin))
     self._nodes.update({
         "lin_pred_cts": self._lin_pred_cts,
         "lin_pred_bin": self._lin_pred_bin
     })
     # factors
     self._split = Split(parts={
         "cts": self._lin_pred_cts,
         "bin": self._lin_pred_bin
     },
                         vector=self.parent)
     self._model_cts = AddVariance(parent=self._lin_pred_cts,
                                   child=self.child_cts,
                                   variance=variance_cts)
     if bin_model == "Logistic":
         self._model_bin = Logistic(
             parent=self._lin_pred_bin,
             child=self.child_bin,
         )
     else:
         self._model_bin = NoisyProbit(parent=self._lin_pred_bin,
                                       child=self.child_bin,
                                       variance=variance_bin)
     self._factors.update({
         "split": self._split,
         "model_cts": self._model_cts,
         "model_bin": self._model_bin
     })
Beispiel #2
0
 def __init__(self, child: GaussianArray, parent: GaussianArray, bias=None, weight=None):
     super().__init__()
     self._deterministic = True
     self.child = child
     self.shape_child = child.shape()
     self.parent = parent
     self.shape_parent = parent.shape()
     self.message_to_child = GaussianArray.uniform(child.shape())
     self.message_to_parent = GaussianArray.uniform(parent.shape())
     if bias is None:
         bias = tf.zeros((1, self.shape_child[1]))
     self.bias = ParameterArray(bias, False, name="WeightedSum.bias")
     if weight is None:
         weight = tf.ones((self.shape_parent[1], self.shape_child[1]))
     self.weight = ParameterArray(weight, False, name="WeightedSum.weight")
     self._parameters = {"bias": self.bias, "weight": self.weight}
Beispiel #3
0
 def __init__(self, child: GaussianArray, parent: GaussianArray, variance: float = 1.):
     super().__init__()
     self._deterministic = False
     self.shape = child.shape()
     self.child = child
     self.parent = parent
     self.message_to_child = GaussianArray.uniform(self.shape)
     self.message_to_parent = GaussianArray.uniform(self.shape)
     self.variance = ParameterArrayLogScale(variance, False, name="AddVariance.variance")
     self._parameters = {"variance": self.variance}
Beispiel #4
0
 def __init__(self, child: GaussianArray, mean: float = 0., variance: float = 1., initial=None, name=""):
     super().__init__()
     self._deterministic = False
     self.mean = ParameterArray(mean, True, name=name+".mean")
     self.variance = ParameterArrayLogScale(variance, True, name=name+".variance")
     self._parameters = {"mean": self.mean, "variance": self.variance}
     self.child = child
     self.shape = child.shape()
     self.prior = GaussianArray.from_shape(self.shape, self.mean.value(), self.variance.value())
     # initialize child
     self.message_to_child = GaussianArray.from_array(initial, tf.ones_like(initial) * variance * 0.1)
     self.child.set_to(self.message_to_child)
Beispiel #5
0
 def __init__(self, vector: GaussianArray, parts: Dict[str, GaussianArray]):
     super().__init__()
     self._deterministic = True
     self.vector = vector
     shape_in = {n: p.shape() for n, p in parts.items()}
     self.parts = parts
     shape_out = vector.shape()
     d = len(shape_out)
     self.message_to_parts = {k: GaussianArray.uniform(s) for k, s in shape_in.items()}
     self.message_to_vector = GaussianArray.uniform(shape_out)
     size = [s[-1] for k, s in shape_in.items()]
     begin = [0, *np.cumsum(size[:-1])]
     self._size = [tuple([*shape_out[:-1], s]) for s in size]
     self._begin = [tuple([*[0] * (d - 1), s]) for s in begin]
     self._name = [k for k, s in shape_in.items()]