Pytorch nn.BCELoss()获取类型错误:“Tensor”对象不可调用

2024-06-23 03:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用adv_loss = torch.nn.BCELoss()计算网络的损失:

当我第一次(第一批)调用函数时,我没有得到错误。但是,如果我第二次调用函数(第二批),我会得到错误'Tensor' object is not callable.

我正在训练GAN,其中output_of_nn_layers = D(img1)(鉴别器网络的输出)和

label = torch.ones(batch, 1, device=device, dtype = torch.float32)

我在发电机培训中也使用了adv_损耗(BCELoss),但在第二批中出现了错误。因此,第一批培训中使用的总次数为3次(鉴别器2次,发生器1次)

for i, (img1, img2) in enumerate(dataloader):
  #do some processing
  loss = adv_loss(output_of_nn_layers, label)
  #do some processing

我收到错误:TypeError: 'Tensor' object is not callable

完整错误日志:


TypeError                                 Traceback (most recent call last)

<ipython-input-52-bafb0a65d4d9> in <module>()
     73       val_real = D(dis_train_real)
     74       print("check4.2")
---> 75       d_real_loss = adv_loss(val_real, real_label)   #BCELoss
     76       print("check4.3")
     77       val_fake = D(dis_train_fake)

TypeError: 'Tensor' object is not callable

我在stackoerflow上发现了类似的问题:Recalling function: Tensor 'object' is not callable

它给出了被调用函数的变量名不能和函数名相同的解决方案。我是不是遗漏了什么

我还尝试使用nn.L1Loss()并得到了相同的错误。 这个错误意味着什么?我能做些什么来移除它


Tags: objectis错误notnntorchreallabel

热门问题