Target size that is different to the input size

I am a beginner and,
I got an error which target size (torch.Size([32])) that is different to the input size (torch.Size([32768]))

from __future__ import print_function
#%matplotlib inline
import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
from torch.autograd import Variable
import GAN_Architecture as GAN
import sys
import h5py
ngpu = 1
#Cuda check
cuda = True if torch.cuda.is_available() else False
FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
#Workers
workers = 2
#Parameters
batch_size = 32
feature_size = 90
nz = 100
#Epochs
num_epochs = 10
#Learning Rate
lr= 0.0002
#Beta1 hyperparam for Adam optimizers
beta1 = 0.5
#PATHS
PATH_save = 'Save_model/conv3d_corr_new2.pth'
PATH_chechpoint = 'Save_model/new2_1.pth'
#Dataset
#Gammaknife is the group name
datapath = "Train/Traind_onlyMeV.h5"
with h5py.File(datapath, "r") as f:
    # Get the data
    data = f['Gammaknife/energy']
    dataset = np.array(data)

dataset2 = torch.Tensor(dataset)
#Create the dataLoader
dataloader = torch.utils.data.DataLoader(dataset2, batch_size = batch_size, 
                                         shuffle=True, num_workers= workers)
#Create the generator
NetG = GAN.Generator(ngpu).to(device)
if(device.type == 'cuda') and (ngpu > 1):
    NetG = nn.DataParallel(NetG, list(range(ngpu)))


NetG.apply(GAN.weights_init)
#Print the Generator model
print(NetG)
NetD = GAN.Discriminator(ngpu).to(device)
if(device.type =='cuda') and (ngpu > 1):
    NetD = nn.DataParallel(NetD, list(range(ngpu)))

NetD.apply(GAN.weights_init)
#Print the Discriminator model
print(NetD)

#Initialize BCELoss(Binary Cross Entropy Loss) function
criterion = nn.BCELoss()

fixed_noise = torch.randn(32,nz, 1, 1, 1, device = device)

#Setup Adam optimizers for both G AND D
optimizerD = optim.Adam(NetD.parameters(), lr=lr, betas=(beta1,0.999))
optimizerG = optim.Adam(NetG.parameters(), lr = lr ,betas=(beta1, 0.999))


NetG.train()
NetD.train()
#Training
feature_list = []
G_losses = []
D_losses = []
eph = 0
print("Starting Training Loop")


iters = 0

for epoch in range(num_epochs):
    for i, data in enumerate(dataloader,0):
        #Train with all-real batch
        NetD.zero_grad()
        # Adversarial ground truths
        real_cpu = data.to(device)
        b_size = real_cpu.size(0)
        real_cpu = real_cpu.unsqueeze(1)
        
        valid_label = Variable(FloatTensor(b_size).fill_(1.0), requires_grad=False)
        fake_label = Variable(FloatTensor(b_size).fill_(0.0), requires_grad=False)
       #Train Discriminator
    
        NetD.zero_grad()
      
        output = NetD(real_cpu).view(-1)

        errD_real = criterion(output,valid_label)
        #Calculate gradients
        errD_real.backward()
        D_x = output.mean().item()
        
        #Generate batch of latent vectors
        noise = torch.cuda.FloatTensor(b_size, nz, 1, 1, 1, device =device)
        #Generate fake image batch with G
        genlabel = np.random.uniform(10, 100, b_size)
        genlabel = Variable(FloatTensor(genlabel))
        genlabel = genlabel.view(b_size, 1, 1, 1, 1)
        fake = NetG(noise.to(device), genlabel)
        #label.fill_(fake_label)
        #Classify all fake batch with D
        output = NetD(fake, genlabel).view(-1)
        #Calculate D's loss on the all fake batch
        errD_fake = criterion(output, fake_label)
        errD_fake.backward(retain_graph=True)
        D_G_z1 = output.mean().item()
        
        #Compute error of D as sum over the fake and real batches
        errD = errD_real + errD_fake
        #Update D
        optimizerD.step()
        
        #Update G
        NetG.zero_grad()
        #label.fill_(fake_label)
        output = NetD(fake, genlabel).view(-1)
        #Calculate G loss
        errG = criterion(output, fake_label)
        #Calculate gradients for G
        errG.backward(retain_graph=True)
        D_G_z2 = output.mean().item()
        #Update G
        optimizerG.step()
        
        #Save losses
        G_losses.append(errG.item())
        D_losses.append(errD.item())
        
        if (iters % 100 == 0) or ((epoch == num_epochs -1) and i == len(dataloader-1)):
                with torch.no_grad():
                    fake = NetG(fixed_noise, genlabel).detach().cpu()
                feature_list.append(vutils.make_grid(fake, padding = 1, normalize = True))
        
        iters += 1

