Skip to content

Instantly share code, notes, and snippets.

@marcoleewow
Last active December 5, 2017 09:49
Show Gist options
  • Save marcoleewow/037fc43733260300083aab0db2d7d9a8 to your computer and use it in GitHub Desktop.
Save marcoleewow/037fc43733260300083aab0db2d7d9a8 to your computer and use it in GitHub Desktop.
import numpy as np
import tensorflow as tf
def CTCLossExactPathTest():
BIG_NEG_NUMBER = -100
logit = np.array([[BIG_NEG_NUMBER, 0., BIG_NEG_NUMBER, BIG_NEG_NUMBER],
[0., BIG_NEG_NUMBER, BIG_NEG_NUMBER, BIG_NEG_NUMBER],
[BIG_NEG_NUMBER, BIG_NEG_NUMBER, 0., BIG_NEG_NUMBER]])
"""
Take softmax will give us:
softmax = [[0., 1., 0., 0.],
[1., 0., 0., 0.],
[0., 0., 1., 0.]]
"""
logit = tf.constant(logit, dtype='float32')
# label = [1, 0, 2]
# target = tf.SparseTensor(indices=[[0, 0], [0, 2]], values=[1, 2], dense_shape=[1, 3]) # this line gave error!
target = tf.SparseTensor(indices=[[0, 0], [0, 1], [0, 2]], values=[1, 0, 2], dense_shape=[1, 3])
seq = tf.constant(np.array([3]), dtype='int32')
logit = tf.expand_dims(logit, axis=1)
# CTC loss
loss = tf.nn.ctc_loss(target, logit, seq)
# run session
with tf.Session() as sess:
prob = sess.run([loss])
print('Tensorflow CTC calculated negative log prob = %.4f' % prob[0])
# neg log probability should give us 0.
assert prob[0] == 0.
CTCLossExactPathTest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment