Tensorflow CTC
Tools to simplify CTC in
tensorflow
Mocked Logits
'Perfect' logits to match any given labels. Basically, a large negative value ($10^{-9}$ by default) everywhere except $0$ at the appropriate index. Plus, blank characters (by default at index 0) interspersed to prevent collapse of equal, consecutive labels
import tensorflow as tf
import tf.ctc as ctc
labs = tf.sparse.from_dense([[1, 2, 3, 0, 0],
[4, 3, 1, 2, 0],
[3, 1, 0, 0, 0]])
logits = ctc.onehot_logits(labs) # 'perfect' logits
# <tf.Tensor: shape=(3, 10, 5), dtype=float32, numpy=
#
# array([[[-1.e+09, 0.e+00, -1.e+09, -1.e+09, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09],
# [-1.e+09, -1.e+09, 0.e+00, -1.e+09, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09],
# [-1.e+09, -1.e+09, -1.e+09, 0.e+00, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09],
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09]],
#
# [[-1.e+09, -1.e+09, -1.e+09, -1.e+09, 0.e+00],
# ...
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09]],
#
# [[-1.e+09, -1.e+09, -1.e+09, 0.e+00, -1.e+09],
# ...
# [ 0.e+00, -1.e+09, -1.e+09, -1.e+09, -1.e+09]]], dtype=float32)>
ctc.loss(labs, logits) # something very close to [0, 0, 0]
Loss
Wrapper around tf.nn.loss but labels must be a SparseTensor (as they should probably be) and the API is trivial (just labels and logits)
(See example above)
Decoding
Wrappers around tf.nn.ctc_greedy_decoder and tf.nn.ctc_beam_search_decoder, except:
- Beam Search supports setting the blank index to 0 (and does so by default)
- Logits are batch-major (as you most likely already have them)
- Trivial API (just pass the logits and optionally config)
import tensorflow as tf
import tf_ctc as ctc
labs = tf.sparse.from_dense([[1, 2, 3, 0, 0],
[4, 3, 1, 2, 0],
[3, 1, 0, 0, 0]])
logits = ctc.onehot_logits(labs) # 'perfect' logits
[top_path, *_], log_probs = ctc.beam_decode(logits)
# or
[top_path], log_probs = ctc.greedy_decode(logits)
tf.sparse.to_dense(top_path)
# <tf.Tensor: shape=(3, 4), dtype=int64, numpy=
# array([[1, 2, 3, 0],
# [4, 3, 1, 2],
# [3, 1, 0, 0]])>
Metrics
Generalizations of accuracy and edit distance (default to $k=1$, i.e. the usual, concrete versions):
- Top-$k$ accuracy: proportion of samples where the label is in the top-$k$ predictions
- Top-$k$ edit distance: minimum edit distance between the label and each of the top-$k$ predictions; averaged across all samples
import tensorflow as tf
import tf_ctc as ctc
labs = tf.sparse.from_dense([[1, 2, 3, 0, 0],
[4, 3, 1, 2, 0],
[3, 1, 0, 0, 0]])
logits = ctc.onehot_logits(labs) # 'perfect' logits
ctc.accuracy(labs, logits) # 1.0
ctc.edit_distance(labs, logits) # 0.0
Testing
Very simple randomized tests using ctc.onehot_logits
pip install tf-ctc[test]
python -m tf.ctc.test
(Note: tests may spit out a warning about jaxtyping. That's just fine)
Metadata
Release files for tf-ctc 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tf_ctc-0.1.8.tar.gz | 7.4 kB | Without provenance | Details for tf_ctc-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tf_ctc-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for tf_ctc-0.1.8-py3-none-any.whl |
Total release size: 15.9 kB
Release files / tf_ctc-0.1.8.tar.gz
| Download URL | tf_ctc-0.1.8.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
31a39c1167c80dad6fe0af211773d421127eaf22a3439a9b810b4bf1ce33eeee
|
|
BLAKE2b-256 checksum How to use checksums |
9e20fd5fec67313d3fb2928b78607264b03e15ebb0b9a8aaf8982702d4da28ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.6
|
Release files / tf_ctc-0.1.8-py3-none-any.whl
| Download URL | tf_ctc-0.1.8-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6682287015245aaa9848254f11e6d9c0bf1e68e5da494e7855d7e90aba511bd2
|
|
BLAKE2b-256 checksum How to use checksums |
44208b7ed4fa4792932e4a6252dd974fe43c02a4ede06473b49c954ccfa6c197
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.6
|
Security for tf-ctc 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.