Whats the equivalent of this Keras snippet in Pytorch?

Shisho_Sama: <p>How can I implement this snippet from keras that is used to generate image-morphs from a latent vector z by a VAE? (the main article is <a href="https://tiao.io/post/tutorial-on-variational-autoencoders-with-a-concise-keras-implementation/" rel="noopener nofollow ugc">here</a>):</p>
<pre><code class="lang-auto"># display a 2D manifold of the digits
n = 15  # figure with 15x15 digits
digit_size = 28

# linearly spaced coordinates on the unit square were transformed
# through the inverse CDF (ppf) of the Gaussian to produce values
# of the latent variables z, since the prior of the latent space
# is Gaussian

z1 = norm.ppf(np.linspace(0.01, 0.99, n))
z2 = norm.ppf(np.linspace(0.01, 0.99, n))
z_grid = np.dstack(np.meshgrid(z1, z2))

x_pred_grid = decoder.predict(z_grid.reshape(n*n, latent_dim)) \
                     .reshape(n, n, digit_size, digit_size)

plt.figure(figsize=(10, 10))
plt.imshow(np.block(list(map(list, x_pred_grid))), cmap='gray')
plt.show()
</code></pre>
<p>I came up with the following snippet, but the outcome is different!</p>
<pre><code class="lang-auto">n = 10  # figure with 10x10 digits
digit_size = 28

# linearly spaced coordinates on the unit square were transformed
# through the inverse CDF (ppf) of the Gaussian to produce values
# of the latent variables z, since the prior of the latent space
# is Gaussian

z1 = torch.linspace(0.01, 0.99, n)
z2 = torch.linspace(0.01, 0.99, n)

z_grid = np.dstack(np.meshgrid(z1, z2))
z_grid = torch.from_numpy(z_grid).to(device)
z_grid = z_grid.reshape(-1, embeddingsize)

x_pred_grid = model.decoder(z_grid)
x_pred_grid= x_pred_grid.cpu().detach().numpy().reshape(-1, 1, 28, 28).transpose(0,2,3,1)

plt.figure(figsize=(10, 10))
plt.imshow(np.block(list(map(list, x_pred_grid))), cmap='gray')
plt.show()
</code></pre>
<p>The problem that I have is  that, first I dont know what the counter part for <code>norm.ppf</code> in Pytorch is, so I just ignored it for now. second, the way the line :</p>
<pre><code class="lang-auto">x_pred_grid = decoder.predict(z_grid.reshape(n*n, latent_dim)) \
                     .reshape(n, n, digit_size, digit_size)
</code></pre>
<p>reshapes the input is impossible for me! he is feeding the (<code>nxn</code>,<code>latent_dim</code>), which for <code>n=10</code>, and <code> latent_dim =10</code>, is <code>(100,10)</code> .<br>
However, when I reshape like<code> (nxn, latent_dim)</code> I get the error :</p>
<blockquote>
<p><strong>RuntimeError</strong> : shape ‘[100, 10]’ is invalid for input of size 200</p>
</blockquote>
<p>So I had to reshape like <code>(-1, embeddingsize)</code> and this I guess is why my output is different .<br>
for the record the keras output is like this :<br>
          <a href="https://tiao.io/post/tutorial-on-variational-autoencoders-with-a-concise-keras-implementation/result_manifold.png" target="_blank" rel="noopener nofollow ugc" class="onebox">
            <img src="https://tiao.io/post/tutorial-on-variational-autoencoders-with-a-concise-keras-implementation/result_manifold.png" width="500" height="500">
          </a>
<br>
and mine is like this :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/a/aac9877a302dd1a9692af34fe08bbcd89566f96d.png" data-download-href="https://discuss.pytorch.org/uploads/default/aac9877a302dd1a9692af34fe08bbcd89566f96d" title="vs"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/a/aac9877a302dd1a9692af34fe08bbcd89566f96d_2_61x500.png" alt="vs" data-base62-sha1="omQWxWmDCyXBfpGhRx92GjxXV93" width="61" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/a/aac9877a302dd1a9692af34fe08bbcd89566f96d_2_61x500.png, https://discuss.pytorch.org/uploads/default/optimized/2X/a/aac9877a302dd1a9692af34fe08bbcd89566f96d_2_91x750.png 1.5x" data-dominant-color="0F0F0F"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">vs</span><span class="informations">95×771 16.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>So how can I closely replicate this keras code in Pytorch? where am I going off road?<br>
Thank you all in advance</p>

Shisho_Sama: <p>OK, Thank God! I finally got the hang of it! here is what I ended up doing !</p>
<pre><code class="lang-auto"># display a 2D manifold of the digits
embeddingsize = model.embedding_size
# figure with 20x20 digits
n = 20  
digit_size = 28

z1 = torch.linspace(-2, 2, n)
z2 = torch.linspace(-2, 2, n)

z_grid = np.dstack(np.meshgrid(z1, z2))
z_grid = torch.from_numpy(z_grid).to(device)
z_grid = z_grid.reshape(-1, embeddingsize)

x_pred_grid = model.decoder(z_grid)
x_pred_grid= x_pred_grid.cpu().detach().view(-1, 1, 28,28)
img = make_grid(x_pred_grid,nrow=n).numpy().transpose(1,2,0)

plt.figure(figsize=(10, 10))
plt.imshow(img)
plt.show()
</code></pre>
<p>and the output is :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/b/bf7f8468f634a6cf739a02dab46e6fe9512aa535.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/bf7f8468f634a6cf739a02dab46e6fe9512aa535" title="visualization_vae_pytorch"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/b/bf7f8468f634a6cf739a02dab46e6fe9512aa535_2_511x500.jpeg" alt="visualization_vae_pytorch" data-base62-sha1="rk4oSpZWW1mvNMaJ7pDDG1YhjV3" width="511" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/b/bf7f8468f634a6cf739a02dab46e6fe9512aa535_2_511x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/b/bf7f8468f634a6cf739a02dab46e6fe9512aa535_2_766x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/b/bf7f8468f634a6cf739a02dab46e6fe9512aa535.jpeg 2x" data-dominant-color="323232"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">visualization_vae_pytorch</span><span class="informations">789×771 149 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>