How to Access the Latent Dimension of an Autoencoder

17 views (last 30 days)
I'm quite new to machine learning. I've only taken one ML class, but I'm hoping to expand on what we did there, which was mainly different ways to cluster data. I've mainly tried the built-in clustering methods so far.
And I wanted to try clustering with a VAE. From what I understand from the paper, a clustering method is going to be applied to the latent dimensions? I've looked at the VAE examples in MATLAB, but they all deal with image data and recreating images. Plus, the latent dimensions weren't explicitly accessed there. How can you access the latent dimensions?
Or am I going in the wrong direction with this idea? Are there more suited neural architectures for clustering?
Thank you in advance, and sorry if this is a vague and terrible question.
  3 Comments
Eunice Chieng
Eunice Chieng on 13 Jul 2024
Thank you for your response! I'm aware of how to cluster datapoints; my main concern is actually on how to access the data that I am going to cluster from an autoencoder since the sample projects that involve autoencoders don't really seem to show this. That's the part I'm asking about.
Umar
Umar on 13 Jul 2024
Hi Eunice,
You have to use the encoder portion of the trained model to obtain latent representations, which you can utilized for clustering. Let me illustrate it with example.
% Load and preprocess your data
data = load('your_dataset.mat');
% Define and train your autoencoder
autoenc = trainAutoencoder(data, hiddenSize);
% Use the encoder to extract features
encodedFeatures = encode(autoenc, data);
% Perform clustering on the encoded features
[idx, C] = kmeans(encodedFeatures, k);
If you have any further questions or need additional assistance, feel free to ask!

Sign in to comment.

Answers (1)

Kaustab Pal
Kaustab Pal on 21 Aug 2024
To retrieve the latent vector from the autoencoder, you can use the encoder network with your input data in the "predict" function. Below is a sample code to illustrate this process:
numLatentChannels = 32;
imageSize = [28 28 1];
% ENCODER PART %
layersE = [
imageInputLayer(imageSize,Normalization="none")
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer
fullyConnectedLayer(numLatentChannels)];
netE = dlnetwork(layersE);
IMG = randn(28,28,1,1); % S, S, C, B
IMG = dlarray(IMG,"SSCB");
latent_vec = predict(netE, IMG) % IMG needs to be a dlarray
% latent_vec is a 32 x 1 dimensional vector
I hope you find this helpful!

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!