Skip to content

Instantly share code, notes, and snippets.

@hbredin
Created March 17, 2015 17:03
Show Gist options
  • Save hbredin/b2738b52beece7571d95 to your computer and use it in GitHub Desktop.
Save hbredin/b2738b52beece7571d95 to your computer and use it in GitHub Desktop.
import simplejson as json
import itertools
LABELS = ['howard', 'leonard', 'non_speech',
'other', 'penny', 'raj', 'sheldon']
def loss(gold, pred, durations=None):
# convert '0' to 'howard', '1' to 'leonard', etc...
gold = [LABELS[int(k)] for k in gold]
pred = [LABELS[int(k)] for k in pred]
# use segment durations if provided
# otherwise artifically set them to 1s (or 250ms, it does not matter)
if durations is None:
durations = [1.] * len(gold)
denominator = 0.
numerator = 0.
for g, p, d in itertools.izip(gold, pred, durations):
# correctly classified as non speech --> no error
if g == 'non_speech' and p == 'non_speech':
continue
# increment denominator with duration of 'speech' segments
if g != 'non_speech':
denominator += d
# increment error duration in case of error
numerator += (g != p) * d
return 100 * numerator / denominator
X = np.load('....X.npy')
durations = X[:, 0]
pred, gold = json.load(open('ep=1.json'))
approxIER = loss(gold, pred, durations=durations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment