-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodels.py
379 lines (306 loc) · 12.6 KB
/
models.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
import torch
import torch.nn as nn
from torch.distributions.normal import Normal
from torch.distributions.multivariate_normal import MultivariateNormal
import math
# Models for tests on paper End-to-End Learning of Communications Systems Without a Channel Model
class Transmitter(nn.Module):
"""
This is going to be the definition of the transmitter.
"""
def __init__(self, m, n, embed_dim=512):
"""
Initialization of the transmitter
Quoting the paper:
The transmitter consists of an MxM embedding with RELU activation functions,
followed by a dense layer of 2N units with linear activations.
This layer outputs 2N reals which are then converted into N complex symbols, and finally normalized
Tensorflow dense = Pytorch Linear layer
Args:
m (int): Transmitter can have up to M different messages. Each of this messages gets encoded
n (int): Length of the encoding
"""
super(Transmitter, self).__init__()
self.n = n
self.transmit = nn.Sequential(
nn.Embedding(num_embeddings=m, embedding_dim=m),
nn.ReLU(),
nn.Linear(in_features=m, out_features=2*n),
)
# # Different architecture tried. Leaving as comment.
# self.transmit = nn.Sequential(
# nn.Embedding(num_embeddings=m, embedding_dim=embed_dim),
# nn.ReLU(),
# nn.Linear(in_features=embed_dim, out_features=m),
# nn.ReLU(),
# nn.Linear(in_features=m, out_features=2*n),
# )
self.normalization = nn.BatchNorm1d(num_features=n)
self.init_weights()
def init_weights(self):
"""
Function to initialize the weights and bias of the linear layers
"""
for m in self.modules():
if type(m) is torch.nn.Linear:
torch.nn.init.normal_(m.weight)
torch.nn.init.zeros_(m.bias)
def forward(self, x):
"""
Forward pass over x
Args:
x of shape (batch_size, 1): Index of the message to pass through the network
Returns:
x of shape (batch_size, n): Messages ready to be transmitted
"""
batch_size = x.shape[0]
x = self.transmit(x)
# Conversion from 2N Real to N complex symbols
x = x.reshape(batch_size, self.n, 2)
x = self.normalization(x)
return x
class Receiver(nn.Module):
"""
This is going to be the definition of the receiver.
"""
def __init__(self, m, n):
"""
Initialization of the receiver
Quoting the paper:
Regarding the receiver, the first layer is a C2R layer which converts
the received N complex symbols into 2N real symbols, while the last layer
is a dense layer of M units with softmax activations which outputs a probability
distribution over M
Tensorflow dense = Pytorch Linear layer
Args:
m (int): Transmitter can have up to M different messages. Each of this messages gets encoded
n (int): Length of the encoding
"""
super(Receiver, self).__init__()
self.n = n
self.receive = nn.Sequential(
nn.Linear(in_features=2*n, out_features=m),
nn.ReLU(),
nn.Linear(in_features=m, out_features=m),
nn.LogSoftmax(dim=1),
)
self.estimate_h = nn.Sequential(
nn.Linear(in_features=2*n, out_features=20),
nn.Tanh(),
nn.Linear(in_features=20, out_features=2),
)
def init_weights(self):
"""
Function to initialize the weights and bias of the linear layers
"""
for m in self.modules():
if type(m) is torch.nn.Linear:
torch.nn.init.normal_(m.weight)
torch.nn.init.zeros_(m.bias)
def forward(self, x, chann_type="AWGN"):
"""
Forward pass over x
Args:
x of shape (batch_size, n, 2): Received messages
Returns:
x of shape (batch_size, m): Probabilities of element i in the batch corresponding to the mth message
"""
batch_size = x.shape[0]
if chann_type == "RBF":
# Conversion from N complex symbols to 2N Real
x_h = x.reshape(batch_size, 2*self.n)
# Pass through the estimate h layer
h_hat = self.estimate_h(x_h)
h_hat = x_h.reshape(batch_size, 1, 2)
# Divide original x by obtained h_hat
x = x/h_hat
# Conversion from N complex symbols to 2N Real
x = x.reshape(batch_size, 2*self.n)
x = self.receive(x)
return x
class Policy(torch.nn.Module):
"""
This is going to be the definition of the policy.
"""
def __init__(self, m, n, sigma_var=0.02):
"""
Initialization of the policy
The action space of the policy are the n bits of the encoding.
The policy has a static variance of sigma_var
Args:
m (int): Transmitter can have up to M different messages. Each of this messages gets encoded
n (int): Length of the encoding
sigma_var (float): Static variance of the policy
"""
super(Policy, self).__init__()
self.state_space = m
self.action_space = n
# Getting the variance and the standard deviation
self.sigma_var = torch.tensor([sigma_var])
self.sigma_std = torch.sqrt(self.sigma_var)
def forward(self, x):
"""
Forward pass over x in the policy
Args:
x of shape (batch_size, n, 2): Output of the transmitter
Returns:
xp of shape (batch_size, n, 2): xp of element i in the batch
xp_logprob of shape (batch_size, n, 2): Log probability of xp over the policy
"""
# Perturbation. Done in the paper to ensure exploration of the policy
# Getting w to first scale x.
w_dist = Normal(torch.tensor([0.0]), self.sigma_std)
w = w_dist.sample(x.shape).to(x.device)
# Get xp
xp = torch.sqrt(1-self.sigma_var).to(x.device).detach()*x + w.squeeze()
# Get the batch shize
batch_size = x.shape[0]
# Reshape all tensors currently in (batch_size, n, 2)
# to easier shape to manage (batch_size, 2*n)
x_dist = x.reshape(batch_size, 2*self.action_space)
xp_dist = xp.reshape(batch_size, 2*self.action_space)
# Log probabilities going to be stored here
xp_logprob_dist = torch.zeros(batch_size, 2*self.action_space).to(x.device)
# Getting the means for all the distributions
policy_means = xp_dist.detach() - torch.sqrt(1-self.sigma_var).to(x.device).detach()*x_dist.detach()
# Covariance matrix is the same for all the distributions
cov_matrix = torch.eye(2*self.action_space).to(x.device) * self.sigma_var.to(x.device)
for i in range(batch_size):
# Get the log probability of each sample in xp
xp_logprob_dist[i, :] = MultivariateNormal(policy_means[i], cov_matrix).log_prob(xp_dist[i])
# Reshape back to (batch_size, n, 2) for standarization
xp_logprob = xp_logprob_dist.reshape(batch_size, self.action_space, 2)
# NB xp does NOT have a gradient. Log probabilities do
return xp, xp_logprob
# ---------------------------------------------------------------------- #
# Models for tests on paper An Introduction to Deep Learning for the Physical Layer
def paper_normalization(x, n):
"""
The paper defines a specific normalization.
Implementing it here to be able to use it Encoder
"""
# Doing the normalization to get the final result of the encoder
normalization_term = math.sqrt(n)/torch.sum(torch.sqrt(x**2), dim=1)
x = x*normalization_term.unsqueeze(1)
return x
class Encoder(nn.Module):
"""
This is going to be the definition of the encoder.
"""
def __init__(self, m, n, embed_dim=512, use_embedding=True, use_paper_norm=False):
"""
The encoder in the paper is implemented with Tensorflow
Layers in Tensorflow:
- Dense + ReLU. Out M
- Alternatively also use Embedding
- Dense + linear. Out n
- Normalization (some normalization defined by the paper) Out n
Layers equivalent in Pytorch:
- Linear + ReLU. Out M
- Alternatively also use Embedding
- Linear + (nothing). Out n
- Normalization (some normalization defined by the paper or Pytorch normalization).
Args:
m (int): Transmitter can have up to M different messages. Each of this messages gets encoded
n (int): Length of the encoding
"""
super(Encoder, self).__init__()
self.n = n
self.use_paper_norm = use_paper_norm
if use_embedding:
# # Different architecture tried. Leaving as comment.
# self.linear_M = nn.Sequential(
# nn.Embedding(num_embeddings=m, embedding_dim=embed_dim),
# nn.ReLU(),
# nn.Linear(in_features=embed_dim, out_features=m),
# nn.ReLU(),
# )
self.linear_M = nn.Sequential(
nn.Embedding(num_embeddings=m, embedding_dim=m),
nn.ReLU(),
)
else:
self.linear_M = nn.Sequential(
nn.Linear(in_features=m, out_features=m),
nn.ReLU(),
)
self.linear_N = nn.Sequential(
nn.Linear(in_features=m, out_features=n),
)
if use_paper_norm:
self.normalization = paper_normalization
else:
self.normalization = nn.BatchNorm1d(num_features=n)
self.init_weights()
def init_weights(self):
"""
Function to initialize the weights and bias of the linear layers
"""
for m in self.modules():
if type(m) is torch.nn.Linear:
torch.nn.init.normal_(m.weight)
torch.nn.init.zeros_(m.bias)
def forward(self, x):
"""
Forward pass over x
Args:
x of shape (batch_size, m): Messages that pass through the autoencoder
Returns:
y of shape (batch_size, n): Messages after encoding+noise+decoding
"""
x = self.linear_M(x)
x = self.linear_N(x.squeeze())
if self.use_paper_norm:
y = self.normalization(x, self.n)
else:
y = self.normalization(x)
return y
class Decoder(nn.Module):
"""
This is going to be the definition of the decoder.
"""
def __init__(self, m, n):
"""
Initialization of the decoder
The decoder in the paper is implemented with Tensorflow
Layers in Tensorflow:
- Dense + ReLU. Out M
- Dense + Softmax. Out M
Layers equivalent in Pytorch:
- Linear + ReLU. Out M
- Linear + LogSoftmax. Out M
LogSoftmax works well with Pytorch function nll_loss. That calculates the Negative Log Likelihood
Args:
m (int): Transmitter can have up to M different messages. Each of this messages gets encoded
n (int): Length of the encoding
"""
super(Decoder, self).__init__()
self.linear_relu = nn.Sequential(
nn.Linear(in_features=n, out_features=m),
nn.ReLU(),
)
self.linear_out = nn.Sequential(
nn.Linear(in_features=m, out_features=m),
nn.LogSoftmax(dim=1),
)
self.init_weights()
def init_weights(self):
"""
Function to initialize the weights and bias of the linear layers
"""
for m in self.modules():
if type(m) is torch.nn.Linear:
torch.nn.init.normal_(m.weight)
torch.nn.init.zeros_(m.bias)
def forward(self, y, chann_type="AWGN"):
"""
Forward pass over y
Args:
y of shape (batch_size, n): Messages that have already passed through Encoder
Returns:
y of shape (batch_size, M): Messages after encoding+noise+decoding
"""
# Decoding phase
y = self.linear_relu(y)
y = self.linear_out(y)
return y