Exemplo n.º 1
0
    def sample(self, Y):
        """ Evaluate the log-probability for the given samples.

        Parameters
        ----------
        Y:      T.tensor
            samples from the upper layer

        Returns
        -------
        X:      T.tensor
            samples from the lower layer
        log_p:  T.tensor
            log-probabilities for the samples in X and Y
        """
        n_X, n_Y, n_hid = self.get_hyper_params(['n_X', 'n_Y', 'n_hid'])
        b, c, W, V, Ub, Uc = self.get_model_params(
            ['b', 'c', 'W', 'V', 'Ub', 'Uc'])

        batch_size = Y.shape[0]
        cond = Y

        #------------------------------------------------------------------
        b_cond = b + T.dot(cond, Ub)  # shape (batch, n_vis)
        c_cond = c + T.dot(cond, Uc)  # shape (batch, n_hid)

        a_init = c_cond
        post_init = T.zeros([batch_size], dtype=floatX)
        vis_init = T.zeros([batch_size], dtype=floatX)
        rand = theano_rng.uniform((n_X, batch_size), nstreams=512)

        def one_iter(Wi, Vi, bi, rand_i, a, vis_i, post):
            hid = self.sigmoid(a)
            pi = self.sigmoid(T.dot(hid, Vi) + bi)
            vis_i = T.cast(rand_i <= pi, floatX)
            post = post + T.log(pi * vis_i + (1 - pi) * (1 - vis_i))
            a = a + T.outer(vis_i, Wi)
            return a, vis_i, post

        [a, vis, post
         ], updates = unrolled_scan(fn=one_iter,
                                    sequences=[W, V.T, b_cond.T, rand],
                                    outputs_info=[a_init, vis_init, post_init],
                                    unroll=self.unroll_scan)
        assert len(updates) == 0
        return vis.T, post[-1, :]
Exemplo n.º 2
0
    def sample(self, Y):
        """ Evaluate the log-probability for the given samples.

        Parameters
        ----------
        Y:      T.tensor
            samples from the upper layer

        Returns
        -------
        X:      T.tensor
            samples from the lower layer        
        log_p:  T.tensor
            log-probabilities for the samples in X and Y
        """
        n_X, n_Y, n_hid = self.get_hyper_params(['n_X', 'n_Y', 'n_hid'])
        b, c, W, V, Ub, Uc = self.get_model_params(['b', 'c', 'W', 'V', 'Ub', 'Uc'])

        batch_size = Y.shape[0]
        cond = Y

        #------------------------------------------------------------------
        b_cond = b + T.dot(cond, Ub)    # shape (batch, n_vis)
        c_cond = c + T.dot(cond, Uc)    # shape (batch, n_hid)
    
        a_init    = c_cond
        post_init = T.zeros([batch_size], dtype=floatX)
        vis_init  = T.zeros([batch_size], dtype=floatX)
        rand      = theano_rng.uniform((n_X, batch_size), nstreams=512)

        def one_iter(Wi, Vi, bi, rand_i, a, vis_i, post):
            hid  = self.sigmoid(a)
            pi   = self.sigmoid(T.dot(hid, Vi) + bi)
            vis_i = T.cast(rand_i <= pi, floatX)
            post  = post + T.log(pi*vis_i + (1-pi)*(1-vis_i))
            a     = a + T.outer(vis_i, Wi)
            return a, vis_i, post

        [a, vis, post], updates = unrolled_scan(
                    fn=one_iter,
                    sequences=[W, V.T, b_cond.T, rand], 
                    outputs_info=[a_init, vis_init, post_init],
                    unroll=self.unroll_scan
                )
        assert len(updates) == 0
        return vis.T, post[-1,:]
