-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
240 lines (190 loc) · 8.36 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import torch
import torch.nn as nn
import numpy as np
from w3lib.html import remove_tags
from nltk.translate.bleu_score import sentence_bleu
#from torchviz import make_dot
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class BleuScore():
def __init__(self, w1, w2, w3, w4):
self.w1 = w1 # 1-gram weights
self.w2 = w2 # 2-grams weights
self.w3 = w3 # 3-grams weights
self.w4 = w4 # 4-grams weights
def compute_blue_score(self, real, predicted):
score = []
for (sent1, sent2) in zip(real, predicted):
sent1 = remove_tags(sent1).split()
sent2 = remove_tags(sent2).split()
score.append(sentence_bleu([sent1], sent2,
weights=(self.w1, self.w2, self.w3, self.w4)))
return score
class LabelSmoothing(nn.Module):
"Implement label smoothing."
def __init__(self, size, padding_idx, smoothing=0.0):
super(LabelSmoothing, self).__init__()
self.criterion = nn.CrossEntropyLoss()
self.padding_idx = padding_idx
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.size = size
self.true_dist = None
def forward(self, x, target):
assert x.size(1) == self.size
true_dist = x.data.clone()
true_dist.fill_(self.smoothing / (self.size - 2))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
true_dist[:, self.padding_idx] = 0 #
mask = torch.nonzero(target.data == self.padding_idx)
if mask.dim() > 0:
true_dist.index_fill_(0, mask.squeeze(), 0.0)
self.true_dist = true_dist
return self.criterion(x, true_dist)
class SeqtoText:
def __init__(self, vocb_dictionary, end_idx):
self.reverse_word_map = dict(zip(vocb_dictionary.values(), vocb_dictionary.keys()))
self.end_idx = end_idx
def sequence_to_text(self, list_of_indices):
# Looking up words in dictionary
words = []
for idx in list_of_indices:
if idx == self.end_idx:
break
else:
words.append(self.reverse_word_map.get(idx))
words = ' '.join(words)
return(words)
def subsequent_mask(size):
"Mask out subsequent positions."
attn_shape = (1, size, size)
subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
return torch.from_numpy(subsequent_mask)
def create_masks(src, trg, padding_idx):
src_mask = (src == padding_idx).unsqueeze(-2).type(torch.FloatTensor) #[batch, 1, seq_len]
trg_mask = (trg == padding_idx).unsqueeze(-2).type(torch.FloatTensor) #[batch, 1, seq_len]
look_ahead_mask = subsequent_mask(trg.size(-1)).type_as(trg_mask.data)
combined_mask = torch.max(trg_mask, look_ahead_mask)
return src_mask.to(device), combined_mask.to(device)
def loss_function(x, trg, padding_idx, criterion):
loss = criterion(x, trg)
mask = (trg != padding_idx).type_as(loss.data)
# a = mask.cpu().numpy()
loss *= mask
return loss.mean()
def PowerNormalize(x):
x_square = torch.mul(x, x)
power = torch.mean(x_square).sqrt()
if power > 1:
x = torch.div(x, power)
return x
def train_step(model,epoch,warm_start, src, trg, pad, opt, criterion):
model.train()
trg_inp = trg[:, :-1]
trg_real = trg[:, 1:]
opt.zero_grad()
src_mask, look_ahead_mask = create_masks(src, trg_inp, pad)
enc_output = model.encoder(src, src_mask)
channel_enc_output = model.channel_encoder(enc_output)
Tx_sig = PowerNormalize(channel_enc_output)
pd=np.random.uniform(0,0.04)
ps =np.random.uniform(0,0.05)
#print(Tx_sig.shape)
if epoch >warm_start:
Rx_sig,commit_loss,gru_out = model.vqlayer(Tx_sig,pd,ps)
else:
Rx_sig = Tx_sig
commit_loss = torch.tensor(0)
#for param in model.vqlayer.parameters():
# print(param.shape)
#print(Rx_sig.shape)
channel_dec_output = model.channel_decoder(Rx_sig)
dec_output = model.decoder(trg_inp, channel_dec_output, look_ahead_mask, src_mask)
pred = model.dense(dec_output)
# pred = model(src, trg_inp, src_mask, look_ahead_mask, n_var)
ntokens = pred.size(-1)
#y_est = x + torch.matmul(n, torch.inverse(H))
#loss1 = torch.mean(torch.pow((x_est - y_est.view(x_est.shape)), 2))
mse_loss = nn.MSELoss()
if epoch >warm_start:
gru_labels = Tx_sig.detach()
gru_loss = mse_loss(gru_out,gru_labels).detach()
#gru_loss=torch.tensor(0)
else:
gru_loss = torch.tensor(0)
ce_loss = loss_function(pred.contiguous().view(-1, ntokens),
trg_real.contiguous().view(-1),
pad, criterion)
loss = ce_loss + commit_loss + 0.1*gru_loss
# loss = loss_function(pred, trg_real, pad)
loss.backward()
#graph = make_dot(loss, params=dict(model.named_parameters()))
# Render the graph
#graph.render("computational_graph_gru.png", format="png")
#for name, param in model.named_parameters():
# if param.grad is None:
# print(f"No gradient for {name}")
# else:
# print(f"Gradient for {name}: {param.grad.norm()}")
opt.step()
return ce_loss.item(),commit_loss.item(),gru_loss.item()
def val_step(model,epoch,warm_start, src, trg, pad, criterion):
trg_inp = trg[:, :-1]
trg_real = trg[:, 1:]
src_mask, look_ahead_mask = create_masks(src, trg_inp, pad)
enc_output = model.encoder(src, src_mask)
channel_enc_output = model.channel_encoder(enc_output)
Tx_sig = PowerNormalize(channel_enc_output)
pd=0.00
ps=0.03
if epoch >warm_start:
Rx_sig,commit_loss,gru_out = model.vqlayer(Tx_sig,pd,ps)
else:
Rx_sig = Tx_sig
commit_loss = torch.tensor(0)
channel_dec_output = model.channel_decoder(Rx_sig)
dec_output = model.decoder(trg_inp, channel_dec_output, look_ahead_mask, src_mask)
pred = model.dense(dec_output)
# pred = model(src, trg_inp, src_mask, look_ahead_mask, n_var)
ntokens = pred.size(-1)
mse_loss = nn.MSELoss()
if epoch >warm_start:
gru_labels = Tx_sig.detach()
gru_loss = mse_loss(gru_out,gru_labels).detach()
else:
gru_loss = torch.tensor(0)
ce_loss = loss_function(pred.contiguous().view(-1, ntokens),
trg_real.contiguous().view(-1),
pad, criterion)
loss = ce_loss + commit_loss + 0.1*gru_loss
# loss = loss_function(pred, trg_real, pad)
return ce_loss.item(),commit_loss.item(),gru_loss.item()
def greedy_decode(model, src, pd,ps, max_len, padding_idx, start_symbol, channel):
"""
这里采用贪婪解码器,如果需要更好的性能情况下,可以使用beam search decode
"""
src_mask = (src == padding_idx).unsqueeze(-2).type(torch.FloatTensor).to(device) #[batch, 1, seq_len]
enc_output = model.encoder(src, src_mask)
channel_enc_output = model.channel_encoder(enc_output)
Tx_sig = PowerNormalize(channel_enc_output)
Rx_sig,commit_loss,_ = model.vqlayer(Tx_sig,pd,ps)
memory = model.channel_decoder(Rx_sig)
outputs = torch.ones(src.size(0), 1).fill_(start_symbol).type_as(src.data)
for i in range(max_len - 1):
# create the decode mask
trg_mask = (outputs == padding_idx).unsqueeze(-2).type(torch.FloatTensor) #[batch, 1, seq_len]
look_ahead_mask = subsequent_mask(outputs.size(1)).type(torch.FloatTensor)
# print(look_ahead_mask)
combined_mask = torch.max(trg_mask, look_ahead_mask)
combined_mask = combined_mask.to(device)
# decode the received signal
dec_output = model.decoder(outputs, memory, combined_mask, None)
pred = model.dense(dec_output)
# predict the word
prob = pred[: ,-1:, :] # (batch_size, 1, vocab_size)
#prob = prob.squeeze()
# return the max-prob index
_, next_word = torch.max(prob, dim = -1)
#next_word = next_word.unsqueeze(1)
#next_word = next_word.data[0]
outputs = torch.cat([outputs, next_word], dim=1)
return outputs