當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PyTorch ctc_loss用法及代碼示例

本文簡要介紹python語言中 torch.nn.functional.ctc_loss 的用法。

用法:

torch.nn.functional.ctc_loss(log_probs, targets, input_lengths, target_lengths, blank=0, reduction='mean', zero_infinity=False)

參數

  • log_probs- 其中 C = number of characters in alphabet including blankT = input lengthN = batch size 。輸出的對數概率(例如,使用 torch.nn.functional.log_softmax() 獲得)。

  • targets- (sum(target_lengths)) 。目標不能為空。在第二種形式中,假設目標是串聯的。

  • input_lengths- 。輸入的長度(每個都必須是 )

  • target_lengths- 。目標的長度

  • blank(int,可選的) -空白標簽。默認

  • reduction(string,可選的) -指定要應用於輸出的縮減:'none' | 'mean' | 'sum''none' :不應用減少,'mean':輸出損失將除以目標長度,然後取批次的平均值,'sum':輸出將被求和。默認值:'mean'

  • zero_infinity(bool,可選的) -是否將無限損失和相關梯度歸零。默認值:False 無限損失主要發生在輸入太短而無法與目標對齊時。

聯結主義時間分類損失。

有關詳細信息,請參閱 CTCLoss

注意

在某些情況下,當在 CUDA 設備上給定張量並使用 CuDNN 時,此運算符可能會選擇非確定性算法來提高性能。如果這是不可取的,您可以嘗試通過設置 torch.backends.cudnn.deterministic = True 來使操作具有確定性(可能以性能為代價)。有關詳細信息,請參閱重現性。

注意

當給定 CUDA 設備上的張量時,此操作可能會產生不確定的梯度。有關詳細信息,請參閱重現性。

例子:

>>> log_probs = torch.randn(50, 16, 20).log_softmax(2).detach().requires_grad_()
>>> targets = torch.randint(1, 20, (16, 30), dtype=torch.long)
>>> input_lengths = torch.full((16,), 50, dtype=torch.long)
>>> target_lengths = torch.randint(10,30,(16,), dtype=torch.long)
>>> loss = F.ctc_loss(log_probs, targets, input_lengths, target_lengths)
>>> loss.backward()

相關用法


注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.nn.functional.ctc_loss。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。