forked from sordonia/hed-dlg
-
Notifications
You must be signed in to change notification settings - Fork 43
/
compute_dialogue_embeddings.py
171 lines (129 loc) · 5.51 KB
/
compute_dialogue_embeddings.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
"""
This script computes dialogue embeddings for dialogues found in a text file.
"""
#!/usr/bin/env python
import argparse
import cPickle
import traceback
import logging
import time
import sys
import math
import os
import numpy
import codecs
import search
import utils
from dialog_encdec import DialogEncoderDecoder
from numpy_compat import argpartition
from state import prototype_state
logger = logging.getLogger(__name__)
class Timer(object):
def __init__(self):
self.total = 0
def start(self):
self.start_time = time.time()
def finish(self):
self.total += time.time() - self.start_time
def parse_args():
parser = argparse.ArgumentParser("Compute dialogue embeddings from model")
parser.add_argument("model_prefix",
help="Path to the model prefix (without _model.npz or _state.pkl)")
parser.add_argument("dialogues",
help="File of input dialogues (tab separated)")
parser.add_argument("output",
help="Output file")
parser.add_argument("--verbose",
action="store_true", default=False,
help="Be verbose")
parser.add_argument("--use-second-last-state",
action="store_true", default=False,
help="Outputs the second last dialogue encoder state instead of the last one")
return parser.parse_args()
def compute_encodings(joined_contexts, model, model_compute_encoding, output_second_last_state = False):
context = numpy.zeros((model.seqlen, len(joined_contexts)), dtype='int32')
context_lengths = numpy.zeros(len(joined_contexts), dtype='int32')
for idx in range(len(joined_contexts)):
context_lengths[idx] = len(joined_contexts[idx])
if context_lengths[idx] < model.seqlen:
context[:context_lengths[idx], idx] = joined_contexts[idx]
else:
# If context is longer tha max context, truncate it and force the end-of-utterance token at the end
context[:model.seqlen, idx] = joined_contexts[idx][0:model.seqlen]
context[model.seqlen-1, idx] = model.eos_sym
context_lengths[idx] = model.seqlen
n_samples = len(joined_contexts)
# Generate the reversed context
reversed_context = numpy.copy(context)
for idx in range(context.shape[1]):
eos_indices = numpy.where(context[:, idx] == model.eos_sym)[0]
prev_eos_index = -1
for eos_index in eos_indices:
reversed_context[(prev_eos_index+2):eos_index, idx] = (reversed_context[(prev_eos_index+2):eos_index, idx])[::-1]
prev_eos_index = eos_index
# Recompute hs only for those particular sentences
# that met the end-of-sentence token
encoder_states = model_compute_encoding(context, reversed_context, model.seqlen)
hs = encoder_states[1]
if output_second_last_state:
second_last_hidden_state = numpy.zeros((hs.shape[1], hs.shape[2]), dtype='float64')
for i in range(hs.shape[1]):
second_last_hidden_state[i, :] = hs[context_lengths[i] - 1, i, :]
return second_last_hidden_state
else:
return hs[-1, :, :]
def main():
args = parse_args()
state = prototype_state()
state_path = args.model_prefix + "_state.pkl"
model_path = args.model_prefix + "_model.npz"
with open(state_path) as src:
state.update(cPickle.load(src))
logging.basicConfig(level=getattr(logging, state['level']), format="%(asctime)s: %(name)s: %(levelname)s: %(message)s")
model = DialogEncoderDecoder(state)
if os.path.isfile(model_path):
logger.debug("Loading previous model")
model.load(model_path)
else:
raise Exception("Must specify a valid model path")
contexts = [[]]
lines = open(args.dialogues, "r").readlines()
if len(lines):
contexts = [x.strip().split('\t') for x in lines]
model_compute_encoding = model.build_encoder_function()
dialogue_encodings = []
# Start loop
joined_contexts = []
batch_index = 0
batch_total = int(math.ceil(float(len(contexts)) / float(model.bs)))
for context_id, context_sentences in enumerate(contexts):
# Convert contextes into list of ids
joined_context = []
if len(context_sentences) == 0:
joined_context = [model.eos_sym]
else:
for sentence in context_sentences:
sentence_ids = model.words_to_indices(sentence.split())
# Add sos and eos tokens
joined_context += [model.sos_sym] + sentence_ids + [model.eos_sym]
# HACK
for i in range(0, 50):
joined_context += [model.sos_sym] + [0] + [model.eos_sym]
joined_contexts.append(joined_context)
if len(joined_contexts) == model.bs:
batch_index = batch_index + 1
logger.debug("[COMPUTE] - Got batch %d / %d" % (batch_index, batch_total))
encs = compute_encodings(joined_contexts, model, model_compute_encoding, args.use_second_last_state)
for i in range(len(encs)):
dialogue_encodings.append(encs[i])
joined_contexts = []
if len(joined_contexts) > 0:
logger.debug("[COMPUTE] - Got batch %d / %d" % (batch_total, batch_total))
encs = compute_encodings(joined_contexts, model, model_compute_encoding, args.use_second_last_state)
for i in range(len(encs)):
dialogue_encodings.append(encs[i])
# Save encodings to disc
cPickle.dump(dialogue_encodings, open(args.output + '.pkl', 'w'))
if __name__ == "__main__":
main()