0

I need a TensorFlow-compatible Python library that provides a second-order derivative for Machine Learning CTC (Connectionist Temporal Classification) loss function.

Namely, a library that provides an implementation of the ctc_loss function in Python API such that, in contrast to tf.nn.ctc_loss, the second gradient must be calculable analytically with reasonable performance like this:

with tf.GradientTape() as tape1: 
    with tf.GradientTape() as tape2:
        loss = ctc_loss(logits=logits, ...)
    gradient = tape2.gradient(loss, sources=logits)
hessian = tape1.gradient(gradient, sources=logits)

This may be needed, for example, for the influence function.

5
  • 1
    Welcome to Software Recommendations! Please note this site is about recommending software, not assets or resources like howtos, manuals/tutorials, code fragments, etc.
    – Izzy
    Commented Feb 9, 2022 at 23:45
  • Can't not agree with you @lzzy. This question is similar to this one that is assumed to be on topic. Besides, as mentioned here "...they should ask for software (or by extension libraries) that solve a problem...". So extension libraries recommendation are acceptable. Commented Feb 10, 2022 at 9:37
  • Asking for libraries of course is on-topic here. But you asked about how to implement that functionality. Your answer proves that approach, stating "The implementation is too long to be cited here". This site is not about code nor implementation nor how-tos or code-fragments etc.pp. Added to that, answers are expected to be self-contained, not just a link to "find it there".
    – Izzy
    Commented Feb 10, 2022 at 21:02
  • 1
    I rewrote the question to make it clear that you are asking for a library that offers a specific function. With that I think it is on-topic.
    – Nicolas Raoul
    Commented Feb 16, 2022 at 9:06
  • Thank you very much @NicolasRaoul ! The question looks much better now. Commented Feb 16, 2022 at 13:46

1 Answer 1

0

github.com/alexeytochin/tf_seq2seq_losses (disclaimer: this is my own project) is a library that offers such a function.

To use it, install tf-seq2seq-losses pip package

pip install tf-seq2seq-losses

Example usage:

import tensorflow as tf
from tf_seq2seq_losses import classic_ctc_loss

batch_size = 1
num_tokens = 3
logit_length = 5
logits=tf.zeros(shape=[batch_size, logit_length, num_tokens], dtype=tf.float32)
with tf.GradientTape(persistent=True) as tape1: 
    tape1.watch([logits])
    with tf.GradientTape() as tape2:
        tape2.watch([logits])
        loss = classic_ctc_loss(
            labels=tf.constant([[1, 2, 2, 1]], dtype=tf.int32),
            logits=logits,
            label_length=tf.constant([4], dtype=tf.int32),
            logit_length=tf.constant([logit_length], dtype=tf.int32),
            blank_index=0,
        )
    gradient = tape2.gradient(loss, sources=logits)
hessian = tape1.batch_jacobian(gradient, source=logits, experimental_use_pfor=False)

License: Apache License, Version 2.0

1
  • 1
    The solution as written was not a good answer, in particular answers are not be place to discuss implementation, and answers should at least have usage instructions and license/price. By the way, here is the recommended workflow for people (like you and me) who write software for their own needs: 1) As soon as you identify the need, post as a question 2) While waiting for answers to show up, implement the software 3) Post your new finished software as a solution. Thanks and welcome! :-)
    – Nicolas Raoul
    Commented Feb 16, 2022 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.