Exemplo n.º 3
0
    def sample(self, Y):
        """ Evaluate the log-probability for the given samples.

        Parameters
        ----------
        Y:      T.tensor
            samples from the upper layer

        Returns
        -------
        X:      T.tensor
            samples from the lower layer       
        log_p:  T.tensor
            log-probabilities for the samples in X and Y
        """
        n_X, n_Y = self.get_hyper_params(['n_X', 'n_Y'])
        b, W, U = self.get_model_params(['b', 'W', 'U'])

        batch_size = Y.shape[0]

        #------------------------------------------------------------------

        a_init    = T.dot(Y, U) + T.shape_padleft(b)   # shape (batch, n_vis)
        post_init = T.zeros([batch_size], dtype=floatX)
        x_init    = T.zeros([batch_size], dtype=floatX)
        rand      = theano_rng.uniform((n_X, batch_size), nstreams=512)

        def one_iter(i, Wi, rand_i, a, X, post):
            pi   = self.sigmoid(a[:,i])
            xi   = T.cast(rand_i <= pi, floatX)
            post = post + T.log(pi*xi + (1-pi)*(1-xi))            
            a    = a + T.outer(xi, Wi) 
            return a, xi, post

        [a, X, post], updates = unrolled_scan(
                    fn=one_iter,
                    sequences=[T.arange(n_X), W, rand],
                    outputs_info=[a_init, x_init, post_init],
                    unroll=self.unroll_scan
                )
        assert len(updates) == 0
        return X.T, post[-1,:]
Exemplo n.º 4
0
    def sample(self, n_samples):
        """ Sample from this toplevel module and return X ~ P(X), log(P(X))

        Parameters
        ----------
        n_samples:
            number of samples to drawn

        Returns
        -------
        X:      T.tensor
            samples from this module
        log_p:  T.tensor
            log-probabilities for the samples returned in X
        """
        n_X, n_hid = self.get_hyper_params(['n_X', 'n_hid'])
        b, c, W, V = self.get_model_params(['b', 'c', 'W', 'V'])

        #------------------------------------------------------------------
    
        a_init    = T.zeros([n_samples, n_hid]) + T.shape_padleft(c)
        post_init = T.zeros([n_samples], dtype=floatX)
        vis_init  = T.zeros([n_samples], dtype=floatX)
        rand      = theano_rng.uniform((n_X, n_samples), nstreams=512)

        def one_iter(Wi, Vi, bi, rand_i, a, vis_i, post):
            hid  = self.sigmoid(a)
            pi   = self.sigmoid(T.dot(hid, Vi) + bi)
            vis_i = T.cast(rand_i <= pi, floatX)
            post  = post + T.log(pi*vis_i + (1-pi)*(1-vis_i))
            a     = a + T.outer(vis_i, Wi)
            return a, vis_i, post

        [a, vis, post], updates = unrolled_scan(
                    fn=one_iter,
                    sequences=[W, V.T, b, rand], 
                    outputs_info=[a_init, vis_init, post_init],
                    unroll=self.unroll_scan
                )
        assert len(updates) == 0
        return vis.T, post[-1,:]
Exemplo n.º 5
0
    def sample(self, Y):
        """ Evaluate the log-probability for the given samples.

        Parameters
        ----------
        Y:      T.tensor
            samples from the upper layer

        Returns
        -------
        X:      T.tensor
            samples from the lower layer       
        log_p:  T.tensor
            log-probabilities for the samples in X and Y
        """
        n_X, n_Y = self.get_hyper_params(['n_X', 'n_Y'])
        b, W, U = self.get_model_params(['b', 'W', 'U'])

        batch_size = Y.shape[0]

        #------------------------------------------------------------------

        a_init = T.dot(Y, U) + T.shape_padleft(b)  # shape (batch, n_vis)
        post_init = T.zeros([batch_size], dtype=floatX)
        x_init = T.zeros([batch_size], dtype=floatX)
        rand = theano_rng.uniform((n_X, batch_size), nstreams=512)

        def one_iter(i, Wi, rand_i, a, X, post):
            pi = self.sigmoid(a[:, i])
            xi = T.cast(rand_i <= pi, floatX)
            post = post + T.log(pi * xi + (1 - pi) * (1 - xi))
            a = a + T.outer(xi, Wi)
            return a, xi, post

        [a, X, post
         ], updates = unrolled_scan(fn=one_iter,
                                    sequences=[T.arange(n_X), W, rand],
                                    outputs_info=[a_init, x_init, post_init],
                                    unroll=self.unroll_scan)
        assert len(updates) == 0
        return X.T, post[-1, :]
