Decodes the output of a softmax. Can use either greedy search (also known as best path) or a constrained dictionary search. Arguments: y_pred: tensor `(samples, time_steps, num_categories)` containing the prediction, or output of the softmax. input_length: tensor `(samp
(y_pred, input_length, greedy=True, beam_width=100, top_paths=1)
| 5634 | |
| 5635 | @keras_export('keras.backend.ctc_decode') |
| 5636 | def ctc_decode(y_pred, input_length, greedy=True, beam_width=100, top_paths=1): |
| 5637 | """Decodes the output of a softmax. |
| 5638 | |
| 5639 | Can use either greedy search (also known as best path) |
| 5640 | or a constrained dictionary search. |
| 5641 | |
| 5642 | Arguments: |
| 5643 | y_pred: tensor `(samples, time_steps, num_categories)` |
| 5644 | containing the prediction, or output of the softmax. |
| 5645 | input_length: tensor `(samples, )` containing the sequence length for |
| 5646 | each batch item in `y_pred`. |
| 5647 | greedy: perform much faster best-path search if `true`. |
| 5648 | This does not use a dictionary. |
| 5649 | beam_width: if `greedy` is `false`: a beam search decoder will be used |
| 5650 | with a beam of this width. |
| 5651 | top_paths: if `greedy` is `false`, |
| 5652 | how many of the most probable paths will be returned. |
| 5653 | |
| 5654 | Returns: |
| 5655 | Tuple: |
| 5656 | List: if `greedy` is `true`, returns a list of one element that |
| 5657 | contains the decoded sequence. |
| 5658 | If `false`, returns the `top_paths` most probable |
| 5659 | decoded sequences. |
| 5660 | Important: blank labels are returned as `-1`. |
| 5661 | Tensor `(top_paths, )` that contains |
| 5662 | the log probability of each decoded sequence. |
| 5663 | """ |
| 5664 | y_pred = math_ops.log(array_ops.transpose(y_pred, perm=[1, 0, 2]) + epsilon()) |
| 5665 | input_length = math_ops.cast(input_length, dtypes_module.int32) |
| 5666 | |
| 5667 | if greedy: |
| 5668 | (decoded, log_prob) = ctc.ctc_greedy_decoder( |
| 5669 | inputs=y_pred, sequence_length=input_length) |
| 5670 | else: |
| 5671 | (decoded, log_prob) = ctc.ctc_beam_search_decoder( |
| 5672 | inputs=y_pred, |
| 5673 | sequence_length=input_length, |
| 5674 | beam_width=beam_width, |
| 5675 | top_paths=top_paths) |
| 5676 | decoded_dense = [ |
| 5677 | sparse_ops.sparse_to_dense( |
| 5678 | st.indices, st.dense_shape, st.values, default_value=-1) |
| 5679 | for st in decoded |
| 5680 | ] |
| 5681 | return (decoded_dense, log_prob) |
| 5682 | |
| 5683 | |
| 5684 | # HIGH ORDER FUNCTIONS |