Example #1
0
def callback(itr):
    def samplefun(num_samps):
        import numpy as np
        z = np.array(np.random.randn(num_samps, zdim), dtype=np.float32)
        return decode(z).eval(session=sess)

    viz.plot_samples(itr, samplefun, savedir='avae_mnist_samples')

    def sample_normal(mu, log_sigmasq, M=5):
        dim = mu.get_shape()[1].value
        eps = tf.random_normal((M, dim), dtype=tf.float32)
        return mu + tf.exp(0.5 * log_sigmasq) * eps

    def recons(num_samps):
        # choose one datapoint, encode it
        subset = X[np.random.choice(X.shape[0], 1)]
        # compute encoder - first aux, then encoder conditioned on aux
        amu, alog_sigmasq = aux_encode(subset)
        asamps = sample_normal(amu, alog_sigmasq, M=24)
        Xa = tf.concat(1, [tf.tile(subset, [24, 1]), asamps])
        zmu, zlog_sigmasq = encode(Xa)
        imgs = decode(sample_normal(zmu, zlog_sigmasq,
                                    M=24)).eval(session=sess)
        return np.row_stack([subset, imgs])

    viz.plot_samples(itr, recons, savedir='avae_mnist_samples', stub='recon')
    test_lb = test_lb_fun.eval(session=sess) * Ntest
    print "test data VLB: ", np.mean(test_lb)
Example #2
0
    def callback(itr):

        # plot samples from the model generative process
        viz.plot_samples(itr, sample_fun, savedir=output_dir)

        # plot reconstructed samples
        viz.plot_samples(itr, recon_fun, savedir=output_dir, stub='recon')

        # evaluate test
        test_lb = test_vlb_fun.eval(session=sess) * Ntest
        print "test data VLB: ", np.mean(test_lb)

        # plot z space for each test data item, colored by digit type
        def plot_test_latent():
            z = latent_space_fun()
            fig = plt.figure()
            ax = plt.gca()
            for i in xrange(10):
                idx = np.argmax(Yt, 1) == i
                ax.scatter(z[idx, 0], z[idx, 1], c=colors[i], label='%d' % i)
            ax.legend()
            fig.savefig(os.path.join(output_dir,
                                     "latent_space_%03d.png" % itr))

        plot_test_latent()
Example #3
0
def callback(itr):
    def samplefun(num_samps):
        import numpy as np
        z = np.array(np.random.randn(num_samps, zdim), dtype=np.float32)
        return decode(z).eval(session=sess)
    viz.plot_samples(itr, samplefun, savedir='avae_mnist_samples')

    def sample_normal(mu, log_sigmasq, M=5):
        dim = mu.get_shape()[1].value
        eps = tf.random_normal((M, dim), dtype=tf.float32)
        return mu + tf.exp(0.5 * log_sigmasq) * eps

    def recons(num_samps):
        # choose one datapoint, encode it
        subset = X[np.random.choice(X.shape[0], 1)]
        # compute encoder - first aux, then encoder conditioned on aux
        amu, alog_sigmasq = aux_encode(subset)
        asamps            = sample_normal(amu, alog_sigmasq, M=24)
        Xa                = tf.concat(1, [tf.tile(subset, [24, 1]),  asamps])
        zmu, zlog_sigmasq = encode(Xa)
        imgs = decode(sample_normal(zmu, zlog_sigmasq, M=24)).eval(session=sess)
        return np.row_stack([subset, imgs])

    viz.plot_samples(itr, recons, savedir='avae_mnist_samples', stub='recon')
    test_lb = test_lb_fun.eval(session=sess) * Ntest
    print "test data VLB: ", np.mean(test_lb)
Example #4
0
    def callback(itr):
        def samplefun(num_samps):
            import numpy as np
            z = np.array(np.random.randn(num_samps, zdim), dtype=np.float32)
            return decode(z).eval(session=sess)

        viz.plot_samples(itr, samplefun, savedir='avae_mnist_samples')

        def sample_normal(mu, log_sigmasq, M=5):
            dim = mu.get_shape()[1].value
            eps = tf.random_normal((M, dim), dtype=tf.float32)
            return mu + tf.exp(0.5 * log_sigmasq) * eps

        def recons(num_samps):
            # choose one datapoint, encode it
            subset = X[np.random.choice(X.shape[0], 1)]
            # compute encoder - first aux, then encoder conditioned on aux
            amu, alog_sigmasq = aux_encode(subset)
            asamps = sample_normal(amu, alog_sigmasq, M=24)
            Xa = tf.concat(1, [tf.tile(subset, [24, 1]), asamps])
            zmu, zlog_sigmasq = encode(Xa)
            imgs = decode(sample_normal(zmu, zlog_sigmasq,
                                        M=24)).eval(session=sess)
            return np.row_stack([subset, imgs])

        viz.plot_samples(itr,
                         recons,
                         savedir='avae_mnist_samples',
                         stub='recon')
        test_lb = test_lb_fun.eval(session=sess) * Ntest
        print "test data VLB: ", np.mean(test_lb)

        # plot z space for each test data item, colored by digit type
        def plot_test_latent():
            amu, alog_sigmasq = aux_encode(Xtest)
            asamps = sample_normal(amu, alog_sigmasq, M=1)
            zmu, zlog_sigmasq = encode(tf.concat(1, [Xtest, asamps]))
            z = sample_normal(zmu, zlog_sigmasq, M=1).eval(session=sess)
            print z.shape
            import matplotlib.pyplot as plt
            fig = plt.figure()
            ax = plt.gca()
            import seaborn as sns
            colors = sns.color_palette('muted', n_colors=10)
            for i in xrange(10):
                idx = np.argmax(Yt, 1) == i
                ax.scatter(z[idx, 0], z[idx, 1], c=colors[i], label='%d' % i)
            ax.legend()
            fig.savefig("avae_mnist_samples/latent_space_%03d.png" % itr)

        plot_test_latent()
Example #5
0
def callback(itr):
    def samplefun(num_samps):
        import numpy as np
        z = np.array(np.random.randn(num_samps, zdim), dtype=np.float32)
        return decode(z).eval(session=sess)
    viz.plot_samples(itr, samplefun, savedir='vae_mnist_samples')

    def sample_z(mu, log_sigmasq, M=5):
        eps = tf.random_normal((M, zdim), dtype=tf.float32)
        return mu + tf.exp(0.5 * log_sigmasq) * eps

    def recons(num_samps):
        # random subset
        subset = X[np.random.choice(X.shape[0], 1)]
        mu, log_sigmasq = encode(subset)
        imgs = decode(sample_z(mu, log_sigmasq, M=24)).eval(session=sess)
        return np.row_stack([subset, imgs])
    viz.plot_samples(itr, recons, savedir='vae_mnist_samples', stub='recon')
    test_lb = test_lb_fun.eval(session=sess) * Ntest
    print "test data VLB: ", np.mean(test_lb)
Example #6
0
def callback(itr):
    def samplefun(num_samps):
        import numpy as np
        z = np.array(np.random.randn(num_samps, zdim), dtype=np.float32)
        return decode(z).eval(session=sess)

    viz.plot_samples(itr, samplefun, savedir='vae_mnist_samples')

    def sample_z(mu, log_sigmasq, M=5):
        eps = tf.random_normal((M, zdim), dtype=tf.float32)
        return mu + tf.exp(0.5 * log_sigmasq) * eps

    def recons(num_samps):
        # random subset
        subset = X[np.random.choice(X.shape[0], 1)]
        mu, log_sigmasq = encode(subset)
        imgs = decode(sample_z(mu, log_sigmasq, M=24)).eval(session=sess)
        return np.row_stack([subset, imgs])

    viz.plot_samples(itr, recons, savedir='vae_mnist_samples', stub='recon')
    test_lb = test_lb_fun.eval(session=sess) * Ntest
    print "test data VLB: ", np.mean(test_lb)
Example #7
0
    def callback(itr):

        # plot samples from the model generative process
        viz.plot_samples(itr, sample_fun, savedir=output_dir)

        # plot reconstructed samples
        viz.plot_samples(itr, recon_fun, savedir=output_dir, stub='recon')

        # evaluate test 
        test_lb = test_vlb_fun()
        print "test data VLB: ", np.mean(test_lb)

        # plot z space for each test data item, colored by digit type
        def plot_test_latent():
            Z, Y = latent_space_fun()
            fig  = plt.figure()
            ax   = plt.gca()
            for i in xrange(10):
                idx = np.argmax(Y, 1) == i
                ax.scatter(Z[idx,0], Z[idx,1], c=colors[i], label='%d'%i)
            ax.legend()
            fig.savefig(os.path.join(output_dir, "latent_space_%03d.png" % itr))
        plot_test_latent()
Example #8
0
        def make_gmm_sample_fun():
            z_gmm = tf.placeholder(tf.float32, shape=(None, zdim))
            sfun_gmm = decode(z_gmm)

            def gmm_sample_fun(num_samps):
                return sfun_gmm.eval(feed_dict={z_gmm: gmm.sample(num_samps)},
                                     session=sess)

            return gmm_sample_fun

        gmm_sample_fun = make_gmm_sample_fun()

        viz.plot_samples(0,
                         gmm_sample_fun,
                         savedir=OUTPUT_DIR,
                         stub='gmm_',
                         sidelen=10)
        viz.plot_samples(0,
                         sample_fun,
                         savedir=OUTPUT_DIR,
                         stub='big_',
                         sidelen=10)

        import matplotlib.pyplot as plt
        xg = yg = np.linspace(-3, 3, 100)
        xx, yy = np.meshgrid(xg, yg)
        fig = plt.figure(figsize=(8, 8))
        z = gmm.score(np.column_stack([xx.flatten(), yy.flatten()]))
        plt.contourf(xx, yy, np.exp(z.reshape(xx.shape)))
        import os
Example #9
0
        zt = ztrain.eval(session=sess, feed_dict={Xtrain:X})

        # fit a mixture model to zt, and sample from GMM prior
        from sklearn.mixture import GMM
        gmm = GMM(n_components=20, covariance_type='full')
        gmm.fit(zt)

        def make_gmm_sample_fun():
            z_gmm    = tf.placeholder(tf.float32, shape=(None, zdim))
            sfun_gmm = decode(z_gmm)
            def gmm_sample_fun(num_samps):
                return sfun_gmm.eval(feed_dict={z_gmm: gmm.sample(num_samps)}, session=sess)
            return gmm_sample_fun
        gmm_sample_fun = make_gmm_sample_fun()

        viz.plot_samples(0, gmm_sample_fun, savedir=OUTPUT_DIR, stub='gmm_', sidelen=10)
        viz.plot_samples(0, sample_fun, savedir=OUTPUT_DIR, stub='big_', sidelen=10)

        import matplotlib.pyplot as plt
        xg = yg = np.linspace(-3, 3, 100)
        xx, yy = np.meshgrid(xg, yg)
        fig = plt.figure(figsize=(8,8))
        z = gmm.score(np.column_stack([xx.flatten(), yy.flatten()]))
        plt.contourf(xx, yy, np.exp(z.reshape(xx.shape)))
        import os
        plt.savefig(os.path.join(OUTPUT_DIR, 'prob_z_gmm.png'))
        plt.close("all")



    else: