import tensorflow as tf


def dense_to_sparse(tensor, blank):

    indices = tf.where(tf.not_equal(tensor, blank))
    values = tf.gather_nd(tensor, indices)
    shape = tf.shape(tensor, out_type=tf.int64)

    return tf.SparseTensor(indices, values, shape)


def edit_distance(labels, logits, sequence_lengths, normalize, name="edit_distance"):

    predictions = tf.nn.ctc_greedy_decoder(
        inputs=tf.transpose(logits, [1, 0, 2]),
        sequence_length=tf.cast(sequence_lengths, tf.int32),
        merge_repeated=False
    )[0][0]

    labels = dense_to_sparse(labels, logits.shape[-1] - 1)

    return tf.reduce_mean(tf.edit_distance(
        hypothesis=tf.cast(predictions, tf.int32),
        truth=tf.cast(labels, tf.int32),
        normalize=normalize
    ), name=name)

b91bbc1700430a30b528f0485a8c0f2cc9a5a2ff
<mask token>

<mask token>


def edit_distance(labels, logits, sequence_lengths, normalize, name=
    'edit_distance'):
    predictions = tf.nn.ctc_greedy_decoder(inputs=tf.transpose(logits, [1, 
        0, 2]), sequence_length=tf.cast(sequence_lengths, tf.int32),
        merge_repeated=False)[0][0]
    labels = dense_to_sparse(labels, logits.shape[-1] - 1)
    return tf.reduce_mean(tf.edit_distance(hypothesis=tf.cast(predictions,
        tf.int32), truth=tf.cast(labels, tf.int32), normalize=normalize),
        name=name)

<mask token>


def dense_to_sparse(tensor, blank):
    indices = tf.where(tf.not_equal(tensor, blank))
    values = tf.gather_nd(tensor, indices)
    shape = tf.shape(tensor, out_type=tf.int64)
    return tf.SparseTensor(indices, values, shape)


def edit_distance(labels, logits, sequence_lengths, normalize, name=
    'edit_distance'):
    predictions = tf.nn.ctc_greedy_decoder(inputs=tf.transpose(logits, [1, 
        0, 2]), sequence_length=tf.cast(sequence_lengths, tf.int32),
        merge_repeated=False)[0][0]
    labels = dense_to_sparse(labels, logits.shape[-1] - 1)
    return tf.reduce_mean(tf.edit_distance(hypothesis=tf.cast(predictions,
        tf.int32), truth=tf.cast(labels, tf.int32), normalize=normalize),
        name=name)

import tensorflow as tf


def dense_to_sparse(tensor, blank):
    indices = tf.where(tf.not_equal(tensor, blank))
    values = tf.gather_nd(tensor, indices)
    shape = tf.shape(tensor, out_type=tf.int64)
    return tf.SparseTensor(indices, values, shape)


def edit_distance(labels, logits, sequence_lengths, normalize, name=
    'edit_distance'):
    predictions = tf.nn.ctc_greedy_decoder(inputs=tf.transpose(logits, [1, 
        0, 2]), sequence_length=tf.cast(sequence_lengths, tf.int32),
        merge_repeated=False)[0][0]
    labels = dense_to_sparse(labels, logits.shape[-1] - 1)
    return tf.reduce_mean(tf.edit_distance(hypothesis=tf.cast(predictions,
        tf.int32), truth=tf.cast(labels, tf.int32), normalize=normalize),
        name=name)

import tensorflow as tf


def dense_to_sparse(tensor, blank):

    indices = tf.where(tf.not_equal(tensor, blank))
    values = tf.gather_nd(tensor, indices)
    shape = tf.shape(tensor, out_type=tf.int64)

    return tf.SparseTensor(indices, values, shape)


def edit_distance(labels, logits, sequence_lengths, normalize, name="edit_distance"):

    predictions = tf.nn.ctc_greedy_decoder(
        inputs=tf.transpose(logits, [1, 0, 2]),
        sequence_length=tf.cast(sequence_lengths, tf.int32),
        merge_repeated=False
    )[0][0]

    labels = dense_to_sparse(labels, logits.shape[-1] - 1)

    return tf.reduce_mean(tf.edit_distance(
        hypothesis=tf.cast(predictions, tf.int32),
        truth=tf.cast(labels, tf.int32),
        normalize=normalize
    ), name=name)
