-
Notifications
You must be signed in to change notification settings - Fork 2
/
analyze.py
130 lines (116 loc) · 4.72 KB
/
analyze.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import re
import numpy as np
import os
from src.rl_tools import *
# This function is used to extract test results from train.log
def extract_test_bleu(id):
pat_bleu = re.compile('- BLEU [^ ]+ [^ ]+ : ([^ \n]+)')
pat_bleu1 = re.compile('- BLEU-1 : ([^ \n]+)')
pat_bleu2 = re.compile('- BLEU-2 : ([^ \n]+)')
pat_bleu3 = re.compile('- BLEU-3 : ([^ \n]+)')
pat_bleu4 = re.compile('- BLEU-4 : ([^ \n]+)')
pat_epoch = re.compile('Starting epoch ([^ \n]+) \.\.\.')
pat_ppl = re.compile('- ppl_sw_pm_test -> ([^ \n]+)')
path_train_log = id + '/train.log'
bleu_dic = {}
with open(path_train_log) as f_in:
con = f_in.read()
end = -10
start = con.find('====================== Starting epoch', end+10)
while start != -1:
end = start
start = con.find('====================== Starting epoch', end+10)
sub_con = con[end:start]
try:
epoch = pat_epoch.search(sub_con).group(1)
bleu = pat_bleu.search(sub_con).group(1)
bleu1 = pat_bleu1.search(sub_con).group(1)
bleu2 = pat_bleu2.search(sub_con).group(1)
bleu3 = pat_bleu3.search(sub_con).group(1)
bleu4 = pat_bleu4.search(sub_con).group(1)
ppl = pat_ppl.search(sub_con).group(1)
# print(' ' + id + '-' + epoch + ' & ' + bleu1 + ' & ' + bleu2 + ' & ' + bleu3 + ' & ' + bleu4 + ' & ' + bleu + '\\\\')
# print(' \\hline')
bleu_dic[int(epoch)] = [float(bleu1), float(bleu2), float(bleu3), float(bleu4), float(bleu), float(ppl)]
except:
break
return bleu_dic
def show_bleu_score(model_name, epoches):
bleu_dic = extract_test_bleu(model_name)
bleu_lis = []
bleu_lis1 = []
bleu_lis2 = []
bleu_lis3 = []
bleu_lis4 = []
ppl = []
for i in epoches:
bleu_lis1.append(bleu_dic[i][0])
bleu_lis2.append(bleu_dic[i][1])
bleu_lis3.append(bleu_dic[i][2])
bleu_lis4.append(bleu_dic[i][3])
bleu_lis.append(bleu_dic[i][4])
ppl.append(bleu_dic[i][5])
print(model_name + ' 1-gram bleu\t' + str(np.mean(bleu_lis1)))
print(model_name + ' 2-gram bleu\t' + str(np.mean(bleu_lis2)))
print(model_name + ' 3-gram bleu\t' + str(np.mean(bleu_lis3)))
print(model_name + ' 4-gram bleu\t' + str(np.mean(bleu_lis4)))
print(model_name + '\t\t bleu' + str(np.mean(bleu_lis)))
print(model_name + '\t\t ppl' + str(np.mean(ppl)))
print(model_name + '\t\t bleus' + str(bleu_lis))
print(model_name + '\t\t ppls' + str(ppl))
print('')
def get_inter_ratio(path):
inter_ratio_lis = []
with open(path) as f_in_sw:
sw = f_in_sw.readlines()
with open('./data/data_acc/jueju7_out.vl.txt') as f_in_pm:
pm = f_in_pm.readlines()
for i in range(len(sw)):
sent_sw = sw[i].strip()
sent_pm = pm[i].strip()
inter_ratio = reward_func_ap(sent_sw, sent_pm, n=5)
inter_ratio_lis.append(inter_ratio)
return np.mean(inter_ratio_lis)
def show_inter_ratio(model_name, epoches):
inter_ratio_lis = []
for j in epoches:
path = '../poem-prose_dump/' + model_name + '/hyp' + str(j) + '.pm-sw.valid.txt'
inter_ratio_lis.append(get_inter_ratio(path))
print('intersection_ratio for ' + model_name + ':\t' + str(np.mean(inter_ratio_lis)))
def get_repetition_ratio(path):
with open(path) as f_in_:
lines = f_in_.readlines()
repetition_ratio_lis = []
for i in range(960):
line = lines[i].strip()
repetition_ratio = 1.0 - len(set(line)) / float(len(line))
repetition_ratio_lis.append(repetition_ratio)
return np.mean(repetition_ratio_lis)
def show_repe_ratio(model_name, epoches):
repe_ratio_lis = []
for j in epoches:
path = model_name + '/hyp' + str(j) + '.pm-sw.valid.txt'
repe_ratio_lis.append(get_repetition_ratio(path))
print('repetition_ratio for ' + model_name + ':\t' + str(np.mean(repe_ratio_lis)))
def main():
# base 0-49
# rl10 0-41 + anti copy loss
# rl18 9-37 + anti repetition loss
# rl22 9-54 + anti copy loss & anti repetition loss
epoches = np.array(range(30, 35))
# epoches = np.array(range(25, 30))
model1 = '/mnt/nfs/work1/hongyu/pengshancai/exp/caibase'
model2 = '/mnt/nfs/work1/hongyu/pengshancai/exp/4902580'
model3 = './dumped/test/4948046'
# model3 = 'testlstm/4976233'
model4 = '/mnt/nfs/work1/hongyu/pengshancai/exp/4949781'
# model4 = 'testlstm/4976257'
show_bleu_score(model1, epoches)
show_bleu_score(model2, epoches-8)
show_bleu_score(model3, epoches)
show_bleu_score(model4, epoches)
show_repe_ratio(model1, epoches)
show_repe_ratio(model2, epoches-8)
show_repe_ratio(model3, epoches)
show_repe_ratio(model4, epoches)
main()