RuntimeError: Function 'AddmmBackward0' returned nan values in its 0th output

Heethanjan_Kanagalin: <p>class Generator(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Generator, self).<strong>init</strong>()<br>
self.model = nn.Sequential(<br>
nn.Linear(NOISE_DIM, 4096),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(4096, 8192),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(8192, 4096),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(4096, 512),<br>
nn.LeakyReLU(0.2)<br>
)<br>
def forward(self, x):<br>
return self.model(x)</p>
<p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Discriminator, self).<strong>init</strong>()<br>
self.model = nn.Sequential(<br>
nn.Linear(NOISE_DIM, 4096),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(4096, 8192),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(8192, 4096),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(4096, 1),<br>
nn.ReLU()<br>
)<br>
def forward(self, x):<br>
return self.model(x)</p>
<p>class Classifier(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Classifier, self).<strong>init</strong>()<br>
self.model = nn.Sequential(<br>
nn.Linear(NOISE_DIM, 4096),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(4096, 8192),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(8192, 4096),<br>
nn.LeakyReLU(0.2),<br>
nn.Linear(4096, 64),<br>
nn.Sigmoid()<br>
)<br>
def forward(self, x):<br>
return self.model(x)</p>
<p><span class="hashtag-raw">#loss</span> functions<br>
cross_entropy = torch.nn.BCELoss()<br>
def cos_dis_arr(a,b):<br>
dis_=<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for i in range(len(a)):<br>
d =distance.cosine(a[i].detach().numpy(),b[i].detach().numpy())<br>
dis_.append(d)<br>
return(np.array(dis_).mean())</p>
<p>def discriminator_loss(real_features,generated_features,real_output, fake_output):<br>
real_loss = cross_entropy(torch.ones_like(real_output), real_output).clone()<br>
fake_loss = cross_entropy(torch.zeros_like(fake_output), fake_output).clone()<br>
total_loss = real_loss.clone() + fake_loss.clone() #+ 0.01*cos_dis_arr(real_features,generated_features)<br>
<span class="hashtag-raw">#print</span>(total_loss.shape)<br>
return total_loss</p>
<p>def classfier_loss(y_true,y_pred):<br>
return cross_entropy(y_true.double(), y_pred.double())</p>
<p>def generator_loss(real_features,generated_features,y_true,y_pred):<br>
<span class="hashtag-raw">#print</span>(cross_entropy(tf.ones_like(fake_output), fake_output).shape)<br>
a= cross_entropy(y_true.double(), y_pred.double())<br>
b= cos_dis_arr(real_features,generated_features)<br>
c = cross_entropy(torch.ones_like(generated_features), generated_features)<br>
wandb.log({“cross_entrophy”: a, “distance”: b, “discriminator_loss”: c})<br>
return a + c + b</p>
<h1><a name="initialize-models-and-optimizers-1" class="anchor" href="#initialize-models-and-optimizers-1"></a>Initialize models and optimizers</h1>
<p>generator = Generator()<br>
discriminator = Discriminator()<br>
classifier = Classifier()<br>
generator_optimizer = optim.Adam(generator.parameters(), lr=1e-4, betas=(BETA1,BETA2))<br>
discriminator_optimizer = optim.Adam(discriminator.parameters(), lr=1e-4, betas=(BETA1,BETA2))<br>
classifier_optimizer = optim.Adam(classifier.parameters(), lr=1e-4, betas=(BETA1,BETA2))</p>
<p>def train_step(images, llb):<br>
# 1 - Create noise tensors<br>
noise2 = torch.normal(0,0.2,(BATCH_SIZE, noise_dim))<br>
# 2 - Generate images and calculate loss values</p>
<pre><code>generator_optimizer.zero_grad()
im__=torch.add(images,noise2)
generated_images = generator(im__)
gen_classifier = classifier(generated_images)
real_classifier = classifier(images)
real_output = discriminator(images)
fake_output = discriminator(generated_images)

gen_loss = generator_loss(images, generated_images, llb, gen_classifier)
print("#####genLoss#######")
print(gen_loss)
gen_loss.backward()
generator_optimizer.step()

################
discriminator_optimizer.zero_grad()
im__=torch.add(images,noise2)
generated_images = generator(im__)
gen_classifier = classifier(generated_images)
real_classifier = classifier(images)
real_output = discriminator(images)
fake_output = discriminator(generated_images)
disc_loss = discriminator_loss(images, generated_images, real_output, fake_output)
disc_loss.backward()
discriminator_optimizer.step()

############

clss_loss = classfier_loss(llb, real_classifier)
gen_see = classfier_loss(llb, gen_classifier)

wandb.log({"gen_loss": gen_loss, "gen_see": gen_see, "clas_loss": clss_loss, "dic_loss_disc": disc_loss})

# 3 - Compute gradients and update weights for generator, discriminator and classifier

classifier_optimizer.zero_grad()
clss_loss.backward()
classifier_optimizer.step()
</code></pre>
<p>import time</p>
<h1><a name="feature-generator-2" class="anchor" href="#feature-generator-2"></a>feature generator</h1>
<p>def syn_features(model,input_feature,num_of_sin_features):<br>
new_features=<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
t_in = torch.reshape(input_feature, (1,512))</p>
<pre><code>for i in range(num_of_sin_features):
    noise22 = torch.normal(0,0.2,(1, 512))
    f_2= torch.add(t_in,noise22)
    #noise = tf.random.normal([1, 512])
    #inp = tf.concat((f_2,noise),1)
    model.eval()
    fe = model(f_2)
    new_features.append(fe)
return(new_features)
</code></pre>
<p>def train(loader,epochs):<br>
for epoch in range(epochs):<br>
start = time.time()<br>
for image_batch, label_batch in loader:<br>
train_step(image_batch, label_batch)<br>
if (epoch + 1) % 3 == 0:<br>
# There is no Checkpoint object in PyTorch; saving weights can be done as follows:<br>
pass<br>
print(‘Time for epoch {} is {} sec’.format(epoch + 1, time.time() - start))<br>
print(“small Test”)<br>
f = random.choice(features)<br>
# noise = torch.randn([1, 512])<br>
noise22 = torch.normal(0,0.2,(1, 512))<br>
f_ = f.reshape((1, 512))<br>
f_2 = f_ + noise22<br>
# inp = torch.cat((f_, noise), 1)<br>
generator.eval()<br>
gen = generator(f_2)<br>
t_in = gen.reshape((512, ))<br>
d = distance.cosine(f, t_in)</p>
<pre><code>    print(f)
    print(gen)
    print(d)
</code></pre>
<p>train(loader,15)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/a/9a44b3fcafbb5838208d6b590e8e31c09a770e53.png" data-download-href="https://discuss.pytorch.org/uploads/default/9a44b3fcafbb5838208d6b590e8e31c09a770e53" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/a/9a44b3fcafbb5838208d6b590e8e31c09a770e53_2_690x388.png" alt="image" data-base62-sha1="m0IJ19H9RoN45gvP1Gm3hgBKB4D" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/a/9a44b3fcafbb5838208d6b590e8e31c09a770e53_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/a/9a44b3fcafbb5838208d6b590e8e31c09a770e53_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/a/9a44b3fcafbb5838208d6b590e8e31c09a770e53_2_1380x776.png 2x" data-dominant-color="333537"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×1080 277 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

FalsoMoralista: <p>Were you able to manage this?</p>