簡體   English   中英

我在訓練我的 GAN 時遇到了值錯誤,例如:“ValueError: Target size (torch.Size([10, 1])) must be the same as input size (torch.Size([10]))”

[英]I got value error while training my GAN such as: “ValueError: Target size (torch.Size([10, 1])) must be the same as input size (torch.Size([10]))”

我是 GANs 和 pytorch 的新手,我研究了一些 repos 來提高我的技能,但是當我嘗試實現這個 repo 時:

https://github.com/jasonbian97/Deep-Learning-Computer-Vision/blob/master/generative_adversarial_networks.ipynb

一切順利,但是當我嘗試更改 def discriminator_loss(logits_real, logits_fake) function 中的這些代碼片段時出現了這樣的錯誤(我更改了它,因為在此更改之前我得到了 ValueError: Target size (torch.Size([128]))必須與輸入大小相同(torch.Size([128, 1])) ):

  logits_real_loss = bce_loss(logits_real, torch.ones(N, 1).to(dtype).to(device))
  logits_fake_loss = bce_loss(logits_fake, torch.zeros(N, 1).to(dtype).to(device))
  loss = logits_real_loss + logits_fake_loss  

  N = logits_fake.shape[0]
  device = logits_fake.device
  loss = (bce_loss(logits_fake, torch.ones(N,1).to(dtype).to(device)))

獲取值錯誤:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-15-b089f365cd5e> in <module>()
      2   d_loss = discriminator_loss(logits_real, logits_fake)
      3   print("Maximum error in d_loss: %g"%rel_error(d_loss_true, d_loss))
----> 4 test_discriminator_loss(answers['logits_real'], answers['logits_fake'],answers['d_loss_true'])

2 frames

<ipython-input-15-b089f365cd5e> in test_discriminator_loss(logits_real, logits_fake, d_loss_true)
      1 def test_discriminator_loss(logits_real, logits_fake, d_loss_true):
----> 2   d_loss = discriminator_loss(logits_real, logits_fake)
      3   print("Maximum error in d_loss: %g"%rel_error(d_loss_true, d_loss))
      4 test_discriminator_loss(answers['logits_real'], answers['logits_fake'],answers['d_loss_true'])

<ipython-input-14-15d437ae3425> in discriminator_loss(logits_real, logits_fake)
     17   N = logits_real.shape[0]
     18   device = logits_real.device
---> 19   logits_real_loss = bce_loss(logits_real, torch.ones(N, 1).to(dtype).to(device))
     20   logits_fake_loss = bce_loss(logits_fake, torch.zeros(N, 1).to(dtype).to(device))
     21   loss = logits_real_loss + logits_fake_loss

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
   2578 
   2579     if not (target.size() == input.size()):
-> 2580         raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
   2581 
   2582     return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

ValueError: Target size (torch.Size([10, 1])) must be the same as input size (torch.Size([10]))

也許解決方案很簡單,但我看不到有人能幫我解決這個問題嗎?

由於沒有給出太多的上下文,所以不能確定它是否能解決你的問題。

從給出的信息來看,問題似乎在於您在損失 function 中作為參數發送的張量形狀不同。 您的輸入損失的維度是[10] ,但是您發送的另一個張量是[N,1] => [10,1]維度。 因此,要快速從問題中恢復過來,您可以執行以下操作-

torch.zeros(N).to(dtype).to(device)

這將使您的張量形狀[N] => [10]與輸入相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM