-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
145 lines (107 loc) · 5.05 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
import torch
import numpy as np
import torch.nn as nn
def get_final_encoder_states(encoder_outputs, mask, bidirectional=False):
last_word_indices = mask.sum(1).long - 1
batch_size, _, encoder_output_dim = encoder_outputs.size()
expanded_indices = last_word_indices.view(-1, 1, 1).expand(batch_size, 1, encoder_output_dim)
final_encoder_output = encoder_outputs.gather(1, expanded_indices)
final_encoder_output = final_encoder_output.squeeze(1)
if bidirectional:
final_forward_output = final_encoder_output[:, :(encoder_output_dim // 2)]
final_backward_output = encoder_outputs[:, 0, (encoder_output_dim // 2)]
final_encoder_output = torch.cat([final_forward_output, final_backward_output], dim=-1)
return final_encoder_output
def get_mask(lens):
'''
:param lens: list of batch, every item is a int means the length of a sample
:return: [batch, max_seq_len]
'''
max_len = max(lens)
batch_size = len(lens)
seq_range = torch.arange(max_len).long()
seq_range = seq_range.unsqueeze(0).expand(batch_size, max_len)
seq_length = lens.unsqueeze(1).expand(batch_size, max_len)
mask = seq_range < seq_length
return mask.float()
def get_mask_2(lens):
max_len = max(lens)
batch_size = len(lens)
mask = torch.FloatTensor(batch_size, max_len)
mask.fill_(0)
for i, l in enumerate(lens):
mask[i, :l]._fill(1.0)
return mask
def get_char_mask(lens):
'''
:param lens: list of batch, in particularly, every item is a list, and every item in the list means the length of word
:return: mask [batch, max_seq_len, max_word_len]
'''
max_seq_len = torch.max(torch.sum(lens > 0, dim=-1)).long().item()
tensor_len = torch.zeros((lens.size(0), max_seq_len))
#first trunk every len to max_seq_len
for i in range(lens.size(0)):
tensor_len[i] = lens[i,:max_seq_len]
batch_size, seq_len = tensor_len.size()
max_word_len = torch.max(tensor_len).int().item()
seq_range = torch.arange(max_word_len).long()
seq_range = seq_range.view(1,1,max_word_len).expand(batch_size, seq_len, max_word_len)
seq_length = lens.unsqueeze(-1).expand(batch_size, seq_len, max_word_len)
mask = seq_range < seq_length
return mask.float()
def lstm_encoder(sequence, lstm, seq_lens, init_states, is_mask=False, get_final_output=False):
batch_size = sequence.size(0)
seq_lens_value = seq_lens.tolist()
assert len(seq_lens_value) == batch_size
sort_ind = np.argsort(seq_lens_value)[::-1].tolist()
sort_seq_lens = [seq_lens_value[i] for i in sort_ind]
emb_sequence = reorder_sequence(sequence, sort_ind)
init_states = (init_states[0].contiguous(), init_states[1].contiguous())
packed_seq = nn.utils.rnn.pack_padded_sequence(emb_sequence, sort_seq_lens, batch_first=True)
packed_out, final_states = lstm(packed_seq, init_states)
lstm_out, _ = nn.utils.rnn.pad_packed_sequence(packed_out, batch_first=True)
back_map = {ind : i for i, ind in enumerate(sort_ind)}
reorder_ind = [back_map[i] for i in range(len(sort_ind))]
lstm_out = reorder_sequence(lstm_out, reorder_ind)
final_states = reorder_lstm_states(final_states, reorder_ind)
if is_mask:
mask = get_mask(seq_lens) # batch, max_seq_lens
assert lstm_out.size(1) == mask.size(1)
lstm_out *= mask.unsqueeze(-1)
return lstm_out, final_states #[batch, max_seq_lens, hid_dim], ([n_layer, batch, hid_dim], [n_layer, batch, hid_dim])
if get_final_output:
mask = get_mask(seq_lens)
lstm_out = get_final_encoder_states(lstm_out, mask, bidirectional=True)
return lstm_out, final_states #[batch, hid_dim], ([n_layer, batch, hid_dim], [n_layer, batch, hid_dim])
def reorder_sequence(emb_sequence, order):
order = torch.LongTensor(order)
return emb_sequence.index_select(index=order, dim=0)
def reorder_lstm_states(states, order):
assert isinstance(states, tuple)
assert len(states) == 2
assert states[0].size() == states[1].size()
assert len(order) == states[0].size()[1]
order = torch.LongTensor(order)
sorted_states = (states[0].index_select(index=order, dim=1), states[1].index_select(index=order, dim=1))
return sorted_states
def tokens_to_indices(tokens, vocab, tokenizer, max_pieces=512):
wordpiece_ids = [vocab.word2id('[CLS]')]
offset = 1
offsets = []
for token in tokens:
text = token.lower()
token_wordpiece_ids = [vocab.word2id(wordpiece) for wordpiece in tokenizer.tokenize(text)]
if len(wordpiece_ids) + len(token_wordpiece_ids) + 1 <= max_pieces:
offsets.append(offset)
offset += len(token_wordpiece_ids)
wordpiece_ids.extend(token_wordpiece_ids)
else:
break
wordpiece_ids.extend([vocab.word2id('[SEP]')])
return wordpiece_ids, offsets
def save_to_max_nozero_len(input):
batch_size, seq_len = input.size()
max_len = torch.max(torch.sum(input > 0, dim=-1)).long().item()
for i in range(len(batch_size)):
input[i] = input[i, :max_len]
return input, max_len