-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
29 lines (22 loc) · 1.02 KB
/
metrics.py
File metadata and controls
29 lines (22 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import math
import os
import re
import numpy as np
def loss_metric(nll, target_length, **kwargs):
return nll / target_length
def nll_loss(nll, target_length, **kwargs):
return nll
def perplexity(nll, target_length, **kwargs):
return math.exp(loss_metric(nll, target_length))
def bleu(real_target_sentence, estimated_target_sentence, **kwargs):
with open('./Output.txt', 'w') as output_file, open('./Reference.txt', 'w') as reference_file:
output_file.write(estimated_target_sentence)
reference_file.write(real_target_sentence)
value = os.popen(
'perl ./multi_bleu.pl ./Reference.txt < ./Output.txt').read()
# value format : BLEU = 77.88, 100.0/100.0/100.0/100.0 (BP=0.779, ratio=0.800, hyp_len=4, ref_len=5)
# extract five BLEU descriptors and return with a tensor to permit of
# using the same sum, division, append operations which are defined with
# the other metrics
return np.array([float(x)
for x in re.findall(r"[0-9]+[.]?[0-9]*", value)[:5]])