Exemplo n.º 1
0
 def testReparameterizeDiag(self):
     S, N, D = 4, 3, 2
     mean = np.random.randn(S, N, D)
     var = np.random.randn(S, N, D)**2
     z = np.random.randn(S, N, D)
     f = mean + z * (var + 1e-6)**0.5
     with tf.Session() as sess:
         assert_allclose(
             f, sess.run(reparameterize(tf.identity(mean), var, z)))
    def sample_from_conditional(self, X, z=None, full_cov=False):
        """
        Calculates self.conditional and also draws a sample

        If z=None then the tensorflow random_normal function is used to generate the
        N(0, 1) samples, otherwise z are used for the whitened sample points

        :param X: Input locations (S,N,D_in)
        :param full_cov: Whether to compute correlations between outputs
        :param z: None, or the sampled points in whitened representation
        :return: mean (S,N,D), var (S,N,N,D or S,N,D), samples (S,N,D)
        """
        mean, var = self.conditional(X, full_cov=full_cov)
        if z is None:
            z = tf.random_normal(tf.shape(mean), dtype=settings.float_type)
        samples = reparameterize(mean, var, z, full_cov=full_cov)
        return samples, mean, var
Exemplo n.º 3
0
    def sample_from_conditional(self, X, z=None, full_cov=False):
        """
        Calculates self.conditional and also draws a sample, adding input propagation if necessary

        If z=None then the tensorflow random_normal function is used to generate the
        N(0, 1) samples, otherwise z are used for the whitened sample points

        :param X: Input locations (S,N,D_in)
        :param full_cov: Whether to compute correlations between outputs
        :param z: None, or the sampled points in whitened representation
        :return: mean (S,N,D), var (S,N,N,D or S,N,D), samples (S,N,D)
        """
        mean, var = self.conditional_SND(X, full_cov=full_cov)

        # set shapes
        S = tf.shape(X)[0]
        N = tf.shape(X)[1]
        D = self.num_outputs

        mean = tf.reshape(mean, (S, N, D))
        if full_cov:
            var = tf.reshape(var, (S, N, N, D))
        else:
            var = tf.reshape(var, (S, N, D))

        if z is None:
            z = tf.random_normal(tf.shape(mean), dtype=settings.float_type)
        samples = reparameterize(mean, var, z, full_cov=full_cov)

        if self.input_prop_dim:
            shape = [tf.shape(X)[0], tf.shape(X)[1], self.input_prop_dim]
            X_prop = tf.reshape(X[:, :, :self.input_prop_dim], shape)

            samples = tf.concat([X_prop, samples], 2)
            mean = tf.concat([X_prop, mean], 2)

            if full_cov:
                shape = (tf.shape(X)[0], tf.shape(X)[1], tf.shape(X)[1],
                         tf.shape(var)[3])
                zeros = tf.zeros(shape, dtype=settings.float_type)
                var = tf.concat([zeros, var], 3)
            else:
                var = tf.concat([tf.zeros_like(X_prop), var], 2)

        return samples, mean, var
Exemplo n.º 4
0
    def testReparameterizeFullCov(self):
        S, N, D = 4, 3, 2

        mean = np.random.randn(S, N, D)
        U = np.random.randn(S, N, N, D)
        var = np.einsum('SnNd,SmNd->Snmd', U,
                        U) + np.eye(N)[None, :, :, None] * 1e-6

        var_flat = np.reshape(np.transpose(var, [0, 3, 1, 2]), [S * D, N, N])
        L_flat = np.linalg.cholesky(var_flat + np.eye(N)[None, :, :] * 1e-6)
        L = np.transpose(np.reshape(L_flat, [S, D, N, N]), [0, 2, 3, 1])

        z = np.random.randn(S, N, D)
        f = mean + np.einsum('SnNd,SNd->Snd', L, z)

        with tf.Session() as sess:
            assert_allclose(
                f,
                sess.run(
                    reparameterize(tf.identity(mean), var, z, full_cov=True)))