Hi,
is there anyone who can look on my code an can say me wy I often get nan when taken an larger batchsize.
Here my code:
import torch
import torchvision
from torchvision import transforms
from PIL import Image
from os import listdir
import random
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
import os
size = 28
batch_size = 10
normalize = transforms.Normalize(
    mean=[0.485, 0.456, 0.406],
    std=[0.229, 0.224, 0.225]
)
transform = transforms.Compose([
    transforms.Resize(size),
    transforms.CenterCrop(size),
    transforms.ToTensor(),
    #normalize
])
train_data_list = []
target_list = []
train_data = []
files = listdir(“Art/dataset_updated/training_set/drawings/”)
for f in range(len(listdir(“CAD/train/”))):
    f = random.choice(files)
    target = Image.open(“Art/dataset_updated/training_set/drawings/” + f)
    target = transform(target)
    t = []
    d = torch.Tensor(3, size, size)
    s = 0
    for i in range(0, 3):
        for n in range(0, size):
            for m in range(0, size):
                #print(“target.”, s, "   Position: " ,i , “,”,n , “,”,m , “,”)
                t.append(d[i][n][m] /255 * 2-1)
                s = s + 1
    target = t
    img = Image.open(“Art/dataset_updated/training_set/drawings/” + f)
    img_tensor = transform(img)/255
    train_data_list.append(img_tensor)
    #isCat = 1 if “cat” in f else 0
    #isDog = 1 if “dog” in f else 0
    #target = [isCat, isDog]
    target_list.append(target)
    if len(train_data_list) >= batch_size:
        train_data.append((torch.stack(train_data_list), target_list))
        train_data_list = []
        break
class GeneratorNet(torch.nn.Module):
    def init(self):
        super(GeneratorNet, self).init()
        n_features = 100
        n_out = 3  size  size
        self.hidden0 = nn.Sequential(
            nn.Linear(n_features, 256),
            nn.LeakyReLU(0.2)
        )
        self.hidden1 = nn.Sequential(
            nn.Linear(256, 512),
            nn.LeakyReLU(0.2)
        )
        self.hidden2 = nn.Sequential(
            nn.Linear(512, 1024),
            nn.LeakyReLU(0.2)
        )
        self.out = nn.Sequential(
            nn.Linear(1024, n_out),
            nn.Tanh()
        )
    def forward(self, x):
        x = self.hidden0(x)
        x = self.hidden1(x)
        x = self.hidden2(x)
        x = self.out(x)
        return x
generator = GeneratorNet()
generator.cuda()
#if os.path.isfile(“Generator.pt”):
    #generator = torch.load(“Generator.pt”)
class DiscriminatorNet(torch.nn.Module):
    def init(self):
        super(DiscriminatorNet, self).init()
        n_features =  3  size  size
        n_out = 1
        self.hidden0 = nn.Sequential(
            nn.Linear(n_features, 1024),
            nn.LeakyReLU(0.2),
            nn.Dropout(0.3)
        )
        self.hidden1 = nn.Sequential(
            nn.Linear(1024, 512),
            nn.LeakyReLU(0.2),
            nn.Dropout(0.3)
        )
        self.hidden2 = nn.Sequential(
            nn.Linear(512, 256),
            nn.LeakyReLU(0.2),
            nn.Dropout(0.3)
        )
        self.out = nn.Sequential(
            torch.nn.Linear(256, n_out),
            torch.nn.Sigmoid()
        )
    def forward(self, x):
        x = self.hidden0(x)
        x = self.hidden1(x)
        x = self.hidden2(x)
        x = self.out(x)
        return x
discriminator = DiscriminatorNet()
discriminator.cuda()
#if os.path.isfile(“Discriminator.pt”):
    #discriminator = torch.load(“Discriminator.pt”)
d_optimizer = optim.Adam(discriminator.parameters(), lr=0.0002)
g_optimizer = optim.Adam(generator.parameters(), lr=0.0002)
loss = nn.BCELoss()
def ones_target(size):
    data = Variable(torch.ones(size, 1).cuda())
    return data
def zeros_target(size):
    data = Variable(torch.zeros(size, 1).cuda())
    return data
def images_to_vectors(images):
    return images.view(images.size(0),  3  size  size )
def vectors_to_images(vectors):
    return vectors.view(vectors.size(0), 3, size, size)
def train_discriminator(optimizer, real_data, fake_data):
    #N = real_data.size(0)
    optimizer.zero_grad()
    prediction_real = discriminator(real_data)
    error_real = loss(prediction_real, ones_target(batch_size)) / batch_size
    error_real.backward()
    prediction_fake = discriminator(fake_data)
    error_fake = loss(prediction_fake, zeros_target(1)) / batch_size
    error_fake.backward()
    optimizer.step()
    return error_real + error_fake, prediction_real, prediction_fake
def train_generator(optimizer, fake_data):
    #N = fake_data.size(0)
    optimizer.zero_grad()
    prediction = discriminator(fake_data)
    error = loss(prediction, ones_target(1)) / batch_size
    print(error)
    error.backward()
    optimizer.step()
    return error
def noise(size):
    n = Variable(torch.randn(size, 100).cuda())
    return n
def variable(num):
    n = Variable(torch.torch(num).cuda())
    return n
num_test_samples = 16
test_noise = noise(num_test_samples).cuda()
num_epochs = 20000
r = 0
e = 0
for epoch in range(num_epochs):
    for data,target in train_data:
        r = r + 1
        #N = real_batch.size(0)
        real_data = Variable(torch.Tensor(target))
        real_data = real_data.cuda()
        fake_data = generator(noise(1)).detach()
        fake_data = fake_data.cuda()
        d_error, d_pred_real, d_pred_fake = 
            train_discriminator(d_optimizer, real_data, fake_data)
        fake_data = generator(noise(1))
        g_error = train_generator(g_optimizer, fake_data)
        if r % 100 == 0 :
            r = 0
            test_images = vectors_to_images(generator(test_noise))
            print(test_images.size())
            #x = transforms.ToPILImage()(test_images)
            x =  transforms.ToPILImage()(test_images.cpu()[0])
            x.save(“Result/out”+r.str()+".png")
            torch.save(generator, “Generator.pt”)
            torch.save(discriminator, “Discriminator.pt”)
            print(“saved image”)
    r = r + 1
    e = e + 1
    if r == 100:
        r = 0
        test_images = vectors_to_images(generator(test_noise))
        print(test_images.size())
        # x = transforms.ToPILImage()(test_images)
        x = transforms.ToPILImage()(test_images.cpu()[0])
        x.save(“Result/out” + e.str() + “.png”)
        torch.save(generator, “Generator.pt”)
        torch.save(discriminator, “Discriminator.pt”)
        print(“saved image”)
    #test_images.save(“Result/out.png”)
    torch.save(generator, “Generator.pt”)
    torch.save(discriminator, “Discriminator.pt”)
# Turn a 3d  Tensor in a png
# b = torch.FloatTensor(3, 128, 128)
# b = transforms.ToPILImage()(img_tensor)
# b.save(“TestNormal.png”)