Additionally, I found the code in the training that causes the error:

real_cpu = data[0].to(device)

which the data[0] is only 1dim tensor but I get the error of 3-dim expected. However, this time, when I unsqueeze it in here:

def forward(self, input):
    input=torch.unsqueeze(input,2)
    return self.main(input)


This time, I get the error:

Dimension out of range (expected to be in the range of [-2, 1], but got 2)

It is kind of confusing. It expects 3dimension but it does not allow me to be 3 dimensions, I don’t know why.
Here is the full code below including discriminator and some steps of the training (sorry for the three response):

Discriminator Code
class Discriminator(nn.Module):
def init(self, ngpu):
super(Discriminator, self).init()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is (1) x 1 x 512
nn.Conv1d(1, ndf2, 4, 4, 0, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf2) x 1 x 128
nn.Conv1d(ndf2, ndf * 4, 4, 4, 0, bias=False),
nn.BatchNorm1d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf4) x 1 x 32
nn.Conv1d(ndf * 4, ndf * 8, 4, 4, 0, bias=False),
nn.BatchNorm1d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf8) x 1 x 8
nn.Conv1d(ndf * 8, ndf * 16, 4, 2, 1, bias=False),
nn.BatchNorm1d(ndf * 16),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf16) x 1 x 4
nn.Conv1d(ndf * 16, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
# state size. 1 x 1 x 1
)
def forward(self, input):
input=torch.unsqueeze(input,2)
return self.main(input)
Create the Discriminator
netD = Discriminator(ngpu).to(device)
Handle multi-gpu if desired
if (device.type == ‘cuda’) and (ngpu > 1):
netD = nn.DataParallel(netD, list(range(ngpu)))
Apply the weights_init function to randomly initialize all weights
to mean=0, stdev=0.2.
netD.apply(weights_init)
Initialize BCELoss function
criterion = nn.BCELoss()
Create batch of latent vectors that we will use to visualize
the progression of the generator
fixed_noise = torch.randn(512, nz, 1, device=device)
Establish convention for real and fake labels during training
real_label = 1
fake_label = 0
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))
Training Loop
Lists to keep track of progress
img_list = []
G_losses = []
D_losses = []
iters = 0
print(“Starting Training Loop…”)
For each epoch
for epoch in range(num_epochs):
# For each batch in the dataloader
for i, data in enumerate(dataloader, 0):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
############################
## Train with all-real batch
netD.zero_grad()
# Format batch
real_cpu = data[0].to(device)
b_size = real_cpu.size(0)
label = torch.full((b_size,), real_label, dtype=torch.float, device=device)
# Forward pass real batch through D
output = netD(real_cpu).view(-1)
.
.
.
.
.
.
.
.