-
Notifications
You must be signed in to change notification settings - Fork 10
/
helper.py
71 lines (57 loc) · 2.04 KB
/
helper.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
import logging
def get_logger(log_file):
logging.basicConfig(level=logging.DEBUG, format='%(message)s', filename=log_file, filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
def log(s):
logging.info(s)
return log
import time
import math
import numpy as np
def asHHMMSS(s):
m = math.floor(s / 60)
s -= m * 60
h = math.floor(m /60)
m -= h *60
return '%d:%d:%d'% (h, m, s)
def timeSince(since, percent):
now = time.time()
s = now - since
es = s / (percent)
rs = es - s
return '%s<%s'%(asHHMMSS(s), asHHMMSS(rs))
PAD_ID, SOS_ID, EOS_ID, UNK_ID = [0, 1, 2, 3]
import torch
from torch.nn import functional as F
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
def gVar(data):
tensor=data
if isinstance(data, np.ndarray):
tensor = torch.from_numpy(data)
return tensor.to(device)
def sequence_mask(seq_len, max_len=None):
'''
Convert sequence lengths to masking vectors
'''
if max_len is None:
max_len = seq_len.data.max()
batch_size = seq_len.size(0)
seq_range = torch.arange(0, max_len, dtype=torch.long, device=seq_len.device)
seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
seq_len_expand = (seq_len.unsqueeze(1).expand_as(seq_range_expand))
return seq_range_expand < seq_len_expand
def reverse_sequence(seqs, seq_lens):
"""
this function takes a torch mini-batch and reverses each sequence(w.r.t. the temporal axis, i.e. axis=1)
in contrast to `reverse_sequences_numpy`, this function plays nice with torch autograd
"""
batch_size, max_seq_len, dim = seqs.size()
rev_seqs = seqs.new_zeros(seqs.size())
for b in range(batch_size):
T = seq_lens[b]
time_slice = torch.arange(T-1, -1, -1, device=seqs.device).type(torch.LongTensor)
rev_seq = torch.index_select(seqs[b, :, :], 0, time_slice)
rev_seqs[b, 0:T.type(torch.LongTensor), :] = rev_seq
return rev_seqs