Why the GAN Discriminator shows wrong classes?

119 Views Asked by At

I created a GAN for text and using VanillaGAN training approach, but the main problem is the sequences that are created are good, but the main problem is that when I use nn.sigmoid to see the discriminator labels it shows [0] for data that is created which are completely real, and it is not correct. Here is my Discriminator code:

class Classifier(nn.Module):
    def __init__(self, hidden_size, hidden_size2, dropout):
        super().__init__()

        self.FC1 = nn.Sequential(
                   nn.Linear(512, hidden_size),
                   nn.LeakyReLU(0.1),
                   nn.Dropout(dropout))
        
        self.FC2 = nn.Sequential(           
                   nn.Linear(hidden_size, hidden_size2),
                   nn.LeakyReLU(0.1),
                   nn.Dropout(dropout)
                  )
            
        self.FC3 = nn.Linear(hidden_size2, 1)

        self.dropout = nn.Dropout(dropout)
        self.bach = nn.BatchNorm1d(512)
        self.bach2 = nn.BatchNorm1d(hidden_size)
        self.bach3 = nn.BatchNorm1d(64)

    def forward(self, x):
                
        z = self.dropout(x)
        z = self.bach(z)
        z = self.FC1(z)
        z = self.bach2(z)
        z = self.FC2(z)
        z = self.bach3(z)
        out = self.FC3(z)


        return out

As input to this Classifier, hidden states of real and fake sequences are feed into this network.

My loss function is BCEWithlogits and this is the Train class

# to create real labels (1s)
def label_real(size):
    data = torch.ones(size, 1)
    return data.to(device)
# to create fake labels (0s)
def label_fake(size):
    data = torch.zeros(size, 1)
    return data.to(device)


# function to train the discriminator network
def train_discriminator(optimizer, data_real, data_fake):
    b_size = data_real.size(1)
    real_label = label_real(b_size)
    fake_label = label_fake(b_size)
    optimizer.zero_grad()
    output_real = discriminator(data_real)
    loss_real = criterion(output_real, real_label)
    output_fake = discriminator(data_fake)
    loss_fake = criterion(output_fake, fake_label)
    loss_real.backward()
    loss_fake.backward()
    optimizer.step()
    return loss_real + loss_fake

I use nn.sigmoid after training and on testing the model. Please help me to know what is wrong with my neural network?

0

There are 0 best solutions below