And this is GAN_Architecture:

# -*- coding: utf-8 -*-
import torch
import torch.nn as nn


ngf = 32
ndf = 32
nz = 100

class Generator(nn.Module):
    def __init__(self, ngpu):
        super(Generator, self).__init__()
        self.ngpu = ngpu
        self.conv1 = nn.ConvTranspose3d( nz, ngf * 8, 4, 1, 1, bias=False)
        self.main = nn.Sequential(
            
            nn.ConvTranspose3d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm3d(ngf * 4),
            nn.ReLU(True),
       
            nn.ConvTranspose3d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm3d(ngf * 2),
            nn.ReLU(True),
          
            nn.ConvTranspose3d( ngf * 2, ngf, 4, 2, 1, bias=False),
            nn.BatchNorm3d(ngf),
            nn.ReLU(True),
           
            nn.ConvTranspose3d( ngf, 1, 4, 1, 1, bias=False),
            nn.ReLU()
         
        )
    

    def forward(self, noise, energy):
        input = self.conv1(noise * energy)
        return self.main(input)

    

class Discriminator(nn.Module):
    def __init__(self,ngpu):
        super(Discriminator, self).__init__()
        self.ngpu = ngpu
        
        self.main = nn.Sequential(
                nn.Conv3d(1, ndf, 3 , 2, 3, bias = False),
                nn.LeakyReLU(0.2, inplace=True),
                
                nn.Conv3d(ndf, ndf*2, 3, 2, 1, bias= False),
                nn.BatchNorm3d(ndf*2),
                nn.LeakyReLU(0.2,inplace=True),
                
                nn.Conv3d(ndf*2, ndf*4, 3, 2, 1, bias= False),
                nn.BatchNorm3d(ndf*4),
                nn.LeakyReLU(0.2, inplace=True),
                
                nn.Conv3d(ndf*4, ndf*8, 3, 2, 1, bias= False),
                nn.BatchNorm3d(ndf *8 ),
                nn.LeakyReLU(0.2, inplace = True),
                
                nn.Conv3d(ndf*8, ndf*4, 3, 2, 0, bias = False),
                nn.Sigmoid()
              
                
            )
              
    def forward(self, energy):
        return self.main(energy)

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        nn.init.normal_(m.weight.data, 0.0, 0.2)
    elif classname.find('BatchNorm') != -1:
        nn.init.normal_(m.weight.data, 1.0, 0.02)
        nn.init.constant_(m.bias.data, 0)

My dataset size is (199,90,90,90) and first dimension represent to how many data I use for training. (90,90,90) is my cube dimension and its a hdf5 file.
I tried couple of things:
1- try to change padding value,
2- try normalize before using torch.tensor but nothing change,
3- try multiply valid_label with 1024 for getting error size,
4- I added

            nn.Linear(ndf*4 + 1, ndf*2),
            nn.LeakyReLU(0.2, inplace=True),

            nn.Linear(ndf*2, 1),
            nn.Sigmoid()

this lines on forward section.
Any recommendation would be appricated.

You need to add error output, otherwise people have to browse through the code in details. Anyway, it means that some layer expects Tensor with size 32768 as an input but gets only Tensor size 32.

what is NetD output shape. it should be 32x1

Sorry I forgot the mention where the error is. in errD_real section

I change ndf*4 to 1 on this section:

nn.Conv3d(ndf*8, ndf*4, 3, 2, 0, bias = False),

So NetD outuput decrease 256 from 32768
NetD output give me a (256*1)
How can I decrease it to 32?

check the shape before .view(-1) i think it’s 32x2x2x2
maybe you need to add a linear layer or averagepooling.

I run code without view and output shape is = torch.Size([32, 1, 2, 2, 2]), I think linear layer will be a solution. Thanks I will try it.