Question regarding latent space in Autoencoder
I am just start learning AE few days ago. From what I know about AE, a latent space will be created after the encoder and then the decoder will regenerate images based on the latent spaces. In my other project, it requires me to embed some new feature into the AE latent space. Below are the AE code that I have try.
AE module
# build autoencoder
import torch
import matplotlib.pyplot as plt

# Creating a PyTorch class
# 28*28 ==&gt; 9 ==&gt; 28*28
class AE(torch.nn.Module):
    def __init__(self):
        super().__init__()
         
        # Building an linear encoder with Linear
        # layer followed by Relu activation function
        # 784 ==&gt; 9
        self.encoder = torch.nn.Sequential(
            torch.nn.Linear(64 * 64, 256),
            torch.nn.ReLU(),
            torch.nn.Linear(256, 128),
            torch.nn.ReLU(),
            torch.nn.Linear(128, 32),
            torch.nn.ReLU(),
            torch.nn.Linear(32, 3)
            )
         
        # Building an linear decoder with Linear
        # layer followed by Relu activation function
        # The Sigmoid activation function
        # outputs the value between 0 and 1
        # 9 ==&gt; 784
        self.decoder = torch.nn.Sequential(
            torch.nn.Linear(3,32),
            torch.nn.ReLU(),
            torch.nn.Linear(32, 128),
            torch.nn.ReLU(),
            torch.nn.Linear(128, 256),
            torch.nn.ReLU(),
            torch.nn.Linear(256, 64*64),
            torch.nn.Sigmoid()
            )
 
    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded


Init
# Model Initialization
model = AE().to(device)
 
# Validation using MSE Loss function
loss_function = torch.nn.MSELoss()
 
# Using an Adam Optimizer with lr = 0.1
optimizer = torch.optim.Adam(model.parameters(),
                             lr = 1e-1,
                             weight_decay = 1e-8)


training
num_epochs = 100
output =[]
for epoch in range(num_epochs):
    for data in loader:
        img, _ = data
        img = img.reshape(-1,64*64)
        img    = img.to(device)
        recon = model(img)                             
        loss   = loss_function(recon, img.data)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(f'epoch [{epoch + 1}/{num_epochs}], loss:{loss. Item(): .4f}')
    output. Append((epoch,img,recon))

My question is may I know how can I obtain the latent space? From what I know about the code there is only encoder and decoder. How can I retrieve the latent space so that I can embed new feature to it? Thank you.

You can simply modify your forward(self, x) function to also return the laten space embedding generated by the encoder:
def forward(self, x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return encoded, decoded

In your train loop you can just add:
embedding, recon = model(img)

