Exemple #1
0
def samples_from_generative_model():
    """
    Generate samples from our trained decoder, plot an image named samples_from_generative_model.png which the first
    row is the data distribution and the second row is the x_hat under distribution.
    :return: None
    """
    # Sample 10 z from prior
    unit_mu = tf.constant(0, shape=[10, latent_dim], dtype=tf.float32)
    unit_variance = tf.constant(1, shape=[10, latent_dim], dtype=tf.float32)
    z = diagonal_gaussian_sampler(unit_mu, unit_variance, latent_dim, 10)  # 10 x 2 matrix

    # For each z, plot p(x|z)
    p = decode(z, training=False)

    # Sample x from p(x|z)
    x_hat = bernoulli_sampler(p, 10, img_flat_size)


    # Concatenate plots into a figure
    with tf.Session() as session:
        p_plot = reshape_into_image(p.eval())
        x_hat_plot = reshape_into_image(1 - x_hat.eval())

    to_plot = np.concatenate((p_plot, x_hat_plot))
    data.save_images(to_plot, 'samples_from_generative_model.png', ims_per_row=10)
Exemple #2
0
def reconstructions_of_data(x):
    """
    Given the input data x, process it through our vae model and output its reconstruction x_hat. Plot an image
    named reconstructions_of_data.png
    :param x: input data
    :return:
    """

    # encode the input data x
    mu, variance = encode(x, training=False)

    # For each x, sample distribution z ~ q(z|x)
    z = diagonal_gaussian_sampler(mu, variance, latent_dim, 10)  # 10 x latent_dim matrix

    # For each z, decode to distribution p(x̃|z), plot.
    p = decode(z, training=False)
    x_hat = bernoulli_sampler(p, 10, img_flat_size)

    # Concatenate all plots into a figure.
    x_plot = reshape_into_image(x)
    with tf.Session() as session:
        p_plot = reshape_into_image(p.eval())
        x_hat_plot = reshape_into_image(1 - x_hat.eval())

    to_plot = np.concatenate((x_plot, p_plot, x_hat_plot))
    data.save_images(to_plot, 'reconstructions_of_data.png', ims_per_row=10)
Exemple #3
0
def decoding_along_a_lattice():
    # load the trained paramters
    w_1 = tf.convert_to_tensor(np.load('w1.npy'), dtype=tf.float32)
    w_2 = tf.convert_to_tensor(np.load('w2.npy'), dtype=tf.float32)
    b_1 = tf.convert_to_tensor(np.load('b1.npy'), dtype=tf.float32)
    b_2 = tf.convert_to_tensor(np.load('b2.npy'), dtype=tf.float32)

    # Create a 20x20 equally spaced grid of z's
    grid_x = np.linspace(-6, 2.5, 20)
    grid_y = np.linspace(4.8, -6.5, 20)

    to_plot = np.zeros((20, 20, 28, 28))
    with tf.Session() as session:
        for i, xi in enumerate(grid_x):
            for j, yi in enumerate(grid_y):
                z_sample = np.array([xi, yi
                                     ]).reshape(1,
                                                latent_dim).astype(np.float32)
                # For each z on the grid plot the generative distribution over x
                hidden = tf.matmul(z_sample, w_1) + b_1
                hidden = tf.nn.tanh(hidden)  # batch_size x n_hidden matrix
                p = tf.matmul(hidden, w_2) + b_2  # batch_size x 784 matrix
                p = tf.nn.sigmoid(p)
                p = tf.clip_by_value(p, 1e-8, 1 - 1e-8)
                # concatenate these plots to a lattice of distributions

                to_plot[j, i] = reshape_into_image(p.eval())

    to_plot = to_plot.reshape(400, 28, 28)
    data.save_images(to_plot, 'decoding_along_lattice.png', ims_per_row=20)
Exemple #4
0
def decoding_along_a_lattice():
    # Create a 20x20 equally spaced grid of z's
    grid_x = np.linspace(-5, 6, 20)
    grid_y = np.linspace(-3, 6, 20)
    axis_x = np.repeat(grid_x[np.newaxis, ...], len(grid_y), axis=0)
    axis_y = np.repeat(grid_y[..., np.newaxis], len(grid_x), axis=1)

    latent_coordinates = np.dstack((axis_y, axis_x))
    latent_coordinates = latent_coordinates.reshape(400, 2).astype(np.float32)
    p = decode(latent_coordinates, training=False)
    with tf.Session() as session:
        to_plot = p.eval().reshape(400, 28, 28)
    data.save_images(to_plot, 'decoding_along_lattice.png', ims_per_row=20)
Exemple #5
0
def reconstructions_of_data():
    # load the trained paramters
    w_1 = tf.convert_to_tensor(np.load('w1.npy'), dtype=tf.float32)
    w_2 = tf.convert_to_tensor(np.load('w2.npy'), dtype=tf.float32)
    w_3 = tf.convert_to_tensor(np.load('w3.npy'), dtype=tf.float32)
    w_4 = tf.convert_to_tensor(np.load('w4.npy'), dtype=tf.float32)
    w_5 = tf.convert_to_tensor(np.load('w5.npy'), dtype=tf.float32)
    b_1 = tf.convert_to_tensor(np.load('b1.npy'), dtype=tf.float32)
    b_2 = tf.convert_to_tensor(np.load('b2.npy'), dtype=tf.float32)
    b_3 = tf.convert_to_tensor(np.load('b3.npy'), dtype=tf.float32)
    b_4 = tf.convert_to_tensor(np.load('b4.npy'), dtype=tf.float32)
    b_5 = tf.convert_to_tensor(np.load('b5.npy'), dtype=tf.float32)

    # Sample 10 xs from the data, plot.
    x = train_images[:10].astype(np.float32)  # 10 x 784 matrix

    # For each x, encode to distribution q(z|x)
    h = tf.matmul(x, w_3) + b_3
    h = tf.nn.tanh(h)  # 100 x 500 matrix
    mu = tf.matmul(h, w_4) + b_4  # 10000 x latent_dim matrix
    log_variance = tf.matmul(h, w_5) + b_5  # 10000 x latent_dim matrix
    variance = tf.exp(log_variance)

    # For each x, sample distribution z ~ q(z|x)
    z = diagonal_gaussian_sampler(mu, variance, latent_dim,
                                  10)  # 10 x latent_dim matrix

    # For each z, decode to distribution p(x̃|z), plot.
    hidden = tf.matmul(z, w_1) + b_1
    hidden = tf.nn.tanh(hidden)  # batch_size x n_hidden matrix
    p = tf.matmul(hidden, w_2) + b_2  # batch_size x 784 matrix
    p = tf.nn.sigmoid(p)
    p = tf.clip_by_value(p, 1e-8, 1 - 1e-8)

    # For each x, sample from the distribution x̃ ~ p(x̃|z), plot.
    x_hat = bernoulli_sampler(p, 10, img_flat_size)

    # Concatenate all plots into a figure.
    x_plot = reshape_into_image(x)
    with tf.Session() as session:
        p_plot = reshape_into_image(p.eval())
        x_hat_plot = reshape_into_image(1 - x_hat.eval())

    to_plot = np.concatenate((x_plot, p_plot, x_hat_plot))
    data.save_images(to_plot, 'reconstructions_of_data.png', ims_per_row=10)
Exemple #6
0
def sampler_from_generative_model():
    # load trained parameters
    w_1 = tf.convert_to_tensor(np.load('w1.npy'), dtype=tf.float32)
    w_2 = tf.convert_to_tensor(np.load('w2.npy'), dtype=tf.float32)
    b_1 = tf.convert_to_tensor(np.load('b1.npy'), dtype=tf.float32)
    b_2 = tf.convert_to_tensor(np.load('b2.npy'), dtype=tf.float32)

    # Sample 10 z from prior
    unit_mu = tf.constant(0, shape=[10, latent_dim], dtype=tf.float32)
    unit_variance = tf.constant(1, shape=[10, latent_dim], dtype=tf.float32)
    z = diagonal_gaussian_sampler(unit_mu, unit_variance, latent_dim,
                                  10)  # 10 x 2 matrix

    # For each z, plot p(x|z)
    # decoding
    # Provides parameters for distribution p(x|z)
    # hidden layer
    hidden = tf.matmul(z, w_1) + b_1
    hidden = tf.nn.tanh(hidden)  # 10 x n_hidden matrix

    # output layer
    p = tf.matmul(hidden, w_2) + b_2  # 10 x 784 matrix
    p = tf.nn.sigmoid(p)
    p = tf.clip_by_value(p, 1e-8, 1 - 1e-8)

    # Sample x from p(x|z)
    x_hat = bernoulli_sampler(p, 10, img_flat_size)

    # Concatenate plots into a figure
    with tf.Session() as session:
        p_plot = reshape_into_image(p.eval())
        x_hat_plot = reshape_into_image(1 - x_hat.eval())

    to_plot = np.concatenate((p_plot, x_hat_plot))
    data.save_images(to_plot,
                     'samples_from_generative_model.png',
                     ims_per_row=10)
