Maison > développement back-end > Tutoriel Python > BCEAvecLogitsLoss dans PyTorch

BCEAvecLogitsLoss dans PyTorch

Linda Hamilton
Libérer: 2024-11-07 06:01:03
original
938 Les gens l'ont consulté

Achetez-moi un café☕

*Mémos :

  • Mon article explique la perte L1 (MAE), la perte L2 (MSE), la perte Huber, le BCE et la perte d'entropie croisée.
  • Mon message explique BCELoss().
  • Mon message explique Sigmoïde.
  • Mon message explique CrossEntropyLoss().

BCEWithLogitsLoss() peut obtenir le tenseur 0D ou plus D de zéro ou plusieurs valeurs (float) calculé par BCE Loss et Sigmoid à partir du tenseur 0D ou plus D de zéro ou plusieurs éléments, comme indiqué ci-dessous :

*Mémos :

  • Le 1er argument pour l'initialisation estweight(Optional-Default:None-Type:tensor of int, float ou bool) : *Mémos :
    • Si ce n'est pas donné, c'est 1.
    • Il doit s'agir du tenseur 0D ou plus D de zéro ou plusieurs éléments.
  • Il existe un argument de réduction pour l'initialisation (Optional-Default:'mean'-Type:str). *'aucun', 'moyenne' ou 'somme' peuvent être sélectionnés.
  • Il existe un argument pos_weight pour l'initialisation (Optional-Default:None-Type:tensor of int ou float) : *Mémos :
    • Si ce n'est pas donné, c'est 1.
    • Il doit s'agir du tenseur 0D ou plus D de zéro ou plusieurs éléments.
  • Il existe des arguments size_average et réduire pour l'initialisation mais ils sont obsolètes.
  • Le 1er argument est input(Required-Type:tensor of float). *Il doit s'agir du tenseur 0D ou plus D de zéro ou plusieurs éléments.
  • Le 2ème argument est target(Required-Type:tensor of float). *Il doit s'agir du tenseur 0D ou plus D de zéro ou plusieurs éléments.
  • l'entrée et la cible doivent avoir la même taille, sinon il y a une erreur.
  • L'entrée 1D ou plus D vide et le tenseur cible avec réduction='mean' renvoient nan.
  • L'entrée 1D ou plus D vide et le tenseur cible avec réduction='sum' renvoient 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.)
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal