-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn_summ_regression.py
145 lines (144 loc) · 4.86 KB
/
rnn_summ_regression.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from rnn_regression import rnn
from xmlparser_multiple import parse_bc3
import numpy
from random import shuffle
from evaluation import divide_data,weightRecall
from python_rouge import rouge
def sorted_index(myList):
return [i[0] for i in sorted(enumerate(myList), key=lambda x:x[1])]
class rnn_summ(object):
def __init__(self,train,test,write_file = None):
f = open('bc3_sentiment_vectors.txt')
vectors = []
index = []
dic = {}
for i in f.readlines():
dic[i.split()[0]] = i.split()[1:]
self.length = len(i.split()[1:])
self.train = train
self.test = test
for i in (self.train+self.test):
for j in i.thread:
for k in j.sentences:
index = '_mail' + str(i.number) + '_' + str(k.index)
#print index
k.feature = dic[index]
self.write_file = write_file
'''
if self.write_file is not None:
self.file = open(self.write_file,'w')
'''
self.train_epoch = 0
def shuffle(self):
shuffle(self.mails)
def init_rnn(self,L1,L2):
if self.write_file is not None:
self.rnn_model = rnn(70,self.length,1,L1,L2,True)
else:
self.rnn_model = rnn(70,self.length,1,L1,L2)
def close_file(self):
if self.file is not None:
self.file.close()
def rnn_train(self,learning_rate):
all_error = 0
self.train_epoch += 1
if self.write_file is not None and self.train_epoch %600 == 0:
f = open(self.write_file + "/epoch_" + str(self.train_epoch),'w')
#self.file.write("train_epoch : "+str(self.train_epoch)+'\n')
for i in self.train:
for j in i.thread:
input_ins = []
label_ins = []
for k in j.sentences:
'''
if k.score == 0:
label_ins.append([0.0])
elif k.score >0 and k.score < 0.35:
label_ins.append([1.0])
elif k.score >0.35 and k.score < 0.7:
label_ins.append([2.0])
else:
label_ins.append([3.0])
'''
label_ins.append([float(k.score)])
input_ins.append(k.feature)
label_array = label_ins
input_ins = input_ins + input_ins
label_ins = label_ins + label_ins
input_ins = numpy.asarray(numpy.float32(input_ins))
label_ins = numpy.asarray(numpy.float32(label_ins))
self.rnn_model.flush_hidden()
error = self.rnn_model.sentence_train(input_ins,label_ins,learning_rate)
if self.train_epoch % 600 == 0 and self.write_file is not None:
self.rnn_model.get_hidden(input_ins)
if self.write_file is not None:
write_string = ""
for i,j in zip(self.rnn_model.hidden_layer,label_array):
for k in i:
write_string += (str(k) +"\t")
write_string += ('\t' + str(j[0]) + '\n')
f.write(write_string)
all_error += error
if self.write_file is not None and self.train_epoch %600 == 0:
f.close()
return all_error
def rnn_test(self):
produce_set = []
for i in self.test:
produce_set.append([])
score_list = []
index_list = []
for j in i.thread:
input_ins = []
label_ins = []
index = []
for k in j.sentences:
input_ins.append(k.feature)
index.append(k.index)
input_ins = input_ins + input_ins
input_ins = numpy.asarray(numpy.float32(input_ins))
softmax_array = self.rnn_model.prob(input_ins)
count = 0
for i in softmax_array[(len(softmax_array)/2) :]:
score = i#(i[1] * 0.33) + (i[2] * 0.66) + (i[3] * 1)
score_list.append(score)
index_list.append(index[count])
count += 1
sorted_index_array = sorted_index(score_list)
sen_length = 0
for j in range(len(index_list)):
if sen_length < float(len(index_list))*0.3:
produce_set[-1].append(index_list[sorted_index_array[len(index_list)-j-1]])
sen_length += 1
else:
break
score = weightRecall(self.test,produce_set)
print score
rouge_eval = rouge(self.test,produce_set)
rouge_score = rouge_eval.eval()['rouge_l_f_score']
print rouge_score
return score,rouge_score
'''
corpus = 'bc3/bc3corpus.1.0/corpus.xml'
annotation = 'bc3/bc3corpus.1.0/annotation.xml'
mails = parse_bc3(corpus,annotation)
tmp = rnn_summ(mails[0:36],mails[36:])
avg_score = 0
avg_rouge = 0
for i in range(1):
tmp.init_rnn(0.01,0)
#tmp.shuffle()
rate = 0.2
for j in range(2500):
#error = tmp.rnn_train(0.2)
if j %100 == 0:
rate = rate*0.9
error = tmp.rnn_train(rate)
if j % 50 == 0:
print error
score,rouge_score = tmp.rnn_test()
avg_score += score
avg_rouge += rouge_score
print "score: " + str(avg_score)
print "rouge: " + str(avg_rouge)
'''