forked from eric-xw/Video-guided-Machine-Translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
187 lines (157 loc) · 6.32 KB
/
utils.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import sys
import re
import string
import json
import time
from collections import Counter
import numpy as np
import logging
# padding, unknown word, end of sentence
base_vocab = ['<PAD>', '<UNK>', '<SOS>', '<EOS>']
padding_idx = base_vocab.index('<PAD>')
sos_idx = base_vocab.index('<SOS>')
eos_idx = base_vocab.index('<EOS>')
def set_logger(log_path):
"""
Set the logger to log info in terminal and file `log_path`.
Example:
```
logging.info("Starting training...")
```
Args:
log_path: (string) where to log
"""
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if not logger.handlers:
# Logging to a file
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(file_handler)
# Logging to console
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(stream_handler)
def clip_gradient(optimizer, grad_clip):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def adjust_learning_rate(optimizer, shrink_factor):
"""
Shrinks learning rate by a specified factor.
:param optimizer: optimizer whose learning rate must be shrunk.
:param shrink_factor: factor in interval (0, 1) to multiply learning rate with.
"""
print("\nDECAYING learning rate.")
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * shrink_factor
print("The new learning rate is %f\n" % (optimizer.param_groups[0]['lr'],))
### Build vocabulary, encode sentences
class Tokenizer(object):
''' Class to tokenize and encode a sentence. '''
SENTENCE_SPLIT_REGEX = re.compile(r'(\W+)') # Split on any non-alphanumeric character
def __init__(self, language, vocab=None, encoding_length=30):
self.language = language
self.encoding_length = encoding_length
self.vocab = vocab
self.word_to_index = {}
if vocab:
for i,word in enumerate(vocab):
self.word_to_index[word] = i
def split_sentence(self, sentence):
if self.language=='en':
return self.split_sentence_en(sentence)
elif self.language=='zh':
return self.split_sentence_zh(sentence)
def split_sentence_en(self, sentence):
''' Break sentence into a list of words and punctuation -- English '''
toks = []
for word in [s.strip().lower() for s in self.SENTENCE_SPLIT_REGEX.split(sentence.strip()) if len(s.strip()) > 0]:
# Break up any words containing punctuation only, e.g. '!?', unless it is multiple full stops e.g. '..'
if all(c in string.punctuation for c in word) and not all(c in '.' for c in word):
toks += list(word)
else:
toks.append(word)
return toks
def split_sentence_zh(self, sentence):
''' Break sentence into a list of characters -- Chinese '''
toks = []
for char in sentence.strip():
toks.append(char)
return toks
def encode_sentence(self, sentence):
if len(self.word_to_index) == 0:
sys.exit('Tokenizer has no vocab')
encoding = []
for word in self.split_sentence(sentence): # reverse input sentences
if word in self.word_to_index:
encoding.append(self.word_to_index[word])
else:
encoding.append(self.word_to_index['<UNK>'])
## cut words first since <EOS> should always be included in the end.
if len(encoding) > self.encoding_length-2:
encoding = encoding[:self.encoding_length-2]
## add <SOS> and <EOS>
encoding = [self.word_to_index['<SOS>'], *encoding, self.word_to_index['<EOS>']]
length = min(self.encoding_length, len(encoding))
if len(encoding) < self.encoding_length:
encoding += [self.word_to_index['<PAD>']] * (self.encoding_length-len(encoding))
return np.array(encoding[:self.encoding_length]), length
def encode_sentence_nopad_2str(self, sentence):
'''Encode a sentence without <SOS> and padding '''
if len(self.word_to_index) == 0:
sys.exit('Tokenizer has no vocab')
encoding = []
for word in self.split_sentence(sentence): # reverse input sentences
if word in self.word_to_index:
encoding.append(self.word_to_index[word])
else:
encoding.append(999999)
string = ' '.join([str(i) for i in np.array(encoding)])
return string # exclude <SOS>
def decode_sentence(self, encoding):
sentence = []
for ix in encoding:
if ix == self.word_to_index['<PAD>']:
break
else:
if ix >= len(self.vocab):
sentence.append('<UNK>')
else:
sentence.append(self.vocab[ix])
return " ".join(sentence) # unreverse before output
def build_vocab(data_dir, language, min_count=5, start_vocab=base_vocab):
''' Build a vocab, starting with base vocab containing a few useful tokens. '''
assert language in ['en', 'zh']
count = Counter()
t = Tokenizer(language)
with open(data_dir+'vatex_training_v1.0.json', 'r') as file:
data = json.load(file)
lan2cap={'en':'enCap', 'zh':'chCap'}
for d in data:
for cap in d[lan2cap[language]]:
count.update(t.split_sentence(cap))
vocab = list(start_vocab)
for word,num in count.most_common():
if num >= min_count:
vocab.append(word)
else:
break
return vocab
def write_vocab(vocab, path):
print ('Writing vocab of size %d to %s' % (len(vocab),path))
with open(path, 'w') as f:
for word in vocab:
f.write("%s\n" % word)
def read_vocab(path):
vocab = []
with open(path) as f:
vocab = [word.strip() for word in f.readlines()]
return vocab