'Can I use the encoder part of Variational autoencoder for the prediction?

I am trying to prediction the Lift to drag ratio of airfoil using the encoder part of variational autoencoder, as previously it was predicted using CNN. Below is the code of CNN model.

class Net1(nn.Module):
    def __init__(self):
        super(Net1, self).__init__()
       
        self.conv1 = nn.Sequential(
            nn.Conv2d(1,10,13),
            nn.BatchNorm2d(10),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(10,20,7),
            #nn.Dropout2d(0.5),
            nn.BatchNorm2d(20),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(20,40,7),
            nn.BatchNorm2d(40),
            #nn.Dropout2d(0.5),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        self.conv4 = nn.Sequential(
            nn.Conv2d(40,80,5),
            nn.BatchNorm2d(80),
            #nn.Dropout2d(0.5),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        
        self.fc1 = nn.Sequential(
            nn.Linear(720,400),
            nn.ReLU(),
            #nn.Dropout(0.5)
        )
        self.fc2 = nn.Linear(400,1)
    
    def forward(self, x):
        f1 = self.conv1(x)
        f2 = self.conv2(f1)
        f3 = self.conv3(f2)
        f4 = self.conv4(f3)
        f4_flat = f4.view(f4.size(0), -1)
        f5 = self.fc1(f4_flat)
        output = self.fc2(f5)
        return output 

I made some changes in the code to make it a variational autoencoder but a warning appears which states that "UserWarning: Using a target size (torch.Size([50, 1])) that is different to the input size (torch.Size([50, 720])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size." Below is the code of the Variational encoder.

class Net1(nn.Module):
    def __init__(self):
        super(Net1, self).__init__()
       
        self.conv1 = nn.Sequential(
            nn.Conv2d(1,10,13),
            nn.BatchNorm2d(10),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(10,20,7),
            #nn.Dropout2d(0.5),
            nn.BatchNorm2d(20),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(20,40,7),
            nn.BatchNorm2d(40),
            #nn.Dropout2d(0.5),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        self.conv4 = nn.Sequential(
            nn.Conv2d(40,80,5),
            nn.BatchNorm2d(80),
            #nn.Dropout2d(0.5),
            nn.MaxPool2d(2,2),
            nn.ReLU()
        )
        
        self.fc1 = nn.Sequential(
            nn.Linear(720,400),
            nn.ReLU(),
            #nn.Dropout(0.5)
        )
        self.fc2 = nn.Linear(400,1)

        self.N = torch.distributions.Normal(0, 1)
        self.N.loc = self.N.loc # hack to get sampling on the GPU
        self.N.scale = self.N.scale
        self.kl = 0

    def forward(self, x):
        f1 = self.conv1(x)
        f2 = self.conv2(f1)
        f3 = self.conv3(f2)
        f4 = self.conv4(f3)
        f4_flat = f4.view(f4.size(0), -1)
        f5 = self.fc1(f4_flat)
        output = self.fc2(f5)
        z = f4_flat + output*self.N.sample(f4_flat.shape)
        self.kl = (output**2 + f4_flat**2 - torch.log(output) - 1/2).sum()
        return z

Does anyone who can help me if it is possible to predict the lift to drag ratio of the airfoil using only the encoder part and how to remove this warning?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source