Exemplo n.º 6
0
    def sample(self, n_samples):
        """ Sample from this toplevel module and return X ~ P(X), log(P(X))

        Parameters
        ----------
        n_samples:
            number of samples to drawn

        Returns
        -------
        X:      T.tensor
            samples from this module
        log_p:  T.tensor
            log-probabilities for the samples returned in X
        """
        n_X, n_hid = self.get_hyper_params(['n_X', 'n_hid'])
        b, c, W, V = self.get_model_params(['b', 'c', 'W', 'V'])

        #------------------------------------------------------------------

        a_init = T.zeros([n_samples, n_hid]) + T.shape_padleft(c)
        post_init = T.zeros([n_samples], dtype=floatX)
        vis_init = T.zeros([n_samples], dtype=floatX)
        rand = theano_rng.uniform((n_X, n_samples), nstreams=512)

        def one_iter(Wi, Vi, bi, rand_i, a, vis_i, post):
            hid = self.sigmoid(a)
            pi = self.sigmoid(T.dot(hid, Vi) + bi)
            vis_i = T.cast(rand_i <= pi, floatX)
            post = post + T.log(pi * vis_i + (1 - pi) * (1 - vis_i))
            a = a + T.outer(vis_i, Wi)
            return a, vis_i, post

        [a, vis, post
         ], updates = unrolled_scan(fn=one_iter,
                                    sequences=[W, V.T, b, rand],
                                    outputs_info=[a_init, vis_init, post_init],
                                    unroll=self.unroll_scan)
        assert len(updates) == 0
        return vis.T, post[-1, :]
Exemplo n.º 7
0
    def sample(self, n_samples):
        """ Sample from this toplevel module and return X ~ P(X), log(P(X))

        Parameters
        ----------
        n_samples:
            number of samples to drawn

        Returns
        -------
        X:      T.tensor
            samples from this module
        log_p:  T.tensor
            log-probabilities for the samples returned in X
        """
        n_X, = self.get_hyper_params(['n_X'])
        b, W = self.get_model_params(['b', 'W'])

        #------------------------------------------------------------------

        a_init    = T.zeros([n_samples, n_X]) + T.shape_padleft(b)
        post_init = T.zeros([n_samples], dtype=floatX)
        x_init    = T.zeros([n_samples], dtype=floatX)
        rand      = theano_rng.uniform((n_X, n_samples), nstreams=512)

        def one_iter(i, Wi, rand_i, a, X, post):
            pi   = self.sigmoid(a[:,i])
            xi   = T.cast(rand_i <= pi, floatX)
            post = post + T.log(pi*xi + (1-pi)*(1-xi))            
            a    = a + T.outer(xi, Wi) 
            return a, xi, post

        [a, X, post], updates = unrolled_scan(
                    fn=one_iter,
                    sequences=[T.arange(n_X), W, rand],
                    outputs_info=[a_init, x_init, post_init],
                    unroll=self.unroll_scan
                )
        assert len(updates) == 0
        return X.T, post[-1,:]
Exemplo n.º 8
0
    def log_prob(self, X):
        """ Evaluate the log-probability for the given samples.

        Parameters
        ----------
        X:      T.tensor 
            samples from X

        Returns
        -------
        log_p:  T.tensor
            log-probabilities for the samples in X
        """
        n_X, n_hid = self.get_hyper_params(['n_X', 'n_hid'])
        b, c, W, V = self.get_model_params(['b', 'c', 'W', 'V'])
        
        batch_size = X.shape[0]
        vis = X

        #------------------------------------------------------------------
    
        a_init    = T.zeros([batch_size, n_hid]) + T.shape_padleft(c)
        post_init = T.zeros([batch_size], dtype=floatX)

        def one_iter(vis_i, Wi, Vi, bi, a, post):
            hid  = self.sigmoid(a)
            pi   = self.sigmoid(T.dot(hid, Vi) + bi)
            post = post + T.log(pi*vis_i + (1-pi)*(1-vis_i))
            a    = a + T.outer(vis_i, Wi)
            return a, post

        [a, post], updates = unrolled_scan(
                    fn=one_iter,
                    sequences=[vis.T, W, V.T, b],
                    outputs_info=[a_init, post_init],
                    unroll=self.unroll_scan
                )
        assert len(updates) == 0
        return post[-1,:]
Exemplo n.º 9
0
    def sample(self, n_samples):
        """ Sample from this toplevel module and return X ~ P(X), log(P(X))

        Parameters
        ----------
        n_samples:
            number of samples to drawn

        Returns
        -------
        X:      T.tensor
            samples from this module
        log_p:  T.tensor
            log-probabilities for the samples returned in X
        """
        n_X, = self.get_hyper_params(['n_X'])
        b, W = self.get_model_params(['b', 'W'])

        #------------------------------------------------------------------

        a_init = T.zeros([n_samples, n_X]) + T.shape_padleft(b)
        post_init = T.zeros([n_samples], dtype=floatX)
        x_init = T.zeros([n_samples], dtype=floatX)
        rand = theano_rng.uniform((n_X, n_samples), nstreams=512)

        def one_iter(i, Wi, rand_i, a, X, post):
            pi = self.sigmoid(a[:, i])
            xi = T.cast(rand_i <= pi, floatX)
            post = post + T.log(pi * xi + (1 - pi) * (1 - xi))
            a = a + T.outer(xi, Wi)
            return a, xi, post

        [a, X, post
         ], updates = unrolled_scan(fn=one_iter,
                                    sequences=[T.arange(n_X), W, rand],
                                    outputs_info=[a_init, x_init, post_init],
                                    unroll=self.unroll_scan)
        assert len(updates) == 0
        return X.T, post[-1, :]
    def post_sample(self, X, Y_init=None, prior=None, rate=1.0):
        """ posterior sampling """
        n_X, n_Y = self.get_hyper_params(['n_X', 'n_Y'])
        W, b = self.get_model_params(['W', 'b'])
        W = W * rate
        b = b * rate

        batch_size = X.shape[0]

        if Y_init is None:
            Y_init = T.cast(theano_rng.binomial((batch_size, W.shape[0]),
                                                nstreams=512),
                            dtype=floatX)
        if prior is None:
            prior = T.zeros(Y_init.shape, dtype=floatX)

        WYb_init = T.dot(Y_init, W) + b
        rand = theano_rng.uniform((n_Y, batch_size), nstreams=512)

        def one_iter(k, Wk, prior_k, rand_k, WYb, Y, X):
            WXb = T.dot(X, Wk) + prior_k
            WYb = WYb - T.outer(Y[:, k], Wk)
            reg_0 = -T.sum(T.nnet.softplus(WYb), axis=1)
            reg_1 = -T.sum(T.nnet.softplus(WYb + Wk), axis=1) + WXb
            prob = 1. / (1 + T.exp(reg_0 - reg_1))
            Y = T.set_subtensor(Y[:, k], T.cast(rand_k <= prob, dtype=floatX))
            WYb = WYb + T.outer(Y[:, k], Wk)
            return WYb, Y

        [WYb_list, Y_list], updates = unrolled_scan(
            fn=one_iter,
            sequences=[T.arange(n_Y), W, prior.T, rand],
            outputs_info=[WYb_init, Y_init],
            non_sequences=[X],
            unroll=self.unroll_scan)

        return Y_list[-1]
Exemplo n.º 11
0
    def log_prob(self, X):
        """ Evaluate the log-probability for the given samples.

        Parameters
        ----------
        X:      T.tensor
            samples from X

        Returns
        -------
        log_p:  T.tensor
            log-probabilities for the samples in X
        """
        n_X, n_hid = self.get_hyper_params(['n_X', 'n_hid'])
        b, c, W, V = self.get_model_params(['b', 'c', 'W', 'V'])

        batch_size = X.shape[0]
        vis = X

        #------------------------------------------------------------------

        a_init = T.zeros([batch_size, n_hid]) + T.shape_padleft(c)
        post_init = T.zeros([batch_size], dtype=floatX)

        def one_iter(vis_i, Wi, Vi, bi, a, post):
            hid = self.sigmoid(a)
            pi = self.sigmoid(T.dot(hid, Vi) + bi)
            post = post + T.log(pi * vis_i + (1 - pi) * (1 - vis_i))
            a = a + T.outer(vis_i, Wi)
            return a, post

        [a, post], updates = unrolled_scan(fn=one_iter,
                                           sequences=[vis.T, W, V.T, b],
                                           outputs_info=[a_init, post_init],
                                           unroll=self.unroll_scan)
        assert len(updates) == 0
        return post[-1, :]