-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
181 lines (138 loc) · 5.23 KB
/
train.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
import random
import time
from prelude import *
from model import HeuristicNet
import torch
import torch.optim as optim
import torch.nn.functional as F
def gen_level(y, R, s, lv):
cost = np.sum(np.square(y - R @ s))
n = np.copy(s)
n[:-lv] = 0
g = tree_g(y, R, n)
lv_par = lv - 1
if lv_par == 0:
par = np.zeros_like(n)
else:
par = np.copy(n)
par[:-lv_par] = 0
g_par = tree_g(y, R, par)
par_mask = 0 if lv_par == 0 else 1
lv_succ = lv + 1
succ_1 = np.copy(n)
succ_1[-lv_succ] = -1
g_succ_1 = tree_g(y, R, succ_1)
succ_2 = np.copy(n)
succ_2[-lv_succ] = 1
g_succ_2 = tree_g(y, R, succ_2)
succ_mask = 0 if lv_succ == s.size else 1
yield (
np.expand_dims(y, 0),
np.expand_dims(R, 0),
np.reshape(cost, [1, 1]),
np.expand_dims(n, 0),
np.reshape(g, [1, 1]),
np.expand_dims(par, 0),
np.reshape(g_par, [1, 1]),
np.reshape(par_mask, [1, 1]),
np.expand_dims(succ_1, 0),
np.reshape(g_succ_1, [1, 1]),
np.expand_dims(succ_2, 0),
np.reshape(g_succ_2, [1, 1]),
np.reshape(succ_mask, [1, 1]),
)
def gen_trajectory(y, R, s):
data_set = []
for lv in range(1, s.size):
for data in gen_level(y, R, s, lv):
data_set.append(data)
return data_set
def random_data_set(snr_low, snr_high, n_ant: int, batch_size: int):
data_set = []
for i_batch in range(batch_size):
snr = np.random.uniform(low=snr_low, high=snr_high)
p = 10 ** (snr / 10)
H = np.sqrt(p / n_ant) / np.sqrt(2) * complex_channel(n_ant)
Q, R = np.linalg.qr(H)
s = qpsk(random_bits([2 * n_ant, 1]))
w = np.random.randn(2 * n_ant, 1)
y = R @ s + Q.T @ w
data_set.extend(gen_trajectory(y, R, s))
random.shuffle(data_set)
return data_set
def preproccess(data):
data = [*zip(*data)]
y = torch.from_numpy(np.concatenate(data[0])).float().cuda()
R = torch.from_numpy(np.concatenate(data[1])).float().cuda()
cost = torch.from_numpy(np.concatenate(data[2])).float().cuda()
n = torch.from_numpy(np.concatenate(data[3])).float().cuda()
g = torch.from_numpy(np.concatenate(data[4])).float().cuda()
par = torch.from_numpy(np.concatenate(data[5])).float().cuda()
g_par = torch.from_numpy(np.concatenate(data[6])).float().cuda()
par_mask = torch.from_numpy(np.concatenate(data[7])).float().cuda()
succ_1 = torch.from_numpy(np.concatenate(data[8])).float().cuda()
g_succ_1 = torch.from_numpy(np.concatenate(data[9])).float().cuda()
succ_2 = torch.from_numpy(np.concatenate(data[10])).float().cuda()
g_succ_2 = torch.from_numpy(np.concatenate(data[11])).float().cuda()
succ_mask = torch.from_numpy(np.concatenate(data[12])).float().cuda()
return y, R, cost, n, g, par, g_par, par_mask, succ_1, g_succ_1, succ_2, g_succ_2, succ_mask
def compute_loss(model, target_model, train_set):
y = train_set[0]
R = train_set[1]
cost = train_set[2]
s = train_set[3]
g = train_set[4]
par = train_set[5]
g_par = train_set[6]
par_mask = train_set[7]
succ_1 = train_set[8]
g_succ_1 = train_set[9]
succ_2 = train_set[10]
g_succ_2 = train_set[11]
succ_mask = train_set[12]
h = model.forward(y, R, s)
f = g + h
h_par = target_model.forward(y, R, par).detach()
f_par = g_par + h_par
h_succ_1 = target_model.forward(y, R, succ_1).detach()
expected_f_1 = g_succ_1 + h_succ_1 * succ_mask
h_succ_2 = target_model.forward(y, R, succ_2).detach()
expected_f_2 = g_succ_2 + h_succ_2 * succ_mask
expected_f = torch.min(expected_f_1, expected_f_2)
loss = torch.mean((f - expected_f) ** 2 + (f - f_par) ** 2 * par_mask + (f - cost) ** 2)
return loss, torch.mean(cost), torch.mean(f), torch.mean(expected_f)
def run_train(snr_low, snr_high, n_ant, batch_size, batch_count, max_epoch):
model = HeuristicNet(n_ant).cuda()
try:
model.load()
except:
pass
print(model)
target_model = HeuristicNet(n_ant).cuda()
target_model.load_state_dict(model.state_dict())
target_model.eval()
optimizer = optim.Adam(model.parameters())
i_epoch = 0
while i_epoch < max_epoch:
t_start = time.time()
for i_batch in range(batch_count):
data_set = random_data_set(snr_low, snr_high, n_ant, batch_size)
train_set = preproccess(data_set)
loss, a, b, c = compute_loss(model, target_model, train_set)
optimizer.zero_grad()
loss.backward()
optimizer.step()
text = "traning epoch={}".format(i_epoch + 1)
text += " loss={:.4f}".format(loss.item())
text += " {:.2f}/{:.2f}/{:.2f}".format(a.item(), b.item(), c.item())
text += " batch={}/{}".format(i_batch + 1, batch_count)
print(text)
if (i_batch + 1) % 1 == 0:
target_model.load_state_dict(model.state_dict())
t_end = time.time()
t_duration = (t_end - t_start) / 60
print("{:.2f} mins elapsed".format(t_duration))
model.save()
i_epoch += 1
if __name__ == "__main__":
run_train(snr_low=5, snr_high=26, n_ant=16, batch_size=200, batch_count=1000, max_epoch=1000)