GAN: RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x189 and 10x512)

Soham_Lotlikar: <pre><code class="lang-auto">
import torch
import torch.nn as nn
import torch.optim as optim
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class EncoderDecoderNet(nn.Module):
    def __init__(self, input_dim, latent_dim, neurons=512):
        super(EncoderDecoderNet, self).__init__()
        
        # Encoder
        self.encoder = nn.Sequential(
            nn.Linear(input_dim, neurons),  # First fully connected layer (input_dim to neurons)
            nn.LeakyReLU(negative_slope=0.2),  # LeakyReLU activation
            nn.Linear(neurons, latent_dim)  # Second fully connected layer to latent space
        )
        
        # Decoder
        self.decoder = nn.Sequential(
            nn.Linear(latent_dim, neurons),  # First fully connected layer (latent_dim to neurons)
            nn.LeakyReLU(negative_slope=0.2),  # LeakyReLU activation
            nn.Linear(neurons, input_dim),  # Second fully connected layer (back to original input size)
            nn.Sigmoid()  # Sigmoid activation to output a value between 0 and 1
        )
    
    def forward(self, x):
        # Pass input through the encoder and then the decoder
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded

# Discriminator for Latent Representation
class DiscriminatorLatent(nn.Module):
    def __init__(self, input_dim, neurons=512):
        super(DiscriminatorLatent, self).__init__()
        
        # Discriminator for latent space consists of two fully connected layers with LeakyReLU activation
        self.discriminator = nn.Sequential(
            nn.Linear(input_dim, neurons),  # First fully connected layer (input_dim to neurons)
            nn.LeakyReLU(negative_slope=0.2),  # LeakyReLU activation
            nn.Linear(neurons, 100),  # Second fully connected layer (neurons to 100)
            nn.LeakyReLU(negative_slope=0.2),  # LeakyReLU activation
            nn.Linear(100, 1),  # Output layer (scalar, representing real or fake)
            nn.Sigmoid()  # Sigmoid activation for probability output (real/fake)
        )
    
    def forward(self, x):
        return self.discriminator(x)

# Discriminator for Image Reconstruction
class DiscriminatorReconstruction(nn.Module):
    def __init__(self, input_dim, neurons=512):
        super(DiscriminatorReconstruction, self).__init__()
        
        # Discriminator for image reconstruction consists of two fully connected layers with LeakyReLU activation
        self.discriminator = nn.Sequential(
            nn.Linear(input_dim, neurons),  # First fully connected layer (input_dim to neurons)
            nn.LeakyReLU(negative_slope=0.2),  # LeakyReLU activation
            nn.Linear(neurons, 100),  # Second fully connected layer (neurons to 100)
            nn.LeakyReLU(negative_slope=0.2),  # LeakyReLU activation
            nn.Linear(100, 1),  # Output layer (scalar, representing real or fake)
            nn.Sigmoid()  # Sigmoid activation for probability output (real/fake)
        )
    
    def forward(self, x):
        return self.discriminator(x)

# Latent Representation Adversarial Loss

# Initialize the networks
input_dim = 189  # This should match your data
latent_dim = 10  # Latent dimension
neurons = 512  # Hidden layer size
encoder = EncoderDecoderNet(input_dim, latent_dim, neurons).to(device)  # Move encoder to GPU
discriminator_latent = DiscriminatorLatent(latent_dim, neurons).to(device)  # Move latent space discriminator to GPU
discriminator_rec = DiscriminatorReconstruction(input_dim, neurons).to(device)  # Move reconstruction discriminator to GPU

# Optimizers
optimizer_encoder = optim.Adam(encoder.parameters(), lr=1e-4)
optimizer_discriminator_latent = optim.Adam(discriminator_latent.parameters(), lr=1e-4)
optimizer_discriminator_rec = optim.Adam(discriminator_rec.parameters(), lr=1e-4)

# Dummy background sample from prior Gaussian distribution
batch_size = 32  # Example batch size

# Convert list to numpy array and then to tensor for better performance
DTrn_background = [sample[0] for sample in DTrn]  # Extract only b_i values (spectral vectors)
DTrn_background_np = np.array(DTrn_background)  # Convert list to numpy array
bi = torch.tensor(DTrn_background_np, dtype=torch.float32).to(device)  # Move tensor to GPU

# Prior Gaussian distribution for background (random noise with same shape as bi)
p_bi = torch.normal(mean=0, std=1, size=(batch_size, input_dim)).to(device)  # Prior Gaussian distribution

# Example: Sample a batch from bi (if you have more data than batch_size)
# Select a random batch of size `batch_size` from `bi` for each iteration
indices = torch.randint(low=0, high=bi.size(0), size=(batch_size,))
batch_bi = bi[indices]


bi.size() = torch.Size([144000, 189])
</code></pre>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x189 and 10x512)</p>
<p>Please help, been trying to solve this error for the past 5 hours, really need this for a hackathon</p>