評價此頁

torch.nn.functional.binary_cross_entropy#

torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=None, reduce=None, reduction='mean')[source]#

計算目標和輸入機率之間的二元交叉熵。

有關詳細資訊,請參閱 BCELoss

引數
  • input (Tensor) – 任意形狀的機率張量。

  • target (Tensor) – 與輸入形狀相同的張量,值在 0 和 1 之間。

  • weight (Tensor, optional) – 如果提供了手動重縮放權重,它會被重複以匹配輸入張量形狀。

  • size_average (bool, optional) – 已棄用(請參閱 reduction)。

  • reduce (bool, optional) – 已棄用(請參閱 reduction)。

  • reduction (str, optional) – 指定要應用於輸出的歸約方式:'none' | 'mean' | 'sum''none':不應用歸約,'mean':輸出的總和將除以輸出中的元素數量,'sum':輸出將被求和。注意:size_averagereduce 正在被棄用,在此期間,指定這兩個引數中的任何一個都將覆蓋 reduction。預設為:'mean'

返回型別

張量

示例

>>> input = torch.randn(3, 2, requires_grad=True)
>>> target = torch.rand(3, 2, requires_grad=False)
>>> loss = F.binary_cross_entropy(torch.sigmoid(input), target)
>>> loss.backward()