# Visualizing 2D latent space of VAE example

**URL:** <https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577>\
**Category:** General\
**Created:** [January 8, 2018, 7:02pm UTC](https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577 "2018-01-08T19:02:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wset2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/wset2/32/125_2.png) [@wset2](https://discourse.edwardlib.org/u/wset2)\
**Post date:** [January 8, 2018, 7:02pm UTC](https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577/1 "2018-01-08T19:02:04Z")

</div>

Hi

In the VAE [example](https://github.com/blei-lab/edward/blob/master/examples/vae.py), we model binarized 28 x 28 pixel images from MNIST. For the d=2 case, is there a way to visualize the latent space such as here: [https://blog.keras.io/building-autoencoders-in-keras.html](https://blog.keras.io/building-autoencoders-in-keras.html) .

I just think it is useful in general if the latent space can be visualized, other than looking at the negative log likelihood.

Thank you!

---

<div class="post-metadata">

**Author:** ![dustin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/dustin/32/134_2.png) [@dustin](https://discourse.edwardlib.org/u/dustin)\
**Post date:** [January 8, 2018, 7:41pm UTC](https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577/2 "2018-01-08T19:41:21Z")

</div>

You can feed data into the placeholder and fetch samples from `qz`. Fetching `qz.mean()` over all test inputs will give you something like Figure 3(b) in [the deep latent Gaussian models paper](https://arxiv.org/abs/1401.4082).

---

<div class="post-metadata">

**Author:** ![wset2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/wset2/32/125_2.png) [@wset2](https://discourse.edwardlib.org/u/wset2)\
**Post date:** [January 8, 2018, 10:57pm UTC](https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577/3 "2018-01-08T22:57:01Z")

</div>

Is there some example code where I can work from to produce such a figure? I am still not too experienced with Edward.

---

<div class="post-metadata">

**Author:** ![dustin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/dustin/32/134_2.png) [@dustin](https://discourse.edwardlib.org/u/dustin)\
**Post date:** [January 8, 2018, 11:36pm UTC](https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577/4 "2018-01-08T23:36:00Z")

</div>

The following should work:

```python
sess = ed.get_session()
encoded_mean_test = sess.run(qz.mean(), {x_ph: x_test})

```

You can append it post-training (to the end of the VAE script) and then plot the two latent dimensions.

---

<div class="post-metadata">

**Author:** ![wset2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/wset2/32/125_2.png) [@wset2](https://discourse.edwardlib.org/u/wset2)\
**Post date:** [January 11, 2018, 5:04pm UTC](https://discourse.edwardlib.org/t/visualizing-2d-latent-space-of-vae-example/577/5 "2018-01-11T17:04:36Z")

</div>

That’s fine now. Thank you 😀
