Plot the latent space of an autoencoder

Hi all

I am trying to build an auto encoder. Actually I did it, however I was wondered to see if there is anyway of plotting latent space ? (my network shrink to two numbers in the latent space) I assumed that no PCA is required then.
below u can find my codes:

#Autoencoder and Autodecoder conv layer…
class Autoencoder(nn.Module):

def init(self):
super(Autoencoder,self).init()

    self.encoder = nn.Sequential(
        nn.Conv1d(1,5,kernel_size=5, stride=2),
        nn.MaxPool1d(3, stride=1),
        nn.ReLU(True),

nn.Conv1d(5,10,kernel_size=5, stride = 2),
nn.MaxPool1d(3,stride=1),
nn.ReLU(True),

        nn.Conv1d(10,15,kernel_size=5, stride=2),
        nn.MaxPool1d(3,stride = 1),
        nn.ReLU(True),

        nn.Conv1d(15,20,kernel_size = 4, stride = 1),
        nn.ReLU(True))

     self.decoder = nn.Sequential(          
        nn.ConvTranspose1d(20,15,kernel_size = 1, stride = 4),
        nn.ReLU(True),
        

        nn.ConvTranspose1d(15,10,kernel_size = 2,stride = 4),
        nn.ReLU(True),
        
        

        nn.ConvTranspose1d(10,5,kernel_size= 9,stride = 2),
        nn.ReLU(True),

        nn.ConvTranspose1d(5,1,kernel_size = 10, stride = 2),
        nn.ReLU (True))

def forward(self,x):
x = self.encoder(x)
x = self.decoder(x)
return x

model = Autoencoder().cuda()

criterion = nn.MSELoss()

optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate,

weight_decay=1e-5)

#training loop
loss = 0
epoch_num = 0
error = []
for epoch in range(num_epochs):
for data in train_loader:
img = data
img = Variable(img).cuda()

output = model (img)
loss = criterion (output,img)

optimizer.zero_grad()
loss.backward()
optimizer.step()
error.append(loss.item())


if epoch%10 == 9:
  epoch_num += epoch_num

  print ('\r Train Epoch : {}/{} \tLoss : {:.4f}'.format (epoch+1,num_epochs,loss/32))

model_save_name = ‘autoencoder2.pt’
path = F"/content/gdrive/My Drive/{model_save_name}"
torch.save(model.state_dict(), path)
plt.plot(error)

/////////////////
thank you

The last line of your encoder reads:

So where does the two element hidden state come in?

15 is previous layer channel number, 20 is last layer channel number, and finally by applying a kernel size = 4 and stride = 1 , the length of the signal that was 94 shrink to just 2…
I would like to plot that.

ps:my data size is (65535,94) that 65535 is my data point number and 94 is length of each data point (I am working with 1d signal)

If your encoder shrinks the input down to an element of length 2, you would still have 20 elements of length 2 coming out of the last layer of your encoder, right? So the output of the encoder is a tensor with shape (batch_size, 20,2,1).

If this is correct, then your could plot each of the (20,2,1) elements of your data set by running PCA on the first dimension and specifying a single principal component. This would essentially be picking the most important filter and would give you elements of size (1,2,1) which you could then plot in 2D.

1 Like

I see,
Just sth is slightly confusing for me. in (20,2,1) what that 1 means?

a lot thanks

It just means that the tensor has 1 column. So (20, 2, 1) means the tensor contains 20 items with 2 rows and 1 column.

1 Like

and 20 means number of channels that I made them by applying kernels, right?
and I have no idea how to apply a PCA on that? Is there any module for that?

Thanks a lot

Yes, that’s right.

Sci-kit learn has a nice PCA implementation. I would use that.

1 Like