-
Notifications
You must be signed in to change notification settings - Fork 52
/
utils.py
76 lines (59 loc) · 2.38 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
__author__ = 'yihanjiang'
import torch
import numpy as np
import math
def errors_ber(y_true, y_pred, positions = 'default'):
y_true = y_true.view(y_true.shape[0], -1, 1)
y_pred = y_pred.view(y_pred.shape[0], -1, 1)
myOtherTensor = torch.ne(torch.round(y_true), torch.round(y_pred)).float()
if positions == 'default':
res = sum(sum(myOtherTensor))/(myOtherTensor.shape[0]*myOtherTensor.shape[1])
else:
res = torch.mean(myOtherTensor, dim=0).type(torch.FloatTensor)
for pos in positions:
res[pos] = 0.0
res = torch.mean(res)
return res
def errors_ber_list(y_true, y_pred):
block_len = y_true.shape[1]
y_true = y_true.view(y_true.shape[0], -1)
y_pred = y_pred.view(y_pred.shape[0], -1)
myOtherTensor = torch.ne(torch.round(y_true), torch.round(y_pred))
res_list_tensor = torch.sum(myOtherTensor, dim = 1).type(torch.FloatTensor)/block_len
return res_list_tensor
def errors_ber_pos(y_true, y_pred, discard_pos = []):
y_true = y_true.view(y_true.shape[0], -1, 1)
y_pred = y_pred.view(y_pred.shape[0], -1, 1)
myOtherTensor = torch.ne(torch.round(y_true), torch.round(y_pred)).float()
tmp = myOtherTensor.sum(0)/myOtherTensor.shape[0]
res = tmp.squeeze(1)
return res
def code_power(the_codes):
the_codes = the_codes.cpu().numpy()
the_codes = np.abs(the_codes)**2
the_codes = the_codes.sum(2)/the_codes.shape[2]
tmp = the_codes.sum(0)/the_codes.shape[0]
res = tmp
return res
def errors_bler(y_true, y_pred, positions = 'default'):
y_true = y_true.view(y_true.shape[0], -1, 1)
y_pred = y_pred.view(y_pred.shape[0], -1, 1)
decoded_bits = torch.round(y_pred)
X_test = torch.round(y_true)
tp0 = (abs(decoded_bits-X_test)).view([X_test.shape[0],X_test.shape[1]])
tp0 = tp0.cpu().numpy()
if positions == 'default':
bler_err_rate = sum(np.sum(tp0,axis=1)>0)*1.0/(X_test.shape[0])
else:
for pos in positions:
tp0[:, pos] = 0.0
bler_err_rate = sum(np.sum(tp0,axis=1)>0)*1.0/(X_test.shape[0])
return bler_err_rate
# note there are a few definitions of SNR. In our result, we stick to the following SNR setup.
def snr_db2sigma(train_snr):
return 10**(-train_snr*1.0/20)
def snr_sigma2db(train_sigma):
try:
return -20.0 * math.log(train_sigma, 10)
except:
return -20.0 * torch.log10(train_sigma)