-
Notifications
You must be signed in to change notification settings - Fork 52
/
ftae_trainer.py
189 lines (122 loc) · 6.6 KB
/
ftae_trainer.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
__author__ = 'yihanjiang'
import torch
import time
import torch.nn.functional as F
eps = 1e-6
from utils import snr_sigma2db, snr_db2sigma, code_power, errors_ber_pos, errors_ber, errors_bler
from loss import customized_loss
from channels import generate_noise
######################################################################################
#
# Trainer, validation, and test for Feedback Block Delay Channel Autoencoder
#
######################################################################################
def ftae_train(epoch, model, optimizer, args, use_cuda = False, verbose = True, mode = 'encoder'):
device = torch.device("cuda" if use_cuda else "cpu")
model.train()
start_time = time.time()
train_loss = 0.0
for batch_idx in range(int(args.num_block/args.batch_size)):
optimizer.zero_grad()
X_train = torch.randint(0, 2, (args.batch_size, args.block_len, args.code_rate_k), dtype=torch.float)
if mode == 'encoder':
fwd_noise = generate_noise(X_train.shape, args, snr_low=args.train_enc_channel_low, snr_high=args.train_enc_channel_high, mode = 'encoder')
else:
fwd_noise = generate_noise(X_train.shape, args, snr_low=args.train_dec_channel_low, snr_high=args.train_dec_channel_high, mode = 'decoder')
fb_noise = generate_noise(X_train.shape, args, snr_low=args.fb_channel_low, snr_high=args.fb_channel_high, mode = 'decoder')
X_train, fwd_noise, fb_noise = X_train.to(device), fwd_noise.to(device), fb_noise.to(device)
output, code = model(X_train, fwd_noise, fb_noise)
output = torch.clamp(output, 0.0, 1.0)
if mode == 'encoder':
loss = customized_loss(output, X_train, args, noise=fwd_noise, code = code)
else:
loss = customized_loss(output, X_train, args, noise=fwd_noise, code = code)
loss.backward()
train_loss += loss.item()
optimizer.step()
end_time = time.time()
train_loss = train_loss /(args.num_block/args.batch_size)
if verbose:
print('====> Epoch: {} Average loss: {:.8f}'.format(epoch, train_loss), \
' running time', str(end_time - start_time))
return train_loss
def ftae_validate(model, optimizer, args, use_cuda = False, verbose = True):
device = torch.device("cuda" if use_cuda else "cpu")
model.eval()
test_bce_loss, test_custom_loss, test_ber= 0.0, 0.0, 0.0
with torch.no_grad():
num_test_batch = int(args.num_block/args.batch_size * args.test_ratio)
for batch_idx in range(num_test_batch):
X_test = torch.randint(0, 2, (args.batch_size, args.block_len, args.code_rate_k), dtype=torch.float)
fwd_noise = generate_noise(X_test.shape, args,
snr_low=args.train_enc_channel_low,
snr_high=args.train_enc_channel_low)
fb_noise = generate_noise(X_test.shape, args,
snr_low=args.fb_channel_low, snr_high=args.fb_channel_high, mode = 'decoder')
X_test, fwd_noise, fb_noise= X_test.to(device), fwd_noise.to(device), fb_noise.to(device)
optimizer.zero_grad()
output, codes = model(X_test, fwd_noise, fb_noise)
output = torch.clamp(output, 0.0, 1.0)
output = output.detach()
X_test = X_test.detach()
test_bce_loss += F.binary_cross_entropy(output, X_test)
test_custom_loss += customized_loss(output, X_test, noise = fwd_noise, args = args, code = codes)
test_ber += errors_ber(output,X_test)
test_bce_loss /= num_test_batch
test_custom_loss /= num_test_batch
test_ber /= num_test_batch
if verbose:
print('====> Test set BCE loss', float(test_bce_loss),
'Custom Loss',float(test_custom_loss),
'with ber ', float(test_ber),
)
report_loss = float(test_bce_loss)
report_ber = float(test_ber)
return report_loss, report_ber
def ftae_test(model, args, use_cuda = False):
device = torch.device("cuda" if use_cuda else "cpu")
model.eval()
# Precomputes Norm Statistics.
if args.precompute_norm_stats:
num_test_batch = int(args.num_block/(args.batch_size)* args.test_ratio)
for batch_idx in range(num_test_batch):
X_test = torch.randint(0, 2, (args.batch_size, args.block_len, args.code_rate_k), dtype=torch.float)
X_test = X_test.to(device)
_ = model.enc(X_test)
print('Pre-computed norm statistics mean ',model.enc.mean_scalar, 'std ', model.enc.std_scalar)
ber_res, bler_res = [], []
snr_interval = (args.snr_test_end - args.snr_test_start)* 1.0 / (args.snr_points-1)
snrs = [snr_interval* item + args.snr_test_start for item in range(args.snr_points)]
print('SNRS', snrs)
sigmas = snrs
for sigma, this_snr in zip(sigmas, snrs):
test_ber, test_bler = .0, .0
with torch.no_grad():
num_test_batch = int(args.num_block/(args.batch_size)* args.test_ratio)
for batch_idx in range(num_test_batch):
X_test = torch.randint(0, 2, (args.batch_size, args.block_len, args.code_rate_k), dtype=torch.float)
fwd_noise = generate_noise(X_test.shape, args, test_sigma=sigma)
fb_noise = generate_noise(X_test.shape, args,
snr_low=args.fb_channel_low, snr_high=args.fb_channel_high, mode = 'decoder')
X_test, fwd_noise, fb_noise= X_test.to(device), fwd_noise.to(device), fb_noise.to(device)
X_hat_test, the_codes = model(X_test, fwd_noise, fb_noise)
test_ber += errors_ber(X_hat_test,X_test)
test_bler += errors_bler(X_hat_test,X_test)
if batch_idx == 0:
test_pos_ber = errors_ber_pos(X_hat_test,X_test)
codes_power = code_power(the_codes)
else:
test_pos_ber += errors_ber_pos(X_hat_test,X_test)
codes_power += code_power(the_codes)
if args.print_pos_power:
print('code power', codes_power/num_test_batch)
if args.print_pos_ber:
print('positional ber', test_pos_ber/num_test_batch)
test_ber /= num_test_batch
test_bler /= num_test_batch
print('Test SNR',this_snr ,'with ber ', float(test_ber), 'with bler', float(test_bler))
ber_res.append(float(test_ber))
bler_res.append( float(test_bler))
print('final results on SNRs ', snrs)
print('BER', ber_res)
print('BLER', bler_res)