예제 #1
0
def make_sampling_computation_graph(model_path, num_samples):
    f = file(model_path, 'rb')
    model = cPickle.load(f)#main_loop = load(model_path)#
    f.close()
    #model = main_loop.model
    selector = Selector(model.top_bricks)
    decoder_mlp1, = selector.select('/decoder_network1').bricks
    decoder_mlp2, = selector.select('/decoder_network2').bricks
    decoder_mlp3, = selector.select('/decoder_network3').bricks
    theano_rng = Random().theano_rng

    z2 = theano_rng.normal(size=(num_samples, decoder_mlp1.input_dim),
                           dtype=theano.config.floatX)

    h2 = decoder_mlp1.apply(z2) 
    h2 = h2[:, :50] + theano.tensor.exp(0.5 * h2[:, 50:]) * theano_rng.normal(size=(num_samples, 50),
                                                                              dtype=theano.config.floatX)


    z1 = theano_rng.normal(size=(num_samples, 10),
                           dtype=theano.config.floatX)

    h1 = decoder_mlp2.apply(theano.tensor.concatenate([h2, z1], axis=1)) 
    h1 = h1[:, :50] + theano.tensor.exp(0.5 * h1[:, 50:]) * theano_rng.normal(size=(num_samples, 50),
                                                                              dtype=theano.config.floatX)

    p = decoder_mlp3.apply(theano.tensor.concatenate([h1, h2], axis=1)).reshape((num_samples, 28, 28))

    return ComputationGraph([p])
예제 #2
0
def make_sampling_computation_graph(model_path, num_samples):
    f = file(model_path, 'rb')
    model = cPickle.load(f)#main_loop = load(model_path)#
    f.close()
    #model = main_loop.model
    selector = Selector(model.top_bricks)
    decoder_mlp1, = selector.select('/decoder_network1').bricks
    decoder_mlp2, = selector.select('/decoder_network2').bricks
    decoder_mlp3, = selector.select('/decoder_network3').bricks
    theano_rng = Random().theano_rng

    z1 = theano_rng.normal(size=(num_samples, decoder_mlp1.input_dim),
                           dtype=theano.config.floatX)

    z2 = decoder_mlp1.apply(z1)
    z2 = z2[:, :40]# + theano.tensor.exp(0.5 * z2[:, 40:]) * theano_rng.normal(size=(num_samples, 40),
                    #                                                          dtype=theano.config.floatX)

    z3 = decoder_mlp2.apply(z2)
    z3 = z3[:, :100] + theano.tensor.exp(0.5 * z3[:, 100:]) * theano_rng.normal(size=(num_samples, 100),
                                                                                dtype=theano.config.floatX)

    p = decoder_mlp3.apply(z3).reshape((num_samples, 28, 28))

    return ComputationGraph([p])
