# The problem about the loss backward()

``````g_error = generator_loss(x)
g_error.backward()
``````

Then the error showed:
backward() takes 1 positional argument but 2 were given.

The value of g_error is tensor([16.8434], grad_fn=)

I want to know the cause of this problem and the way to solve.

Could you post a small executable code snippet?
This would make debugging a bit easier.

Thank you very much! Here is the related code.

``````class VerificationMatcher_ldaloss(nn.Module):
def __init__(self,te,P,y,lamda,cta,w,mu,sigma,sr,T):
super(VerificationMatcher_ldaloss,self).__init__()
self.te=te
self.P=P
self.y=y
self.lamda=lamda
self.cta=cta
self.sr=sr
self.w=w
self.mu=mu
self.sigma=sigma
self.T=T
#self.beta=beta
return
def forward(self,x):
fwa=extractiVector()#self.y+x,self.sr,self.w,self.mu,self.sigma)
#print(wa)
voice=self.y+x
wa=fwa(voice,self.sr,self.w,self.mu,self.sigma,self.T)
ta=self.P.float().mm(wa)

ta=torch.t(ta)
#self.te=torch.t(self.te)
#print(te)
#te=torch()
#print(torch.norm(self.te.float(), 2, 1))
#print(2)
cos = nn.CosineSimilarity(dim=0, eps=1e-6)
#print(self.te)
loss =1- self.lamda*(cos(self.te[0].float(),ta[0])-self.cta)+torch.norm(x).item()
print(loss)
return loss

generator_loss=VerificationMatcher_ldaloss(te=te,P=P,y=y,lamda=lamda,cta=cta,
w=w,mu=mu,sigma=sigma,sr=sr,T=T)
def train_a_gan(G_net, G_optimizer,generator_loss, show_every=250,
noise_size=200, num_epochs=1000):
iter_count = 0
sample_noise = (torch.rand(1, noise_size) - 0.5) / 0.5
for epoch in range(num_epochs):
#sample_noise = (torch.rand(1, noise_size) - 0.5) / 0.5 # -1 ~ 1 的均匀分布
g_fake_seed = Variable(sample_noise)#.cuda()
if torch.cuda.is_available():
g_fake_seed=g_fake_seed.cuda()
fake_voices = G_net(g_fake_seed) # 生成的假的数据

g_error = generator_loss(fake_voices)

#weights=torch.ones(1)*0.1
#g_error.backward(weights,retain_graph=True)
g_error.backward()
G_optimizer.step() # 优化生成网络
if (iter_count % show_every == 0):
print('Iter: {}, G:{:.4}'.format(iter_count,g_error.item()))
voices_numpy = fake_voices.data.cpu().numpy()
#show_images(imgs_numpy[0:16])
#plt.show()
#print()
iter_count += 1

train_a_gan(G_net, G_optimizer,generator_loss, show_every=25, noise_size=200, num_epochs=100001)

``````

The error is:
TypeError: backward() takes 1 positional argument but 2 were given
in the line " g_error.backward()"

Thanks for the code!
Unfortunately I cannot debug it smoothly, as all arguments (with their shapes) are missing as well as the definition of `extractiVector()`.

One issue in your `forward` method is, that you are detaching `torch.norm(x)` by calling `item` on it, thus it works as a constant value.
The error message is strange given your code, as no arguments are passed to `g_error.backward()`.
Could you try to simplify the code a bit or post all arguments using `torch.randn` with the corresponding shapes?