Rumah > pembangunan bahagian belakang > Tutorial Python > BCEWithLogitsLoss dalam PyTorch

BCEWithLogitsLoss dalam PyTorch

Linda Hamilton
Lepaskan: 2024-11-07 06:01:03
asal
939 orang telah melayarinya

Beli Saya Kopi☕

*Memo:

  • Siaran saya menerangkan L1 Loss(MAE), L2 Loss(MSE), Huber Loss, BCE dan Cross Entropy Loss.
  • Siaran saya menerangkan BCELoss().
  • Siaran saya menerangkan Sigmoid.
  • Siaran saya menerangkan CrossEntropyLoss().

BCEWithLogitsLoss() boleh mendapatkan 0D atau lebih D tensor bagi sifar atau lebih nilai(float) yang dikira oleh BCE Loss dan Sigmoid daripada 0D atau lebih D tensor sifar atau lebih elemen seperti yang ditunjukkan di bawah:

*Memo:

  • Argumen pertama untuk permulaan ialah berat(Pilihan-Lalai:Tiada-Jenis:tensor int, float atau bool): *Memo:
    • Jika ia tidak diberikan, ia adalah 1.
    • Ia mestilah tensor 0D atau lebih D bagi sifar atau lebih elemen.
  • Terdapat hujah pengurangan untuk permulaan(Pilihan-Lalai:'mean'-Type:str). *'tiada', 'min' atau 'jumlah' boleh dipilih.
  • Terdapat argumen pos_weight untuk pemulaan(Optional-Default:None-Type:tensor of int or float): *Memo:
    • Jika ia tidak diberikan, ia adalah 1.
    • Ia mestilah tensor 0D atau lebih D bagi sifar atau lebih elemen.
  • Terdapat hujah saiz_purata dan kurangkan untuk permulaan tetapi ia tidak digunakan lagi.
  • Argumen pertama ialah input(Required-Type:tensor of float). *Ia mestilah tensor 0D atau lebih D bagi sifar atau lebih elemen.
  • Argumen ke-2 ialah sasaran(Jenis-Jenis:tensor apungan). *Ia mestilah tensor 0D atau lebih D bagi sifar atau lebih elemen.
  • input dan sasaran mestilah sama saiz jika tidak terdapat ralat.
  • Input 1D atau lebih D kosong dan tensor sasaran dengan reduction='min' return nan.
  • Input 1D atau lebih D kosong dan tensor sasaran dengan reduction='sum' mengembalikan 0.. BCEWithLogitsLoss in PyTorch
import torch
from torch import nn

tensor1 = torch.tensor([ 8., -3., 0.,  1.,  5., -2.])
tensor2 = torch.tensor([-3.,  7., 4., -2., -9.,  6.])
       # -w*(p*y*log(1/(1+exp(-x))+(1-y)*log(1-(1/1+exp(-x))))
       # -1*(1*(-3)*log(1/(1+exp(-8)))+(1-(-3))*log(1-(1/(1+exp(-8)))))
       # ↓↓↓↓↓↓↓
       # 32.0003 + 21.0486 + 0.6931 + 3.3133 + 50.0067 + 50.0067 = 82.8423
       # 119.1890 / 6 = 19.8648
bcelogits = nn.BCEWithLogitsLoss()
bcelogits(input=tensor1, target=tensor2)
# tensor(19.8648)

bcelogits
# BCEWithLogitsLoss()

print(bcelogits.weight)
# None

bcelogits.reduction
# 'mean'

bcelogits = nn.BCEWithLogitsLoss(weight=None,
                                 reduction='mean',
                                 pos_weight=None)
bcelogits(input=tensor1, target=tensor2)
# tensor(19.8648)

bcelogits = nn.BCEWithLogitsLoss(reduction='sum')
bcelogits(input=tensor1, target=tensor2)
# tensor(119.1890)

bcelogits = nn.BCEWithLogitsLoss(reduction='none')
bcelogits(input=tensor1, target=tensor2)
# tensor([32.0003, 21.0486, 0.6931, 3.3133, 50.0067, 12.1269])

bcelogits = nn.BCEWithLogitsLoss(weight=torch.tensor([0., 1., 2., 3., 4., 5.]))
bcelogits(input=tensor1, target=tensor2)
# tensor(48.8394)

bcelogits = nn.BCEWithLogitsLoss(
                pos_weight=torch.tensor([0., 1., 2., 3., 4., 5.])
            )
bcelogits(input=tensor1, target=tensor2)
# tensor(28.5957)

bcelogits = nn.BCEWithLogitsLoss(weight=torch.tensor(0.))
bcelogits(input=tensor1, target=tensor2)
# tensor(0.)

bcelogits = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(0.))
bcelogits(input=tensor1, target=tensor2)
# tensor(13.8338)

bcelogits = nn.BCEWithLogitsLoss(weight=torch.tensor([0, 1, 2, 3, 4, 5]))
bcelogits(input=tensor1, target=tensor2)
# tensor(48.8394)

bcelogits = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([0, 1, 2, 3, 4, 5]))
bcelogits(input=tensor1, target=tensor2)
# tensor(28.5957)

bcelogits = nn.BCEWithLogitsLoss(weight=torch.tensor(0))
bcelogits(input=tensor1, target=tensor2)
# tensor(0.)

bcelogits = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(0))
bcelogits(input=tensor1, target=tensor2)
# tensor(13.8338)

bcelogits = nn.BCEWithLogitsLoss(
              weight=torch.tensor([True, False, True, False, True, False])
          )
bcelogits(input=tensor1, target=tensor2)
# tensor(13.7834)

bcelogits = nn.BCEWithLogitsLoss(weight=torch.tensor([False]))
bcelogits(input=tensor1, target=tensor2)
# tensor(0.)

tensor1 = torch.tensor([[8., -3., 0.], [1., 5., -2.]])
tensor2 = torch.tensor([[-3., 7., 4.], [-2., -9., 6.]])

bcelogits = nn.BCEWithLogitsLoss()
bcelogits(input=tensor1, target=tensor2)
# tensor(19.8648)

tensor1 = torch.tensor([[[8.], [-3.], [0.]], [[1.], [5.], [-2.]]])
tensor2 = torch.tensor([[[-3.], [7.], [4.]], [[-2.], [-9.], [6.]]])

bcelogits = nn.BCEWithLogitsLoss()
bcelogits(input=tensor1, target=tensor2)
# tensor(19.8648)

tensor1 = torch.tensor([])
tensor2 = torch.tensor([])

bcelogits = nn.BCEWithLogitsLoss(reduction='mean')
bcelogits(input=tensor1, target=tensor2)
# tensor(nan)

bcelogits = nn.BCEWithLogitsLoss(reduction='sum')
bcelogits(input=tensor1, target=tensor2)
# tensor(0.)
Salin selepas log masuk

Atas ialah kandungan terperinci BCEWithLogitsLoss dalam PyTorch. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan