首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PYTORCH支持的torch.nn.CTCLoss和torch_baidu_ctc支持的CTCLoss有区别吗?

PYTORCH支持的torch.nn.CTCLoss和torch_baidu_ctc支持的CTCLoss有区别吗?
EN

Stack Overflow用户
提问于 2019-05-03 09:16:20
回答 2查看 3.3K关注 0票数 3

PYTORCH支持的torch.nn.CTCLoss和torch_baidu_ctc支持的CTCLoss有区别吗?

我想,当我比较教程代码时,我没有注意到任何区别。

有谁知道真相吗?

教程代码位于下面。

代码语言:javascript
复制
import torch
from torch_baidu_ctc import ctc_loss, CTCLoss

# Activations. Shape T x N x D.
# T -> max number of frames/timesteps
# N -> minibatch size
# D -> number of output labels (including the CTC blank)
x = torch.rand(10, 3, 6)
# Target labels
y = torch.tensor([
# 1st sample
1, 1, 2, 5, 2,
# 2nd
1, 5, 2,
# 3rd
4, 4, 2, 3,
],
dtype=torch.int,
)
# Activations lengths
xs = torch.tensor([10, 6, 9], dtype=torch.int)
# Target lengths
ys = torch.tensor([5, 3, 4], dtype=torch.int)

# By default, the costs (negative log-likelihood) of all samples are 
summed.
# This is equivalent to:
#   ctc_loss(x, y, xs, ys, average_frames=False, reduction="sum")
loss1 = ctc_loss(x, y, xs, ys)

# You can also average the cost of each sample among the number of 
frames.
# The averaged costs are then summed.
loss2 = ctc_loss(x, y, xs, ys, average_frames=True)

# Instead of summing the costs of each sample, you can perform
# other `reductions`: "none", "sum", or "mean"
#
# Return an array with the loss of each individual sample
losses = ctc_loss(x, y, xs, ys, reduction="none")
#
# Compute the mean of the individual losses
loss3 = ctc_loss(x, y, xs, ys, reduction="mean")
#
# First, normalize loss by number of frames, later average losses
loss4 = ctc_loss(x, y, xs, ys, average_frames=True, reduction="mean")


# Finally, there's also a nn.Module to use this loss.
ctc = CTCLoss(average_frames=True, reduction="mean", blank=0)
loss4_2 = ctc(x, y, xs, ys)

# Note: the `blank` option is also available for `ctc_loss`.
# By default it is 0.

torch.nn.CTCLoss

代码语言:javascript
复制
T = 50      # Input sequence length
C = 20      # Number of classes (excluding blank)
N = 16      # Batch size
S = 30      # Target sequence length of longest target in batch
S_min = 10  # Minimum target length, for demonstration purposes

# Initialize random batch of input vectors, for *size = (T,N,C)
input = torch.randn(T, N, C).log_softmax(2).detach().requires_grad_()

# Initialize random batch of targets (0 = blank, 1:C+1 = classes)
target = torch.randint(low=1, high=C+1, size=(N, S), dtype=torch.long)

input_lengths = torch.full(size=(N,), fill_value=T, dtype=torch.long)
target_lengths = torch.randint(low=S_min, high=S, size=(N,), 
dtype=torch.long)
ctc_loss = nn.CTCLoss()
loss = ctc_loss(input, target, input_lengths, target_lengths)
loss.backward()

我是韩国人。英语不是我的第一语言。所以我的英语不太好。如果有任何东西没有很好地交付,请留下评论。我会尽快修改句子的。

EN

回答 2

Stack Overflow用户

发布于 2019-05-03 17:41:37

1.0 version以来,CTC只损失了PyTorch的一部分,而且它是一种更好的方法,因为它本身就是PyTorch的一部分。如果您使用的是PyTorch 1.0或更高版本,请使用torch.nn.CTCLoss

warp-ctc似乎没有得到维护,最后一次提交更改核心代码是在2017年。后来,他们只修复了TensorFlow (一个已经过时的版本)的绑定。

票数 3
EN

Stack Overflow用户

发布于 2019-05-22 20:39:27

注意Pytorch CTC loss将log softmax概率作为输入,而百度的CTC loss没有提到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55962112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档