Exemple #7
0
def interpolation_plot():
    # load the trained paramters
    w_3 = tf.convert_to_tensor(np.load('w3.npy'), dtype=tf.float32)
    w_4 = tf.convert_to_tensor(np.load('w4.npy'), dtype=tf.float32)
    b_3 = tf.convert_to_tensor(np.load('b3.npy'), dtype=tf.float32)
    b_4 = tf.convert_to_tensor(np.load('b4.npy'), dtype=tf.float32)
    w_1 = tf.convert_to_tensor(np.load('w1.npy'), dtype=tf.float32)
    w_2 = tf.convert_to_tensor(np.load('w2.npy'), dtype=tf.float32)
    b_1 = tf.convert_to_tensor(np.load('b1.npy'), dtype=tf.float32)
    b_2 = tf.convert_to_tensor(np.load('b2.npy'), dtype=tf.float32)

    # Sample 3 pairs of data with different classes
    pair_1 = train_images[0:2].astype(np.float32)  # 5 and 0
    pair_2 = train_images[2:4].astype(np.float32)  # 4 and 1
    pair_3 = train_images[4:6].astype(np.float32)  # 9 and 2

    # Encode the data in each pair, and take the mean vectors
    h = tf.matmul(pair_1, w_3) + b_3
    h = tf.nn.tanh(h)  # 100 x 500 matrix
    mu_pair_1 = tf.matmul(h, w_4) + b_4  # 2 x latent_dim matrix

    h = tf.matmul(pair_2, w_3) + b_3
    h = tf.nn.tanh(h)  # 100 x 500 matrix
    mu_pair_2 = tf.matmul(h, w_4) + b_4  # 2 x latent_dim matrix

    h = tf.matmul(pair_3, w_3) + b_3
    h = tf.nn.tanh(h)  # 100 x 500 matrix
    mu_pair_3 = tf.matmul(h, w_4) + b_4  # 2 x latent_dim matrix

    with tf.Session() as session:
        mu_pair_1 = mu_pair_1.eval()
        mu_pair_2 = mu_pair_2.eval()
        mu_pair_3 = mu_pair_3.eval()

    # Linearly interpolate between these mean vectors
    pair_1_inter = linear_interpolation(mu_pair_1[0],
                                        mu_pair_1[1])  # 10 x 2 matrix
    pair_2_inter = linear_interpolation(mu_pair_2[0],
                                        mu_pair_2[1])  # 10 x 2 matrix
    pair_3_inter = linear_interpolation(mu_pair_3[0],
                                        mu_pair_3[1])  # 10 x 2 matrix

    # Along the interpolation, plot the distributions p(x|z_α)
    hidden = tf.matmul(pair_1_inter, w_1) + b_1
    hidden = tf.nn.tanh(hidden)  # 10 x n_hidden matrix
    p_1 = tf.matmul(hidden, w_2) + b_2  # 10 x 784 matrix
    p_1 = tf.nn.sigmoid(p_1)
    p_1 = tf.clip_by_value(p_1, 1e-8, 1 - 1e-8)

    hidden = tf.matmul(pair_2_inter, w_1) + b_1
    hidden = tf.nn.tanh(hidden)  # 10 x n_hidden matrix
    p_2 = tf.matmul(hidden, w_2) + b_2  # 10 x 784 matrix
    p_2 = tf.nn.sigmoid(p_2)
    p_2 = tf.clip_by_value(p_2, 1e-8, 1 - 1e-8)

    hidden = tf.matmul(pair_3_inter, w_1) + b_1
    hidden = tf.nn.tanh(hidden)  # 10 x n_hidden matrix
    p_3 = tf.matmul(hidden, w_2) + b_2  # 10 x 784 matrix
    p_3 = tf.nn.sigmoid(p_3)
    p_3 = tf.clip_by_value(p_3, 1e-8, 1 - 1e-8)

    with tf.Session() as session:
        p1_plot = p_1.eval()
        p2_plot = p_2.eval()
        p3_plot = p_3.eval()

    to_plot = np.concatenate((p1_plot, p2_plot, p3_plot))
    data.save_images(to_plot, 'interpolation.png', ims_per_row=10)