Computes the CTC loss and gradients. Most users will want fwd_bwd.ctc_loss This function returns the computed gradient, it does not have a gradient of its own defined. Args: logits: tensor of shape [frames, batch_size, num_labels] labels: tensor of shape [batch_size, max_label_seq
(logits, labels, label_length, logit_length, unique=None)
| 578 | |
| 579 | |
| 580 | def ctc_loss_and_grad(logits, labels, label_length, logit_length, unique=None): |
| 581 | """Computes the CTC loss and gradients. |
| 582 | |
| 583 | Most users will want fwd_bwd.ctc_loss |
| 584 | |
| 585 | This function returns the computed gradient, it does not have a gradient |
| 586 | of its own defined. |
| 587 | |
| 588 | Args: |
| 589 | logits: tensor of shape [frames, batch_size, num_labels] |
| 590 | labels: tensor of shape [batch_size, max_label_seq_length] |
| 591 | label_length: tensor of shape [batch_size] Length of reference label |
| 592 | sequence in labels. |
| 593 | logit_length: tensor of shape [batch_size] Length of input sequence in |
| 594 | logits. |
| 595 | unique: (optional) unique label indices as computed by unique(labels) If |
| 596 | supplied, enables an implementation that is faster and more memory |
| 597 | efficient on TPU. |
| 598 | |
| 599 | Returns: |
| 600 | loss: tensor of shape [batch_size] |
| 601 | gradient: tensor of shape [frames, batch_size, num_labels] |
| 602 | """ |
| 603 | |
| 604 | num_labels = _get_dim(logits, 2) |
| 605 | max_label_seq_length = _get_dim(labels, 1) |
| 606 | |
| 607 | ilabel_log_probs = nn_ops.log_softmax(logits) |
| 608 | state_log_probs = _ilabel_to_state(labels, num_labels, ilabel_log_probs) |
| 609 | state_trans_probs = _ctc_state_trans(labels) |
| 610 | initial_state_log_probs, final_state_log_probs = ctc_state_log_probs( |
| 611 | label_length, max_label_seq_length) |
| 612 | fwd_bwd_log_probs, log_likelihood = _forward_backward_log( |
| 613 | state_trans_log_probs=math_ops.log(state_trans_probs), |
| 614 | initial_state_log_probs=initial_state_log_probs, |
| 615 | final_state_log_probs=final_state_log_probs, |
| 616 | observed_log_probs=state_log_probs, |
| 617 | sequence_length=logit_length) |
| 618 | |
| 619 | if unique: |
| 620 | olabel_log_probs = _state_to_olabel_unique(labels, num_labels, |
| 621 | fwd_bwd_log_probs, unique) |
| 622 | else: |
| 623 | olabel_log_probs = _state_to_olabel(labels, num_labels, fwd_bwd_log_probs) |
| 624 | |
| 625 | grad = math_ops.exp(ilabel_log_probs) - math_ops.exp(olabel_log_probs) |
| 626 | loss = -log_likelihood |
| 627 | return loss, grad |
| 628 | |
| 629 | |
| 630 | def _ctc_loss_grad(op, grad_loss, _): |
no test coverage detected