| 486 | // Using (GravesTh) Eq 7.26 & 7.34. |
| 487 | template <typename TT> |
| 488 | void CTCLossCalculator<TT>::CalculateGradient(const std::vector<int>& l_prime, |
| 489 | const Matrix& y, |
| 490 | const Matrix& log_alpha, |
| 491 | const Matrix& log_beta, |
| 492 | TT log_p_z_x, Matrix* dy) const { |
| 493 | // Only working with the leftmost part of dy for this batch element. |
| 494 | auto dy_b = dy->leftCols(y.cols()); |
| 495 | |
| 496 | // It is possible that no valid path is found if the activations for the |
| 497 | // targets are zero. |
| 498 | if (log_p_z_x == kLogZero<TT>()) { |
| 499 | LOG(WARNING) << "No valid path found."; |
| 500 | dy_b = y; |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | int L = y.rows(); |
| 505 | int T = y.cols(); |
| 506 | int U = l_prime.size(); |
| 507 | |
| 508 | for (int t = 0; t < T - output_delay_; ++t) { |
| 509 | Array prob_sum(L); |
| 510 | prob_sum.setConstant(kLogZero<TT>()); |
| 511 | |
| 512 | for (int u = 0; u < U; ++u) { |
| 513 | int l = l_prime[u]; |
| 514 | prob_sum[l] = LogSumExp(prob_sum[l], log_alpha(u, t) + log_beta(u, t)); |
| 515 | } |
| 516 | |
| 517 | for (int l = 0; l < L; ++l) { |
| 518 | // Negative term in (GravesTh) Eq 7.28. |
| 519 | auto negative_term = expf(prob_sum[l] - log_p_z_x); |
| 520 | |
| 521 | dy_b(l, output_delay_ + t) = y(l, output_delay_ + t) - negative_term; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | template <class TT> |
| 527 | void CTCLossCalculator<TT>::GetLPrimeIndices(const std::vector<int>& l, |