r"""The Connectionist Temporal Classification loss. Args: pred: The probabilities of the output, shape is (T, N, C) , where T=input length, N=batch size, and C=number of classes (including blank). pred_lengths: number of time steps for each sequence in ``pred``, sha
(
pred: Tensor,
pred_lengths: Tensor,
label: Tensor,
label_lengths: Tensor,
blank: int = 0,
reduction: str = "mean",
)
| 332 | |
| 333 | |
| 334 | def ctc_loss( |
| 335 | pred: Tensor, |
| 336 | pred_lengths: Tensor, |
| 337 | label: Tensor, |
| 338 | label_lengths: Tensor, |
| 339 | blank: int = 0, |
| 340 | reduction: str = "mean", |
| 341 | ) -> Tensor: |
| 342 | r"""The Connectionist Temporal Classification loss. |
| 343 | |
| 344 | |
| 345 | Args: |
| 346 | pred: The probabilities of the output, shape is (T, N, C) , |
| 347 | where T=input length, N=batch size, and C=number of classes (including blank). |
| 348 | pred_lengths: number of time steps for each sequence in ``pred``, shape is (N, ) |
| 349 | label: groundtruth labels, containing the indices of groundtruth |
| 350 | symbols for each sequence at each output time step, and the blank |
| 351 | symbol should not be included. shape is (N, S) or (sum(label_lengths)). |
| 352 | label_lengths: number of time steps for each sequence in the groundtruth, shape is (N, ) |
| 353 | blank: the blank symbol number, default 0 |
| 354 | reduction: the reduction to apply to the output: 'none' | 'mean' | 'sum'. Default: 'mean' |
| 355 | |
| 356 | Returns: |
| 357 | loss value. |
| 358 | |
| 359 | Examples: |
| 360 | |
| 361 | >>> pred = Tensor([[[0.0614, 0.9386],[0.8812, 0.1188]],[[0.699, 0.301 ],[0.2572, 0.7428]]]) |
| 362 | >>> pred_lengths = Tensor([2, 2]) |
| 363 | >>> label = Tensor([1, 1]) |
| 364 | >>> label_lengths = Tensor([1, 1]) |
| 365 | >>> F.nn.ctc_loss(pred, pred_lengths, label, label_lengths) |
| 366 | Tensor(0.1504417, device=xpux:0) |
| 367 | |
| 368 | """ |
| 369 | T, N, C = pred.shape |
| 370 | |
| 371 | assert ( |
| 372 | pred_lengths.size == N |
| 373 | ), "pred_lengths must be equal to batch_size {}, but got {}".format( |
| 374 | N, pred_lengths.size |
| 375 | ) |
| 376 | assert ( |
| 377 | label_lengths.size == N |
| 378 | ), "label_lengths must be euqal to batch_size {}, but got {}".format( |
| 379 | N, label_lengths.size |
| 380 | ) |
| 381 | assert ( |
| 382 | blank >= 0 and blank < C |
| 383 | ), "blank must be in label range [0, {}), but got {}".format(C, blank) |
| 384 | assert ( |
| 385 | pred_lengths.min() > 0 and pred_lengths.max() <= T |
| 386 | ), "pred_lengths must be in range ({}, {}], bug got min {}, max {}".format( |
| 387 | 0, T, pred_lengths.min(), pred_lengths.max() |
| 388 | ) |
| 389 | |
| 390 | if label.ndim == 1: # concatenated label |
| 391 | assert label_lengths.min() > 0, "label lengths muse be positive" |
nothing calls this directly
no test coverage detected