예제 #3
0
    def generate_forward_diffusion_sample(self, X_noiseless):
        """
        Corrupt a training image with t steps worth of Gaussian noise, and
        return the corrupted image, as well as the mean and covariance of the
        posterior q(x^{t-1}|x^t, x^0).
        """

        X_noiseless = X_noiseless.reshape(
            (-1, self.n_colors, self.spatial_width, self.spatial_width))

        n_images = X_noiseless.shape[0].astype('int16')
        rng = Random().theano_rng
        # choose a timestep in [1, self.trajectory_length-1].
        # note the reverse process is fixed for the very
        # first timestep, so we skip it.
        # TODO for some reason random_integer is missing from the Blocks
        # theano random number generator.
        t = T.floor(rng.uniform(size=(1,1), low=1, high=self.trajectory_length,
            dtype=theano.config.floatX))
        t_weights = self.get_t_weights(t)
        N = rng.normal(size=(n_images, self.n_colors, self.spatial_width, self.spatial_width),
            dtype=theano.config.floatX)

        # noise added this time step
        beta_forward = self.get_beta_forward(t)
        # decay in noise variance due to original signal this step
        alpha_forward = 1. - beta_forward
        # compute total decay in the fraction of the variance due to X_noiseless
        alpha_arr = 1. - self.beta_arr
        alpha_cum_forward_arr = T.extra_ops.cumprod(alpha_arr).reshape((self.trajectory_length,1))
        alpha_cum_forward = T.dot(t_weights.T, alpha_cum_forward_arr)
        # total fraction of the variance due to noise being mixed in
        beta_cumulative = 1. - alpha_cum_forward
        # total fraction of the variance due to noise being mixed in one step ago
        beta_cumulative_prior_step = 1. - alpha_cum_forward/alpha_forward

        # generate the corrupted training data
        X_uniformnoise = X_noiseless + (rng.uniform(size=(n_images, self.n_colors, self.spatial_width, self.spatial_width),
            dtype=theano.config.floatX)-T.constant(0.5,dtype=theano.config.floatX))*T.constant(self.uniform_noise,dtype=theano.config.floatX)
        X_noisy = X_uniformnoise*T.sqrt(alpha_cum_forward) + N*T.sqrt(1. - alpha_cum_forward)

        # compute the mean and covariance of the posterior distribution
        mu1_scl = T.sqrt(alpha_cum_forward / alpha_forward)
        mu2_scl = 1. / T.sqrt(alpha_forward)
        cov1 = 1. - alpha_cum_forward/alpha_forward
        cov2 = beta_forward / alpha_forward
        lam = 1./cov1 + 1./cov2
        mu = (
                X_uniformnoise * mu1_scl / cov1 +
                X_noisy * mu2_scl / cov2
            ) / lam
        sigma = T.sqrt(1./lam)
        sigma = sigma.reshape((1,1,1,1))

        mu.name = 'mu q posterior'
        sigma.name = 'sigma q posterior'
        X_noisy.name = 'X_noisy'
        t.name = 't'

        return X_noisy, t, mu, sigma
예제 #4
0
def make_sampling_computation_graph(model_path, num_samples):
    f = file(model_path, 'rb')
    model = cPickle.load(f)#main_loop = load(model_path)#
    f.close()
    #model = main_loop.model
    selector = Selector(model.top_bricks)
    decoder_mlp2, = selector.select('/decoder_network2').bricks
    decoder_mlp1, = selector.select('/decoder_network1').bricks
    upsample_mlp2, = selector.select('/upsample_network2').bricks
    upsample_mlp1, = selector.select('/upsample_network1').bricks
    theano_rng = Random().theano_rng

    z2 = theano_rng.normal(size=(num_samples, decoder_mlp2.input_dim),
                           dtype=theano.config.floatX)

    h2_params = decoder_mlp2.apply(z2)
    length = int(h2_params.eval().shape[1]/2)
    h2_mu = h2_params[:, :length]
    h2_lognu = h2_params[:, length:]
    h2 = h2_mu + theano.tensor.exp(0.5 * h2_lognu) * theano_rng.normal(size=h2_mu.shape,
                                                                       dtype=h2_mu.dtype)
    
    z1 = theano_rng.normal(size=(num_samples, decoder_mlp1.input_dim),
                           dtype=theano.config.floatX)

    h1_tilde_params = decoder_mlp1.apply(z1)
    length = int(h1_tilde_params.eval().shape[1]/2)
    h1_tilde_mu = h1_tilde_params[:, :length]
    h1_tilde_lognu = h1_tilde_params[:, length:]
    h1_tilde = h1_tilde_mu + theano.tensor.exp(0.5 * h1_tilde_lognu) * theano_rng.normal(size=h1_tilde_mu.shape,
                                                                                         dtype=h1_tilde_mu.dtype)


    import pdb; pdb.set_trace()
    h1 = upsample_mlp1.apply(h2) + h1_tilde
  
    p = upsample_mlp2.apply(h1).reshape((num_samples, 28, 28))

    return ComputationGraph([p])
예제 #5
0
def make_sampling_computation_graph(model_path, num_samples):
    f = file(model_path, 'rb')
    model = cPickle.load(f)#main_loop = load(model_path)#
    f.close()
    #model = main_loop.model
    selector = Selector(model.top_bricks)
    decoder_mlp, = selector.select('/decoder_network').bricks
    theano_rng = Random().theano_rng

    z = theano_rng.normal(size=(num_samples, decoder_mlp.input_dim),
                          dtype=theano.config.floatX)
    p = decoder_mlp.apply(z).reshape((num_samples, 28, 28))

    return ComputationGraph([p])