Plotting the latent space

pedram1: <p>Hi All</p>
<p>I trained a variational autoencoder, however don’t know how I can plot my latent space. Below you can see my codes that used for defining the class of autoencoder:(I would like to plot the z)</p>
<hr>
<p>class B_VAE(nn.Module):</p>
<p>def <strong>init</strong>(self,zdims):<br>
super().<strong>init</strong>()<br>
self.zdims = zdims<br>
<span class="hashtag">#Encoder</span> layer<br>
self.encoder = nn.Sequential(nn.Conv1d(1,4,kernel_size = 4, stride = 3),<br>
nn.MaxPool1d(kernel_size = 4, stride = 3),<br>
nn.Tanh(),</p>
<pre><code>        nn.Conv1d(4,8,kernel_size=4, stride = 2),
        nn.MaxPool1d(kernel_size = 4,stride = 2),
        nn.Tanh(),
                 
        
        nn.Conv1d(8,12,kernel_size = 4, stride = 2),
        nn.MaxPool1d(kernel_size = 4,stride = 1),
        nn.Tanh(),

        nn.Conv1d(12,16,kernel_size = 4, stride = 1),
        nn.MaxPool1d(kernel_size = 3,stride = 1),
        nn.Tanh())

#Cov FC LAYER
self.fc_mu = nn.Linear(16,self.zdims)
self.fc_std = nn.Linear(16,self.zdims)

#Deconv FC LAYER

self.fc_d = nn.Linear(self.zdims,16)

#Decoder layer
self.decoder = nn.Sequential(nn.ConvTranspose1d(16,12,kernel_size = 5, stride = 2),
        nn.Tanh(),
        

        nn.ConvTranspose1d(12,8,kernel_size = 5,stride = 3),
        nn.Tanh(),
                    

        nn.ConvTranspose1d(8,4,kernel_size= 12,stride = 4),
        nn.Tanh(),

        nn.ConvTranspose1d(4,1,kernel_size = 18, stride = 9),
        nn.Tanh())
</code></pre>
<p>def parameterization_trick(self,mu,logvar):</p>
<pre><code>  std = torch.exp(logvar*0.5)
  # sample epslion from N(0,1)
  eps = torch.randn_like(std)
  # sampling now can be done by shifting the eps by (adding) the mean 
    # and scaling it by the variance.
  return mu+eps*std
</code></pre>
<p>def encode(self,imgs):</p>
<pre><code>  output = self.encoder(imgs)
  output = output.view(-1,16)
  mu = self.fc_mu(output)
  logvar = self.fc_std(output)
  z = self.parameterization_trick(mu,logvar)
  return mu,logvar,z
</code></pre>
<p>def decode(self,z):</p>
<pre><code>  deconv_input = (self.fc_d(z))
  deconv_input = deconv_input.view(-1,16,2)
  reconstructed_img = self.decoder(deconv_input)
  return reconstructed_img
</code></pre>
<p>def forward(self,x):<br>
mu,logvar,z = self.encode(x)<br>
reconstructed_img = self.decode(z)<br>
return reconstructed_img, mu, logvar</p>
<p>def loss_disentangled_vae(outputs, imgs, mu, logvar, Beta):<br>
criterion = nn.MSELoss()<br>
recons_loss = criterion(outputs,imgs)<br>
kl = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())<br>
return (recons_loss+(Beta*kl))</p>
<hr>
<p>Thanks</p>

ptrblck: <p>I don’t think there is an easy way to plot a multi-dimensional tensor.<br>
Often your latent vector has &gt;100 dimensions, so you could try to apply some dimensionality reduction techniques such as t-SNE, PCA etc. to be able to plot these tensors in a 2D scatter plot.</p>

pedram1: <p>Hi Patrick</p>
<p>Thank you for your comments. I actually use PCA to reduce dim of my latent space and  then plot that.<br>
After training my model, I can see that reconstruction become awful <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:">  Actually trained model can not even reconstruct the input roughly.</p>
<p>I would guess that sth should be wrong in my codes. I tried many things however, could not find that. May I ask you to take a quick look to my model and see if you can find my mistake?<br>
zdim = 20<br>
Beta = 3</p>
<p>ps: classic AE gives me perfect result however this one almost gives me nothing.</p>
<p>I appreciate you time,<br>
Wash your hands after reading this massage, <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>Pedram</p>