-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.py
394 lines (304 loc) · 12.8 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 1 09:47:54 2020
@author: HQ Xie
utils.py
"""
import os
import math
import torch
import time
import torch.nn as nn
import numpy as np
from w3lib.html import remove_tags
from nltk.translate.bleu_score import sentence_bleu
from models.mutual_info import sample_batch, mutual_information
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))
# 按照index将input重新排列
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
# 第一行加入了<strat> 符号,不需要加入计算
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 NoamOpt:
"Optim wrapper that implements rate."
def __init__(self, model_size, factor, warmup, optimizer):
self.optimizer = optimizer
self._step = 0
self.warmup = warmup
self.factor = factor
self.model_size = model_size
self._rate = 0
self._weight_decay = 0
def step(self):
"Update parameters and rate"
self._step += 1
rate = self.rate()
weight_decay = self.weight_decay()
for p in self.optimizer.param_groups:
p['lr'] = rate
p['weight_decay'] = weight_decay
self._rate = rate
self._weight_decay = weight_decay
# update weights
self.optimizer.step()
def rate(self, step = None):
"Implement `lrate` above"
if step is None:
step = self._step
# if step <= 3000 :
# lr = 1e-3
# if step > 3000 and step <=9000:
# lr = 1e-4
# if step>9000:
# lr = 1e-5
lr = self.factor * \
(self.model_size ** (-0.5) *
min(step ** (-0.5), step * self.warmup ** (-1.5)))
return lr
# return lr
def weight_decay(self, step = None):
"Implement `lrate` above"
if step is None:
step = self._step
if step <= 3000 :
weight_decay = 1e-3
if step > 3000 and step <=9000:
weight_decay = 0.0005
if step>9000:
weight_decay = 1e-4
weight_decay = 0
return weight_decay
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)
class Channels():
def AWGN(self, Tx_sig, n_var):
Rx_sig = Tx_sig + torch.normal(0, n_var, size=Tx_sig.shape).to(device)
return Rx_sig
def Rayleigh(self, Tx_sig, n_var):
shape = Tx_sig.shape
H_real = torch.normal(0, math.sqrt(1/2), size=[1]).to(device)
H_imag = torch.normal(0, math.sqrt(1/2), size=[1]).to(device)
H = torch.Tensor([[H_real, -H_imag], [H_imag, H_real]]).to(device)
Tx_sig = torch.matmul(Tx_sig.view(shape[0], -1, 2), H)
Rx_sig = self.AWGN(Tx_sig, n_var)
# Channel estimation
Rx_sig = torch.matmul(Rx_sig, torch.inverse(H)).view(shape)
return Rx_sig
def Rician(self, Tx_sig, n_var, K=1):
shape = Tx_sig.shape
mean = math.sqrt(K / (K + 1))
std = math.sqrt(1 / (K + 1))
H_real = torch.normal(mean, std, size=[1]).to(device)
H_imag = torch.normal(mean, std, size=[1]).to(device)
H = torch.Tensor([[H_real, -H_imag], [H_imag, H_real]]).to(device)
Tx_sig = torch.matmul(Tx_sig.view(shape[0], -1, 2), H)
Rx_sig = self.AWGN(Tx_sig, n_var)
# Channel estimation
Rx_sig = torch.matmul(Rx_sig, torch.inverse(H)).view(shape)
return Rx_sig
def initNetParams(model):
'''Init net parameters.'''
for p in model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
return model
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 SNR_to_noise(snr):
snr = 10 ** (snr / 10)
noise_std = 1 / np.sqrt(2 * snr)
return noise_std
def train_step(model, src, trg, n_var, pad, opt, criterion, channel, mi_net=None):
model.train()
trg_inp = trg[:, :-1]
trg_real = trg[:, 1:]
channels = Channels()
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)
if channel == 'AWGN':
Rx_sig = channels.AWGN(Tx_sig, n_var)
elif channel == 'Rayleigh':
Rx_sig = channels.Rayleigh(Tx_sig, n_var)
elif channel == 'Rician':
Rx_sig = channels.Rician(Tx_sig, n_var)
else:
raise ValueError("Please choose from AWGN, Rayleigh, and Rician")
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))
loss = loss_function(pred.contiguous().view(-1, ntokens),
trg_real.contiguous().view(-1),
pad, criterion)
if mi_net is not None:
mi_net.eval()
joint, marginal = sample_batch(Tx_sig, Rx_sig)
mi_lb, _, _ = mutual_information(joint, marginal, mi_net)
loss_mine = -mi_lb
loss = loss + 0.0009 * loss_mine
# loss = loss_function(pred, trg_real, pad)
loss.backward()
opt.step()
return loss.item()
def train_mi(model, mi_net, src, n_var, padding_idx, opt, channel):
mi_net.train()
opt.zero_grad()
channels = Channels()
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)
if channel == 'AWGN':
Rx_sig = channels.AWGN(Tx_sig, n_var)
elif channel == 'Rayleigh':
Rx_sig = channels.Rayleigh(Tx_sig, n_var)
elif channel == 'Rician':
Rx_sig = channels.Rician(Tx_sig, n_var)
else:
raise ValueError("Please choose from AWGN, Rayleigh, and Rician")
joint, marginal = sample_batch(Tx_sig, Rx_sig)
mi_lb, _, _ = mutual_information(joint, marginal, mi_net)
loss_mine = -mi_lb
loss_mine.backward()
torch.nn.utils.clip_grad_norm_(mi_net.parameters(), 10.0)
opt.step()
return loss_mine.item()
def val_step(model, src, trg, n_var, pad, criterion, channel):
channels = Channels()
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)
if channel == 'AWGN':
Rx_sig = channels.AWGN(Tx_sig, n_var)
elif channel == 'Rayleigh':
Rx_sig = channels.Rayleigh(Tx_sig, n_var)
elif channel == 'Rician':
Rx_sig = channels.Rician(Tx_sig, n_var)
else:
raise ValueError("Please choose from AWGN, Rayleigh, and Rician")
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)
loss = loss_function(pred.contiguous().view(-1, ntokens),
trg_real.contiguous().view(-1),
pad, criterion)
# loss = loss_function(pred, trg_real, pad)
return loss.item()
def greedy_decode(model, src, n_var, max_len, padding_idx, start_symbol, channel):
"""
这里采用贪婪解码器,如果需要更好的性能情况下,可以使用beam search decode
"""
# create src_mask
channels = Channels()
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)
if channel == 'AWGN':
Rx_sig = channels.AWGN(Tx_sig, n_var)
elif channel == 'Rayleigh':
Rx_sig = channels.Rayleigh(Tx_sig, n_var)
elif channel == 'Rician':
Rx_sig = channels.Rician(Tx_sig, n_var)
else:
raise ValueError("Please choose from AWGN, Rayleigh, and Rician")
#channel_enc_output = model.blind_csi(channel_enc_output)
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